Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
std.experimental.allocator.building_blocks.fallback_allocator
- struct FallbackAllocator(Primary, Fallback);
- 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.
- Primary primary;
- Fallback fallback;
- static FallbackAllocator instance;
- enum uint 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);
- bool expand(ref 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.
- bool reallocate(ref void[] b, size_t newSize);
- Ternary owns(void[] b);
- void[] resolveInternalPointer(void* p);
- 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.
- Ternary 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 FallbackAllocator instance. To initialize with allocators that don't have state, use their it static member.Examples:
import std.experimental.allocator.building_blocks.region : Region; import std.experimental.allocator.gc_allocator : GCAllocator; 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);