Class Memory
Represents a WebAssembly memory.
Inheritance
Namespace: Wasmtime
Assembly: Wasmtime.Dotnet.dll
Syntax
public class Memory : object
Constructors
| Improve this Doc View SourceMemory(Store, Int64, Nullable<Int64>, Boolean)
Creates a new WebAssembly memory.
Declaration
public Memory(Store store, long minimum = null, long? maximum = null, bool is64Bit = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Store | store | The store to create the memory in. |
| System.Int64 | minimum | The minimum number of WebAssembly pages. |
| System.Nullable<System.Int64> | maximum | The maximum number of WebAssembly pages, or |
| System.Boolean | is64Bit |
|
Fields
| Improve this Doc View SourcePageSize
The size, in bytes, of a WebAssembly memory page.
Declaration
public const int PageSize = null
Field Value
| Type | Description |
|---|---|
| System.Int32 |
Properties
| Improve this Doc View SourceIs64Bit
Gets a value that indicates whether this type of memory represents a 64-bit memory.
Declaration
public bool Is64Bit { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
Maximum
Gets the maximum memory size (in WebAssembly page units).
Declaration
public long? Maximum { get; }
Property Value
| Type | Description |
|---|---|
| System.Nullable<System.Int64> | The maximum memory size (in WebAssembly page units), or |
Minimum
Gets the minimum memory size (in WebAssembly page units).
Declaration
public long Minimum { get; }
Property Value
| Type | Description |
|---|---|
| System.Int64 | The minimum memory size (in WebAssembly page units). |
Methods
| Improve this Doc View SourceGetLength()
Gets the current length of the memory, in bytes.
Declaration
public long GetLength()
Returns
| Type | Description |
|---|---|
| System.Int64 | Returns the current length of the memory, in bytes. |
GetPointer()
Returns a pointer to the start of the memory. The length for which the pointer is valid can be retrieved with GetLength().
Declaration
public IntPtr GetPointer()
Returns
| Type | Description |
|---|---|
| IntPtr | Returns a pointer to the start of the memory. |
Remarks
The pointer may become invalid if the memory grows.
This may happen if the memory is explicitly requested to grow or grows as a result of WebAssembly execution.
Therefore, the returned pointer should not be used after calling the grow method or after calling into WebAssembly code.
GetSize()
Gets the current size of the memory, in WebAssembly page units.
Declaration
public long GetSize()
Returns
| Type | Description |
|---|---|
| System.Int64 | Returns the current size of the memory, in WebAssembly page units. |
GetSpan()
Gets the span of the memory.
Declaration
public Span<byte> GetSpan()
Returns
| Type | Description |
|---|---|
| Span<System.Byte> | Returns the span of the memory. |
Remarks
The span may become invalid if the memory grows.
This may happen if the memory is explicitly requested to grow or grows as a result of WebAssembly execution.
Therefore, the returned span should not be used after calling the grow method or after calling into WebAssembly code.
GetSpan(Int64, Int32)
Gets a span of a section of the memory.
Declaration
public Span<byte> GetSpan(long address, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address of the start of the span. |
| System.Int32 | length | The length of the span. |
Returns
| Type | Description |
|---|---|
| Span<System.Byte> | Returns the span of a section of the memory. |
Remarks
The span may become invalid if the memory grows.
This may happen if the memory is explicitly requested to grow or grows as a result of WebAssembly execution.
Therefore, the returned span should not be used after calling the grow method or after calling into WebAssembly code.
GetSpan<T>(Int32)
Gets the span of the memory viewed as a specific type, starting at a given address.
Declaration
public Span<T> GetSpan<T>(int address)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | address | The zero-based address of the start of the span. |
Returns
| Type | Description |
|---|---|
| Span<T> | Returns the span of the memory. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The span may become invalid if the memory grows.
This may happen if the memory is explicitly requested to grow or grows as a result of WebAssembly execution.
Therefore, the returned span should not be used after calling the grow method or after calling into WebAssembly code.
Note that WebAssembly always uses little endian as byte order. On platforms that use big endian, you will need to convert numeric values accordingly.
GetSpan<T>(Int64, Int32)
Gets a span of a section of the memory.
Declaration
public Span<T> GetSpan<T>(long address, int length)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address of the start of the span. |
| System.Int32 | length | The length of the span. |
Returns
| Type | Description |
|---|---|
| Span<T> | Returns the span of a section of the memory. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The span may become invalid if the memory grows.
This may happen if the memory is explicitly requested to grow or grows as a result of WebAssembly execution.
Therefore, the returned span should not be used after calling the grow method or after calling into WebAssembly code.
Note that WebAssembly always uses little endian as byte order. On platforms that use big endian, you will need to convert numeric values accordingly.
Grow(Int64)
Grows the memory by the specified number of pages.
Declaration
public long Grow(long delta)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | delta | The number of WebAssembly pages to grow the memory by. |
Returns
| Type | Description |
|---|---|
| System.Int64 | Returns the previous size of the Webassembly memory, in pages. |
Remarks
This method will invalidate previously returned values from GetSpan.
Read<T>(Int64)
Read a struct from memory.
Declaration
public T Read<T>(long address)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| T | Returns the struct read from memory. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the struct to read. Ensure layout in C# is identical to layout in WASM. |
Remarks
Note that WebAssembly always uses little endian as byte order. On platforms that use big endian, you will need to convert numeric values accordingly.
ReadByte(Int64)
Reads a byte from memory.
Declaration
public byte ReadByte(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Byte | Returns the byte read from memory. |
ReadDouble(Int64)
Reads a double from memory.
Declaration
public double ReadDouble(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Double | Returns the double read from memory. |
ReadInt16(Int64)
Reads a short from memory.
Declaration
public short ReadInt16(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Int16 | Returns the short read from memory. |
ReadInt32(Int64)
Reads an int from memory.
Declaration
public int ReadInt32(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Returns the int read from memory. |
ReadInt64(Int64)
Reads a long from memory.
Declaration
public long ReadInt64(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Int64 | Returns the long read from memory. |
ReadIntPtr(Int64)
Reads an IntPtr from memory.
Declaration
public IntPtr ReadIntPtr(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| IntPtr | Returns the IntPtr read from memory. |
ReadNullTerminatedString(Int64)
Reads a null-terminated UTF-8 string from memory.
Declaration
public string ReadNullTerminatedString(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.String | Returns the string read from memory. |
ReadSingle(Int64)
Reads a single from memory.
Declaration
public float ReadSingle(long address)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
Returns
| Type | Description |
|---|---|
| System.Single | Returns the single read from memory. |
ReadString(Int64, Int32, Nullable<Encoding>)
Reads a string from memory with the specified encoding.
Declaration
public string ReadString(long address, int length, Encoding? encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
| System.Int32 | length | The length of bytes to read. |
| System.Nullable<Encoding> | encoding | The encoding to use when reading the string; if null, UTF-8 encoding is used. |
Returns
| Type | Description |
|---|---|
| System.String | Returns the string read from memory. |
Write<T>(Int64, T)
Write a struct to memory.
Declaration
public void Write<T>(long address, T value)
where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to read from. |
| T | value | The struct to write. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Note that WebAssembly always uses little endian as byte order. On platforms that use big endian, you will need to convert numeric values accordingly.
WriteByte(Int64, Byte)
Writes a byte to memory.
Declaration
public void WriteByte(long address, byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Byte | value | The byte to write. |
WriteDouble(Int64, Double)
Writes a double to memory.
Declaration
public void WriteDouble(long address, double value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Double | value | The double to write. |
WriteInt16(Int64, Int16)
Writes a short to memory.
Declaration
public void WriteInt16(long address, short value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Int16 | value | The short to write. |
WriteInt32(Int64, Int32)
Writes an int to memory.
Declaration
public void WriteInt32(long address, int value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Int32 | value | The int to write. |
WriteInt64(Int64, Int64)
Writes a long to memory.
Declaration
public void WriteInt64(long address, long value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Int64 | value | The long to write. |
WriteIntPtr(Int64, IntPtr)
Writes an IntPtr to memory.
Declaration
public void WriteIntPtr(long address, IntPtr value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| IntPtr | value | The IntPtr to write. |
WriteSingle(Int64, Single)
Writes a single to memory.
Declaration
public void WriteSingle(long address, float value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.Single | value | The single to write. |
WriteString(Int64, String, Nullable<Encoding>)
Writes a string at the given address with the given encoding.
Declaration
public int WriteString(long address, string value, Encoding? encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int64 | address | The zero-based address to write to. |
| System.String | value | The string to write. |
| System.Nullable<Encoding> | encoding | The encoding to use when writing the string; if null, UTF-8 encoding is used. |
Returns
| Type | Description |
|---|---|
| System.Int32 |