Struct wasmedge_sys::Memory
source · pub struct Memory { /* private fields */ }
Expand description
Defines a WebAssembly memory instance, which is a linear memory described by its type. Each memory instance consists of a vector of bytes and an optional maximum size, and its size is a multiple of the WebAssembly page size (64KiB of each page).
Implementations§
source§impl Memory
impl Memory
sourcepub fn create(ty: &MemType) -> WasmEdgeResult<Self>
pub fn create(ty: &MemType) -> WasmEdgeResult<Self>
Create a new Memory to be associated with the given capacity limit.
Arguments
ty
- The type of the new Memory instance.
Errors
- If fail to create the memory instance, then WasmEdgeError::Mem(MemError::Create) is returned.
Example
use wasmedge_sys::{MemType, Memory};
let ty = MemType::create(10, Some(20), false).expect("fail to create memory type");
let memory = Memory::create(&ty);
sourcepub fn ty(&self) -> WasmEdgeResult<MemType>
pub fn ty(&self) -> WasmEdgeResult<MemType>
sourcepub fn set_data(
&mut self,
data: impl AsRef<[u8]>,
offset: u32
) -> WasmEdgeResult<()>
pub fn set_data( &mut self, data: impl AsRef<[u8]>, offset: u32 ) -> WasmEdgeResult<()>
Copies the data from the given input buffer into the Memory.
Arguments
-
data
- The data buffer to copy. -
offset
- The data start offset in the Memory.
Errors
If the sum of the offset
and the data length is larger than the size of the Memory,
then an error is returned.
use wasmedge_sys::{Memory, MemType};
use wasmedge_types::error::{CoreError, CoreExecutionError, WasmEdgeError};
// create a Memory: the min size 1 and the max size 2
let ty = MemType::create(1, Some(2), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");
// set data and the data length is larger than the data size in the memory
let result = mem.set_data(vec![1; 10], u32::pow(2, 16) - 9);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Box::new(WasmEdgeError::Core(CoreError::Execution(CoreExecutionError::MemoryOutOfBounds))));
Example
use wasmedge_sys::{MemType, Memory};
// create a Memory: the min size 1 and the max size 2
let ty = MemType::create(1, Some(2), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");
// page count
let count = mem.size();
assert_eq!(count, 1);
// set data
mem.set_data(vec![1; 10], 10).expect("fail to set data");
// get data
let data = mem.get_data(10, 10).expect("fail to get data");
assert_eq!(data, vec![1; 10]);
sourcepub fn data_pointer(&self, offset: u32, len: u32) -> WasmEdgeResult<*const u8>
pub fn data_pointer(&self, offset: u32, len: u32) -> WasmEdgeResult<*const u8>
sourcepub fn data_pointer_mut(
&mut self,
offset: u32,
len: u32
) -> WasmEdgeResult<*mut u8>
pub fn data_pointer_mut( &mut self, offset: u32, len: u32 ) -> WasmEdgeResult<*mut u8>
sourcepub fn size(&self) -> u32
pub fn size(&self) -> u32
Returns the size, in WebAssembly pages (64 KiB of each page), of this wasm memory.
sourcepub fn grow(&mut self, count: u32) -> WasmEdgeResult<()>
pub fn grow(&mut self, count: u32) -> WasmEdgeResult<()>
Grows this WebAssembly memory by count
pages.
Arguments
count
- The page counts to be extended to the Memory.
Errors
If fail to grow the page count, then an error is returned.
Example
use wasmedge_sys::{MemType, Memory};
// create a Memory with a limit range [10, 20]
let ty = MemType::create(10, Some(20), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");
// check page count
let count = mem.size();
assert_eq!(count, 10);
// grow 5 pages
mem.grow(10).expect("fail to grow the page count");
assert_eq!(mem.size(), 20);
sourcepub fn as_ptr(&self) -> *const WasmEdge_MemoryInstanceContext
Available on crate feature ffi
only.
pub fn as_ptr(&self) -> *const WasmEdge_MemoryInstanceContext
ffi
only.Provides a raw pointer to the inner memory context.
Trait Implementations§
source§impl Memory for Memory
Available on crate feature async
and Linux only.
impl Memory for Memory
async
and Linux only.