Garbage Collection Idea
Craig Black
cblack at ara.com
Sat Jun 3 19:40:10 PDT 2006
"Sean Kelly" <sean at f4.ca> wrote in message
news:e5prin$1jcc$2 at digitaldaemon.com...
> Craig Black wrote:
>>
>>> Another potential problem is this:
>>>
>>> # class Foo { ... }
>>> # Foo bar = new Foo;
>>> # void* quxx = cast(void*)bar;
>>>
>>> What happens now? We can no longer tell what type of memory is pointed
>>> to by quxx, but we *still* need to scan it. The only way I can think of
>>> to solve this would be to mark each segment of memory with the kind of
>>> allocation (class, struct or otherwise), and what the type is. That, or
>>> you can just revert to blind scanning, but then it's no better than the
>>> default GC (and more complex code-wise).
>>
>> I didn't initially consider this case, but it wouldn't be to hard to
>> accomodate it. Each allocation unit, whether it is a class instance or
>> not, would have to get an id that describes its identity.
>
> char** c = cast(char**) malloc( HEIGHT );
> memset( c, 0, HEIGHT );
> gc_addRange( c, c + HEIGHT );
Good point. With my approach, the GC API would have to be altered such that
you would not be able to add a range to it without specifying an identity.
However, you could have an "unknown" identity, in which case the GC would
work on the allocation unit in a traditional fashion.
> Where would this identity data go? Another example:
>
> class C {
> int val;
> }
> int* p;
> {
> C c = new C;
> p = &c.val;
> }
>
> The instance of C must be kept alive because there is a reference to it...
> but to a member element, not the root of the object.
The GC has the range information, so this is not a problem. If the pointer
lies within the range of the allocation unit, then it knows where the
identity information would be kept, (either at the beginning or end of the
allocation unit).
-Craig
More information about the Digitalmars-d
mailing list