std.experimental.allocator.building_blocks.scoped_allocator
- struct ScopedAllocator(ParentAllocator);
-
ScopedAllocator
delegates all allocation requests toParentAllocator
. When destroyed, theScopedAllocator
object automatically callsdeallocate
for all memory allocated through its lifetime. (ThedeallocateAll
function is also implemented with the same semantics.)deallocate
is also supported, which is where most implementation effort and overhead ofScopedAllocator
go. Ifdeallocate
is not needed, a simpler design combiningAllocatorList
withRegion
is recommended.- Examples:
-
import std.experimental.allocator.mallocator : Mallocator; import std.typecons : Ternary; ScopedAllocator!Mallocator alloc; writeln(alloc.empty); // Ternary.yes const b = alloc.allocate(10); writeln(b.length); // 10 writeln(alloc.empty); // Ternary.no
- Allocator parent;
-
If
ParentAllocator
is stateful,parent
is a property giving access to anAffixAllocator!ParentAllocator
. Otherwise,parent
is an alias forAffixAllocator!ParentAllocator.instance
. - enum auto alignment;
-
Alignment offered
- size_t goodAllocSize(size_t n);
-
Forwards to
parent.goodAllocSize
(which accounts for the management overhead). - void[] allocate(size_t n);
-
Allocates memory. For management it actually allocates extra memory from the parent.
- bool expand(ref void[] b, size_t delta);
-
Forwards to
parent.expand(b, delta)
. - bool reallocate(ref void[] b, size_t s);
-
Reallocates
b
to new sizes
. - Ternary owns(void[] b);
-
Forwards to
parent.owns(b)
. - bool deallocate(void[] b);
-
Deallocates
b
. - bool deallocateAll();
-
Deallocates all memory allocated.
- const pure nothrow @nogc @safe Ternary empty();
-
Returns
Ternary.yes
if this allocator is not responsible for any memory,Ternary.no
otherwise. (Never returnsTernary.unknown
.)
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_experimental_allocator_building_blocks_scoped_allocator.html