caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Alexey Egorov <electreg@list.ru>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Some questions regarding OCaml FFI
Date: Mon, 3 Apr 2017 17:35:39 +0200	[thread overview]
Message-ID: <37807ca9-e783-f69e-04f1-e8edf653f8d3@gerd-stolpmann.de> (raw)
In-Reply-To: <1491173922.446722065@f400.i.mail.ru>

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

      reply	other threads:[~2017-04-03 15:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-02 22:58 Alexey Egorov
2017-04-03 15:35 ` Gerd Stolpmann [this message]

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=37807ca9-e783-f69e-04f1-e8edf653f8d3@gerd-stolpmann.de \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=electreg@list.ru \
    /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).