[MLton-user] FFI and pointer relocation
Matthew Fluet
fluet at tti-c.org
Mon Nov 26 14:10:08 PST 2007
On Mon, 26 Nov 2007, Dave Herman wrote:
>> GC may move the ref cell. As a rule of thumb (unless you really know
>> what you are doing), any data you wish to modify on the C side should
>> be allocated from the C heap. If you wish this stuff to work from
>> both SML/NJ and MLton, I recommend that you use ML-NLFFI (the learning
>> curve is a bit steep, but it works).
>
> Yeah, I've briefly played with ML-NLFFI before but MLton's native FFI is so
> easy to use that I've stuck with that for now.
>
> For now I'll just allocate the cell in C and export a C function to check if
> the cell has changed.
If there is only one cell (which can be statically allocated), then using
MLton's _address (with the MLton.Pointer structure) or _symbol FFI may
be more efficient. There is a slight overhead in making a C call, and if
you are simply checking the cell's contents, then the above can all be
done without leaving (the generated) ML code. See
http://mlton.org/ForeignFunctionInterfaceSyntax
for the syntax.
More information about the MLton-user
mailing list