RegionAllocatorStack

This object represents a segmented stack. Memory can be allocated from this stack using a std.regionallocator RegionAllocator. object. Multiple RegionAllocator objects may be created per RegionAllocatorStack but each RegionAllocator uses a single RegionAllocatorStack.

For most use cases it's convenient to use the default thread-local instance of RegionAllocatorStack, which is lazily instantiated on the first call to the global function std.regionallocator.newRegionAllocator. Occasionally it may be useful to have multiple independent stacks in one thread, in which case a RegionAllocatorStack can be created manually.

RegionAllocatorStack is reference counted and has reference semantics. When the last copy of a given instance goes out of scope, the memory held by the RegionAllocatorStack instance is released back to the heap. This cannot happen before memory allocated to a RegionAllocator instance is released back to the stack, because a RegionAllocator holds a copy of the RegionAllocatorStack instance it uses.

Constructors

this
this(size_t segmentSize, GCScan shouldScan)

Create a new RegionAllocatorStack with a given segment size in bytes.

Members

Functions

gcScanned
bool gcScanned()

Whether this stack is scanned by the garbage collector.

newRegionAllocator
RegionAllocator newRegionAllocator()

Creates a new RegionAllocator region using this stack.

Examples

1 import std.regionallocator;
2 
3 void main() {
4     fun1();
5 }
6 
7 void fun1() {
8     auto stack = RegionAllocatorStack(1_048_576, GCScan.no);
9     fun2(stack);
10 
11     // At the end of fun1, the last copy of the RegionAllocatorStack
12     // instance pointed to by stack goes out of scope.  The memory
13     // held by stack is released back to the heap.
14 }
15 
16 void fun2(RegionAllocatorStack stack) {
17     auto alloc = stack.newRegionAllocator();
18     auto arr = alloc.newArray!(double[])(1_024);
19 
20     // At the end of fun2, the last copy of the RegionAllocator instance
21     // pointed to by alloc goes out of scope.  The memory used by arr
22     // is released back to stack.
23 }

Meta