I have quite a few C structs structured like
typedef struct {
unsigned int a;
unsigned int b;
} StructA;
And a lot of functions like
void doSomethingWith(StructA*,StructB*,StructC*);
Is there an easy way to call these functions with Haskell FFI? Like, is there something that behaves like the & operator in C? (I imagine there isn't, but if there was I'd like to know). Do I have to make the Haskell side data
's instance of storeable (I don't have any constructor functions for these structs).
Also: If I have to pass a struct instead of a struct pointer (not a hypothetical question, I have a few functions like that - it's not my code so I can't do anything about it), can I just pass the components of the struct instead? Like if I want to call
void function(StructA);
can I do this with
foreign import ccall "function" :: CUInt -> CUInt -> IO()
?
To pass a reference to Haskell data to C, where the memory is allocated in the Haskell heap, and C will operate on the data directly, you need to:
Storable
instance that maps A to the an identical byte structure asStructA
).There are several consequences to consider in this approach:
Other options:
StablePtr