caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] GC, Anonymous Functions, and C
@ 2004-12-17 21:55 Jonathan Roewen
  2004-12-17 22:56 ` Richard Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jonathan Roewen @ 2004-12-17 21:55 UTC (permalink / raw)
  To: caml-list

Hi,

When passing an anonymous ocaml function to a C function, is it safe
to store that value in an array, and then call it later at an abitrary
time, or will the GC reclaim it? And if so, what should be done to
stop the GC from doing so?

Basically, we're trying to do interrupt handling from OCaml, and need
to store the anonymous functions somewhere for the IDT to jump into,
to provide a little context for the problem.

Regards,

Jonathan Roewen


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] GC, Anonymous Functions, and C
  2004-12-17 21:55 [Caml-list] GC, Anonymous Functions, and C Jonathan Roewen
@ 2004-12-17 22:56 ` Richard Jones
  2004-12-17 23:05 ` David Brown
  2004-12-17 23:09 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: Richard Jones @ 2004-12-17 22:56 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1131 bytes --]

On Sat, Dec 18, 2004 at 10:55:23AM +1300, Jonathan Roewen wrote:
> Hi,
> 
> When passing an anonymous ocaml function to a C function, is it safe
> to store that value in an array, and then call it later at an abitrary
> time, or will the GC reclaim it? And if so, what should be done to
> stop the GC from doing so?
> 
> Basically, we're trying to do interrupt handling from OCaml, and need
> to store the anonymous functions somewhere for the IDT to jump into,
> to provide a little context for the problem.

The GC might reclaim the value (function, or whatever) unless you
register it as a global root.  To do this you need to call
caml_register_global_root on each value which you put in the array.
Furthermore you ought to call caml_remove_global_root when you remove
the value from the array and no longer need it.

Rich.

-- 
Richard Jones.  http://www.annexia.org/  http://www.j-london.com/
>>>   http://www.team-notepad.com/ - collaboration tools for teams   <<<
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://youunlimited.co.uk/ - Personal improvement courses

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] GC, Anonymous Functions, and C
  2004-12-17 21:55 [Caml-list] GC, Anonymous Functions, and C Jonathan Roewen
  2004-12-17 22:56 ` Richard Jones
@ 2004-12-17 23:05 ` David Brown
  2004-12-17 23:09 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: David Brown @ 2004-12-17 23:05 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

On Sat, Dec 18, 2004 at 10:55:23AM +1300, Jonathan Roewen wrote:

> When passing an anonymous ocaml function to a C function, is it safe
> to store that value in an array, and then call it later at an abitrary
> time, or will the GC reclaim it? And if so, what should be done to
> stop the GC from doing so?

You'll need to register the value as a global root, using
register_global_root.  Please see the Interfacing C with Objective Caml,
where there is brief mention of this function.  It is also important to
dereference the value each time you use it.

static value hook = Val_int (0);

  ...
  hook = passed_argument;
  register_global_root (&hook);

  ...
  callback (hook, arg);

You can also use remove_global_root() later to remove the reference.

Dave


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] GC, Anonymous Functions, and C
  2004-12-17 21:55 [Caml-list] GC, Anonymous Functions, and C Jonathan Roewen
  2004-12-17 22:56 ` Richard Jones
  2004-12-17 23:05 ` David Brown
@ 2004-12-17 23:09 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: Damien Doligez @ 2004-12-17 23:09 UTC (permalink / raw)
  To: caml-list

On 17 déc. 2004, at 22:55, Jonathan Roewen wrote:

> Basically, we're trying to do interrupt handling from OCaml, and need
> to store the anonymous functions somewhere for the IDT to jump into,
> to provide a little context for the problem.

This is going to be extremely tricky, if it ever works, because
OCaml code can allocate memory by calling malloc.  And malloc
is not reentrant, so calling it from a signal handler cannot be
correct unless your main program never calls malloc, which rules
out a large number of functions of the C stdlib.  And you don't
even know which ones exactly...

-- Damien


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-12-17 23:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-17 21:55 [Caml-list] GC, Anonymous Functions, and C Jonathan Roewen
2004-12-17 22:56 ` Richard Jones
2004-12-17 23:05 ` David Brown
2004-12-17 23:09 ` Damien Doligez

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