Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell FFI - How to handle C functions that accept or return structs instead of pointers to structs?

Tags:

c

haskell

ffi

Of course the answer is to somehow pass/take a contiguous block of memory, so the question is more about how to do that. For now I could still avoid the issue by writing wrapper functions on the C side, but that's not much of a permament solution.

like image 683
Cubic Avatar asked Jun 05 '12 19:06

Cubic


1 Answers

The FFI doesn't support arbitrary pass by value Haskell storable types.

You may only pass values of type (and some of these are pointers):

Int#, Word#,
Char#,
Float#, Double#,
Addr#,
StablePtr# a, MutableByteArray#, ForeignObj#, and ByteArray#.

So, to pass a structure you must wrap the call via a C wrapper; which takes a pointer and passes its value to the C function you wish to actually call.

A recent GHC extension allows for "primop" imports -- which bypass the FFI mechanism and support arbitrary calling conventions and passing structures via unboxed tuples. E.g.

foreign import prim "ITCHv41_run"
  parseITCHv41# :: Addr# -> Word#
                -> (# Int#, Word#, Word#, Word#, Word#, Word# #)

You can use these to do tricky low level stuff like this.

like image 140
Don Stewart Avatar answered Oct 19 '22 00:10

Don Stewart