caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* C strings in O'Caml
@ 1997-05-13  6:59 Jean-Christophe Filliatre
  1997-05-14 16:14 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Jean-Christophe Filliatre @ 1997-05-13  6:59 UTC (permalink / raw)
  To: caml-list


[version française à la fin]

Hello,

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 ? (I
know  how to  do  it using copy_string).   I read the reference manual
about C interfacing,  but there  is nothing  about that. Thank  you in
advance for any answer.

-- 
Jean-Christophe FILLIATRE
  email: Jean-Christophe.Filliatre@ens-lyon.fr
  WWW  : http://www.ens-lyon.fr/~jcfillia


[français]

Bonjour,

J'interface une bibliothèque C en O'Caml et  j'ai le problème suivant.
J'ai une chaîne  de caractères dans la  biblio C que je  voudrais voir
comme  une  chaîne  de caractères  O'Caml.  Cela  est-il possible sans
copier la chaîne ? (Je sais le faire en utilisant copy_string). Il n'y
a rien à  ce propos dans le manuel  de référence. Merci d'avance  pour
toute réponse.

-- 
Jean-Christophe FILLIATRE
  email: Jean-Christophe.Filliatre@ens-lyon.fr
  WWW  : http://www.ens-lyon.fr/~jcfillia







^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C strings in O'Caml
  1997-05-13  6:59 C strings in O'Caml Jean-Christophe Filliatre
@ 1997-05-14 16:14 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1997-05-14 16:14 UTC (permalink / raw)
  To: Jean-Christophe.Filliatre; +Cc: caml-list

> 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





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-05-15  9:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-05-13  6:59 C strings in O'Caml Jean-Christophe Filliatre
1997-05-14 16:14 ` Xavier Leroy

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).