@@ -74,12 +74,14 @@ import GHC.Prim (byteArrayContents#, unsafeCoerce#)
74
74
import GHC.ForeignPtr
75
75
#endif
76
76
77
+ import GHC.Base ( Int (.. ) )
78
+
77
79
import Foreign.Ptr
78
80
import Foreign.Marshal.Array ( advancePtr , copyArray , moveArray )
79
81
80
82
import Control.Monad.Primitive
81
- import Data.Primitive.Addr
82
83
import Data.Primitive.Types (Prim )
84
+ import qualified Data.Primitive.Types as DPT
83
85
84
86
import GHC.Word (Word8 , Word16 , Word32 , Word64 )
85
87
import GHC.Ptr (Ptr (.. ))
@@ -163,13 +165,11 @@ instance Storable a => G.MVector MVector a where
163
165
164
166
storableZero :: forall a m . (Storable a , PrimMonad m ) => MVector (PrimState m ) a -> m ()
165
167
{-# INLINE storableZero #-}
166
- storableZero (MVector n fp) = unsafePrimToPrim . withForeignPtr fp $ \ (Ptr p) -> do
167
- let q = Addr p
168
- setAddr q byteSize (0 :: Word8 )
168
+ storableZero (MVector n fp) = unsafePrimToPrim . withForeignPtr fp $ \ ptr-> do
169
+ memsetPrimPtr_vector (castPtr ptr) byteSize (0 :: Word8 )
169
170
where
170
171
x :: a
171
172
x = undefined
172
-
173
173
byteSize :: Int
174
174
byteSize = n * sizeOf x
175
175
@@ -195,13 +195,34 @@ storableSet (MVector n fp) x
195
195
do_set 1
196
196
197
197
storableSetAsPrim
198
- :: (Storable a , Prim b ) => Int -> ForeignPtr a -> a -> b -> IO ()
198
+ :: forall a b . (Storable a , Prim b ) => Int -> ForeignPtr a -> a -> b -> IO ()
199
199
{-# INLINE [0] storableSetAsPrim #-}
200
- storableSetAsPrim n fp x y = withForeignPtr fp $ \ (Ptr p) -> do
201
- poke (Ptr p) x
202
- let q = Addr p
203
- w <- readOffAddr q 0
204
- setAddr (q `plusAddr` sizeOf x) (n- 1 ) (w `asTypeOf` y)
200
+ storableSetAsPrim n fp x y = withForeignPtr fp $ \ ptr -> do
201
+ poke ptr x
202
+ -- we dont equate storable and prim reps, so we need to write to a slot
203
+ -- in storable
204
+ -- then read it back as a prim
205
+ w<- peakPrimPtr_vector ((castPtr ptr) :: Ptr b ) 0
206
+ memsetPrimPtr_vector ((castPtr ptr) `plusPtr` sizeOf x ) (n- 1 ) w
207
+
208
+
209
+
210
+ {-
211
+ AFTER primitive 0.7 is pretty old, move to using setPtr. which is really
212
+ a confusing misnomer for whats often called memset (intialize )
213
+ -}
214
+ -- Fill a memory block with the given value. The length is in
215
+ -- elements of type @a@ rather than in bytes.
216
+ memsetPrimPtr_vector :: forall a c m . (Prim c , PrimMonad m ) => Ptr a -> Int -> c -> m ()
217
+ memsetPrimPtr_vector (Ptr addr# ) (I # n# ) x = primitive_ (DPT. setOffAddr# addr# 0 # n# x)
218
+ {-# INLINE memsetPrimPtr_vector #-}
219
+
220
+
221
+ -- Read a value from a memory position given by an address and an offset.
222
+ -- The offset is in elements of type @a@ rather than in bytes.
223
+ peakPrimPtr_vector :: (Prim a , PrimMonad m ) => Ptr a -> Int -> m a
224
+ peakPrimPtr_vector (Ptr addr# ) (I # i# ) = primitive (DPT. readOffAddr# addr# i# )
225
+ {-# INLINE peakPrimPtr_vector #-}
205
226
206
227
{-# INLINE mallocVector #-}
207
228
mallocVector :: Storable a => Int -> IO (ForeignPtr a )
0 commit comments