[Haskell-cafe] C binding <-> safer haskell API

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Thu Feb 10 09:39:26 UTC 2022


Sorry I sent the email before finishin it :))

----- PICCA Frederic-Emmanuel <frederic-emmanuel.picca at synchrotron-soleil.fr> a écrit :
> Hello,
> 
>  I am in this case, the Cube C struct contains at least two realy big arrays.
> I wan to modify them in place.
> 
> > If however always copying the cube is too expensive, then you can take
> > an approach similar to that used in Array or Vector, where Cubes are
> > available in both immutable and mutable forms, with freeze, thaw,
> > unsafeFreeze and unsafeThaw operations connecting the two.
> > 
> >     data Cube = Cube ...
> >     data STCube s = STCube s ... 
> >     data IOCube = IOCube ...
> > 
> > Mutable cubes can be modified in place in either the IO or ST Monads.
> 
> I like this idea, I looked a t the MVector implementation
> 
> data MVector s a = MVector {-# UNPACK #-} !Int                -- ^ Offset in underlying array
>                            {-# UNPACK #-} !Int                -- ^ Size of slice
>                            {-# UNPACK #-} !(MutableArray s a) -- ^ Underlying array
>         deriving ( Typeable )
> 
> type IOVector = MVector RealWorld
> type STVector s = MVector s
> 
> 
> I imagine that in my case I can can just define a.
> 
> data MCube s = MCube (ForeignPtr C'HklBinocularsCube)
> 
> and then
> 
> type IOCube = NCube RealWorld
> 


Then it seems to me that I just need to define a merge function like this

merge :: PrimMonad m => MCube (PrimState m) -> Frame -> m () 

and

then

an unsafeFreeze to avoid copy.

It that correct ?

Cheers

Fred




More information about the Haskell-Cafe mailing list