FallbackAllocator

FallbackAllocator is the allocator equivalent of an "or" operator in algebra. An allocation request is first attempted with the Primary allocator. If that returns null, the request is forwarded to the Fallback allocator. All other requests are dispatched appropriately to one of the two allocators.

In order to work, FallbackAllocator requires that Primary defines the owns method. This is needed in order to decide which allocator was responsible for a given allocation.

FallbackAllocator is useful for fast, special-purpose allocators backed up by general-purpose allocators. The example below features a stack region backed up by the GCAllocator.

struct FallbackAllocator (
Primary
Fallback
) {}

Members

Aliases

fallback
alias fallback = Fallback.instance
Undocumented in source.
primary
alias primary = Primary.instance
Undocumented in source.

Functions

alignedAllocate
void[] alignedAllocate(size_t s, uint a)

FallbackAllocator offers alignedAllocate iff at least one of the allocators also offers it. It attempts to allocate using either or both.

alignedReallocate
bool alignedReallocate(void[] b, size_t newSize, uint a)
Undocumented in source. Be warned that the author may not have intended to support it.
allocate
void[] allocate(size_t s)

Allocates memory trying the primary allocator first. If it returns null, the fallback allocator is tried.

deallocate
bool deallocate(void[] b)

deallocate is defined if and only if at least one of the allocators define deallocate. It works as follows. If primary.owns(b), then the request is forwarded to primary.deallocate if it is defined, or is a no-op otherwise. If primary does not own b, then the request is forwarded to fallback.deallocate if it is defined, or is a no-op otherwise.

empty
Ternary empty()

empty is defined if both allocators also define it.

expand
bool expand(void[] b, size_t delta)

expand is defined if and only if at least one of the allocators defines expand. It works as follows. If primary.owns(b), then the request is forwarded to primary.expand if it is defined, or fails (returning false) otherwise. If primary does not own b, then the request is forwarded to fallback.expand if it is defined, or fails (returning false) otherwise.

owns
Ternary owns(void[] b)

owns is defined if and only if both allocators define owns. Returns primary.owns(b) | fallback.owns(b).

reallocate
bool reallocate(void[] b, size_t newSize)

reallocate works as follows. If primary.owns(b), then primary.reallocate(b, newSize) is attempted. If it fails, an attempt is made to move the allocation from primary to fallback.

resolveInternalPointer
Ternary resolveInternalPointer(void* p, void[] result)

resolveInternalPointer is defined if and only if both allocators define it.

Variables

alignment
enum uint alignment;

The alignment offered is the minimum of the two allocators' alignment.

fallback
Fallback fallback;

The fallback allocator.

instance
enum FallbackAllocator instance;

If both Primary and Fallback are stateless, FallbackAllocator defines a static instance called instance.

primary
Primary primary;

The primary allocator.

Meta