-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9dcd56
commit d869e7d
Showing
8 changed files
with
116 additions
and
143 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ members = [ | |
"lox-vm", | ||
"lox-syntax", | ||
"lox-std", | ||
"lox-gc" | ||
"lox-gc", | ||
"lox-mmap" | ||
] | ||
|
||
# For flamegraph | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |