std.experimental.allocator.building_blocks.fallback_allocator
- struct FallbackAllocator(Primary, Fallback);
-
FallbackAllocatoris the allocator equivalent of an "or" operator in algebra. An allocation request is first attempted with thePrimaryallocator. If that returnsnull, the request is forwarded to theFallbackallocator. All other requests are dispatched appropriately to one of the two allocators.In order to work,
FallbackAllocatorrequires thatPrimarydefines theownsmethod. This is needed in order to decide which allocator was responsible for a given allocation.
FallbackAllocatoris useful for fast, special-purpose allocators backed up by general-purpose allocators. The example below features a stack region backed up by theGCAllocator.- Primary primary;
-
The primary allocator.
- Fallback fallback;
-
The fallback allocator.
- static FallbackAllocator instance;
-
If both
PrimaryandFallbackare stateless,FallbackAllocatordefines a static instance calledinstance. - enum uint alignment;
-
The alignment offered is the minimum of the two allocators' alignment.
- void[] allocate(size_t s);
-
Allocates memory trying the primary allocator first. If it returns
null, the fallback allocator is tried. - void[] alignedAllocate(size_t s, uint a);
-
FallbackAllocatoroffersalignedAllocateiff at least one of the allocators also offers it. It attempts to allocate using either or both. - bool expand(ref void[] b, size_t delta);
-
expandis defined if and only if at least one of the allocators definesexpand. It works as follows. Ifprimary.owns(b), then the request is forwarded toprimary.expandif it is defined, or fails (returningfalse) otherwise. Ifprimarydoes not ownb, then the request is forwarded tofallback.expandif it is defined, or fails (returningfalse) otherwise. - bool reallocate(ref void[] b, size_t newSize);
-
reallocateworks as follows. Ifprimary.owns(b), thenprimary.reallocate(b, newSize)is attempted. If it fails, an attempt is made to move the allocation fromprimarytofallback.If
primarydoes not ownb, thenfallback.reallocate(b, newSize)is attempted. If that fails, an attempt is made to move the allocation fromfallbacktoprimary. - Ternary owns(void[] b);
-
ownsis defined if and only if both allocators defineowns. Returnsprimary.owns(b) | fallback.owns(b). - Ternary resolveInternalPointer(const void* p, ref void[] result);
-
resolveInternalPointeris defined if and only if both allocators define it. - bool deallocate(void[] b);
-
deallocateis defined if and only if at least one of the allocators definedeallocate. It works as follows. Ifprimary.owns(b), then the request is forwarded toprimary.deallocateif it is defined, or is a no-op otherwise. Ifprimarydoes not ownb, then the request is forwarded tofallback.deallocateif it is defined, or is a no-op otherwise. - Ternary empty();
-
emptyis defined if both allocators also define it.- Returns:
primary.empty & fallback.empty
- FallbackAllocator!(Primary, Fallback) fallbackAllocator(Primary, Fallback)(auto ref Primary p, auto ref Fallback f);
-
Convenience function that uses type deduction to return the appropriate
FallbackAllocatorinstance. To initialize with allocators that don't have state, use theiritstatic member.- Examples:
-
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
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_experimental_allocator_building_blocks_fallback_allocator.html