v0.4.0

FixedBuffer structv0.1.0

Source
An allocator that advances a pointer through storage it does not own.
pub struct FixedBuffer {
    // private fields
}

Owns no storage and has no destructor, but prohibits copying because duplicated bump state could hand the same bytes to two callers. Use Handle when a stored Allocator handle is needed.

Constructors

FixedBuffer

pub unsafe func FixedBuffer(
    storage: *var opaque,
    capacity: uint
) -> FixedBuffer

An allocator over capacity bytes at storage. storage is borrowed and kept; nothing is copied and nothing is taken ownership of. A capacity of zero, or a null storage, gives an allocator that refuses every request with OutOfMemory — which is a usable allocator rather than an error, and is what a caller testing exhaustion wants. Cannot fail. Runs in constant time and allocates nothing. Safety: the storage must stay valid and untouched by anything else for as long as this allocator or any handle to it is used. Nothing here tracks that.

NameTypeDescription
storage*var opaquethe caller's bytes to hand out of
capacityuinthow many bytes storage holds

Returns: the buffer, having handed out nothing

Source

Methods

Handle

pub unsafe func Handle(*var self) -> Allocator

An Allocator handle addressing this buffer, for a container to keep. self stays the caller's. Everything allocated through the handle advances this allocator's state rather than a copy of it. Safety: the handle addresses the buffer and must not outlive it or survive it being moved, and nothing may Reset the buffer while an allocation made through the handle is still in use. Nothing here tracks that.

Returns: a copyable handle addressing this buffer

Source

Reset

pub func Reset(&var self)

Rewinds every allocation, making the whole buffer available again. self is borrowed mutably. Every pointer this allocator handed out becomes invalid. Cannot fail. Runs in constant time and allocates nothing. Nothing may be read from or written to any earlier allocation afterwards.

Source

BytesUsed

pub func BytesUsed(&self) -> uint

How many bytes have been handed out, including alignment padding. self is borrowed.

Returns: how many bytes have been handed out

Source

BytesRemaining

pub func BytesRemaining(&self) -> uint

How many bytes remain, before any alignment padding the next request needs. self is borrowed. A request of exactly this size succeeds only when it needs no padding.

Returns: how many bytes are left to hand out

Source

Implements Allocator

FixedBuffer conforms to Allocator.

Allocate

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

Takes storage matching layout from the buffer. result receives the address when this reports None. A zero-sized layout gives the shared empty block and consumes nothing. Reports OutOfMemory when what remains cannot hold the request, counting the padding its alignment needs -- so a request the size of BytesRemaining can still fail when it has to be aligned first. Runs in constant time and allocates nothing beyond the buffer. The storage lives until the allocator is reset, and no longer.

NameTypeDescription
layoutLayoutthe size and alignment wanted

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

Source

Deallocate

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

Accepts a release and rewinds only the most recent allocation. Everything else is reclaimed by Reset rather than one block at a time, so an ordinary release reports None having done nothing. Accepting rather than refusing lets this allocator stand in for any other in generic code that releases what it takes.

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

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

Grows or shrinks a block, extending the most recent allocation in place where the buffer has room. The two layouts must share an alignment. Anything that is not the most recent allocation takes new storage and copies, leaving the old space behind until the next reset. Shrinking to a zero-sized layout releases the block and reports the shared empty address rather than the one it rewound off of, so the result is releasable with the layout it was asked for.

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