WasmMemory Class

A memory (a memory address). The bytes of a memory that is not shared are a .NET array (WasmMemory.Data), replaced when the memory grows. The bytes of a shared memory are a WasmSharedMemoryBlock, which never moves: a shared memory can be used by the stores of every thread, and its methods can be called from any thread.

public abstract class WasmMemory : WasmExtern
Inheritance
WasmExtern WasmMemory

Properties

NameDescription
DataGets the bytes of a memory that is not shared (the length is the size of the memory in bytes). The array is replaced when the memory grows (see WasmMemory.DataChanged): do not keep it.
KindGets the kind of the value.
SharedBlockGets the bytes of a shared memory, or null for a memory that is not shared.
SizeGets the size of the memory in pages.
TypeGets the type of the memory.

Data

Gets the bytes of a memory that is not shared (the length is the size of the memory in bytes). The array is replaced when the memory grows (see WasmMemory.DataChanged): do not keep it.

public abstract byte[] Data { get; }

Property value

byte[]

Exceptions

NotSupportedException
The memory is shared.

Gets the kind of the value.

public override WasmExternKind Kind { get; }

Property value

WasmExternKind

SharedBlock

WasmObjects.cs

Gets the bytes of a shared memory, or null for a memory that is not shared.

public virtual WasmSharedMemoryBlock? SharedBlock { get; }

Size

Gets the size of the memory in pages.

public abstract ulong Size { get; }

Property value

ulong

Type

Gets the type of the memory.

public abstract WasmMemoryType Type { get; }

Property value

WasmMemoryType

Methods

NameDescription
AtomicNotify(ulong, uint)Notifies the waiters of an address of a shared memory (memory.atomic.notify), from any thread, and returns the number of waiters notified.
AtomicWait(ulong, long, int, TimeSpan?, WasmWaitSignal?)Blocks the calling thread until a waiter of an address of a shared memory is notified or the timeout elapses (memory.atomic.wait32 and memory.atomic.wait64), when the address contains expected. It can be called from any thread.
GetSpan()Returns the bytes of the memory. The span is invalid after the memory grows, unless the memory is shared.
Grow(ulong)Grows the memory (mem_grow) and returns its previous size in pages.
GrowTo(ulong)Grows the memory to newSize pages, and returns its previous size in pages: the memory does not change when its size is newSize or more. The threads that share a memory can grow it concurrently: a shared memory grows to the new size, never beyond it.

AtomicNotify(ulong, uint)

WasmObjects.cs

Notifies the waiters of an address of a shared memory (memory.atomic.notify), from any thread, and returns the number of waiters notified.

public virtual uint AtomicNotify(ulong address, uint count)

Parameters

address ulong
The address: a multiple of 4.
count uint
The maximum number of waiters to notify.

Returns

uint

Exceptions

WasmTrapException
The address is out of bounds or not aligned.
NotSupportedException
The memory is not shared.

AtomicWait(ulong, long, int, TimeSpan?, WasmWaitSignal?)

WasmObjects.cs

Blocks the calling thread until a waiter of an address of a shared memory is notified or the timeout elapses (memory.atomic.wait32 and memory.atomic.wait64), when the address contains expected. It can be called from any thread.

public virtual WasmWaitResult AtomicWait(ulong address, long expected, int size, TimeSpan? timeout, WasmWaitSignal? signal = null)

Parameters

address ulong
The address: a multiple of size.
expected long
The expected value (the low 32 bits for a 32-bit wait).
size int
The size of the value in bytes: 4 or 8.
timeout TimeSpan?
The maximum duration of the wait, or null to wait until notified.
signal WasmWaitSignal?
Set right before the thread joins the waiters of the address, or null.

Returns

WasmWaitResult

Exceptions

WasmTrapException
The address is out of bounds or not aligned.
NotSupportedException
The memory is not shared.

Returns the bytes of the memory. The span is invalid after the memory grows, unless the memory is shared.

public virtual Span<byte> GetSpan()

Returns

Span<byte>

Grow(ulong)

Grows the memory (mem_grow) and returns its previous size in pages.

public abstract ulong Grow(ulong delta)

Parameters

delta ulong
The number of pages to add.

Returns

ulong

Exceptions

WasmAllocationException
The memory cannot grow.

GrowTo(ulong)

WasmObjects.cs

Grows the memory to newSize pages, and returns its previous size in pages: the memory does not change when its size is newSize or more. The threads that share a memory can grow it concurrently: a shared memory grows to the new size, never beyond it.

public virtual ulong GrowTo(ulong newSize)

Parameters

newSize ulong
The size in pages.

Returns

ulong

Exceptions

WasmAllocationException
The memory cannot grow.

Events

NameDescription
DataChangedOccurs after the bytes of a memory that is not shared are replaced: the memory grew, by WasmMemory.Grow or by a memory.grow instruction. It is raised on the thread of the store.

DataChanged

Occurs after the bytes of a memory that is not shared are replaced: the memory grew, by WasmMemory.Grow or by a memory.grow instruction. It is raised on the thread of the store.

public event EventHandler? DataChanged