Skip to main content

IsolatedMemory

Struct IsolatedMemory 

Source
pub struct IsolatedMemory<const MAX_PAGES: usize> { /* private fields */ }
Expand description

Isolated linear memory for a single Wasm module.

MAX_PAGES is the compile-time maximum (from the Wasm module’s declared maximum or a CLI override). The backing array is fully pre-allocated.

Implementations§

Source§

impl<const MAX_PAGES: usize> IsolatedMemory<MAX_PAGES>

Source

pub fn try_new(initial_pages: usize) -> Result<Self, ConstructionError>

Create a new IsolatedMemory with initial_pages active.

§Errors

Returns ConstructionError::MemoryInitialPagesExceedsMax if initial_pages > MAX_PAGES.

Source

pub fn try_init( slot: &mut MaybeUninit<Self>, initial_pages: usize, ) -> Result<(), ConstructionError>

Initialize an IsolatedMemory in-place within a caller-provided slot.

Unlike try_new, this writes directly into slot without ever creating a large Result<Self, E> on the call stack. Use this when MAX_PAGES is large, to avoid stack overflow in debug builds.

§Errors

Returns ConstructionError::MemoryInitialPagesExceedsMax if initial_pages > MAX_PAGES.

Source

pub fn page_count(&self) -> usize

Current number of active pages.

Source

pub fn active_size(&self) -> usize

Current active size in bytes.

Source

pub fn grow(&mut self, delta: u32) -> i32

Wasm memory.grow — returns previous page count, or -1 on failure. No allocation occurs: the backing array is already sized to MAX_PAGES.

Source

pub fn size(&self) -> i32

Wasm memory.size — returns current page count.

Source

pub fn memory_copy(&mut self, dst: u32, src: u32, len: u32) -> WasmResult<()>

Wasm memory.copy — copy len bytes from src to dst.

Semantics match memmove: overlapping source and destination regions are handled correctly. Traps (OutOfBounds) if either region extends beyond the current active memory.

Source

pub fn load_i32(&self, offset: usize) -> WasmResult<i32>

Load an i32 from linear memory with bounds checking.

Source

pub fn load_i64(&self, offset: usize) -> WasmResult<i64>

Load an i64 from linear memory with bounds checking.

Source

pub fn load_u8(&self, offset: usize) -> WasmResult<u8>

Load a u8 (i32.load8_u) from linear memory with bounds checking.

Source

pub fn load_u16(&self, offset: usize) -> WasmResult<u16>

Load a u16 (i32.load16_u) from linear memory with bounds checking.

Source

pub fn load_f32(&self, offset: usize) -> WasmResult<f32>

Load an f32 from linear memory with bounds checking.

Source

pub fn load_f64(&self, offset: usize) -> WasmResult<f64>

Load an f64 from linear memory with bounds checking.

Source

pub fn store_i32(&mut self, offset: usize, value: i32) -> WasmResult<()>

Store an i32 into linear memory with bounds checking.

Source

pub fn store_i64(&mut self, offset: usize, value: i64) -> WasmResult<()>

Store an i64 into linear memory with bounds checking.

Source

pub fn store_u8(&mut self, offset: usize, value: u8) -> WasmResult<()>

Store a u8 (i32.store8) into linear memory with bounds checking.

Source

pub fn store_u16(&mut self, offset: usize, value: u16) -> WasmResult<()>

Store a u16 (i32.store16) into linear memory with bounds checking.

Source

pub fn store_f32(&mut self, offset: usize, value: f32) -> WasmResult<()>

Store an f32 into linear memory with bounds checking.

Source

pub fn store_f64(&mut self, offset: usize, value: f64) -> WasmResult<()>

Store an f64 into linear memory with bounds checking.

Source

pub fn init_data(&mut self, offset: usize, data: &[u8]) -> WasmResult<()>

Initialize a region of memory from a byte slice (Wasm data segment).

Copies data into linear memory starting at offset. Equivalent to calling store_u8 for each byte, but avoids emitting N separate function calls in generated code.

§Errors

Returns Err(WasmTrap::OutOfBounds) if offset + data.len() exceeds active_pages * PAGE_SIZE.

Source

pub unsafe fn load_i32_unchecked(&self, offset: usize) -> i32

Load i32 without bounds checking.

§Safety

Caller must guarantee offset + 3 < active_size().

Source

pub unsafe fn load_i64_unchecked(&self, offset: usize) -> i64

Load i64 without bounds checking.

§Safety

Caller must guarantee offset + 7 < active_size().

Source

pub unsafe fn store_i32_unchecked(&mut self, offset: usize, value: i32)

Store i32 without bounds checking.

§Safety

Caller must guarantee offset + 3 < active_size().

Source

pub unsafe fn store_i64_unchecked(&mut self, offset: usize, value: i64)

Store i64 without bounds checking.

§Safety

Caller must guarantee offset + 7 < active_size().

Source

pub fn as_slice(&self) -> &[u8]

Read-only access to the active memory region.

Source

pub fn as_mut_slice(&mut self) -> &mut [u8]

Mutable access to the active memory region.

Auto Trait Implementations§

§

impl<const MAX_PAGES: usize> Freeze for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> RefUnwindSafe for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> Send for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> Sync for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> Unpin for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> UnsafeUnpin for IsolatedMemory<MAX_PAGES>

§

impl<const MAX_PAGES: usize> UnwindSafe for IsolatedMemory<MAX_PAGES>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.