Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: oleg@okmij.org
To: p.donadeo@gmail.com, caml-list@inria.fr
Subject: C binding and GC interaction: storing values outside the heap
Date: Wed,  8 Sep 2010 01:41:25 -0700 (PDT)	[thread overview]
Message-ID: <20100908084125.9C47F17460@Adric.ern.nps.edu> (raw)


Paolo Donadeo wrote: 
> The problem is that, for several good reasons, I need a copy, or a
> reference, to the OCaml value representing the lua_State *inside* the
> Lua state (I mean the C data structure).

Data structures with the mixture of OCaml values and non-OCaml
(unboxed) values are needed indeed, it seems. I had to emulate these
data structures since the captured native-code delimited continuation,
a part of the C stack, is exactly such mixed value. Leaking memory
can not be tolerated since continuations could be captured at high
rate (the delimcc distribution has several tests for that).

The solution was a custom GC scanning function. The mixed value is
allocated off the OCaml heap. All allocated values are linked.  The
pointer to the allocated value is wrapped into a OCaml custom block,
with a finalizer, which unlinks the value being finalized. The links
are not NOT heap pointers; they are not known to GC and so do not
prevent finalization. The tricky part is registering a GC scan
call-back. GC invokes the registered call-backs at the end.  The
call-back should go through all linked mixed values, and apply the GC
scanning action (passed as the argument to the callback) to all OCaml
values within the mixed values.

This solution is inspired by the implementation of thread contexts
in the native thread implementation, see the file
	otherlibs/systhreads/posix.c
in the OCaml distribution. The delimcc implementation is in the file
stacks-native.c of the delimcc distribution. 

The solution does not seem to be very efficient; it would be great if
OCaml supported mixed values directly (that is, permitted a custom
block with a custom GC scanning function). When GC scans such a custom
value, it invokes the user-provided function like a GC call-back.
The custom GC callback should know which parts of the mixed value are
OCaml values; it would do the GC action on those values.



             reply	other threads:[~2010-09-08  8:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-08  8:41 oleg [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-09-07 20:58 Paolo Donadeo

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=20100908084125.9C47F17460@Adric.ern.nps.edu \
    --to=oleg@okmij.org \
    --cc=caml-list@inria.fr \
    --cc=p.donadeo@gmail.com \
    /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