[MLton-user] ffi newbie
Matthew Fluet
fluet at tti-c.org
Wed Sep 5 12:22:10 PDT 2007
On Wed, 5 Sep 2007, Sean McLaughlin wrote:
> I'm having trouble using the foreign function interface. The
> bug I have is strange, as I can only get it to occur in the context
> of a larger program.
If you have a large program that exhibits the problem, you can post it to
http://www.mlton.org/TemporaryUpload
> I implemented bindings for CFSQP in OCaml with little trouble,
> and thus have confidence that in principle the SML-CFSQP connection
> should work. The problem I face with MLton is that the functions
> need to take arrays of doubles. Since these must be allocated
> in the ML heap, I'm not sure how to create such arrays in C.
> The solution I came up with is to allocate a global array for
> the arguments in ML, and to set the values from C, finally calling
> the SML function on the global array.
Right, we currently do not have a way of registering roots/pointers with
the garbage collector. When you are in C, a pointer to an ML object isn't
really valid after calling an ML exported function, since during the ML
execution, the garbage collector may have moved the object.
The global array and exporting a function to update the array seems
reasonable, and should work with the FFI.
> I kept getting segfaults though, so I simplified the code until
> it no longer does anything except modify that global argument array.
> I figure it's simple enough now for someone to see my mistake.
The code looks fine to me. I was able to run the program without getting
any segfaults, on both amd64-linux and x86-darwin.
More information about the MLton-user
mailing list