caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Some questions regarding OCaml FFI
@ 2017-04-02 22:58 Alexey Egorov
  2017-04-03 15:35 ` Gerd Stolpmann
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Egorov @ 2017-04-02 22:58 UTC (permalink / raw)
  To: caml-list

Hello,

I have some questions about OCaml FFI:

1) Should I declare local variable with CAMLlocalN if its used "temporarily", for example:
CAMLreturn(Val_long(1));
vs
CAMLlocal1(longval);
longval = Val_long(1);
CAMLreturn(longval);

2) Should I call 'caml_acquire_runtime_system()' prior to calling OCaml functions from C main()? If so - should I call it before or after 'caml_startup()'?

Thanks!

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

* Re: [Caml-list] Some questions regarding OCaml FFI
  2017-04-02 22:58 [Caml-list] Some questions regarding OCaml FFI Alexey Egorov
@ 2017-04-03 15:35 ` Gerd Stolpmann
  0 siblings, 0 replies; 2+ messages in thread
From: Gerd Stolpmann @ 2017-04-03 15:35 UTC (permalink / raw)
  To: Alexey Egorov, caml-list

On 03.04.17 00:58, Alexey Egorov wrote:

> Hello,
>
> I have some questions about OCaml FFI:
>
> 1) Should I declare local variable with CAMLlocalN if its used "temporarily", for example:
> CAMLreturn(Val_long(1));
> vs
> CAMLlocal1(longval);
> longval = Val_long(1);
> CAMLreturn(longval);
The specialty of a CAMLlocal variable is that the value is then a local 
root, and this has important effects on the garbage collector. In 
particular the value is considered as reachable (and thus not 
deallocated if it is the last reference to it), and second, if the block 
for this value is moved to a new memory address, the pointer is updated.

Of course, this all is only meaningful if the value is really a block 
(and not just an integer). So in your case this alone means that 
CAMLlocal is not needed. Also, you only need this if you call into the 
OCaml runtime for memory allocation (i.e. a GC is possible). You do not 
do this here (CAMLreturn is harmless in this respect - just deletes the 
local roots).

> 2) Should I call 'caml_acquire_runtime_system()' prior to calling OCaml functions from C main()? If so - should I call it before or after 'caml_startup()'?
Hah! The new naming is not clearer than the old... No, you don't need to.

Gerd

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

end of thread, other threads:[~2017-04-03 15:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-02 22:58 [Caml-list] Some questions regarding OCaml FFI Alexey Egorov
2017-04-03 15:35 ` Gerd Stolpmann

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