[Haskell-cafe] ffi and pass by value structs

Adam Langley agl at imperialviolet.org
Wed Jan 30 12:48:21 EST 2008


On Jan 30, 2008 5:16 AM, Federico Squartini
<federico.squartini at googlemail.com> wrote:
> foreign import ccall "print_couple_ptr" printCouple::Ptr Couple->IO Int
>
> But not the first, as a struct it's not an atomic type.
> So how does one solve this issue?

(This applies to x86-64 only!)

In the case of your struct couple, I believe that it's small enough
that it will be passed in registers:

http://www.x86-64.org/documentation/abi.pdf
> 1. If the size of an object is larger than two eightbytes, or it contains unaligned
>   fields, it has class MEMORY.

However, it's larger than this the C compiler will actually call
memcpy to make a copy before calling the function with a pointer to
the copy on the parent's stack.

With the Haskell FFI, the portable solution is probably to write a
very simple wrapper .c file which takes a pointer and calls the real
function with the contents of that pointer. Then you just call your
wrapper function. You can make these functions by the dozen with the
preprocessor.


AGL

-- 
Adam Langley                                      agl at imperialviolet.org
http://www.imperialviolet.org                       650-283-9641


More information about the Haskell-Cafe mailing list