Landin library reference source

core/small

Shared: available on every enabled target.

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

Queries use a pointer to avoid copying the inline array. Popping from spilled storage retains that allocation; release returns to an empty inline representation. The element type must supply zeroable evidence.

Items

Executable example

This complete program is maintained in the repository runtime tests. View source.

import core/mem
import core/small

exercise: () -> (ok: bool) ! mem.out_of_memory | mem.empty | mem.out_of_bounds =
    ok = false
    mut backing: [32]u8 = zeroed
    mut state := mem.arena_over_unchecked(addr backing[0], 32)
    mut values := small.new(item: i32, inline_slots: 1)
    try small.push(values, state, i32(41))
    return when small.length(addr values) <> 1 or mem.arena_used(state) <> 0
    slot: ptr mut i32 = try small.at(values, usize(0))
    slot.val = 42
    value := try small.pop(values)
    return when value <> 42
    _ = small.pop(values) else (problem)
        ok = problem == mem.empty and small.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

small type

public small: type (item: type is zeroable, fixed inline_slots: u32) = struct
    storage: small_value(item, inline_slots)
end small

core/small/small.ldn:17

Vector with an inline prefix and allocator-backed spill storage. Only initialized slots are readable; the item type must conform to zeroable.

at function

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

core/small/small.ldn:140

Lend a writable item pointer. Reports mem.out_of_bounds before forming an invalid pointer; end the pointer before a push or release.

A writable slot, with the same binding lock as vec.at [0800].

capacity function

public capacity: (item: type is zeroable, fixed inline_slots: u32,
                  value: ptr small(item, inline_slots)) -> (count: usize)

core/small/small.ldn:41

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

get function

public get: (item: type is zeroable, fixed inline_slots: u32,
             value: ptr small(item, inline_slots), index: usize)
            -> (result: item from value) ! mem.out_of_bounds

core/small/small.ldn:122

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

length function

public length: (item: type is zeroable, fixed inline_slots: u32,
                value: ptr small(item, inline_slots)) -> (count: usize)

core/small/small.ldn:34

Return the initialized item count without copying inline storage.

A read-only pointer accepts addresses of mutable and immutable containers without copying their inline storage.

new function

public new: (item: type is zeroable, fixed inline_slots: u32)
            -> (value: small(item, inline_slots))

core/small/small.ldn:23

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

pop function

public pop: (item: type is zeroable, fixed inline_slots: u32,
             inout value: small(item, inline_slots))
            -> (removed: item from value) ! mem.empty

core/small/small.ldn:150

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

push function

public push: (item: type is zeroable, fixed inline_slots: u32,
              provider: type is mem.allocator,
              escaping inout value: small(item, inline_slots),
              inout state: provider, escaping added: item)
             -> none ! mem.out_of_memory

core/small/small.ldn:73

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.

release function

public release: (item: type is zeroable, fixed inline_slots: u32,
                 provider: type is mem.allocator,
                 inout value: small(item, inline_slots),
                 inout state: provider) -> none

core/small/small.ldn:165

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

used function

public used: (item: type is zeroable, fixed inline_slots: u32,
              inout value: small(item, inline_slots))
             -> (view: []mut item from value)

core/small/small.ldn:110

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