[MLton-user] Calling into SML from C

Matthew Fluet fluet at tti-c.org
Sat Apr 14 03:43:59 PDT 2007


Zach Anderson wrote:
> Vesa Karvonen wrote:
>> On 4/12/07, Zach Anderson <zra at cs.berkeley.edu> wrote:
>>> Is there an easy way to call into SML code from a program with a C
>>> main()?
> 
>> Not at the moment AFAIK.  However, it is easy to make an application
>> with MLton that calls C code (library) which then calls SML code.  For
>> example, you can have an application with an event loop written in C
>> that calls SML callbacks.  The MLton FFI supports calling from C to SML
>> (http://mlton.org/CallingFromCToSML).
> 
> I've essentially decided to go with this solution. I'm instrumenting 
> legacy C code with calls into SML code, so I just have my SML code call 
> the C main, etc.
> 
> However, I've run into a problem. At the moment all that the SML calls 
> do is take a bunch of ints(I've also tried words), and do nothing but 
> return unit. This causes lots and lots of memory to be allocated, and 
> lots of time to be spent in the GC. Are the integers being boxed? What 
> integral type should I use so that the integers I pass to the SML calls 
> aren't boxed? Is that possible?

No integer (or word) type in MLton is boxed.

What constitutes "lots and lots of memory" and "lots of time to be spent 
in GC"?  There will be a certain amount of memory overhead in 
initializing the ML program.  And there will almost certainly be some 
allocation done in calling any exported ML function.  So, I would expect 
that if you write a loop in C that does nothing but repeatedly call an 
exported ML function, then you will see memory being allocated (and 
subsequently GCed).  Do you have example code that demonstrates the 
situation you are seeing?





More information about the MLton-user mailing list