Allocator interfacev0.1.0
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.
| Name | Type | Description |
|---|---|---|
layout | Layout | the size and alignment wanted |
Returns: the address of the storage, or why none was taken
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.
| Name | Type | Description |
|---|---|---|
block | *var opaque | an address this allocator returned |
layout | Layout | the layout it was allocated with |
Returns: nothing when the release was accepted, or why it was refused
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.
| Name | Type | Description |
|---|---|---|
block | *var opaque | the existing allocation's address |
oldLayout | Layout | the layout block was allocated with |
newLayout | Layout | the layout wanted, which must have the same alignment |
Returns: the address the block is at afterwards, or why it was not resized
Implementations
| Type | Summary |
|---|---|
Arena | An allocator that advances a pointer and reclaims everything at once. |
FixedBuffer | An allocator that advances a pointer through storage it does not own. |
Pool | An allocator that serves small blocks from per-class free lists. |
SystemAllocator | An allocator that asks the operating system for pages. |
Related
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.
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.
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.
Parameters
| Name | Type | Description |
|---|---|---|
block | *var opaque | the address to test, never dereferenced |
Returns: whether it is the address a zero-sized allocation reports