WasmPStack

interface WasmPStack(source)

The "pstack" (pseudo-stack) API is a special-purpose allocator intended solely for use with allocating small amounts of memory such as that needed for output pointers. It is more efficient than the scoped allocation API, and covers many of the use cases for that API, but it has a tiny static memory limit (with an unspecified total size no less than 2kb).

The pstack API is typically used like:

const pstack = sqlite3.wasm.pstack;
const stackPtr = pstack.pointer;
try {
const ptr = pstack.alloc(8);
// ==> pstack.pointer === ptr
const otherPtr = pstack.alloc(8);
// ==> pstack.pointer === otherPtr
...
}finally{
pstack.restore(stackPtr);
// ==> pstack.pointer === stackPtr
}

Properties

Link copied to clipboard
abstract val pointer: JsAny

This property resolves to the current pstack position pointer. This value is intended only to be saved for passing to restore(). Writing to this memory without first reserving it via pstack.alloc() (or equivalent) leads to undefined results.

Link copied to clipboard
abstract val quota: Int

This property resolves to the total number of bytes available in the pstack, including any space which is currently allocated. This value is a compile-time constant.

Link copied to clipboard
abstract val remaining: Int

This property resolves to the amount of space remaining in the pstack.

Functions

Link copied to clipboard
abstract fun alloc(n: Int): WasmPointer
abstract fun alloc(n: String): WasmPointer

Attempts to allocate the given number of bytes from the pstack. On success, it zeroes out a block of memory of the given size, adjusts the pstack pointer, and returns a pointer to the memory. On error, returns throws a WasmAllocError. The memory must eventually be released using pstack.restore().

Link copied to clipboard

Attempts to allocate the given number of bytes from the pstack.

Link copied to clipboard
abstract fun allocChunks(n: Int, sz: Int): ReadonlyArray<WasmPointer>
abstract fun allocChunks(n: Int, sz: String): ReadonlyArray<WasmPointer>

alloc()'s n chunks, each sz bytes, as a single memory block and returns the addresses as an array of n element, each holding the address of one chunk.

Link copied to clipboard
fun WasmPStack.allocChunks(n: Int, sz: IR): ReadonlyArray<WasmPointer>

alloc()'s n chunks, each sz bytes, as a single memory block and returns the addresses as an array of n element, each holding the address of one chunk.

Link copied to clipboard
abstract fun allocPtr(n: Int, safePtrSize: Boolean): JsAny

A convenience wrapper for allocChunks() which sizes each chunk as either 8 bytes (safePtrSize is truthy) or wasm.ptrSizeof (if safePtrSize is falsy).

Link copied to clipboard

Allocates a pointer and set is to 0.

fun WasmPStack.allocPtr(howMany: UInt): ReadonlyArray<WasmPointer>

Allocates howMany pointers as a single chunk of memory and zeroes them out.

Link copied to clipboard
abstract fun restore(pstackPtr: JsAny)

Sets the current pstack position to the given pointer. Results are undefined if the passed-in value did not come from pstack.pointer or if memory allocated in the space before the given pointer are used after this call.