caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Chris Hecker <checker@d6.com>
To: caml-list@inria.fr
Subject: [Caml-list] register_global_root, malloc, etc.
Date: Thu, 26 Jul 2001 01:48:24 -0700	[thread overview]
Message-ID: <4.3.2.7.2.20010726013540.027fa990@shell16.ba.best.com> (raw)


Say I've got the following C struct:

struct foo
{
	int something;
	value callback;
};

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?  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?

Thanks,
Chris

-------------------
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


             reply	other threads:[~2001-07-26  8:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-26  8:48 Chris Hecker [this message]
2001-08-14  8:25 ` Xavier Leroy
2001-08-15  2:26   ` Alexander V. Voinov
2001-08-16  0:59     ` Chris Hecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4.3.2.7.2.20010726013540.027fa990@shell16.ba.best.com \
    --to=checker@d6.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).