Struct wasmtime::SharedMemory
source · [−]pub struct SharedMemory(_, _);
Expand description
A constructor for externally-created shared memory.
The threads proposal adds the concept of “shared memory” to WebAssembly.
This is much the same as a Wasm linear memory (i.e., Memory
), but can be
used concurrently by multiple agents. Because these agents may execute in
different threads, SharedMemory
must be thread-safe.
When the threads proposal is enabled, there are multiple ways to construct shared memory:
- for imported shared memory, e.g.,
(import "env" "memory" (memory 1 1 shared))
, the user must supply aSharedMemory
with the externally-created memory as an import to the instance–e.g.,shared_memory.into()
. - for private or exported shared memory, e.g.,
(export "env" "memory" (memory 1 1 shared))
, Wasmtime will create the memory internally during instantiation–access usingInstance::get_shared_memory()
.
Examples
let mut config = Config::new();
config.wasm_threads(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 2))?;
let module = Module::new(&engine, r#"(module (memory (import "" "") 1 2 shared))"#)?;
let instance = Instance::new(&mut store, &module, &[shared_memory.into()])?;
// ...
Implementations
sourceimpl SharedMemory
impl SharedMemory
sourcepub fn new(engine: &Engine, ty: MemoryType) -> Result<Self>
pub fn new(engine: &Engine, ty: MemoryType) -> Result<Self>
Construct a SharedMemory
by providing both the minimum
and
maximum
number of 64K-sized pages. This call allocates the necessary
pages on the system.
sourcepub fn ty(&self) -> MemoryType
pub fn ty(&self) -> MemoryType
Return the type of the shared memory.
sourcepub fn data_size(&self) -> usize
pub fn data_size(&self) -> usize
Returns the byte length of this memory.
The returned value will be a multiple of the wasm page size, 64k.
For more information and examples see the documentation on the
Memory
type.
sourcepub fn data(&self) -> *mut [u8]
pub fn data(&self) -> *mut [u8]
Return access to the available portion of the shared memory.
Because the memory is shared, it is possible that this memory is being modified in other threads–in other words, the data can change at any time. Users of this function must manage synchronization and locking to this region of memory themselves.
Not only can the data change, but the length of this region can change
as well. Other threads can call memory.grow
operations that will
extend the region length but–importantly–this will not be reflected in
the size of region returned by this function.
sourcepub fn grow(&mut self, delta: u64) -> Result<u64>
pub fn grow(&mut self, delta: u64) -> Result<u64>
Grows this WebAssembly memory by delta
pages.
This will attempt to add delta
more pages of memory on to the end of
this Memory
instance. If successful this may relocate the memory and
cause Memory::data_ptr
to return a new value. Additionally any
unsafely constructed slices into this memory may no longer be valid.
On success returns the number of pages this memory previously had before the growth succeeded.
Errors
Returns an error if memory could not be grown, for example if it exceeds
the maximum limits of this memory. A
ResourceLimiter
is another example of
preventing a memory to grow.
Trait Implementations
sourceimpl Clone for SharedMemory
impl Clone for SharedMemory
sourcefn clone(&self) -> SharedMemory
fn clone(&self) -> SharedMemory
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourcefn from(r: SharedMemory) -> Self
fn from(r: SharedMemory) -> Self
Performs the conversion.
Auto Trait Implementations
impl !RefUnwindSafe for SharedMemory
impl Send for SharedMemory
impl Sync for SharedMemory
impl Unpin for SharedMemory
impl !UnwindSafe for SharedMemory
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more