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

source

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
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);
source

pub fn ty(&self) -> WasmEdgeResult<MemType>

Returns the type of the Memory.

Errors

If fail to get the type from the Memory, then an error is returned.

source

pub fn get_data(&self, offset: u32, len: u32) -> WasmEdgeResult<Vec<u8>>

Copies the data from the Memory to the output buffer.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length.

Errors

If the offset + len is larger than the data size in the Memory, then an error is returned.

source

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]);
source

pub fn data_pointer(&self, offset: u32, len: u32) -> WasmEdgeResult<*const u8>

Returns the const data pointer to the Memory.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length. If the size of offset + len is larger than the data size in the Memory

Errors

If fail to get the data pointer, then an error is returned.

source

pub fn data_pointer_mut( &mut self, offset: u32, len: u32 ) -> WasmEdgeResult<*mut u8>

Returns the data pointer to the Memory.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length. If the size of offset + len is larger than the data size in the Memory

Errors

If fail to get the data pointer, then an error is returned.

source

pub fn size(&self) -> u32

Returns the size, in WebAssembly pages (64 KiB of each page), of this wasm memory.

source

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);
source

pub fn as_ptr(&self) -> *const WasmEdge_MemoryInstanceContext

Available on crate feature ffi only.

Provides a raw pointer to the inner memory context.

Trait Implementations§

source§

impl Clone for Memory

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Memory

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Memory

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Memory for Memory

Available on crate feature async and Linux only.
source§

fn get_data<T: Sized>(&self, offset: WasmPtr<T>) -> Result<&T, Errno>

source§

fn get_slice<T: Sized>( &self, offset: WasmPtr<T>, len: usize ) -> Result<&[T], Errno>

source§

fn get_iovec<'a>( &self, iovec_ptr: WasmPtr<__wasi_ciovec_t>, iovec_len: __wasi_size_t ) -> Result<Vec<IoSlice<'a>>, Errno>

source§

fn mut_data<T: Sized>(&mut self, offset: WasmPtr<T>) -> Result<&mut T, Errno>

source§

fn mut_slice<T: Sized>( &mut self, offset: WasmPtr<T>, len: usize ) -> Result<&mut [T], Errno>

source§

fn mut_iovec( &mut self, iovec_ptr: WasmPtr<__wasi_iovec_t>, iovec_len: __wasi_size_t ) -> Result<Vec<IoSliceMut<'_>>, Errno>

source§

fn write_data<T: Sized>( &mut self, offset: WasmPtr<T>, data: T ) -> Result<(), Errno>

Auto Trait Implementations§

§

impl !RefUnwindSafe for Memory

§

impl Send for Memory

§

impl Sync for Memory

§

impl Unpin for Memory

§

impl !UnwindSafe for Memory

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V