haskell - Storable empty data declaration -
i'm attempting create haskell wrapper c library. underlying structs complicated express explicit types, , don't use them other passing between c functions, i'm using emptydatadecls
let ghc work out me.
what need pointer 1 of these data types, when attempt create 1 alloca
complains data not of type storable
. example:
{-# language foreignfunctioninterface, emptydatadecls #-} module main import foreign.marshal.alloc import foreign.ptr data struct foreign import ccall "header.h get_struct" get_struct :: ptr struct -> io () main = alloca $ \ptr -> get_struct ptr
ghc won't compile this, saying there's no instance storable struct
. implement myself:
instance storable struct sizeof _ = ... alignment _ = ...
but comes close defeating purpose - don't want have define such things if don't care what's in struct.
i've noticed pointer pointer works fine, because ptr
class storable
. can accomplish i'm aiming using peek
on ptr
before calling get_struct
:
main = alloca $ \ptr -> ptr <- peek ptr get_struct ptr
this feels hack, though.
is there way empty data declarations considered storable
without defining instance?
you can't allocate if don't know how big is. function going ignore argument? pass in null pointer. otherwise, need allocate enough space struct - don't cut corners allocating zero-byte or pointer-sized buffer, called function write past end of buffer, corrupting memory.
either finish data declaration, or write storable instance proper size , alignment values; there's no way around providing size/alignment data in form.
Comments
Post a Comment