Allocator

public protocol Allocator

Protocol for raw-memory allocators.

Allocator is the indirection collections use so they can be parameterised over allocation strategy (e.g. Array[T, A], Buffer[T, A], custom arenas). All three methods are mutating so stateful allocators (arenas, pools) can update their bookkeeping; stateless wrappers around malloc don't need to.

Examples

var alloc = SystemAllocator(); if let .Some(p) = alloc.allocate(Layout.of[Int64]()) { // ... use p ... alloc.deallocate(p, Layout.of[Int64]()) }

Methods

mutating func allocate(Layout) -> RawPointer?

Returns a pointer to a fresh region matching layout, or .None when allocation fails. Returned memory is uninitialised.

mutating func deallocate(RawPointer, Layout)

Releases memory previously returned by allocate / reallocate. layout must match the layout used to obtain the pointer.

Safety

ptr must have been produced by this allocator (or a clone of it) for layout. Mismatching the layout, double-freeing, or freeing a pointer from another allocator is undefined behavior.

mutating func reallocate(RawPointer, Layout, Layout) -> RawPointer?

Resizes the allocation at ptr from oldLayout to newLayout. On failure the original allocation is left intact and .None is returned. On success the old pointer must not be reused — use the returned pointer instead.

Defined in lang/std/memory/allocator.ks