I use Haskell FFI with the C library, which determines the number of types structcontaining elements that are pointers to doubles, intended to be processed as arrays of doubles:
typedef struct Foo {
int length;
double* values;
} FooT;
In my Haskell bindings to this library, I have an equivalent data type that I'm trying to use Data.Vector.Storable.Vector Doublefor an array:
data Foo = Foo {
length :: Int,
values :: Data.Vector.Storable.Vector Double
} deriving (Show, Eq)
To streamline the data between the C library and my Haskell code, I certainly have to write instances Storablefor these types. I am trying to develop a use case Data.Vector.Storable.unsafeFromForeignPtrfor creating Haskell Vectorfrom arrays double*that the C library allocated and populated on the heap. I hope that by doing this, I can avoid copying the contents of the arrays double*and just have Vectora kind of wrapper over the array. (A side question would be: given that arrays double*can be up to 10,000 doubles, is it worth chasing this not copying?)
This is what I still have. I use macros hsc2hsto generate an implementation Storable peek:
instance Storable Foo where
alignment _ = alignment (undefined :: CDouble)
sizeOf _ =
peek ptr = do
len <- (
valuesField <- ((
let values' = DV.unsafeFromForeignPtr0 valuesField len
return Foo { length = len, values = values' }
poke ptr (Foo len values') = do
(#poke FooT, length) ptr len
DV.unsafeWith values' (\ptrValues -> (
, peek #peek values ForeignPtr Double, unsafeFromForeignPtr. #peek :
valuesField <- (((\ hsc_ptr -> peekByteOff hsc_ptr 16)) ptr) :: IO (ForeignPtr Double)
, Storable ForeignPtr Double. , ForeignPtr Double, struct peek .
, (, ) struct , unsafeFromForeignPtr?