[Haskell-cafe] c2hs, size_t, and CSize -- argh!

Duncan Coutts duncan.coutts at googlemail.com
Thu Oct 1 15:23:31 EDT 2009

On Thu, 2009-10-01 at 13:00 -0400, John Van Enk wrote:
> Hi Duncan,
> Yes, I forgot to leave out that I'd like to see 'size_t' mapped to
> CSize.
> As a (dirty) workaround, one can use 'castPtr' as the marshaler when
> dealing with pointers to 'size_t'. I'm a little more concerned about
> FunPtr's (though 'castPtr' still makes the issue go away).
> Here's my specific case dealing with function pointers:

Ohhh, if you're dealing with pointers to CSize then it's easy, c2hs
supports that directly. See the c2hs user guide section on the pointer

{# pointer *size_t as CSizePtr -> CSize #}

This tells c2hs that when it sees a *size_t type in a function argument
or result that it should be imported as Ptr CSize.

It generates a type alias and uses that when it imports functions eg:

{# pointer *size_t as CSizePtr -> CSize #}

foo :: Ptr CSize -> IO ()
foo = {# call foo as raw_foo #}


type CSizePtr = Ptr (CSize)

foo :: Ptr CSize -> IO ()
foo = raw_foo

foreign import ccall safe "foo.chs.h foo"
  raw_foo :: CSizePtr -> IO ()


More information about the Haskell-Cafe mailing list