Skip to content

Commit

Permalink
Add lox-mmap crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksecond committed Jun 16, 2023
1 parent d9dcd56 commit d869e7d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 143 deletions.
109 changes: 8 additions & 101 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ members = [
"lox-vm",
"lox-syntax",
"lox-std",
"lox-gc"
"lox-gc",
"lox-mmap"
]

# For flamegraph
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ Also constants are not yet deduplicated in the compiler.

# Test suite

Tests are copied from https://github.com/munificent/craftinginterpreters/tree/master/test.
Tests are copied from https://github.com/munificent/craftinginterpreters/tree/master/test.

# Instruments

Run `codesign -s - -v -f --entitlements debug.plist target/release/lox` to codesign the release binary.
This will allow instruments to work properly.

8 changes: 8 additions & 0 deletions debug.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.get-task-allow</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion lox-gc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mmap = "*"
arrayvec = "0.7.2"
lox-mmap = { path = "../lox-mmap" }
40 changes: 1 addition & 39 deletions lox-gc/src/heap.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
use std::cell::Cell;
use lox_mmap::MemoryMap;

//TODO Use mmap again
//TODO Impl mmap ourselves
//TODO Merge reams when sweeping
//TODO Figure out dropping

struct MemoryMap {
size: usize,
data: *mut u8,
}

impl MemoryMap {
pub fn new(size: usize) -> Self {
unsafe {
let layout = std::alloc::Layout::array::<u8>(size).unwrap().align_to(4096).unwrap();
let data = std::alloc::alloc(layout);

Self {
size,
data,
}
}
}

pub fn data(&self) -> *mut u8 {
self.data
}
}

impl Drop for MemoryMap {
fn drop(&mut self) {
let layout = std::alloc::Layout::array::<u8>(self.size).unwrap().align_to(4096).unwrap();
unsafe {
std::alloc::dealloc(self.data, layout);
}
}
}

/// Returns the number of pages needed for `n` bytes (rounding up).
const fn bytes_to_pages(n: usize) -> usize {
Expand All @@ -51,7 +17,6 @@ const fn bytes_to_pages(n: usize) -> usize {
struct PdIdx(u32);

struct AddrSpace {
//mem: mmap::MemoryMap,
mem: MemoryMap,

used_pds: Cell<u32>,
Expand Down Expand Up @@ -79,9 +44,6 @@ impl AddrSpace {

/// Constructs a new [`AddrSpace`]. This will return `None` on error.
pub fn create() -> Option<Self> {
//use mmap::*;

//let mem = MemoryMap::new(Self::TOTAL_BYTES, &[MapOption::MapReadable, MapOption::MapWritable]).ok()?;
let mem = self::MemoryMap::new(Self::TOTAL_BYTES);

Some(Self {
Expand Down
9 changes: 9 additions & 0 deletions lox-mmap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "lox-mmap"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2"
80 changes: 80 additions & 0 deletions lox-mmap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::ptr;

//TODO support windows (using 'windows' crate)
//TODO consider replacing *mut u8 with NonNull
//TODO use libc::MAP_HUGETLB on supported platforms
//TODO check errno on failure and report correctly
//TODO return Result instead of panicking
//TODO Split into a module per platform

pub struct MemoryMap {
size: usize,
data: *mut u8,
}

impl MemoryMap {
pub fn data(&self) -> *mut u8 {
self.data
}
}

#[cfg(miri)]
impl MemoryMap {
pub fn new(size: usize) -> Self {
unsafe {
let layout = std::alloc::Layout::array::<u8>(size)
.unwrap()
.align_to(4096)
.unwrap();
let data = std::alloc::alloc(layout);

Self { size, data }
}
}

}

#[cfg(miri)]
impl Drop for MemoryMap {
fn drop(&mut self) {
let layout = std::alloc::Layout::array::<u8>(self.size)
.unwrap()
.align_to(4096)
.unwrap();
unsafe {
std::alloc::dealloc(self.data, layout);
}
}
}

#[cfg(all(not(miri), unix))]
impl MemoryMap {
pub fn new(size: usize) -> Self {
let addr = unsafe {
libc::mmap(
ptr::null_mut(),
size,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE,
-1,
0,
)
};

assert!(!addr.is_null() && addr != libc::MAP_FAILED, "mmap failed");

Self {
data: addr as _,
size,
}
}
}

#[cfg(all(not(miri), unix))]
impl Drop for MemoryMap {
fn drop(&mut self) {
unsafe {
libc::munmap(self.data as _, self.size);
}
}
}

0 comments on commit d869e7d

Please sign in to comment.