[MLton] Re: [MLton-user] FFI and pointer relocation
Matthew Fluet
fluet at tti-c.org
Wed Nov 28 09:22:46 PST 2007
On Wed, 28 Nov 2007, Wesley W. Terpstra wrote:
> My comments were mostly motivated by the aesthetic cleanliness of using a
> single C call followed by _symbol invocations to grab the result and only
> noticed the cost of IntInf conversions afterwards. Your solution to the FFI
> issue raised on mlton-user was a good MLton FFI programming pattern I wanted
> to emphasize as being generally useful.
Actually, I don't think either the existing FFI pattern or the pattern I
proposed on mlton-user is the best one for this situation. Dave Herman's
situation (as I understood it) was one where the cell would be updated
asynchronously by the C side (via a signal handler or a callback handed to
a threaded GUI). The Time_getTimeOfDay function updates the time cell(s)
synchronously. So, the best pattern would seem to be
C_Time_t Time_sec;
C_SUSeconds_t Time_usec;
C_Int_t Time_getTimeOfDay(Ref(C_Time_t) sec, Ref(C_SUSeconds_t) usec) {
struct timeval timeval;
int res;
res = gettimeofday (&timeval, (struct timezone*)NULL);
*((C_Time_t*)sec) = timeval.tv_sec;
*((C_SUSeconds_t*)usec) = timeval.tv_usec;
return res;
}
The ML ref cells will almost certainly be globalized (even if one uses the
One structure to make them thread safe), so there would be no allocation
overhead in the call to Time_getTimeOfDay.
More information about the MLton
mailing list