auto storage class - infer or RAII?

Boris Kolar boris.kolar at globera.com
Tue Nov 14 01:30:04 PST 2006


== Quote from Boris Kolar (boris.kolar at globera.com)'s article
> 2. Avoiding garbage collection. This can be accomplished, for example, with custom
> allocation (stack, for example). We can solve this by allowing a single parameter
> of type DeterministicAllocator to 'new' keyword in all cases (even if 'new' is not
> overriden in class). Maybe Allocator should be defined as a 'void* delegate()'.
> For example:
>     // defined in Phobos and treated as special by compiler
>     typedef void* delegate() DeterministicAllocator;
>     class Foo {
>         // 'new' is NOT defined here
>     }
>     void test() {
>         DeterministicAllocator myOwnAllocator = stackAllocator;
>         Foo foo = new(myOwnAllocator) Foo();
>     }

Of course, I forgot about deallocaion, so allocators would be defined as something
like:

// somewhere in Phobos ...
typedef void* delegate() Allocate;
typedef void delegate(void*) Deallocate;

struct NormalAllocator {
    Allocate allocate;
    Deallocate deallocate;
}
struct DeterministicAllocator {
    Allocate allocate;
    Deallocate deallocate;
}

// in application
DeterministicAllocator stack = deterministicStack;
void test() {
    Foo foo = new(stack) Foo();
    // foo is destroyed here, because stack is DeterministicAllocator
}



More information about the Digitalmars-d mailing list