[Haskell-cafe] question concerning ANSI "void *"
Galchin Vasili
vigalchin at gmail.com
Fri Feb 8 12:13:46 EST 2008
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?
Thanks, Vasili
On 2/7/08, Adam Langley <agl at imperialviolet.org> wrote:
>
> 2008/2/7 Galchin Vasili <vigalchin at gmail.com>:
> > Ok .. I am writing a Haskell function that will call down into the
> ANSI
> > C library .. blah :: .... -> Ptr Word8 ....-> .... The underlying C
> function
> > that "blah" is calling has a "void *" so I am using "Ptr Word 8" to
> model
> > the "void *".
>
> Depending on the context, "void *" is generally either taken as a Ptr
> () (for an opaque pointer) or Ptr Word8 (for calls like memcpy).
>
> > I propose to have the callers of function "blah" to populate a
> > data structure something like "Ptr Buz" where "data Buz = { ........}
> and
> > then do a "recast :: Ptr Word 8 -> Ptr Buz" when invoking function
> "blah".
> > Does this seem reasonable? Is there a better way?
>
> Generally, Ptr x is only used where x is either a shadow type or a
> Bits type. Having a Ptr Baz where Baz is an ADT seems a little odd. If
> you need to translate a structure from Haskell to C code, probably you
> are better off having callers pass in a Baz then, internal to the
> wrapping, fill out the C structure and call the FFI function with a
> Ptr CBaz (where CBaz is a shadow type).
>
>
> AGL
>
> --
> 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/e4775ae7/attachment.htm
More information about the Haskell-Cafe
mailing list