krfkeith / tart

Automatically exported from code.google.com/p/tart
0 stars 0 forks source link

Implement weak references #31

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is actually pretty straightforward to implement. A new class, 
tart.gc.WeakRef, will have a signature like this: "class WeakRef[T <: Object]". 
It contains an internal pointer of type T (Although this should probably be in 
a base class, WeakRefBase, which uses a plain Object pointer.)

Then we override the normal tracing behavior by annotating with the 
@TraceMethod attribute. The trace method will, instead of tracing the 
reference, simply add a pointer to the weak reference to a buffer.

The weak ref buffer is a fixed-length array of pointers to WeakRefBase objects. 
Additional buffers are allocated as needed during garbage collection, and 
maintained in a singly-linked list.

At the end of the garbage collection cycle, iterate through all of the weak 
references in the buffers, and update the pointer value, either to the new 
object location, or setting them to null if the object was dead.

Since the pointer to the head of the buffer list is not a root, the buffers 
will be automatically reclaimed during the next collection cycle.

An additional possibility is to have a more complex WeakRef subclass that gets 
a notification when the object is deleted. This should be called after the 
object has been freed, so as to remove any possibility of the callback causing 
the object to become live again.

Original issue reported on code.google.com by viri...@gmail.com on 23 Mar 2011 at 8:48