caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Joel Reymont <joelr1@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
Date: Wed, 28 Mar 2007 12:47:05 +0100	[thread overview]
Message-ID: <20070328114704.GC1202@furbychan.cocan.org> (raw)
In-Reply-To: <E888843F-390B-49FB-82E5-662088922B56@gmail.com>

On Wed, Mar 28, 2007 at 12:33:46PM +0100, Joel Reymont wrote:
> The other issue is that I don't know how to deal with strings  
> allocated in OCaml. Will they be  collected automatically?
> 
> The strings _must_ be allocated in OCaml but I can guarantee that  
> they will not be modified in the C code. Any suggestions on how to  
> deal with this?

String_val (ocaml_str) will give you a pointer to the bytes in the
string.  It's even ASCIIZ (ie. ASCII NUL-terminated) so you can
directly use C str* functions on it.

There are two catches: The small one is that OCaml strings may contain
NUL characters internally, which is obviously not allowed in C
strings, so you have to either guarantee on the OCaml side that this
won't be the case, or else go through hoops on the C side.

The bigger catch is that the garbage collector can and will move the
string around whenever it likes.  So if you save the pointer from
String_val, go back into OCaml code and back into C, and try to reuse
the saved pointer, it won't necessarily point to the string any more.
The string might even have been freed by the GC.  If you want to do
that, either rewrite your code so it doesn't save the pointer like
this, strdup the string to make a private copy in C, or register a
global root.

Rich.

-- 
Richard Jones
Red Hat


  reply	other threads:[~2007-03-28 11:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-28 11:33 Joel Reymont
2007-03-28 11:47 ` Richard Jones [this message]
2007-03-28 12:25 ` [Caml-list] " micha
2007-03-28 12:51   ` Joel Reymont
2007-03-28 13:28     ` Richard Jones
2007-03-28 14:34     ` micha
2007-03-28 14:46       ` Joel Reymont
2007-03-28 14:56         ` micha
2007-03-28 14:57         ` Xavier Leroy
2007-03-28 15:07           ` Joel Reymont
2007-03-28 22:47             ` Joel Reymont
2007-03-28 12:55 ` Serge Aleynikov
2007-03-28 12:01   ` Joel Reymont
2007-03-28 13:40     ` Serge Aleynikov
2007-03-28 12:13   ` Joel Reymont
2007-03-28 13:53     ` Serge Aleynikov

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=20070328114704.GC1202@furbychan.cocan.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=joelr1@gmail.com \
    /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).