Modules

std.mem

Span metadata, copy and equality helpers, and the allocator surface.

Status

Runnable today:

APIReturnNotes
std.mem.copy(dst, src)usizeCopies from Span<u8> into caller-owned MutSpan<u8> storage and returns the copied byte count.
std.mem.fill(dst, value)usizeFills caller-owned MutSpan<u8> storage with a u8 byte and returns the filled byte count.
std.mem.eql(a, b)BoolCompares string-backed byte inputs.
std.mem.span(value)Span<u8>Builds a native Span<u8> view over a string literal.
std.mem.len(bytes)usizeReturns the length of a fixed array, Span<T>, or MutSpan<T>.
std.mem.eqlBytes(a, b)BoolCompares two Span<T>/MutSpan<T> values with the same element type.
std.mem.peekByte(ptr)u8Direct Wasm-only primitive for explicit byte reads from exported linear memory.
std.mem.pokeByte(ptr, value)BoolDirect Wasm-only primitive for explicit byte writes into exported linear memory.
std.mem.nullAlloc()NullAllocCreates an allocator that always returns null, useful for proving code does not allocate.
std.mem.fixedBufAlloc(buffer)FixedBufAllocCreates a mutable fixed-buffer allocator from caller-owned MutSpan<u8> bytes.
std.mem.arena(buffer)FixedBufAllocArena-style alias over the fixed-buffer allocator model; reset rewinds the caller-owned storage.
std.mem.pageAlloc()PageAllocExplicit host allocator handle metadata; it never creates an ambient global allocator.
std.mem.generalAlloc()GeneralAllocExplicit general allocator handle metadata; callers still pass allocator state deliberately.
std.mem.allocBytes(alloc, len)Maybe<MutSpan<u8>>Allocates bytes from NullAlloc or a mutable FixedBufAlloc binding.
std.mem.byteBuf(alloc, len)Maybe<owned<ByteBuf>>Creates an owned byte buffer backed by explicit caller-provided allocator storage.
std.mem.bufBytes(&buf)MutSpan<u8>Borrows writable bytes from an owned ByteBuf.
std.mem.bufLen(&buf)usizeReturns the live length of a ByteBuf.
std.mem.reset(&mut arena)VoidResets caller-owned arena/fixed-buffer allocation state.
std.mem.capacity(arena)usizeReports fixed-buffer capacity.
std.mem.vec(storage)VecMonomorphic byte vector over caller-owned mutable storage.
std.mem.vecPush(&mut vec, value)BoolAppends one byte when capacity remains; returns false instead of growing implicitly.
std.mem.vecLen(&vec)usizeReports current vector length.
std.mem.vecCapacity(&vec)usizeReports caller-provided vector capacity.
std.mem.mapEmpty() / std.mem.setEmpty()Map / SetEmpty fixed metadata values with no allocation.
std.mem.mapLen(&map) / std.mem.setLen(&set)usizeReports 0 for the current empty metadata values.

Example

shape SliceView {    bytes: Span<u8>,    values: Span<i32>,} pub fun main(world: World) -> Void raises {    let bytes: Span<u8> = std.mem.span("zero-memory")    let same = std.mem.span("zero-memory")    let mut scratch: [11]u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]    let copied = std.mem.copy(scratch, bytes)    let mut ints: [3]i32 = [1, 2, 3]    let intSpan: MutSpan<i32> = ints    intSpan[1] = 20    let view = SliceView { bytes: bytes, values: intSpan }    if copied == 11 && std.mem.len(view.bytes) == 11 && std.mem.eqlBytes(view.bytes, same) &&        std.mem.len(view.values) == 3 && std.mem.eqlBytes(view.values, intSpan) {        check world.out.write("memory type forms runnable\n")    }}

Allocator Example

pub fun main(world: World) -> Void raises {    let mut storage: [8]u8 = [0, 0, 0, 0, 0, 0, 0, 0]    let mut alloc: FixedBufAlloc = std.mem.fixedBufAlloc(storage)    let bytes = std.mem.allocBytes(alloc, 4)     if bytes.has {        bytes.value[0] = 90        check world.out.write("fixed buffer allocated\n")    }}

Effects: none beyond writes performed by caller code.

Allocation behavior:

  • NullAlloc always returns null.
  • FixedBufAlloc and Arena return MutSpan<u8> views into caller-owned storage.
  • ByteBuf owns a slice of explicit allocator storage and never reaches for a global heap.

Ownership: returned spans borrow from the original fixed buffer; no heap ownership is created.

Target support: current compiler targets.

Reporting Contract

zero mem --json <input> reports the allocator contract in machine-readable form:

  • memoryBudgets: stack, static, heap, arena, fixed-buffer, collection-capacity, allocator-capacity, requested-allocation, and linear-memory floor budgets.
  • allocatorFacts: NullAlloc, FixedBufAlloc, Arena, PageAlloc, and GeneralAlloc usage, capacity, failure behavior, and hidden-global-allocator status.
  • allocationInstrumentation: pay-as-used hooks for attempts, successes, failures, requested bytes, granted bytes, and peak live bytes.
  • collectionFacts: fixed-capacity Vec, owned ByteBuf, and empty Map/Set metadata, including growth/failure/cleanup behavior.

All heap budgets are explicit. A program that only uses std.mem remains at heapBytes: 0, globalHeapBytes: 0, and hiddenHeapAllocation: false unless an allocator API documents otherwise.

Design Notes

No standard collection may silently allocate from a global heap. Heap-owning APIs will require an allocator capability and document ownership, capacity, and cleanup.