On 9/11/2011 12:53 PM, dsimcha wrote:
On 9/11/2011 12:39 PM, Martin Nowak wrote:
RegionAllocator should have a dup function (better name required),
which will call newRegionAllocator on it's stack.
This is needed to create a nested scope without knowing the used stack.

martin

Good idea. This does suggest a little refactoring, though, because I
really don't want to add a **third** way to create a RegionAllocator.

Hmm, just off the top of my head, this might solve tons of problems:

1. Take RegionAllocatorStack out of the public API. It would still exist conceptually, though.

2. newRegionAllocator() -> newRegionAllocator(newStackSize = 0); If newStackSize == 0, return a RegionAllocator that uses the thread-local stack. If newStackSize > 0, create a new stack (hidden from the user) and return a new RegionAllocator on that stack.

3. Add dup(). Maybe call it newInstance() instead, or even just newRegionAllocator(). Someone please suggest a good name. If using explicit stacks the workflow would be:

void fun() {
    // The whole stack is freed on exiting from fun, along with arr.
    auto baseAlloc = newRegionAllocator(42 * 1024);
    auto arr = baseAlloc.newArray!(int[])(666);
    gun(baseAlloc);
}

void gun(RegionAllocator alloc) {
    auto alloc2 = alloc.dup();

    // arr2 is allocated on alloc2, freed on exit.
    auto arr2 = alloc2.newArray!(int[])(42);
}


Reply via email to