Landin library reference discuss source

Shared: available on every enabled target.

Memory providers and typed storage: the foundation for allocation, initialized prefixes and caller-owned backing.

Call What it does
mem.allocate, mem.release Take a block from a provider, and give it back through the same provider with the same extent.
mem.resize Give a block a new extent, larger or smaller, in place or by moving it.
mem.storage_over Make typed storage over the caller's bytes.
mem.copy, mem.fill, mem.find Copy, set and search byte blocks.

Supply storage explicitly, through allocator evidence. A provider is a handle passed by value, named memory in every signature, so several containers share one provider without locking it. Two things live in other modules: the arena is in core/arena, and core/fault injects deterministic failures over any provider.

resize keeps the bytes that the two extents share. When a resize is refused, the block stays as it was. A provider that cannot resize in place answers with relocate, which allocates, copies and frees. A bump provider of your own needs the arithmetic in aligned_offset.

storage_over does not establish ownership or prove alias lifetimes.

Byte blocks are slices, with their lengths checked:

  • copy puts every byte of a source at the start of a destination. It follows the rule of memmove, so the two may overlap in either direction. When the destination is shorter, it reports out_of_bounds before it writes anything.
  • fill sets every byte.
  • find gives the index of a byte's first occurrence, or the slice's length when there is none. So source[0..<find(source, v)] is always the part before the byte.
  • To compare two byte strings, use cmp.compare.

On hosted targets copy, fill and find move a usize word at a time. How they do it depends on the target and the direction:

Where How the words move
x86-64 and arm64 Copying up and searching load and store words at any address, from the first byte, and move bytes only in the tail.
Other hosted targets, and copying down or filling anywhere Words move only at a multiple of the word. A head before them and a tail after them move a byte at a time.
Firmware Plain byte loops, whose code is smaller.

Items

allocator

concept core/mem/mem.ldn:49 ↑ index
public allocator: type = concept (provider: type)

Allocate the requested byte extent or report out_of_memory. Return backing valid until the matching free; honor the requested alignment.

    alloc: (self: provider, size: usize, alignment: usize)
           -> (block: ptr mut u8) ! out_of_memory

Extend this same block in place. Return false without changing the block or provider state when extension is refused.

    grow: (self: provider, block: ptr mut u8,
           old_size: usize, new_size: usize, alignment: usize)
          -> (grown: bool)

Give a successful block a new byte extent, larger or smaller, in place or by moving it, and return where it now is. Its first min(old_size, new_size) bytes are the ones it held, it honors the same alignment, and it is freed with new_size; a moved block's old address is given back and is not used again. Report out_of_memory with the block and provider unchanged when there is no room for it. A provider that cannot resize in place answers with relocate; one that can grows in place here first, since a container grows its storage through resize alone.

    resize: (self: provider, block: ptr mut u8,
             old_size: usize, new_size: usize, alignment: usize)
            -> (moved: ptr mut u8) ! out_of_memory

Return a successful block with its current extent. The caller ends all uses first and must use the original provider.

    free: (self: provider, block: ptr mut u8, size: usize) -> none
end allocator

Explicit allocation capability, given by a handle: a small copyable value passed by value that refers to its provider's state. Free each successful block once through the provider that allocated it, with its current byte extent. Keep the provider's state and backing alive until all uses end. Alignment zero or one means byte alignment; zero-byte requests follow the provider's documented policy.

byte_buffer

type core/mem/bytes.ldn:9 ↑ index
public byte_buffer: type = buffer

Opaque descriptor for an initialized byte allocation, retaining its full allocation extent.

public storage: type (item: type) = raw(item)

Opaque typed storage over caller-owned backing. Tracks capacity and an initialized prefix; it does not own an allocator or prove the backing lifetime.

The alias is the opaque name other core modules can put in fields and signatures. It still normalizes to raw(item), whose private declaration keeps the representation inaccessible outside this module.

public empty: atom

No initialized item or backing allocation is available for this operation.

public not_empty: atom

The operation requires storage with an empty initialized prefix.

public not_found: atom

The sought key, node, file, argument, variable, text or symbol is not there: what every library lookup that finds nothing fails with (D2028).

out_of_bounds

atom core/mem/mem.ldn:13 ↑ index
public out_of_bounds: atom

The index is outside the initialized prefix.

This atom and empty are the two every core container fails with. An index past the initialized prefix is out_of_bounds whether the container is raw storage, a vector, a spill list or a bounded log; taking from a container with nothing to take is empty. A key lookup that finds nothing fails not_found, because a missing key is not a bad index.

out_of_memory

atom core/mem/mem.ldn:38 ↑ index
public out_of_memory: atom

The provider cannot satisfy the size or alignment request, or its arithmetic cannot represent the extent.

public admit: (item: type, inout store: storage(item),
               escaping into store value: item)
              -> (index: usize) ! out_of_memory

Append a complete value into a spare slot and return its index. Reports out_of_memory without publishing a new prefix when no slot is available.

admit_bytes

function core/mem/mem.ldn:355 ↑ index
public admit_bytes: (inout store: storage(u8), count: usize)
                    -> none ! out_of_memory

Count the first count bytes of the spare capacity, which the caller has written, as initialized: the prefix ends past them. Reports out_of_memory, changing nothing, when fewer than count bytes are spare.

admit_copy

function core/mem/mem.ldn:378 ↑ index
public admit_copy: (inout store: storage(u8), bytes: []u8)
                   -> none ! out_of_memory

Copy bytes after a byte storage's initialized prefix and count them in it, with copy, a word at a time where it moves words. Reports out_of_memory, changing nothing, when the spare capacity is shorter than bytes.

aligned_offset

function core/mem/mem.ldn:563 ↑ index
public aligned_offset: (base: ptr mut u8, used: usize, extent: usize,
                        size: usize, alignment: usize)
                       -> (offset: usize) ! out_of_memory

Return the first offset at or after used within an extent-byte range at base whose address meets alignment and leaves room for size bytes. Reports out_of_memory when the range or the address space cannot hold it. This is the arithmetic every bump provider needs; the caller publishes offset + size as its new use only on success.

Zero alignment has the same byte-aligned meaning as one. Every operation that could overflow is guarded, including the end of a zero- or nonzero- sized result, so failure leaves allocator state for the caller to retain.

allocate

function core/mem/mem.ldn:79 ↑ index
public allocate: (provider: type is allocator, memory: provider,
                  size: usize, alignment: usize)
                 -> (block: ptr mut u8) ! out_of_memory

Request a byte extent with the supplied alignment through the provider. Propagates out_of_memory; the caller owns the resulting allocation obligation.

allocate_bytes

function core/mem/bytes.ldn:20 ↑ index
public allocate_bytes: (provider: type is allocator, memory: provider,
                        count: usize)
                       -> (result: byte_buffer) ! out_of_memory

Allocate count zero-initialized bytes. A zero count returns an empty buffer without calling the provider; allocation failure reports out_of_memory.

allocate_item

function core/mem/allocation.ldn:6 ↑ index
public allocate_item: (item: type, provider: type is allocator,
                       memory: provider, escaping value: item)
                      -> (object: ptr mut item) ! out_of_memory

Allocate storage for one item and store value in it. Reports out_of_memory without returning a partial allocation.

Allocated objects are published only after their complete initial value has been stored. Reference-containing initial values must be retainable.

public bytes: (source: byte_buffer) -> (view: []mut u8 from source)

Lend the buffer's mutable initialized bytes. The buffer must remain allocated while the view is used.

capacity

function core/mem/mem.ldn:312 ↑ index
public capacity: (item: type, store: storage(item)) -> (count: usize)

Return the logical slot capacity, including slots for zero-sized items.

public clear: (item: type, inout store: storage(item)) -> none

Discard all initialized items in constant time while retaining backing and capacity. Resources referred to by those values remain the caller's responsibility.

copy

function ↑ index

When compiler.os == freestanding core/mem/blocks.ldn:20

public copy: (into: []mut u8, source: []u8) -> none ! out_of_bounds

Copy every byte of source to the start of into, as C's memmove does: the two may overlap, in either direction, and into then holds what source held before the copy. Reports out_of_bounds before writing anything when into is shorter than source; bytes of into past the copy are untouched.

A destination that begins inside its source is copied from the last byte down, so no byte is read after it has been overwritten; elsewhere either direction gives the same bytes, and which is taken is not said.

When not (compiler.os == freestanding) core/mem/blocks.ldn:84

public copy: (into: []mut u8, source: []u8) -> none ! out_of_bounds

Copy every byte of source to the start of into, as C's memmove does: the two may overlap, in either direction, and into then holds what source held before the copy. Reports out_of_bounds before writing anything when into is shorter than source; bytes of into past the copy are untouched.

A destination that begins inside its source is copied from the last byte down, so no byte is read after it has been overwritten; elsewhere either direction gives the same bytes, and which is taken is not said.

copy_prefix

function core/mem/mem.ldn:475 ↑ index
public copy_prefix: (item: type, inout target: storage(item),
                     source: storage(item), count: usize)
                    -> none ! out_of_bounds | not_empty | out_of_memory

Copy the first count initialized source items, in order, into an empty target. Reports out_of_bounds when the source holds fewer, not_empty when the target already holds items and out_of_memory when its capacity is too small, each before any mutation. The source remains initialized.

A zero-sized item has one logical value repeated at the same address, so its prefix is established with one complete typed store and extended to count in constant time.

public detach: (item: type, inout store: storage(item))
               -> (block: ptr mut u8 from store)
               ! not_empty | empty

Detach backing from an empty descriptor and reset it. Reports not_empty if items remain, or empty if no allocation exists. The returned pointer must still be freed through its provider.

empty_storage

function core/mem/mem.ldn:148 ↑ index
public empty_storage: (item: type) -> (store: storage(item))

Create an empty descriptor without allocating backing.

A descriptor without an allocation is not a zero-address allocation. It owns no extent, admits no item and cannot yield a pointer on disposal.

fill

function ↑ index

When compiler.os == freestanding core/mem/blocks.ldn:42

public fill: (into: []mut u8, value: u8) -> none

Set every byte of into to value.

When not (compiler.os == freestanding) core/mem/blocks.ldn:238

public fill: (into: []mut u8, value: u8) -> none

Set every byte of into to value.

find

function ↑ index

When compiler.os == freestanding core/mem/blocks.ldn:53

public find: (source: []u8, value: u8) -> (index: usize)

The index of the first byte of source equal to value, or the length of source when there is none, so that source[0..<find(source, v)] is always the part before it.

When not (compiler.os == freestanding); compiler.arch == x86_64 or compiler.arch == arm64 core/mem/blocks.ldn:287

public find: (source: []u8, value: u8) -> (index: usize)

The index of the first byte of source equal to value, or the length of source when there is none, so that source[0..<find(source, v)] is always the part before it.

When not (compiler.os == freestanding); not (compiler.arch == x86_64 or compiler.arch == arm64) core/mem/blocks.ldn:309

public find: (source: []u8, value: u8) -> (index: usize)

The index of the first byte of source equal to value, or the length of source when there is none, so that source[0..<find(source, v)] is always the part before it.

public get: (item: type, store: storage(item), index: usize)
            -> (value: item from held store) ! out_of_bounds

Copy the item at index. Reports out_of_bounds before reading outside the initialized prefix; references inside the copy retain their origin.

public grow: (provider: type is allocator, memory: provider,
              block: ptr mut u8, old_size: usize, new_size: usize,
              alignment: usize) -> (grown: bool)

Grow a block of old_size bytes to new_size where it is, through the provider. Returns true when the block grew; free it with new_size from then on. Returns false, with nothing changed, when the provider refuses, as any provider may.

public length: (item: type, store: storage(item)) -> (count: usize)

Return the number of initialized items in the prefix.

release

function core/mem/mem.ldn:139 ↑ index
public release: (provider: type is allocator, memory: provider,
                 block: ptr mut u8, size: usize) -> none

Return a block to its provider using the allocation's current extent. The caller must end all uses first; no item cleanup runs.

release_bytes

function core/mem/bytes.ldn:58 ↑ index
public release_bytes: (provider: type is allocator, memory: provider,
                       inout value: byte_buffer) -> none

Free the buffer using its saved extent and reset it. Empty buffers require no provider call. End all byte views before deletion.

release_item

function core/mem/allocation.ldn:19 ↑ index
public release_item: (item: type, provider: type is allocator,
                      memory: provider, sink object: ptr mut item) -> none

Free one typed allocation through its original provider. End all aliases first; resources referenced by the item are not released automatically.

Consumption ends this binding only. Copies and outstanding aliases remain the caller's manual-lifetime responsibility, as for the allocator itself.

release_storage

function core/mem/mem.ldn:178 ↑ index
public release_storage: (item: type, provider: type is allocator,
                         inout store: storage(item), memory: provider)
                        -> none

Give a storage's block back through the provider that gave it, first discarding its initialized prefix without reading it, and leave it with no storage. Release what the items refer to first: nothing here does.

relocate

function core/mem/mem.ldn:125 ↑ index
public relocate: (provider: type is allocator, memory: provider,
                  block: ptr mut u8, old_size: usize, new_size: usize,
                  alignment: usize)
                 -> (moved: ptr mut u8) ! out_of_memory

Resize by moving, the resize of a provider that cannot resize in place: allocate new_size bytes with alignment, copy the first min(old_size, new_size) bytes there and free the old block with old_size. A refused allocation reports out_of_memory with the old block untouched. A provider's own resize entry calls it once any in-place change it can make has been refused.

replace

function core/mem/mem.ldn:426 ↑ index
public replace: (item: type, inout store: storage(item), index: usize,
                 escaping into store value: item) -> none ! out_of_bounds

Overwrite an initialized item. Reports out_of_bounds before mutation; the caller handles resources held by the replaced value.

reserve_admit_copy

function core/mem/mem.ldn:277 ↑ index
public reserve_admit_copy: (provider: type is allocator,
                            inout store: storage(u8), memory: provider,
                            want: usize, bytes: []u8)
                           -> none ! out_of_memory

Copy bytes after a byte storage's initialized prefix and count them in it, as admit_copy does, first making room for want bytes in all as reserve_storage does when the storage holds fewer. bytes may be a view of the storage's own block (D1122): the block keeps its bytes when it grows or moves, so they are read where the block now is, and what is appended is what they held when the call began. A move invalidates views of the old block once the call has returned. out_of_memory, when the provider refuses or want bytes would not hold the prefix and bytes, leaves the storage, its prefix and its capacity as they were.

reserve_storage

function core/mem/mem.ldn:241 ↑ index
public reserve_storage: (item: type, provider: type is allocator,
                         inout store: storage(item), memory: provider,
                         want: usize) -> none ! out_of_memory

Make room for at least want items, keeping the initialized prefix. A storage with a block asks the provider to resize it, which grows it in place or moves it; one without, or whose items have no size, takes a fresh block, the prefix copied into it and the old one freed. A move invalidates views of the old block. out_of_memory leaves the storage, its prefix and its capacity as they were. This is the containers' one way of taking slots.

resize

function core/mem/mem.ldn:99 ↑ index
public resize: (provider: type is allocator, memory: provider,
                block: ptr mut u8, old_size: usize, new_size: usize,
                alignment: usize)
               -> (moved: ptr mut u8) ! out_of_memory

Give a block of old_size bytes the extent new_size through the provider, which may move it: the result holds the block's first min(old_size, new_size) bytes and is freed with new_size. Propagates out_of_memory, after which the old block is still the caller's.

spare_bytes

function core/mem/mem.ldn:332 ↑ index
public spare_bytes: (store: storage(u8)) -> (room: []mut u8 from store)

Lend the bytes past a byte storage's initialized prefix, its spare capacity, for the caller to write before admit_bytes counts them: the buffer a read fills. They hold whatever the backing held. End uses of the view before the storage is reserved, released or disposed.

storage_extent

function core/mem/mem.ldn:165 ↑ index
public storage_extent: (item: type, slots: usize)
                       -> (extent: usize) ! out_of_memory

Return the bytes slots items take: zero for a zero-sized item, which still has its slots. Reports out_of_memory when they do not fit usize.

storage_over

function core/mem/mem.ldn:157 ↑ index
public storage_over: (item: type, base: ptr mut u8, slots: usize)
                     -> (result: storage(item) from base)

Describe slots items over an existing byte range without allocating or initializing items. The caller supplies sufficient, correctly aligned backing and keeps it alive; the extent multiplication must be representable.

transfer

function core/mem/mem.ldn:437 ↑ index
public transfer: (item: type, inout target: storage(item),
                  source: storage(item), index: usize)
                 -> (admitted: usize) ! out_of_bounds | out_of_memory

Copy one initialized source item to the end of the target. Checks source index and target capacity before mutation; the source remains initialized.

The complete source value is read before the destination's narrow unchecked append. The new prefix is published only after its typed store.

public used: (item: type, store: storage(item))
             -> (view: []mut item from store)

Lend a mutable slice of the initialized prefix. End uses of the view before invalidating or freeing its backing.

withdraw

function core/mem/mem.ldn:522 ↑ index
public withdraw: (item: type, inout store: storage(item))
                 -> (value: item from held store) ! empty

Remove and return the last initialized item, the raw pop. Reports empty when the prefix is empty; backing and capacity remain available.

Example program

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

The program, 19 lines
compiler/tests/fixtures/runtime/library-shared-mem/main.ldn19 lines
import core/mem

public main: () -> (code: i32) =
    code = 1
    mut bytes: [8]u8 = zeroed
    mem.fill(bytes[0..<8], u8('.'))
    mem.copy(bytes[2..<8], "mem") else none
    mem.copy(bytes[0..<2], "long") else none
    return when mem.find(bytes[0..<8], u8('m')) <> 2
      or bytes[0] <> u8('.') or bytes[5] <> u8('.')
    mut slots: [4]u8 = zeroed
    mut store := mem.storage_over(item: u8, base: addr slots[0], slots: 4)
    _ = mem.admit(store, bytes[3]) else 9
    first := mem.get(store, 0) else u8(0)
    missing := mem.get(store, 1) else u8(0)
    if first == u8('e') and missing == 0 and mem.length(store) == 1 then
        code = 42
    end if
end main