Haskell FFI - can you obtain a C Pointer from a Haskell data structure? -


i have quite few c structs structured like

typedef struct {     unsigned int a;     unsigned int b; } structa; 

and lot of functions like

void dosomethingwith(structa*,structb*,structc*); 

is there easy way call these functions haskell ffi? like, there behaves & operator in c? (i imagine there isn't, if there i'd know). have make haskell side data's instance of storeable (i don't have constructor functions these structs).

also: if have pass struct instead of struct pointer (not hypothetical question, have few functions - it's not code can't it), can pass components of struct instead? if want call

void function(structa); 

can with

foreign import ccall "function" :: cuint -> cuint -> io() 

?

to pass reference haskell data c, memory allocated in haskell heap, , c operate on data directly, need to:

  • ensure has correct shape in memory (via storable instance maps identical byte structure structa).
  • allocate , fill pinned memory on haskell heap, via mallocforeignptr

there several consequences consider in approach:

  • ghc de-allocate value once drop references foreignptr -- need sure c side won't touch again
  • you're letting c mess stuff on haskell heap, make sure correct

other options:

  • pass opaque references c via stableptr
  • allocate , memory on c side, , use finalizer free it.

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -