allocators/core

sourceallocators/core.sx

Arena

pub type Arena = { buf: ref void, offset: usize, cap: usize }

An arena hands out memory from a single large block.

Individual allocations cannot be freed — the whole arena is freed at once. This is ideal for request-scoped or phase-scoped allocations.

Layout of an Arena struct (3 fields × 8 bytes each): offset 0: buf — ref void, pointer to the backing block offset 8: offset — usize, current bump position (bytes used) offset 16: cap — usize, total capacity in bytes

Fields

NameType
bufref void
offsetusize
capusize

AllocError

pub enum AllocError = { OutOfMemory }

An error that occurs when attempting to allocate memory.

Variants

  • OutOfMemory

Allocator

pub spec Allocator = {
    pub fn new(capacity: usize) result[Allocator, AllocError]
    pub fn free(a: self) void
    pub fn alloc(a: self, size: usize) option[ref void]
}

Interface for the classic bump allocator.

Functions

  • pub fn new(capacity: usize) result[Allocator, AllocError]
  • pub fn free(a: self) void
  • pub fn alloc(a: self, size: usize) option[ref void]

new

pub fn (Arena) new(capacity: usize) result[Allocator, AllocError] = trust { ... }

Create a new arena backed by a block of capacity bytes. Capacity must be > 0.

free

pub fn (Arena) free(arena: Arena) void = trust { ... }

Free the entire arena and its backing block.

alloc

pub fn (Arena) alloc(arena: Arena, size: usize) option[ref void]

Allocate size bytes from the arena.

Requires size > 0. Returns none if there is not enough space remaining.

alloc_or_none

pub fn (Arena) alloc_or_none(arena: Arena, size: usize) ref void

Allocate size bytes from the arena, returning none when it is exhausted.

This is a convenience wrapper around alloc for call sites that prefer a nullable pointer over an option result.

alloc_raw

pub fn (Arena) alloc_raw(arena: Arena, size: usize) ref void = trust { ... }

Allocate size bytes without wrapping the result in an option.

Returns none when the arena is exhausted.

used

pub fn (Arena) used(arena: Arena) usize

Return the number of bytes currently used.

cap

pub fn (Arena) cap(arena: Arena) usize

Return the total capacity in bytes.

remaining

pub fn (Arena) remaining(arena: Arena) usize

Return the number of bytes remaining. Contracts: used <= cap, so the subtraction never underflows.

reset

pub fn (Arena) reset(arena: Arena) void = trust { ... }

Reset the arena — all previously allocated pointers become invalid.

Stack

type Stack = { buf: ref void, offset: usize, cap: usize }

A Stack Allocator allows for LIFO (Last-In, First-Out) memory management.

You can "save" the current position and "restore" to it later.

Layout (identical to Arena for binary compatibility): offset 0: buf — ref void offset 8: offset — usize offset 16: cap — usize

Fields

NameType
bufref void
offsetusize
capusize

new

pub fn (Stack) new(capacity: usize) result[Allocator, AllocError] = trust { ... }

Create a new stack allocator.

alloc

pub fn (Stack) alloc(stack: Stack, size: usize) option[ref void]

Allocate memory from the stack.

save

pub fn (Stack) save(stack: Stack) usize

Capture the current state of the stack. The returned usize is a "marker" or "checkpoint".

restore

pub fn (Stack) restore(stack: Stack, checkpoint: usize) void

Restore the stack to a previous checkpoint. This effectively "frees" everything allocated after the 'save' call.

free

pub fn (Stack) free(stack: Stack) void = trust { ... }

Free the underlying memory.

GeneralAllocator

pub spec GeneralAllocator = {
    pub fn alloc(a: self, size: usize) option[ref void]
    pub fn free(a: self, ptr: ref void) void
    pub fn realloc(a: self, ptr: ref void, new_size: usize) option[ref void]
}

Functions

  • pub fn alloc(a: self, size: usize) option[ref void]
  • pub fn free(a: self, ptr: ref void) void
  • pub fn realloc(a: self, ptr: ref void, new_size: usize) option[ref void]

HeapAllocator

pub type HeapAllocator = {  }

A simple heap-backed general allocator.

alloc

pub fn (HeapAllocator) alloc(_: HeapAllocator, size: usize) option[ref void] = trust { ... }

Allocate size bytes.

free

pub fn (HeapAllocator) free(_: HeapAllocator, ptr: ref void) void = trust { ... }

Free a previously allocated block.

realloc

pub fn (HeapAllocator) realloc(_: HeapAllocator, ptr: ref void, new_size: usize) option[ref void] = trust { ... }

Reallocate a block.

bump_alloc_unwrap

pub fn bump_alloc_unwrap[A: Allocator](ac: A, size: usize) ref void

Allocates memory, returns the pointer on success, and returns none on failure.

alloc_in

pub fn alloc_in[A: GeneralAllocator](ac: A, size: usize) option[ref void]

Allocates "size" bytes using the specified allocator.

Returns some containing a pointer to the allocated memory on success, or none if the allocation fails.

Requires size to be greater than zero.

free_in

pub fn free_in[A: GeneralAllocator](ac: A, ptr: ref void) void

Frees memory allocated by the specified allocator.

Does nothing if ptr is none.

realloc_in

pub fn realloc_in[A: GeneralAllocator](ac: A, ptr: ref void, new_size: usize) option[ref void]

Resizes a memory allocation using the specified allocator.

If ptr is none, allocates a new memory block of new_size bytes. Otherwise, resizes the existing allocation to new_size bytes. Returns some containing a pointer to the resized memory on success, or none if the allocation fails. Requires new_size to be greater than zero.

ScopedArenaAllocator

pub spec ScopedArenaAllocator = {}

Opaque handle type for ScopedArena.

ScopedArena

pub type ScopedArena = { head: mut ref void, cur: mut ref void, block_size: mut usize }

A growable, resettable arena made of linked blocks.

reset reclaims all individual allocations at once while keeping the backing blocks for reuse. free releases the blocks and the arena handle.

Fields

NameType
headmut ref void
curmut ref void
block_sizemut usize

ScopedArenaBlock

type ScopedArenaBlock = { buf: mut ref void, offset: mut usize, cap: mut usize, next: mut ref ScopedArenaBlock }

Fields

NameType
bufmut ref void
offsetmut usize
capmut usize
nextmut ref ScopedArenaBlock

scoped_block_new

private
fn scoped_block_new(block_size: usize) result[ref ScopedArenaBlock, AllocError] = trust { ... }

new

pub fn (ScopedArena) new(block_size: usize) result[ScopedArenaAllocator, AllocError] = trust { ... }

Create a new scoped arena. block_size is the size of each backing block.

alloc

pub fn (ScopedArena) alloc(a: ScopedArenaAllocator, size: usize) option[ref void] = trust { ... }

Allocate size bytes from the arena, growing with fresh blocks as needed.

alloc_or_none

pub fn (ScopedArena) alloc_or_none(a: ScopedArenaAllocator, size: usize) ref void

Allocate size bytes, returning none on exhaustion.

alloc_raw

pub fn (ScopedArena) alloc_raw(a: ScopedArenaAllocator, size: usize) ref void = trust { ... }

Allocate size bytes without wrapping the result in an option.

Returns none when allocation fails.

reset

pub fn (ScopedArena) reset(a: ScopedArenaAllocator) void = trust { ... }

Reclaim all arena allocations while retaining the backing blocks.

free

pub fn (ScopedArena) free(a: ScopedArenaAllocator) void = trust { ... }

Release all backing blocks and the arena handle.

ListArena

pub type ListArena = { arena: mut ScopedArenaAllocator }

A general allocator backed by a scoped arena.

This is useful for large groups of short-lived lists that all share one arena and are released together. Individual frees are no-ops, the backing arena is released separately.

Fields

NameType
arenamut ScopedArenaAllocator

alloc

pub fn (ListArena) alloc(a: ListArena, size: usize) option[ref void] = trust { ... }

Allocate a block from the list arena.

free

pub fn (ListArena) free(_: ListArena, _: ref void) void

Individual frees are no-ops, the whole arena is released at once.

realloc

pub fn (ListArena) realloc(a: ListArena, ptr: ref void, new_size: usize) option[ref void] = trust { ... }

Copy into a fresh arena block. The old block remains arena-owned.