Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: finalisation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <Damien.Doligez@i...>
Subject: Re: finalisation
>From: skaller <skaller@maxtal.com.au>

>In Vyper, my ocaml based Python interpreter, I have now enabled
>python __del__ methods, which are finalisers for python class instances.
>CPython uses reference counting, and so it finalises an acyclic graph
>of objects top down: first the finaliser (__del__ method) of the root
>object is invoked, possibly causing some child object's ref counts
>to go to zero, in which case they're finalised recursively, then the
>attributes of the object are detached, causing all remaining child
>reference count to be decremented, possibly going to zero (and invoking
>finalisers).

If python specifies reference counting, I guess the only solution is
to implement reference counting.  You can use O'Caml's finalisers to
help you in the following way:

Add a reference count field to each finalisable object.  Initialise it
to 1 upon allocation and register a finalisation function for this
object.  Increment the reference count when you create a new reference
from another finalised object, and decrement it when you remove a
reference (you can do it by simply calling its finalisation function).

The finalisation function will do the following:

1. Decrement the reference count.
2. If the reference count is not 0, stop here.
3. Do your finalisation work for this object.
4. Call the finalisation function of each object that is referenced by
   this one.

The reference keeps track of other finalisable objets that point to
this one, plus one (the initial value) that keeps track of all other
pointers to this object, with the help of O'Caml's GC.


>An algorithm which finds a garbage object on which no other garbage
>objects depend (if any) can proceed to safely finalise that object.
>By applying this algorithm repeatedly to all garbage objects, a correct
>order of finalisation is found for acyclic graphs of objects.
>
>In principle, the garbage collector must already know how to do this,
>although I do not know the details enough to know if there is a
>convenient way to reuse existing code to sequence finalisers
>correctly in this case.

No.  The garbage collector never looks at the pointers between dead
objects.  That's one of the reasons why it is a lot more efficient
than reference counting.

-- Damien