From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA04134; Tue, 14 Aug 2001 10:25:41 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 KAA04159 for ; Tue, 14 Aug 2001 10:25:40 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f7E8PcP24854; Tue, 14 Aug 2001 10:25:38 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA04124; Tue, 14 Aug 2001 10:25:37 +0200 (MET DST) Date: Tue, 14 Aug 2001 10:25:37 +0200 From: Xavier Leroy To: Chris Hecker Cc: caml-list@inria.fr Subject: Re: [Caml-list] register_global_root, malloc, etc. Message-ID: <20010814102537.A4081@pauillac.inria.fr> References: <4.3.2.7.2.20010726013540.027fa990@shell16.ba.best.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <4.3.2.7.2.20010726013540.027fa990@shell16.ba.best.com>; from checker@d6.com on Thu, Jul 26, 2001 at 01:48:24AM -0700 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk [I see that your message was left unanswered. Hope the following late answer will help.] > If I'm writing a module in C and want to have an abstract type that > corresponds to that struct (allocated with new/malloc/etc. from C), > is the following code correct? > > CAMLlocal1(result); > foo *p = new foo; > p->callback = Val_unit; > register_global_root(&(p->callback)); > result = alloc_small(1,Abstract_tag); > Field(result,0) = (value)p; > CAMLreturn(result); > > Then, later, I can just assign another closure passed to a C function (and CAMLparam'ed) to p->callback without worrying about it, like this: > > value set_callback( value fooval, value callback ) > { > CAMLparam2(fooval,callback); > foo *p = (foo *)Field(fooval,0); > p->callback = callback; > CAMLreturn(Val_unit); > } > > Is that correct? Yes. > Should I use Custom_tag and register all the finalization functions > and whatnot for my abstract type, or is Abstract_tag good enough > assuming I've got a free_foo function that users of the module are > supposed to call to deallocate the abstract type? free_foo should > call remove_global_root before deleting the memory, right? But I > don't need to do anything to explicitly delete the callback or the > Abstract_tag block that was passed in since the GC will handle it? You're 100% correct. The only advantage of Custom_tag over Abstract_tag is that finalization can be handled by the GC instead of by the user (via free_foo). GC-based finalization is safer in the sense that you're certain that the object cannot be reached again by Caml code. With user-managed finalization, there is always the risk that the program will call free_foo, then still use the "foo" value afterwards. - Xavier Leroy ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr