lightweight struct handling in FFI?

Antony Courtney antony at apocalypse.org
Wed Sep 18 16:19:39 EDT 2002


Hello FFI-ers,

While the FFI spec. is excellent, I'd really like to see a companion 
document with real examples of how to use the FFI for the easy, obvious 
kinds of library interfacing tasks that are likely to arise in practice.

Such examples would hopefully help answer things like the following 
rather naieve question that I have:

Is there a particular "best" way to deal with C functions that take 
"lightweight" struct values (or pointers to such structs), such as the 
classic:

struct point {
   float x, y;
};

?

Should I treat this is an opaque ForeignPtr, use heap allocation 
everywhere, and abandon pattern matching on the Haskell side?  Or should 
I create a data type on the Haskell side, and marshall and un-marshall 
this to the C side as necessary (perhaps by writing wrapper routines 
that take x and y as separate arguments)?  Or can I use Storable in some 
way to do better than either of the above options?  It's just not clear 
to me from the specification.

Any help would be appreciated.

Thanks,

	-antony

-- 
Antony Courtney
Grad. Student, Dept. of Computer Science, Yale University
antony at apocalypse.org          http://www.apocalypse.org/pub/u/antony




More information about the FFI mailing list