From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Jean-Christophe.Filliatre@ens-lyon.fr
Cc: caml-list@inria.fr
Subject: Re: C strings in O'Caml
Date: Wed, 14 May 1997 18:14:52 +0200 (MET DST) [thread overview]
Message-ID: <199705141614.SAA19082@pauillac.inria.fr> (raw)
In-Reply-To: <199705130659.IAA22102@anjou.ens-lyon.fr> from Jean-Christophe Filliatre at "May 13, 97 08:59:06 am"
> I'm interfacing a C library in O'Caml and I faced the following
> problem. I have a C string declared in the library, and I want to see
> it as a Caml string. Is it possible to do it without copying it ?
If you have control over the allocation of the string, then you can
put a Caml string header in the word preceding the beginning of the
string, and twiddle the last byte of the last word of the string as
described in byterun/alloc.c. Then, you can just pass the char * as a
Caml value and any Caml code should work on it quite happily. (The
char * pointer must be word-aligned, by the way.)
If the C string is allocated outside of your code (e.g. in a library),
then you can't fully disguise it as a Caml string. You can still return
the char * as a Caml value (provided it's word-aligned), but you can't
use String.length neither any of the "safe" string functions (those
that checks the bounds) on this string. You can still use the
undocumented (and highly unsafe) String.unsafe_get, String.unsafe_set,
String.unsafe_blit and String.unsafe_fill on that pseudo Caml string.
Just be very, very careful with out-of-bounds accesses.
- Xavier Leroy
prev parent reply other threads:[~1997-05-15 9:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-05-13 6:59 Jean-Christophe Filliatre
1997-05-14 16:14 ` Xavier Leroy [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=199705141614.SAA19082@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=Jean-Christophe.Filliatre@ens-lyon.fr \
--cc=caml-list@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