caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Hendrik Tews <H.Tews@cs.ru.nl>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Interfacing with C question...
Date: Thu, 25 Jan 2007 17:26:22 +0100	[thread overview]
Message-ID: <wwu4pqfrs29.fsf@tandem.cs.ru.nl> (raw)
In-Reply-To: <002d01c73fdc$6254d430$6a7ba8c0@treble> (David Allsopp's message of "Wed, 24 Jan 2007 17:23:36 -0000")

"David Allsopp" <dra-news@metastack.com> writes:

   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");

Note the type! format_result_closure is not of type value, Rule 2
does not apply!

   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?

Yes it is OK. And you cannot use CAMLlocal or
register_global_root, because they only deal with values and not
pointers to values. The manual guarantees that the value pointed
to by the result of caml_named_value doesn't move. Probably
caml_named_value allocates a value outside the heap, registers it
as a global root and gives you back its address.

   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. You should think of values as pointers that point to data
that is moved around by the garbage collector. If there is any
chance that the garbage collector is called, then you must make
sure that it updates your pointer when it moves the data. Hence
you have to register the value.

If the garbage collector is not called under any circumstances
then the data will not move, the pointer doesn't need to get
updated and you don't have to register the value.

Furthermore, if your are sure Is_long(your_variable) is true
under any circumstances, you don't have to register
your_variable, because it is not a pointer. 

Of course all that is strongly discouraged.

Bye,

Hendrik


  parent reply	other threads:[~2007-01-25 16:26 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 ` [Caml-list] " Remi Vanicat
2007-01-25 16:26 ` Hendrik Tews [this message]
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=wwu4pqfrs29.fsf@tandem.cs.ru.nl \
    --to=h.tews@cs.ru.nl \
    --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).