stdx.allocator.building_blocks.fallback_allocator
-
Declaration
struct
FallbackAllocator
(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
Primary
primary
;The
primary
allocator. -
Declaration
Fallback
fallback
;The
fallback
allocator. -
Declaration
static FallbackAllocator
instance
;If both and are stateless, defines a static
instance
called
.instance
-
Declaration
enum uint
alignment
;The
alignment
offered 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
bool
expand
(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
bool
reallocate
(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
Ternary
owns
(void[]b
);is defined if and only if both allocators define . Returns .
-
Declaration
Ternary
resolveInternalPointer
(const void*p
, ref void[]result
);is defined if and only if both allocators define it.
-
Declaration
bool
deallocate
(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
Ternary
empty
();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);