From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA01087 for caml-redistribution; Fri, 4 Feb 2000 19:43:06 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA10270 for ; Fri, 4 Feb 2000 17:27:45 +0100 (MET) Received: from tobago.inria.fr (tobago.inria.fr [128.93.8.21]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id RAA19158; Fri, 4 Feb 2000 17:27:35 +0100 (MET) Received: (from doligez@localhost) by tobago.inria.fr (8.6.10/8.6.6) id RAA17717; Fri, 4 Feb 2000 17:27:34 +0100 Date: Fri, 4 Feb 2000 17:27:34 +0100 From: Damien Doligez Message-Id: <200002041627.RAA17717@tobago.inria.fr> To: caml-list@inria.fr, skaller@maxtal.com.au Subject: Re: finalisation Sender: weis >From: skaller >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