caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Remi Vanicat" <remi.vanicat@gmail.com>
To: "OCaml List" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Interfacing with C question...
Date: Thu, 25 Jan 2007 07:27:50 +0100	[thread overview]
Message-ID: <6b8a91420701242227n39a919d8vab80b40140a85740@mail.gmail.com> (raw)
In-Reply-To: <002d01c73fdc$6254d430$6a7ba8c0@treble>

2007/1/24, David Allsopp <dra-news@metastack.com>:
> Sorry if this an RADBOTFM case. Rule 2 in Chapter 18 of the manual states
> that all local variables of type value must be declared using CAMLlocal
> macros. However, later on when demonstrating caml_callback we get the
> statements:
>
> value* format_result_closure = caml_named_value("format_result");
> return strdup(String_val(caml_callback(*format_result_closure, Val_int(n))))
>
> (I've "simplified" the opening lines for clarity here - naturally it should
> be static and once only!).
>
> Two questions arise:
>
> 1. Presumably it's OK to cache values returned by caml_named_value without
> declaring them in a CAMLlocal "call" or by using register_global_root?

No, any C pointer to a caml value must be known to the caml GC,
because the caml GC might move the caml value. But you might no
declare such a pointer if you are sur that the GC won't be triger
between your affectation of the value to the C variable, and the use
of the C variable.*

In the given exemple, this is the case: nothing is done between the
affectation and the use.

By the way, when in doubt, or when you are a begginer not nowing well
how the GC work, you should always use the CAMLlocal call.

> 2. The result of caml_callback is passed straight to String_val. Therefore,
> if I expand the line to:
>
> value result = caml_callback(*format_result_closure, Val_int(n)));
> return strdup(String_val(result));
>
> then does that work ok without using CAMLlocal1(result);

Yes, for the same reason: you do nothing between the affectation of
the result function, and the use of the variable.


  reply	other threads:[~2007-01-25  6:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-24 17:23 David Allsopp
2007-01-25  6:27 ` Remi Vanicat [this message]
2007-01-25 16:26 ` [Caml-list] " Hendrik Tews
2007-01-25 18:29   ` skaller
2007-01-26  0:16     ` Robert Roessler

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=6b8a91420701242227n39a919d8vab80b40140a85740@mail.gmail.com \
    --to=remi.vanicat@gmail.com \
    --cc=caml-list@yquem.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).