std.experimental.allocator.building_blocks.fallback_allocator.FallbackAllocator/fallbackAllocator
- multiple declarations
Function fallbackAllocator
Convenience function that uses type deduction to return the appropriate
FallbackAllocator
instance. To initialize with allocators that don't have
state, use their it
static member.
FallbackAllocator!(Primary,Fallback) fallbackAllocator(Primary, Fallback)
(
auto ref Primary p,
auto ref Fallback f
);
Example
import std .experimental .allocator .building_blocks .region : Region;
import std .experimental .allocator .gc_allocator : GCAllocator;
import std .typecons : Ternary;
auto a = fallbackAllocator(Region!GCAllocator(1024), GCAllocator .instance);
auto b1 = a .allocate(1020);
writeln(b1 .length); // 1020
writeln(a .primary .owns(b1)); // Ternary.yes
auto b2 = a .allocate(10);
writeln(b2 .length); // 10
writeln(a .primary .owns(b2)); // Ternary.no
Struct 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.
struct FallbackAllocator(Primary, Fallback)
;
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
.
Fields
Name | Type | Description |
---|---|---|
fallback
|
Fallback | The fallback allocator. |
instance
|
FallbackAllocator | If both Primary and Fallback are stateless, FallbackAllocator
defines a static instance called instance .
|
primary
|
Primary | The primary allocator. |
Methods
Name | Description |
---|---|
alignedAllocate
|
FallbackAllocator offers alignedAllocate iff at least one of the
allocators also offers it. It attempts to allocate using either or both.
|
allocate
|
Allocates memory trying the primary allocator first. If it returns null , the fallback allocator is tried.
|
deallocate
|
deallocate is defined if and only if at least one of the allocators
define deallocate . It works as follows. If primary ,
then the request is forwarded to primary if it is defined,
or is a no-op otherwise. If primary does not own b , then the
request is forwarded to fallback if it is defined, or is a
no-op otherwise.
|
empty
|
empty is defined if both allocators also define it.
|
expand
|
expand is defined if and only if at least one of the allocators
defines expand . It works as follows. If primary , then the
request is forwarded to primary if it is defined, or fails
(returning false ) otherwise. If primary does not own b , then
the request is forwarded to fallback if it is defined, or fails
(returning false ) otherwise.
|
owns
|
owns is defined if and only if both allocators define owns .
Returns primary .
|
reallocate
|
reallocate works as follows. If primary , then primary is attempted. If it fails, an attempt is
made to move the allocation from primary to fallback .
|
resolveInternalPointer
|
resolveInternalPointer is defined if and only if both allocators
define it.
|