Landin library reference discuss source

Shared: available on every enabled target.

A list that keeps an inline prefix and spills into allocated storage when it fills.

Queries use a pointer, so they do not copy the inline array.

Popping from spilled storage keeps that allocation. release returns the list to an empty inline representation.

Items need no zero image: the list stores pointers and atom-bearing records like any other item.

Items

Types 1

Also on this page

public list: type (item: type, fixed inline_slots: usize) =
    spill_value(item, inline_slots)

Vector with an inline prefix of inline_slots items that spills into allocator-backed storage when it fills. Only initialized slots are readable; no item needs a zero image. Its storage is the module's own.

public at: (item: type, fixed inline_slots: usize,
            inout value: list(item, inline_slots), index: usize)
           -> (slot: ptr mut item from value) ! mem.out_of_bounds

Lend a writable item pointer, with the same binding lock as vec.at [0800]. Reports mem.out_of_bounds before forming an invalid pointer; end the pointer before a push or release.

public capacity: (item: type, fixed inline_slots: usize,
                  value: ptr list(item, inline_slots)) -> (count: usize)

Return the inline capacity before spilling, or the backing vector capacity afterwards.

public clear: (item: type, fixed inline_slots: usize,
               inout value: list(item, inline_slots)) -> none

Forget every item without reading them, keeping any spill storage for the next pushes, as vec.clear does. Does not clean up resources the items refer to.

public get: (item: type, fixed inline_slots: usize,
             value: ptr list(item, inline_slots), index: usize)
            -> (result: item from held value) ! mem.out_of_bounds

Copy the indexed item without copying the container. Reports mem.out_of_bounds outside its initialized prefix.

public last: (item: type, fixed inline_slots: usize,
              value: ptr list(item, inline_slots))
             -> (result: item from held value) ! mem.empty

Copy the last item. Reports mem.empty when there is none.

public length: (item: type, fixed inline_slots: usize,
                value: ptr list(item, inline_slots)) -> (count: usize)

Return the initialized item count. A read-only pointer accepts addresses of mutable and immutable containers without copying their inline storage.

public new: (item: type, fixed inline_slots: usize)
            -> (value: list(item, inline_slots))

Create an empty inline vector without allocating. Unused inline slots have no readable item value.

public pop: (item: type, fixed inline_slots: usize,
             inout value: list(item, inline_slots))
            -> (removed: item from held value) ! mem.empty

Remove and return the last item, retaining spill capacity. Reports mem.empty when empty; popping does not switch back to inline storage.

public push: (item: type, fixed inline_slots: usize,
              provider: type is mem.allocator,
              escaping inout value: list(item, inline_slots),
              memory: provider, escaping into value added: item)
             -> none ! mem.out_of_memory

Append inline while room remains, then spill to allocated storage. Allocation failure leaves the vector unchanged. Growth or the first spill invalidates old item views.

public release: (item: type, fixed inline_slots: usize,
                 provider: type is mem.allocator,
                 inout value: list(item, inline_slots),
                 memory: provider) -> none

Free any spill allocation through its original provider and reset to an empty inline vector. Does not clean up resources referenced by items.

public reserve: (item: type, fixed inline_slots: usize,
                 provider: type is mem.allocator,
                 escaping inout value: list(item, inline_slots),
                 memory: provider, want: usize)
                -> none ! mem.out_of_memory

Ensure room for at least want items: inline while they fit, and otherwise in spill storage of at least want, spilling now if the list has not. Failure leaves the list as it was. Spilling or growing invalidates old item views.

public used: (item: type, fixed inline_slots: usize,
              inout value: list(item, inline_slots))
             -> (view: []mut item from value)

Lend the initialized mutable prefix from inline or spill storage. The container and its backing must remain valid for the view's lifetime.

Example program

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

The program, 29 lines
compiler/tests/fixtures/runtime/library-shared-spill/main.ldn29 lines
import core/arena
import core/mem
import core/spill

exercise: () -> (ok: bool) ! mem.out_of_memory | mem.empty | mem.out_of_bounds =
    ok = false
    mut backing: [32]u8 = zeroed
    mut space: arena.space = zeroed
    memory := arena.over_unchecked(space, addr backing[0], 32)
    mut values := spill.new(item: i32, inline_slots: 1)
    try spill.push(values, memory, i32(41))
    return when spill.length(addr values) <> 1 or arena.length(memory) <> 0
    slot: ptr mut i32 = try spill.at(values, usize(0))
    slot.val = 42
    value := try spill.pop(values)
    return when value <> 42
    _ = spill.pop(values) else (problem)
        ok = problem == mem.empty and spill.length(addr values) == 0
        return
    end
end exercise

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