lily/docs/spec/alloc.md
koniifer d1334ada04 adjust allocator spec to use slices as return type
optimise raw allocator
make allocators use new spec
remove redundant `lily.` prefix from lily tests
kill SimpleAllocator (nobody liked it)
2025-02-02 14:57:11 +00:00

1.1 KiB

allocators

Tip

well designed allocators should ensure they only deallocate or reallocate allocations they made.

  1. all spec compliant allocators should implement:

unless otherwise stated, functions can be optionally inline.
names of arguments are up to programmer discretion.
names and signature of functions must be identical to shown below.

Allocator := struct {
    new := fn(): Self
    /// prepare to be deallocated.
    deinit := fn(self: ^Self): void
    /// should return null on failure.
    /// should dealloc any intermediate allocations on failure.
    alloc := fn(self: ^Self, $T: type, count: uint): ?[]T
    /// same behaviour as alloc, except:
    /// must be zeroed.
    alloc_zeroed := fn(self: ^Self, $T: type, count: uint): ?[]T
    /// same behaviour as alloc, except:
    /// must move data to new allocation,
    /// must ensure the old allocation is freed at some point.
    realloc := fn(self: ^Self, $T: type, ptr: ^T, new_count: uint): ?[]T
    /// must dealloc or schedule the freeing of the given allocation
    dealloc := fn(self: ^Self, $T: type, ptr: ^T): void
}