FixedBuffer structv0.1.0
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 storable Allocator adapter is needed.
Constructors
FixedBuffer
pub 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.
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.
| Name | Type | Description |
|---|---|---|
storage | *var opaque | the caller's bytes to hand out of |
capacity | uint | how many bytes storage holds |
Returns: the buffer, having handed out nothing
Methods
Handle
pub func Handle(self: *var FixedBuffer) -> FixedBufferHandle
A copyable interface adapter pointing at this allocator.
self is borrowed mutably and stays the caller's. Everything allocated through the handle advances this
allocator's state rather than a copy of it, which is the whole reason the handle exists.
The handle borrows the allocator and must not outlive it or survive it being moved.
Returns: a copyable adapter pointing at this buffer
Reset
pub func Reset(self: &var FixedBuffer)
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.
BytesUsed
pub func BytesUsed(self: &FixedBuffer) -> uint
How many bytes have been handed out, including alignment padding.
self is borrowed.
Returns: how many bytes have been handed out
BytesRemaining
pub func BytesRemaining(self: &FixedBuffer) -> 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
Related
FixedBufferHandle
pub struct FixedBufferHandle {
// private fields
}
A copyable interface adapter pointing at a FixedBuffer.
Implements Allocator
FixedBufferHandle conforms to Allocator.
Allocate
pub func Allocate(
self: &FixedBufferHandle,
layout: Layout
) -> (*var opaque) ! 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.
| Name | Type | Description |
|---|---|---|
layout | Layout | the size and alignment wanted |
Returns: the address of the storage, or why none was taken
Deallocate
pub func Deallocate(
self: &FixedBufferHandle,
block: *var opaque,
layout: Layout
) -> ! 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.
| 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
pub func Reallocate(
self: &FixedBufferHandle,
block: *var opaque,
oldLayout: Layout,
newLayout: Layout
) -> (*var opaque) ! 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.
| 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