Landin library reference discuss source

Shared: available on every enabled target.

A reclaiming allocator over fixed-size slots and caller-supplied bookkeeping.

over fills the caller's pool.space and returns the pool's handle. The pool allocates no backing storage of its own. Keep both payload backing and metadata alive while allocations exist.

  • It reuses the lowest free slot.
  • It rejects malformed frees without reclaiming a slot.
  • A zero-byte allocation still occupies one slot.
  • A growth and a resize keep the block in its slot, to any extent the slot holds, and refuse a larger one, since no slot is larger. So a list over a pool grows in place until its slot is full.

Items

public provider: type = fixed_pool

A reclaiming allocator over uniform slots in caller storage. It is a handle: copies share one pool. It reuses the lowest free index and has no fallback allocator. Even a zero-byte allocation occupies a slot. A growth or a resize keeps the block in its slot, at any extent the slot holds, and refuses a larger one.

public slot: type = struct
    size: usize
    free_index: usize
end slot

Caller-owned bookkeeping record for one pool slot. Supply an initialized slice to over; the provider maintains its fields after construction.

public space: type = pool_state

Caller-owned state of a pool: its geometry, free-heap size and counters. Declare it zeroed and give it to over, which fills it; it must outlive the pool handle.

allocations

function core/pool/pool.ldn:278 ↑ index
public allocations: (memory: provider) -> (count: usize)

Return the number of successful allocations, including zero-byte allocations.

bookkeeping_capacity

function core/pool/pool.ldn:267 ↑ index
public bookkeeping_capacity: (memory: provider) -> (count: usize)

Return the length of the supplied metadata slice, which may exceed the configured slot count.

capacity

function core/pool/pool.ldn:261 ↑ index
public capacity: (memory: provider) -> (count: usize)

Return the configured number of allocatable slots.

public frees: (memory: provider) -> (count: usize)

Return the number of successful slot reclamations.

public live: (memory: provider) -> (count: usize)

Return the number of currently allocated slots.

public over: (inout state: space, base: ptr mut u8, extent: usize,
              slot_size: usize, slot_count: usize, slot_alignment: usize,
              bookkeeping: []mut slot)
             -> (handle: provider from state, base, bookkeeping)
             ! mem.out_of_memory

Fill state for a pool over caller backing and bookkeeping and return its handle. Validates positive slot size, alignment, extent and record capacity before changing state. Keep state and both backing ranges alive until all allocations end.

slot_size is positive. The caller supplies the initialized bookkeeping slice; its length is the finite bookkeeping capacity, and slot_count may be zero but may not exceed it. The maximum usize marks a free slot, so a pool with slots cannot use that slot size. Such a pool aligns its first slot from the absolute backing address, counts that padding against extent, and proves that every later slot and its complete payload fit both the backing extent and the target address space before publishing the provider.

rejected_frees

function core/pool/pool.ldn:289 ↑ index
public rejected_frees: (memory: provider) -> (count: usize)

Return frees rejected for an invalid address, extent or already-free slot. A rejected free does not reclaim a slot.

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 27 lines
compiler/tests/fixtures/runtime/library-shared-pool/main.ldn27 lines
import core/mem
import core/pool

exercise: () -> (ok: bool) ! mem.out_of_memory =
    ok = false
    mut backing: [64]u8 = zeroed
    mut slots: [1]pool.slot = zeroed
    mut space: pool.space = zeroed
    slab := try pool.over(space, addr backing[0], 64, 16, 1, 1, slots[0..<1])
    address := try mem.allocate(slab, usize(16), usize(1))
    address.val = 42
    return when address.val <> 42 or pool.live(slab) <> 1
    _ = mem.allocate(slab, usize(1), usize(1)) else (problem)
        _ = problem
        mem.release(slab, address, usize(16))
        ok = pool.live(slab) == 0 and pool.frees(slab) == 1
        return
    end
end exercise

public main: () -> (code: i32) =
    code = 1
    ok := exercise() else false
    if ok then
        code = 42
    end if
end main