zena:memory
import {…} from 'zena:memory';
Classes
Memory
final class Memory
Represents the WASM linear memory as an array-like object. Provides typed access to memory via operator overloading.
size(): i32
Get the current size of linear memory in 64KB pages.
grow(pages: i32): i32
Grow linear memory by the specified number of pages. Returns the previous size in pages, or -1 on failure.
getU8(ptr: i32): i32
Load an unsigned byte from the given address.
setU8(ptr: i32, value: i32): void
Store a byte at the given address.
getI32(ptr: i32): i32
Load a 32-bit signed integer from the given address.
setI32(ptr: i32, value: i32): void
Store a 32-bit integer at the given address.
getI64(ptr: i32): i64
Load a 64-bit signed integer from the given address.
setI64(ptr: i32, value: i64): void
Store a 64-bit integer at the given address.
getF32(ptr: i32): f32
Load a 32-bit float from the given address.
setF32(ptr: i32, value: f32): void
Store a 32-bit float at the given address.
getF64(ptr: i32): f64
Load a 64-bit float from the given address.
setF64(ptr: i32, value: f64): void
Store a 64-bit float at the given address.
FreeListAllocator
final class FreeListAllocator implements Allocator
A free-list allocator that supports both alloc() and free().
Uses a simple first-fit free list. Each block has an 8-byte header: [size: i32, next: i32] (next is only used when block is free)
This is the recommended allocator for general use where memory needs to be reclaimed.
new(startPtr: i32)
BumpAllocator
final class BumpAllocator implements Allocator
A bump-pointer allocator for arena-style memory management.
Fast allocation (just increment a pointer), but free() is a no-op. Use reset() to free all allocations at once.
To create a BumpAllocator, first allocate a region from the root allocator:
if (let (true, region) = defaultAllocator.alloc(4096)) {
let arena = new BumpAllocator(region, 4096);
if (let (true, ptr) = arena.alloc(64)) {
// use ptr
}
arena.reset(); // Reuse the arena
defaultAllocator.free(region);
}
Best for: temporary allocations with known lifetime, scratch buffers.
new(startPtr: i32, size: i32)
Create a BumpAllocator over a pre-allocated memory region.
alloc(bytes: i32): inline (true, i32) | inline (false, _)
Allocate bytes bytes. Returns (true, ptr) on success, (false, _) if exhausted.
allocAligned(bytes: i32, align: i32): inline (true, i32) | inline (false, _)
Allocate with alignment. Returns (true, ptr) on success, (false, _) if exhausted.
free(ptr: i32): void
Free is a no-op for bump allocators. Use reset() instead.
reset(): void
Reset the allocator to its initial state, allowing reuse. WARNING: This invalidates ALL previously allocated pointers!
Interfaces
Allocator
interface Allocator
Interface for linear memory allocators. Libraries should accept Allocator to allow flexible memory strategies.
Allocation methods return (true, ptr) | (false, _) to force callers
to handle allocation failures. Use pattern matching:
if (let (true, ptr) = alloc.alloc(size)) {
// use ptr
} else {
// handle out of memory
}
alloc(bytes: i32): inline (true, i32) | inline (false, _)
Allocate bytes bytes. Returns (true, ptr) on success, (false, _) on failure.
allocAligned(bytes: i32, align: i32): inline (true, i32) | inline (false, _)
Allocate bytes bytes aligned to align (must be power of 2).
free(ptr: i32): void
Free a previously allocated pointer. May be a no-op for some allocators.
Variables
defaultAllocator
let defaultAllocator: FreeListAllocator
The default allocator for general use. Supports alloc() and free(). This is the FreeListAllocator.default instance.