[MLton-user] calling MLton from MLton via FFI

"Wesley W. Terpstra" <[email protected]>
Newsgroups gmane.comp.lang.ml.mlton.devel
Message-ID <[email protected]>
On Wed, Mar 9, 2011 at 6:36 PM, Christopher Cramer <[email protected]
> wrote:

> I need to call some C functions with a pointer to the middle of an array.
> The way I have been doing this is to create a function in ML, and export
> it, so that I can convert the array to a pointer and then add the offset.
>
> Is this safe?


No.

The moment you call ML from C any pointers into the heap you previously held
become invalid. You calling back into MLton via FFI is just a *really short*
path through C. ;)

You cannot assume that the parameter pointer given to gzreadoffset is valid.
The garbage collector might move it at any time.

A good way to achieve what you'd like to do is make the gzreadoffset as a
small piece of C code:

int gzreadoffset(gzFile *f,  unsigned char* buf, int offset, int len) {
  gzread(f, buf+offset, len);
  // buf is still valid because we haven't called out to SML
}

_______________________________________________
MLton mailing list
[email protected]
http://mlton.org/mailman/listinfo/mlton
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.