v0.4.0

Allocator interfacev0.1.0

Source
The operations every allocator provides.
pub interface Allocator {
    func Allocate(
        &var self,
        layout: Layout
    ) -> *var opaque throws AllocError;
    func Deallocate(
        &var self,
        block: *var opaque,
        layout: Layout
    ) throws AllocError;
    func Reallocate(
        &var self,
        block: *var opaque,
        oldLayout: Layout,
        newLayout: Layout
    ) -> *var opaque throws AllocError;
}

Written as an interface so a generic can require it — func Build<A: Allocator>(allocator: &var A) borrows the caller's allocator and resolves calls at each instantiation with nothing dispatched at run time — and so a call can borrow any allocator as &var Allocator. Every operation may write its receiver: an arena advances its pointer and a pool its free lists. A container keeps its allocator as an Allocator value, which is a copyable handle to the implementation and owns nothing. A stateless allocator such as SystemAllocator converts to one anywhere and stays valid forever; a stateful one gives a handle only through an unsafe Handle, whose caller promises the allocator outlives it.

Requirements

Allocate

func Allocate(
    &var self,
    layout: Layout
) -> *var opaque throws AllocError

Takes storage matching layout and reports where it landed. The address satisfies the layout's alignment. The contents are undefined. A zero-sized layout gives a non-null aligned address that nothing may be read from or written to. The storage is the caller's until Deallocate takes it back with an equal layout.

NameTypeDescription
layoutLayoutthe size and alignment wanted

Returns: the address of the storage, or why none was taken

Source

Deallocate

func Deallocate(
    &var self,
    block: *var opaque,
    layout: Layout
) throws AllocError

Gives back storage that this allocator handed out. block must be an address this allocator returned, and layout must equal the layout it was given with. Reports InvalidBlock where the allocator can tell, and is undefined where it cannot. Every pointer into the block becomes invalid, and the storage may be handed to the next allocation at once.

NameTypeDescription
block*var opaquean address this allocator returned
layoutLayoutthe layout it was allocated with

Returns: nothing when the release was accepted, or why it was refused

Source

Reallocate

func Reallocate(
    &var self,
    block: *var opaque,
    oldLayout: Layout,
    newLayout: Layout
) -> *var opaque throws AllocError

Changes the size of a block, preserving as much of its contents as both layouts hold. block and oldLayout describe the existing allocation; newLayout is what is wanted. The address reported may or may not be the address it was given, so the caller must use what comes back. On success the first min(old, new) bytes are preserved and the original block is gone. On failure the original block is untouched and still the caller's. A newLayout of size zero reports the shared empty address, exactly as Allocate would, so the result is always releasable with the layout it was asked for. The two layouts must have the same alignment. An allocator reports Unsupported rather than silently moving a block to a different alignment. The old pointer is invalid once this succeeds, whether or not the address changed.

NameTypeDescription
block*var opaquethe existing allocation's address
oldLayoutLayoutthe layout block was allocated with
newLayoutLayoutthe layout wanted, which must have the same alignment

Returns: the address the block is at afterwards, or why it was not resized

Source

Implementations

TypeSummary
ArenaAn allocator that advances a pointer and reclaims everything at once.
FixedBufferAn allocator that advances a pointer through storage it does not own.
PoolAn allocator that serves small blocks from per-class free lists.
SystemAllocatorAn allocator that asks the operating system for pages.

EmptyBlock

pub const EmptyBlock: uint = 0xFFFFFFFFFFFF0000;

The address a zero-sized allocation reports. Not null, so a caller need not branch, and aligned to anything, so it satisfies every layout. Nothing may be read from or written to it: it is an address that exists rather than storage that does. Every allocator in this package returns exactly this for a zero-sized layout, which makes a zero-sized block recognizable and its release free.

Source

EmptyBlockAddress

pub func EmptyBlockAddress() -> *var opaque

The address a zero-sized allocation reports, as a pointer. Takes nothing and returns the same value every time. Cannot fail. Runs in constant time and allocates nothing.

Source

Returns: the address every zero-sized allocation reports

IsEmptyBlock

pub func IsEmptyBlock(block: *var opaque) -> bool

Whether block is the address a zero-sized allocation reports. block is read by value and never dereferenced. Cannot fail. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
block*var opaquethe address to test, never dereferenced

Returns: whether it is the address a zero-sized allocation reports