www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multiple, Interacting GCs

IMHO, what we need (perhaps in 2.0) is the ability to have multiple GCs 
in a program, each managing its own heap.  When any GC detects a 
reference which points into some other heap, it does NOT perform GC on 
that other heap, but it will TELL the other heap about the reference. 
This allows the latter heap to know that some of its objects are not 
garbage (or that they are) even when some references are references 
coming in from other heaps.

Phobos would provide a single, basic GC (probably the one that currently 
exists) which would function as the "root" GC.  It would also include a 
stub GC which calls C-style allocators (a non-GC).

I would propose this sort of syntax:

class MyGC : GC { ... }
MyGC myGC = new MyGC;

class Foo { ... }
Foo foo = myGC.new Foo;  // this is allocated by 'myGC'

Foo f2 = new Foo;  // this is allocated by the phobos GC
Foo f3 = std.gc.nonGC.new Foo;  // this is allocated by malloc()

(Incidentally, in the example above, you notice that the 'myGC' object 
would be allocated by the standard heap allocator and thus its lifetime 
is managed by the standard Phobos GC.)

If the user explicitly deletes an object, they don't have to specify 
which GC should do the delete; the appropriate GC is automatically 

delete foo;  // since foo refers to an object in 'myGC's heap,
              // 'myGC' handles deletion
delete f2;   // phobos deletes this
delete f3;   // 'nonGC' calls free() here
Jun 09 2004