Use mmap
This commit is contained in:
parent
b1be3f46c2
commit
af2788a06d
|
@ -17,6 +17,7 @@ memory_units = "0.3.0"
|
||||||
libm = { version = "0.1.2", optional = true }
|
libm = { version = "0.1.2", optional = true }
|
||||||
num-rational = "0.2.2"
|
num-rational = "0.2.2"
|
||||||
num-traits = "0.2.8"
|
num-traits = "0.2.8"
|
||||||
|
libc = "0.2.58"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_matches = "1.1"
|
assert_matches = "1.1"
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
//! An implementation of a `ByteBuf` based on virtual memory.
|
||||||
|
//!
|
||||||
|
//! This implementation uses `mmap` on POSIX systems (and should use `VirtualAlloc` on windows).
|
||||||
|
|
||||||
|
use std::ptr::{self, NonNull};
|
||||||
|
use std::slice;
|
||||||
|
|
||||||
|
struct Mmap {
|
||||||
|
ptr: NonNull<u8>,
|
||||||
|
len: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mmap {
|
||||||
|
fn new(len: usize) -> Self {
|
||||||
|
assert!(len > 0);
|
||||||
|
unsafe {
|
||||||
|
let ptr = libc::mmap(
|
||||||
|
ptr::null_mut(),
|
||||||
|
len,
|
||||||
|
libc::PROT_READ | libc::PROT_WRITE,
|
||||||
|
libc::MAP_PRIVATE | libc::MAP_ANON,
|
||||||
|
-1,
|
||||||
|
0,
|
||||||
|
);;
|
||||||
|
assert!(ptr as isize != -1);
|
||||||
|
Self {
|
||||||
|
ptr: NonNull::new(ptr as *mut u8).unwrap(),
|
||||||
|
len,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
|
unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
|
unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Mmap {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let r = unsafe { libc::munmap(self.ptr.as_ptr() as *mut libc::c_void, self.len) };
|
||||||
|
assert_eq!(r, 0, "munmap failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ByteBuf {
|
||||||
|
mmap: Option<Mmap>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ByteBuf {
|
||||||
|
pub fn new(len: usize) -> Self {
|
||||||
|
let mmap = if len == 0 { None } else { Some(Mmap::new(len)) };
|
||||||
|
|
||||||
|
Self { mmap }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn realloc(&mut self, new_len: usize) {
|
||||||
|
let new_mmap = if new_len == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
if self.len() == 0 {
|
||||||
|
Some(Mmap::new(new_len))
|
||||||
|
} else {
|
||||||
|
let mut new_mmap = Mmap::new(new_len);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let src = self.mmap.as_ref().unwrap().as_slice();
|
||||||
|
let dst = new_mmap.as_slice_mut();
|
||||||
|
|
||||||
|
ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(new_mmap)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.mmap = new_mmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.mmap.as_ref().map(|m| m.len).unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
|
self.mmap.as_ref().map(|m| m.as_slice()).unwrap_or(&[])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
|
self.mmap.as_mut().map(|m| m.as_slice_mut()).unwrap_or(&mut [])
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,13 +69,15 @@ impl fmt::Debug for MemoryInstance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod rust_alloc as byte_buf;
|
mod mmap;
|
||||||
use self::rust_alloc::ByteBuf;
|
use self::mmap::ByteBuf;
|
||||||
|
|
||||||
|
// mod rust_alloc as byte_buf;
|
||||||
|
// use self::rust_alloc::ByteBuf;
|
||||||
|
|
||||||
// mod vec_backed;
|
// mod vec_backed;
|
||||||
// use self::vec_backed::ByteBuf;
|
// use self::vec_backed::ByteBuf;
|
||||||
|
|
||||||
|
|
||||||
struct CheckedRegion {
|
struct CheckedRegion {
|
||||||
offset: usize,
|
offset: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
|
@ -196,7 +198,10 @@ impl MemoryInstance {
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
let mut buffer = self.buffer.borrow_mut();
|
||||||
let region =
|
let region =
|
||||||
self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?;
|
self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?;
|
||||||
Ok(T::from_little_endian(&buffer.as_slice_mut()[region.range()]).expect("Slice size is checked"))
|
Ok(
|
||||||
|
T::from_little_endian(&buffer.as_slice_mut()[region.range()])
|
||||||
|
.expect("Slice size is checked"),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy data from memory at given offset.
|
/// Copy data from memory at given offset.
|
||||||
|
@ -289,8 +294,7 @@ impl MemoryInstance {
|
||||||
buffer: &mut ByteBuf,
|
buffer: &mut ByteBuf,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
) -> Result<CheckedRegion, Error>
|
) -> Result<CheckedRegion, Error> {
|
||||||
{
|
|
||||||
let end = offset.checked_add(size).ok_or_else(|| {
|
let end = offset.checked_add(size).ok_or_else(|| {
|
||||||
Error::Memory(format!(
|
Error::Memory(format!(
|
||||||
"trying to access memory block of size {} from offset {}",
|
"trying to access memory block of size {} from offset {}",
|
||||||
|
|
Loading…
Reference in New Issue