[Haskell-cafe] question concerning ANSI "void *"
agl at imperialviolet.org
Fri Feb 8 12:23:18 EST 2008
On Feb 8, 2008 9:13 AM, Galchin Vasili <vigalchin at gmail.com> wrote:
> Let's take a concrete but "made up" case .. suppose we want to call through
> to pthread_create and pass the (void *) argument to pthread_create which in
> turn gets interpreted by the pthread that is launched. How would one
> populate the C struct that is passed to the launched pthread keeping in mind
> that this C struct is variable in length? From the FFI how would one model
> this C struct?
It tough to be helpful with such a generic request. Here are some
options that you can consider:
1) Write a wrapper function in C which has a nicer FFI interface to
call from Haskell. Using cabal this is pretty painless and, if the
interface suits it, it probably the easiest solution.
2) Call pthread_create directly with the FFI. You can give the FFI
function a Haskell type with 'Ptr ()' or 'Ptr X', it doesn't really
matter. However the type system serves you best, do it that way. This
means that you need to populate the struct yourself in Haskell. The
issue with this is that the local system defines lots of things like
padding and alignment which mean that the layout of the structure in
memory is complex and platform specific. Tools like hsc2hs or c2hs
will be very helpful here. Dealing with the variable length probably
isn't an issue. Usually variable length structures have a fixed header
and a variable tail, where the tail is an array of primitives. You can
malloc the correct sized region, use one of the previous tools to fill
in the fixed header and then use poke to complete the tail.
I might be able to be more helpful if you give the actual struct and
function prototype that you're trying to wrap.
Adam Langley agl at imperialviolet.org
More information about the Haskell-Cafe