Struct RegionAllocatorStack

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

struct 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 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

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

Properties

NameTypeDescription
gcScanned[get] boolWhether this stack is scanned by the garbage collector.

Methods

NameDescription
newRegionAllocator Creates a new RegionAllocator region using this stack.

Examples

import std.regionallocator;

void main() {
    fun1();
}

void fun1() {
    auto stack = RegionAllocatorStack(1_048_576, GCScan.no);
    fun2(stack);

    // At the end of fun1, the last copy of the RegionAllocatorStack
    // instance pointed to by stack goes out of scope.  The memory
    // held by stack is released back to the heap.
}

void fun2(RegionAllocatorStack stack) {
    auto alloc = stack.newRegionAllocator();
    auto arr = alloc.newArray!(double[])(1_024);

    // At the end of fun2, the last copy of the RegionAllocator instance
    // pointed to by alloc goes out of scope.  The memory used by arr
    // is released back to stack.
}