This commit is contained in:
Lachlan Sneff
2019-01-29 14:15:59 -08:00
parent 581363119b
commit bc78738bb7
9 changed files with 96 additions and 40 deletions

View File

@ -30,13 +30,13 @@ impl DynamicMemory {
pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
let memory = {
let mut memory =
sys::Memory::with_size((desc.min as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
sys::Memory::with_size((desc.minimum as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
.ok()?;
if desc.min != 0 {
if desc.minimum != 0 {
unsafe {
memory
.protect(
0..(desc.min as usize * WASM_PAGE_SIZE),
0..(desc.minimum as usize * WASM_PAGE_SIZE),
sys::Protect::ReadWrite,
)
.ok()?;
@ -48,13 +48,13 @@ impl DynamicMemory {
let mut storage = Box::new(DynamicMemory {
memory,
current: desc.min,
max: desc.max,
current: desc.minimum,
max: desc.maximum,
});
let storage_ptr: *mut DynamicMemory = &mut *storage;
local.base = storage.memory.as_ptr();
local.bound = desc.min as usize * WASM_PAGE_SIZE;
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
local.memory = storage_ptr as *mut ();
Some(storage)

View File

@ -23,6 +23,29 @@ pub struct Memory {
}
impl Memory {
/// Create a new `Memory` from a [`MemoryDescriptor`]
///
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
///
/// Usage:
///
/// ```
/// # use wasmer_runtime_core::types::MemoryDescriptor;
/// # use wasmer_runtime_core::memory::Memory;
/// # use wasmer_runtime_core::error::Result;
///
/// # fn create_memory() -> Result<()> {
/// let descriptor = MemoryDescriptor {
/// minimum: 10,
/// maximum: None,
/// shared: false,
/// };
///
/// let memory = Memory::new(descriptor)?;
///
/// # Ok(())
/// # }
/// ```
pub fn new(desc: MemoryDescriptor) -> Option<Self> {
let mut vm_local_memory = Box::new(vm::LocalMemory {
base: ptr::null_mut(),
@ -46,10 +69,15 @@ impl Memory {
})
}
/// Return the [`MemoryDescriptor`] that this memory
/// was created with.
///
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
pub fn descriptor(&self) -> MemoryDescriptor {
self.desc
}
/// Grow this memory by the specfied number of pages.
pub fn grow(&mut self, delta: u32) -> Option<u32> {
match &mut *self.storage.borrow_mut() {
(MemoryStorage::Dynamic(ref mut dynamic_memory), ref mut local) => {
@ -62,8 +90,13 @@ impl Memory {
}
}
/// This returns the number of pages in the memory.
pub fn current_pages(&self) -> u32 {
/// The size, in bytes, of this memory.
pub fn bytes(&self) -> usize {
(self.size() as usize) * WASM_PAGE_SIZE
}
/// The size, in wasm pages, of this memory.
pub fn size(&self) -> u32 {
match &*self.storage.borrow() {
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.current(),
(MemoryStorage::Static(ref static_memory), _) => static_memory.current(),
@ -271,7 +304,7 @@ impl fmt::Debug for Memory {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Memory")
.field("desc", &self.desc)
.field("size", &(self.current_pages() as usize * WASM_PAGE_SIZE))
.field("size", &self.bytes())
.finish()
}
}

View File

@ -33,11 +33,11 @@ impl StaticMemory {
let memory = {
let mut memory =
sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE).ok()?;
if desc.min != 0 {
if desc.minimum != 0 {
unsafe {
memory
.protect(
0..(desc.min as usize * WASM_PAGE_SIZE),
0..(desc.minimum as usize * WASM_PAGE_SIZE),
sys::Protect::ReadWrite,
)
.ok()?;
@ -49,13 +49,13 @@ impl StaticMemory {
let mut storage = Box::new(StaticMemory {
memory,
current: desc.min,
max: desc.max,
current: desc.minimum,
max: desc.maximum,
});
let storage_ptr: *mut StaticMemory = &mut *storage;
local.base = storage.memory.as_ptr();
local.bound = desc.min as usize * WASM_PAGE_SIZE;
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
local.memory = storage_ptr as *mut ();
Some(storage)