Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c2hs to marshal a void*

Tags:

haskell

c2hs

ffi

My C function looks like this:

void *c_shm_create(char*, int);

My .chs file looks like this:

{-# LANGUAGE ForeignFunctionInterface #-}

module System.Shm.Internal.Bindings
    ( c_shmCreate
    )
where
#include "hs_shm.h"
import C2HS

{#fun unsafe c_shm_create as c_shmCreate
    { `String'
    , `Int' } -> `Ptr ()' #}

This is the error I get:

src\System\Shm\Internal\Bindings.chs:12: (column 18) [ERROR]  >>> Missing "out" marshaller!
  There is no default marshaller for this combination of Haskell and C type:
  Haskell type: Ptr ()
  C type      : (Ptr ())

I can't find any mention for void pointer (Ptr ()) in the c2hs documentation. How do I marshal this?

like image 457
kvanbere Avatar asked Mar 19 '26 06:03

kvanbere


1 Answers

Making the following change:

{#fun c_shm_create as c_shmCreate { `String' , `Int' } -> `Ptr ()' id #}

I'm unsure if this is a bug or is intentional. A Haskell data type and C struct may be considered 'equal' in that they represent the same data, but are not represented the same (the struct is pure bytes on the heap while the datatype is pointers and such) so you will need a marshaling function that isn't just id.

like image 126
user2407038 Avatar answered Mar 22 '26 04:03

user2407038



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!