[Haskell-cafe] question concerning ANSI "void *"

Galchin Vasili vigalchin at gmail.com
Fri Feb 8 13:55:22 EST 2008


a couple of concrete examples:

typedef struct {char a; int b; char str[8]} type1;

typedef struct {long c; char d} type2;

So to pthread_create (just an example function) we could be passing a struct
of type1 or a struct of type2 .. i.e. arbitrary length and content ... I am
trying to better understand this. I see some of the poke functions mentioned
in the FFI. Which one are you alluding to?

Regards, Vasili


On 2/8/08, Adam Langley <agl at imperialviolet.org> wrote:
>
> 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[1] 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.
>
> Cheers
>
>
>
> [1] http://therning.org/magnus/archives/tag/hsc2hs
> [2] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/
>
> --
> Adam Langley                                      agl at imperialviolet.org
> http://www.imperialviolet.org                       650-283-9641
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080208/deb63180/attachment.htm


More information about the Haskell-Cafe mailing list