CoW
This commit is contained in:
parent
b67af25899
commit
97661cf3fe
|
@ -404,7 +404,7 @@ pub use self::func::{FuncInstance, FuncInvocation, FuncRef, ResumableError};
|
||||||
pub use self::global::{GlobalInstance, GlobalRef};
|
pub use self::global::{GlobalInstance, GlobalRef};
|
||||||
pub use self::host::{Externals, HostError, NopExternals, RuntimeArgs};
|
pub use self::host::{Externals, HostError, NopExternals, RuntimeArgs};
|
||||||
pub use self::imports::{ImportResolver, ImportsBuilder, ModuleImportResolver};
|
pub use self::imports::{ImportResolver, ImportsBuilder, ModuleImportResolver};
|
||||||
pub use self::memory::{MemoryInstance, MemoryRef, LINEAR_MEMORY_PAGE_SIZE};
|
pub use self::memory::{MemoryBackend, MemoryInstance, MemoryRef, LINEAR_MEMORY_PAGE_SIZE};
|
||||||
pub use self::module::{ExternVal, ModuleInstance, ModuleRef, NotStartedModuleRef};
|
pub use self::module::{ExternVal, ModuleInstance, ModuleRef, NotStartedModuleRef};
|
||||||
pub use self::runner::{StackRecycler, DEFAULT_CALL_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
|
pub use self::runner::{StackRecycler, DEFAULT_CALL_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
|
||||||
pub use self::table::{TableInstance, TableRef};
|
pub use self::table::{TableInstance, TableRef};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
use std::ptr::{self, NonNull};
|
use std::ptr::{self, NonNull};
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
use super::MemoryBackend;
|
||||||
|
|
||||||
struct Mmap {
|
struct Mmap {
|
||||||
/// The pointer that points to the start of the mapping.
|
/// The pointer that points to the start of the mapping.
|
||||||
|
@ -111,11 +112,15 @@ impl Drop for Mmap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ByteBuf {
|
pub struct MmapByteBuf {
|
||||||
mmap: Option<Mmap>,
|
mmap: Option<Mmap>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ByteBuf {
|
impl MmapByteBuf {
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
MmapByteBuf { mmap: None }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(len: usize) -> Result<Self, &'static str> {
|
pub fn new(len: usize) -> Result<Self, &'static str> {
|
||||||
let mmap = if len == 0 {
|
let mmap = if len == 0 {
|
||||||
None
|
None
|
||||||
|
@ -124,8 +129,14 @@ impl ByteBuf {
|
||||||
};
|
};
|
||||||
Ok(Self { mmap })
|
Ok(Self { mmap })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn realloc(&mut self, new_len: usize) -> Result<(), &'static str> {
|
impl MemoryBackend for MmapByteBuf {
|
||||||
|
fn alloc(&mut self, initial: usize, _maximum: usize) -> Result<(), &'static str> {
|
||||||
|
self.realloc(initial)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn realloc(&mut self, new_len: usize) -> Result<(), &'static str> {
|
||||||
let new_mmap = if new_len == 0 {
|
let new_mmap = if new_len == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -143,22 +154,22 @@ impl ByteBuf {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
fn len(&self) -> usize {
|
||||||
self.mmap.as_ref().map(|m| m.len).unwrap_or(0)
|
self.mmap.as_ref().map(|m| m.len).unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
fn as_slice(&self) -> &[u8] {
|
||||||
self.mmap.as_ref().map(|m| m.as_slice()).unwrap_or(&[])
|
self.mmap.as_ref().map(|m| m.as_slice()).unwrap_or(&[])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
self.mmap
|
self.mmap
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.map(|m| m.as_slice_mut())
|
.map(|m| m.as_slice_mut())
|
||||||
.unwrap_or(&mut [])
|
.unwrap_or(&mut [])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn erase(&mut self) -> Result<(), &'static str> {
|
fn erase(&mut self) -> Result<(), &'static str> {
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
if len > 0 {
|
if len > 0 {
|
||||||
// The order is important.
|
// The order is important.
|
||||||
|
@ -176,14 +187,14 @@ impl ByteBuf {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::ByteBuf;
|
use super::{MmapByteBuf, MemoryBackend};
|
||||||
|
|
||||||
const PAGE_SIZE: usize = 4096;
|
const PAGE_SIZE: usize = 4096;
|
||||||
|
|
||||||
// This is not required since wasm memories can only grow but nice to have.
|
// This is not required since wasm memories can only grow but nice to have.
|
||||||
#[test]
|
#[test]
|
||||||
fn byte_buf_shrink() {
|
fn byte_buf_shrink() {
|
||||||
let mut byte_buf = ByteBuf::new(PAGE_SIZE * 3).unwrap();
|
let mut byte_buf = MmapByteBuf::new(PAGE_SIZE * 3).unwrap();
|
||||||
byte_buf.realloc(PAGE_SIZE * 2).unwrap();
|
byte_buf.realloc(PAGE_SIZE * 2).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,13 @@ use Error;
|
||||||
|
|
||||||
#[cfg(all(unix, not(feature = "vec_memory")))]
|
#[cfg(all(unix, not(feature = "vec_memory")))]
|
||||||
#[path="mmap_bytebuf.rs"]
|
#[path="mmap_bytebuf.rs"]
|
||||||
mod bytebuf;
|
mod mmap_bytebuf;
|
||||||
|
|
||||||
#[cfg(any(not(unix), feature = "vec_memory"))]
|
#[cfg(all(unix, not(feature = "vec_memory")))]
|
||||||
#[path = "vec_bytebuf.rs"]
|
use self::mmap_bytebuf::MmapByteBuf;
|
||||||
mod bytebuf;
|
|
||||||
|
|
||||||
use self::bytebuf::ByteBuf;
|
// #[cfg(any(not(unix), feature = "vec_memory"))]
|
||||||
|
// mod bytebuf;
|
||||||
|
|
||||||
/// Size of a page of [linear memory][`MemoryInstance`] - 64KiB.
|
/// Size of a page of [linear memory][`MemoryInstance`] - 64KiB.
|
||||||
///
|
///
|
||||||
|
@ -43,6 +43,15 @@ impl ::core::ops::Deref for MemoryRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait MemoryBackend {
|
||||||
|
fn alloc(&mut self, initial: usize, maximum: usize) -> Result<(), &'static str>;
|
||||||
|
fn realloc(&mut self, new_len: usize) -> Result<(), &'static str>;
|
||||||
|
fn len(&self) -> usize;
|
||||||
|
fn as_slice(&self) -> &[u8];
|
||||||
|
fn as_slice_mut(&mut self) -> &mut [u8];
|
||||||
|
fn erase(&mut self) -> Result<(), &'static str>;
|
||||||
|
}
|
||||||
|
|
||||||
/// Runtime representation of a linear memory (or `memory` for short).
|
/// Runtime representation of a linear memory (or `memory` for short).
|
||||||
///
|
///
|
||||||
/// A memory is a contiguous, mutable array of raw bytes. Wasm code can load and store values
|
/// A memory is a contiguous, mutable array of raw bytes. Wasm code can load and store values
|
||||||
|
@ -60,7 +69,7 @@ pub struct MemoryInstance {
|
||||||
/// Memory limits.
|
/// Memory limits.
|
||||||
limits: ResizableLimits,
|
limits: ResizableLimits,
|
||||||
/// Linear memory buffer with lazy allocation.
|
/// Linear memory buffer with lazy allocation.
|
||||||
buffer: RefCell<ByteBuf>,
|
buffer: RefCell<Box<dyn MemoryBackend>>,
|
||||||
initial: Pages,
|
initial: Pages,
|
||||||
current_size: Cell<usize>,
|
current_size: Cell<usize>,
|
||||||
maximum: Option<Pages>,
|
maximum: Option<Pages>,
|
||||||
|
@ -142,17 +151,21 @@ impl MemoryInstance {
|
||||||
let limits = ResizableLimits::new(initial.0 as u32, maximum.map(|p| p.0 as u32));
|
let limits = ResizableLimits::new(initial.0 as u32, maximum.map(|p| p.0 as u32));
|
||||||
|
|
||||||
let initial_size: Bytes = initial.into();
|
let initial_size: Bytes = initial.into();
|
||||||
|
let bytebuf = MmapByteBuf::new(initial_size.0).map_err(|err| Error::Memory(err.to_string()))?;
|
||||||
|
|
||||||
Ok(MemoryInstance {
|
Ok(MemoryInstance {
|
||||||
limits: limits,
|
limits: limits,
|
||||||
buffer: RefCell::new(
|
buffer: RefCell::new(Box::new(bytebuf)),
|
||||||
ByteBuf::new(initial_size.0).map_err(|err| Error::Memory(err.to_string()))?,
|
|
||||||
),
|
|
||||||
initial: initial,
|
initial: initial,
|
||||||
current_size: Cell::new(initial_size.0),
|
current_size: Cell::new(initial_size.0),
|
||||||
maximum: maximum,
|
maximum: maximum,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_backend(&self, backend: Box<dyn MemoryBackend>) {
|
||||||
|
*self.buffer.borrow_mut() = backend;
|
||||||
|
}
|
||||||
|
|
||||||
/// Return linear memory limits.
|
/// Return linear memory limits.
|
||||||
pub(crate) fn limits(&self) -> &ResizableLimits {
|
pub(crate) fn limits(&self) -> &ResizableLimits {
|
||||||
&self.limits
|
&self.limits
|
||||||
|
@ -296,7 +309,7 @@ impl MemoryInstance {
|
||||||
|
|
||||||
fn checked_region(
|
fn checked_region(
|
||||||
&self,
|
&self,
|
||||||
buffer: &mut ByteBuf,
|
buffer: &mut Box<dyn MemoryBackend>,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
size: usize,
|
size: usize,
|
||||||
) -> Result<CheckedRegion, Error> {
|
) -> Result<CheckedRegion, Error> {
|
||||||
|
@ -324,7 +337,7 @@ impl MemoryInstance {
|
||||||
|
|
||||||
fn checked_region_pair(
|
fn checked_region_pair(
|
||||||
&self,
|
&self,
|
||||||
buffer: &mut ByteBuf,
|
buffer: &mut Box<dyn MemoryBackend>,
|
||||||
offset1: usize,
|
offset1: usize,
|
||||||
size1: usize,
|
size1: usize,
|
||||||
offset2: usize,
|
offset2: usize,
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
//! An implementation of `ByteBuf` based on a plain `Vec`.
|
||||||
|
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use std::{
|
||||||
|
slice,
|
||||||
|
mem,
|
||||||
|
};
|
||||||
|
use super::MemoryBackend;
|
||||||
|
|
||||||
|
pub struct RawByteBuf {
|
||||||
|
ptr: *mut u8,
|
||||||
|
len: usize,
|
||||||
|
cap: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawByteBuf {
|
||||||
|
pub fn from_raw_parts(ptr: *mut u8, len: usize, cap: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
ptr,
|
||||||
|
len,
|
||||||
|
cap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(len: usize) -> Result<Self, &'static str> {
|
||||||
|
let mut v = vec![0u8; len];
|
||||||
|
let cap = len;
|
||||||
|
let ptr = v.as_mut_ptr();
|
||||||
|
mem::forget(v);
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
ptr,
|
||||||
|
len,
|
||||||
|
cap,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MemoryBackend for RawByteBuf {
|
||||||
|
|
||||||
|
pub fn realloc(&mut self, new_len: usize) -> Result<(), &'static str> {
|
||||||
|
if new_len > self.cap {
|
||||||
|
return Err("exceeds cap");
|
||||||
|
}
|
||||||
|
self.len = new_len;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.len
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
|
unsafe {
|
||||||
|
slice::from_raw_parts(self.ptr, self.len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
|
unsafe {
|
||||||
|
slice::from_raw_parts_mut(self.ptr, self.len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn erase(&mut self) -> Result<(), &'static str> {
|
||||||
|
for v in self.as_slice_mut() {
|
||||||
|
*v = 0;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue