mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-25 22:51:32 +00:00
Remove SharedStaticMemory and simplify surrounding code.
This commit is contained in:
@ -1,12 +1,10 @@
|
|||||||
use crate::error::GrowError;
|
use crate::error::GrowError;
|
||||||
use crate::{
|
use crate::{error::CreationError, sys, types::MemoryDescriptor, units::Pages, vm};
|
||||||
error::CreationError,
|
|
||||||
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
#[doc(hidden)]
|
||||||
sys,
|
pub const SAFE_STATIC_HEAP_SIZE: usize = 1 << 32; // 4 GiB
|
||||||
types::MemoryDescriptor,
|
#[doc(hidden)]
|
||||||
units::Pages,
|
pub const SAFE_STATIC_GUARD_SIZE: usize = 1 << 31; // 2 GiB
|
||||||
vm,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// This is an internal-only api.
|
/// This is an internal-only api.
|
||||||
///
|
///
|
@ -1,8 +0,0 @@
|
|||||||
#[doc(hidden)]
|
|
||||||
pub const SAFE_STATIC_HEAP_SIZE: usize = 1 << 32; // 4 GiB
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub const SAFE_STATIC_GUARD_SIZE: usize = 1 << 31; // 2 GiB
|
|
||||||
|
|
||||||
mod unshared;
|
|
||||||
|
|
||||||
pub use self::unshared::StaticMemory;
|
|
@ -1,110 +0,0 @@
|
|||||||
/*use crate::error::GrowError;
|
|
||||||
use crate::{
|
|
||||||
error::CreationError,
|
|
||||||
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
|
||||||
sys,
|
|
||||||
types::MemoryDescriptor,
|
|
||||||
units::Pages,
|
|
||||||
vm,
|
|
||||||
};
|
|
||||||
|
|
||||||
use parking_lot::{Mutex, MutexGuard};
|
|
||||||
|
|
||||||
/// This is an internal-only api.
|
|
||||||
///
|
|
||||||
/// A static memory allocates 6GB of *virtual* memory when created
|
|
||||||
/// in order to allow the WebAssembly module to contain no bounds-checks.
|
|
||||||
///
|
|
||||||
/// Additionally, static memories stay at a single virtual address, so there is no need
|
|
||||||
/// to reload its address on each use.
|
|
||||||
///
|
|
||||||
/// Static memories take a relatively long time to create, so if memories are short-lived,
|
|
||||||
/// it's recommended that a dynamic memory is used. There is currently no user-facing api that
|
|
||||||
/// allows them to select the type of memory used however.
|
|
||||||
pub struct SharedStaticMemory {
|
|
||||||
memory: sys::Memory,
|
|
||||||
current: Pages,
|
|
||||||
max: Option<Pages>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SharedStaticMemory {
|
|
||||||
pub(in crate::memory) fn new(
|
|
||||||
desc: MemoryDescriptor,
|
|
||||||
local: &mut vm::LocalMemory,
|
|
||||||
) -> Result<Box<Self>, CreationError> {
|
|
||||||
let memory = {
|
|
||||||
let mut memory = sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE)
|
|
||||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
|
||||||
if desc.minimum != Pages(0) {
|
|
||||||
unsafe {
|
|
||||||
memory
|
|
||||||
.protect(0..desc.minimum.bytes().0, sys::Protect::ReadWrite)
|
|
||||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memory
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut storage = Box::new(SharedStaticMemory {
|
|
||||||
memory,
|
|
||||||
current: desc.minimum,
|
|
||||||
max: desc.maximum,
|
|
||||||
lock: Mutex::new(()),
|
|
||||||
});
|
|
||||||
let storage_ptr: *mut SharedStaticMemory = &mut *storage;
|
|
||||||
|
|
||||||
local.base = storage.memory.as_ptr();
|
|
||||||
local.bound = desc.minimum.bytes().0;
|
|
||||||
local.memory = storage_ptr as *mut ();
|
|
||||||
|
|
||||||
Ok(storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn lock(&mut self) -> MutexGuard<()> {
|
|
||||||
self.lock.lock()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(&self, _: &MutexGuard<()>) -> Pages {
|
|
||||||
self.current
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory, _: &MutexGuard<()>) -> Result<Pages, GrowError> {
|
|
||||||
let new_pages = self.current.checked_add(delta).map_err(|e| e.into())?;
|
|
||||||
|
|
||||||
if let Some(max) = self.max {
|
|
||||||
if new_pages > max {
|
|
||||||
return Err(GrowError::ExceededMaxPagesForMemory(
|
|
||||||
new_pages.0 as usize,
|
|
||||||
max.0 as usize,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = unsafe {
|
|
||||||
self.memory
|
|
||||||
.protect(
|
|
||||||
self.current.bytes().0..new_pages.bytes().0,
|
|
||||||
sys::Protect::ReadWrite,
|
|
||||||
)
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}?;
|
|
||||||
|
|
||||||
local.bound = new_pages.bytes().0;
|
|
||||||
|
|
||||||
let old_pages = self.current;
|
|
||||||
|
|
||||||
self.current = new_pages;
|
|
||||||
|
|
||||||
Ok(old_pages)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
|
||||||
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
|
||||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
Reference in New Issue
Block a user