[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] idea of how to implement a weak array



On Sun, Mar 05, 2006 at 12:51:13AM +0100, Julian Rohrhuber wrote:
> An array that has been marked weak should behave as if it could not 
> contain pointers (i.e. it is not scanned any further by marking it 
> black instead of grey). When the memory of an object that it points 
> to is reused, the array will point to some weird unpredictable data. 
> The only thing that we can be reasonably sure is that the data will 
> be different from the original object.
>
> Now the weak array could be encapsuled in an object that keeps a 
> normal list of  copies of all objects in the weak array. When trying 
> to access an object, it would compare the data at the two locations 
> and if they are not equal, return nil and remove also the copied 
> object from the second list (which is then garbage
> collected).
>
> This would fail in the (improbable?) case that an equal object is 
> stored at the same memory address again.

nice idea, but there appears to be a problem: memory isn't
necessarily reused directly after finishing a collection, so
the weak array may point to an `object' that is still equal
to the copy but contains dangling pointers.

another proposal: a WeakArray points to instances of
WeakPointer, which store a reference to the array, their
index and the actual data. when the WeakPointer is finalized
(at the beginning of a flip), it nils the corresponding slot
of its WeakArray in the finalizer func. the downside is that
you have to deal with WeakPointers explicitly and not keep
references to the data they point to.

and another one: keep a weak identity dictionary at a
special global location; before a flip, scan the white list
for objects contained in the weak dict and nil any slots
found. the downside is that flips are not in O(1) anymore
(they aren't anyway with finalizers).

<sk>