stdx.allocator.building_blocks.fallback_allocator
-
Declaration
structFallbackAllocator(Primary, Fallback);is the allocator equivalent of an "or" operator in algebra. An allocation request is first attempted with the allocator. If that returns , the request is forwarded to the allocator. All other requests are dispatched appropriately to one of the two allocators.
Discussion
In order to work, requires that defines the method. This is needed in order to decide which allocator was responsible for a given allocation.
is useful for fast, special-purpose allocators backed up by general-purpose allocators. The example below features a stack region backed up by the .-
Declaration
Primaryprimary;The
primaryallocator. -
Declaration
Fallbackfallback;The
fallbackallocator. -
Declaration
static FallbackAllocatorinstance;If both and are stateless, defines a static
instancecalled.instance -
Declaration
enum uintalignment;The
alignmentoffered is the minimum of the two allocators'alignment. -
Declaration
void[]allocate(size_ts);Allocates memory trying the primary allocator first. If it returns , the fallback allocator is tried.
-
Declaration
void[]alignedAllocate(size_ts, uinta);offers iff at least one of the allocators also offers it. It attempts to allocate using either or both.
-
Declaration
boolexpand(ref void[]b, size_tdelta);is defined if and only if at least one of the allocators defines . It works as follows. If , then the request is forwarded to if it is defined, or fails (returning ) otherwise. If does not own , then the request is forwarded to if it is defined, or fails (returning ) otherwise.
-
Declaration
boolreallocate(ref void[]b, size_tnewSize);works as follows. If , then is attempted. If it fails, an attempt is made to move the allocation from to .
Discussion
If does not own , then is attempted. If that fails, an attempt is made to move the allocation from to .
-
Declaration
Ternaryowns(void[]b);is defined if and only if both allocators define . Returns .
-
Declaration
TernaryresolveInternalPointer(const void*p, ref void[]result);is defined if and only if both allocators define it.
-
Declaration
booldeallocate(void[]b);is defined if and only if at least one of the allocators define . It works as follows. If , then the request is forwarded to if it is defined, or is a no-op otherwise. If does not own , then the request is forwarded to if it is defined, or is a no-op otherwise.
-
Declaration
Ternaryempty();is defined if both allocators also define it.
Return Value
-
-
Declaration
FallbackAllocator!(Primary, Fallback)fallbackAllocator(Primary, Fallback)(auto ref Primaryp, auto ref Fallbackf);Convenience function that uses type deduction to return the appropriate instance. To initialize with allocators that don't have state, use their static member.
Examples
import stdx.allocator.building_blocks.region : Region; import stdx.allocator.gc_allocator : GCAllocator; import stdx.allocator.internal : Ternary; auto a = fallbackAllocator(Region!GCAllocator(1024), GCAllocator.instance); auto b1 = a.allocate(1020); assert(b1.length == 1020); assert(a.primary.owns(b1) == Ternary.yes); auto b2 = a.allocate(10); assert(b2.length == 10); assert(a.primary.owns(b2) == Ternary.no);