From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 19F34BC69 for ; Tue, 14 Aug 2007 12:15:38 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7EAFaQr001167 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Tue, 14 Aug 2007 12:15:37 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1IKtR1-0004uv-00 for ; Tue, 14 Aug 2007 11:15:35 +0100 Date: Tue, 14 Aug 2007 11:15:35 +0100 To: caml-list@inria.fr Subject: Weak hash table for attaching extra data to an object Message-ID: <20070814101535.GA14485@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.9i From: Richard Jones X-Miltered: at discorde with ID 46C180C8.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; hash:01 node:01 nodes:01 hash:01 ocaml:01 pointers:01 bindings:01 pxp:01 nodes:01 callers:98 garbage:01 garbage:01 remi:01 vanicat:01 data:02 Problem: I have a module which I can't edit[1]. This module, let's call it Document, stores a representation of an XML tree in internal node objects: type intnode What I want to do is -- completely outside Document -- attach my own extra information to these nodes. The key point is that if the garbage collector collects an intnode, then I want my extra information to be garbage collected too. My proposed solution would use a weak hash table similar to / derived from this one: http://remi.vanicat.free.fr/ocaml/hweak/. I would store in this hash table mappings from (weak) intnode pointers to my extra data structure. Remi's Hweak module isn't quite what I need because the data is also weak. Since nothing except the hash table itself is tracking my extra data, the GC can potentially drop all of the bindings, and that's not much help. I tried to modify Hweak so that the data is not weak, but that proved to be quite hard. I wonder if anyone has done this, or can suggest a better way to solve this problem? The issue of attaching extra data to some existing object would seem to be a fairly common one ... Rich. [1] By "can't edit" I really mean "don't want to modify". I want Document to be general purpose. Also, unlike PXP, I want multiple callers to be able to individually attach data to nodes. -- Richard Jones Red Hat