[MLton] Representation of strings in the FFI

Jens Axel Søgaard jensaxel@soegaard.net
Wed, 27 Apr 2005 10:03:32 +0200

Stephen Weeks wrote:

>>Does this mean that it isn't possible to determine the length
>>of an exported string at the C level?
> It is quite possible.  The exported string is both a char* and a
> pointer to a MLton array, from which one can extract the length using
> GC_arrayNumElements.

Ok - the layout of arrays were described in runtime/gc.h. The length
and other GC info is to the left of contents of the array (or string).
I take it, I should use GC_arrayNumElementsp at the C level.

/* The array size is stored before the header */
static inline uint* GC_arrayNumElementsp (pointer a) {
         return ((uint*)a - 2);

static inline int GC_arrayNumElements (pointer a) {
         return *(GC_arrayNumElementsp (a));

Jens Axel Søgaard