on master help sign in
Use Proxy type in the sizeOf method of Unbox
96a7c99Adithya Kumar
core/src/Streamly/Internal/Data/Array.hs+1
@@ -122,6 +122,7 @@ import Control.Exception (assert)
122import Control.Monad (when)
122import Control.Monad (when)
123import Control.Monad.IO.Class (MonadIO(..))
123import Control.Monad.IO.Class (MonadIO(..))
124import Data.Functor.Identity (Identity)
124import Data.Functor.Identity (Identity)
125+import Data.Proxy (Proxy(..))
125import Data.Word (Word8)
126import Data.Word (Word8)
126import Foreign.C.String (CString)
127import Foreign.C.String (CString)
127import Foreign.Ptr (castPtr)
128import Foreign.Ptr (castPtr)
core/src/Streamly/Internal/Data/Array/ArrayMacros.h+8−7
@@ -8,22 +8,23 @@
8--
8--
9-- XXX Check the core to see if max can be statically eliminated. llvm can
9-- XXX Check the core to see if max can be statically eliminated. llvm can
10-- eliminate the comparison, but not sure if GHC NCG can.
10-- eliminate the comparison, but not sure if GHC NCG can.
11−#define SIZE_OF(a) max 1 (sizeOf (undefined :: a))
11+#define SIZE_OF(a) max 1 (sizeOf (Proxy :: Proxy a))
12+#define STORABLE_SIZE_OF(a) max 1 (sizeOf (undefined :: a))
12
13
13-- Move the pointer to ith element of specified type. Type is specified as the
14-- Move the pointer to ith element of specified type. Type is specified as the
14-- type variable in the signature of the function where this macro is used.
15-- type variable in the signature of the function where this macro is used.
15−#define PTR_NEXT(ptr,a) ptr `plusPtr` SIZE_OF(a)
16+#define PTR_NEXT(ptr,a) ptr `plusPtr` STORABLE_SIZE_OF(a)
16−#define PTR_PREV(ptr,a) ptr `plusPtr` negate (SIZE_OF(a))
17+#define PTR_PREV(ptr,a) ptr `plusPtr` negate (STORABLE_SIZE_OF(a))
17
18
18−#define PTR_INDEX(ptr,i,a) ptr `plusPtr` (SIZE_OF(a) * i)
19+#define PTR_INDEX(ptr,i,a) ptr `plusPtr` (STORABLE_SIZE_OF(a) * i)
19−#define PTR_RINDEX(ptr,i,a) ptr `plusPtr` negate (SIZE_OF(a) * (i + 1))
20+#define PTR_RINDEX(ptr,i,a) ptr `plusPtr` negate (STORABLE_SIZE_OF(a) * (i + 1))
20
21
21-- XXX If we know that the array is guaranteed to have size multiples of the
22-- XXX If we know that the array is guaranteed to have size multiples of the
22-- element size then we can use a simpler check saying "ptr < end". Since we
23-- element size then we can use a simpler check saying "ptr < end". Since we
23-- always allocate in multiples of elem we can use the simpler check and assert
24-- always allocate in multiples of elem we can use the simpler check and assert
24-- the rigorous check.
25-- the rigorous check.
25−#define PTR_VALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) <= end
26+#define PTR_VALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) <= end
26−#define PTR_INVALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) > end
27+#define PTR_INVALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) > end
27
28
28-------------------------------------------------------------------------------
29-------------------------------------------------------------------------------
29-- Macros to access array indices
30-- Macros to access array indices
core/src/Streamly/Internal/Data/Array/Mut/Stream.hs+1
@@ -30,6 +30,7 @@ where
30import Control.Monad.IO.Class (MonadIO(..))
30import Control.Monad.IO.Class (MonadIO(..))
31import Control.Monad (when)
31import Control.Monad (when)
32import Data.Bifunctor (first)
32import Data.Bifunctor (first)
33+import Data.Proxy (Proxy(..))
33import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
34import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
34import Streamly.Internal.Data.Array.Mut.Type (Array(..))
35import Streamly.Internal.Data.Array.Mut.Type (Array(..))
35import Streamly.Internal.Data.Fold.Type (Fold(..))
36import Streamly.Internal.Data.Fold.Type (Fold(..))
core/src/Streamly/Internal/Data/Array/Type.hs+1
@@ -81,6 +81,7 @@ import Control.Exception (assert)
81import Control.Monad (replicateM)
81import Control.Monad (replicateM)
82import Control.Monad.IO.Class (MonadIO(..))
82import Control.Monad.IO.Class (MonadIO(..))
83import Data.Functor.Identity (Identity(..))
83import Data.Functor.Identity (Identity(..))
84+import Data.Proxy (Proxy(..))
84import Data.Word (Word8)
85import Data.Word (Word8)
85import GHC.Base (build)
86import GHC.Base (build)
86import GHC.Exts (IsList, IsString(..))
87import GHC.Exts (IsList, IsString(..))
core/src/Streamly/Internal/Data/Fold.hs+3−2
@@ -309,6 +309,7 @@ import Data.Bifunctor (first)
309import Data.Bits (shiftL, shiftR, (.|.), (.&.))
309import Data.Bits (shiftL, shiftR, (.|.), (.&.))
310import Data.Either (isLeft, isRight, fromLeft, fromRight)
310import Data.Either (isLeft, isRight, fromLeft, fromRight)
311import Data.Int (Int64)
311import Data.Int (Int64)
312+import Data.Proxy (Proxy(..))
312import Data.Word (Word32)
313import Data.Word (Word32)
313import Foreign.Storable (Storable, peek)
314import Foreign.Storable (Storable, peek)
314import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
315import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
@@ -1707,7 +1708,7 @@ takeEndBySeq patArr (Fold fstep finitial fextract) =
1707 | patLen == 1 -> do
1708 | patLen == 1 -> do
1708 pat <- liftIO $ Array.unsafeIndexIO 0 patArr
1709 pat <- liftIO $ Array.unsafeIndexIO 0 patArr
1709 return $ Partial $ SplitOnSeqSingle acc pat
1710 return $ Partial $ SplitOnSeqSingle acc pat
1710− | SIZE_OF(a) * patLen <= sizeOf (undefined :: Word) ->
1711+ | SIZE_OF(a) * patLen <= sizeOf (Proxy :: Proxy Word) ->
1711 return $ Partial $ SplitOnSeqWord acc 0 0
1712 return $ Partial $ SplitOnSeqWord acc 0 0
1712 | otherwise -> do
1713 | otherwise -> do
1713 (rb, rhead) <- liftIO $ Ring.new patLen
1714 (rb, rhead) <- liftIO $ Ring.new patLen
@@ -1844,7 +1845,7 @@ takeEndBySeq_ patArr (Fold fstep finitial fextract) =
1844 pat <- liftIO $ Array.unsafeIndexIO 0 patArr
1845 pat <- liftIO $ Array.unsafeIndexIO 0 patArr
1845 return $ Partial $ SplitOnSeqSingle acc pat
1846 return $ Partial $ SplitOnSeqSingle acc pat
1846 -- XXX Need to add tests for this case
1847 -- XXX Need to add tests for this case
1847− | SIZE_OF(a) * patLen <= sizeOf (undefined :: Word) ->
1848+ | SIZE_OF(a) * patLen <= sizeOf (Proxy :: Proxy Word) ->
1848 return $ Partial $ SplitOnSeqWord acc 0 0
1849 return $ Partial $ SplitOnSeqWord acc 0 0
1849 | otherwise -> do
1850 | otherwise -> do
1850 (rb, rhead) <- liftIO $ Ring.new patLen
1851 (rb, rhead) <- liftIO $ Ring.new patLen
core/src/Streamly/Internal/Data/Fold/Chunked.hs+1
@@ -60,6 +60,7 @@ import Control.Applicative (liftA2)
60import Control.Exception (assert)
60import Control.Exception (assert)
61import Control.Monad.IO.Class (MonadIO(..))
61import Control.Monad.IO.Class (MonadIO(..))
62import Data.Bifunctor (first)
62import Data.Bifunctor (first)
63+import Data.Proxy (Proxy(..))
63import Streamly.Internal.Data.Unboxed (peekWith, sizeOf, Unbox)
64import Streamly.Internal.Data.Unboxed (peekWith, sizeOf, Unbox)
64import GHC.Types (SPEC(..))
65import GHC.Types (SPEC(..))
65import Streamly.Internal.Data.Array.Mut.Type (touch)
66import Streamly.Internal.Data.Array.Mut.Type (touch)
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+2−1
@@ -38,6 +38,7 @@ where
38
38
39#include "inline.hs"
39#include "inline.hs"
40
40
41+import Data.Proxy (Proxy(..))
41import Control.Monad.IO.Class (MonadIO(..))
42import Control.Monad.IO.Class (MonadIO(..))
42import Streamly.Internal.Data.Unboxed
43import Streamly.Internal.Data.Unboxed
43 ( MutableByteArray(..)
44 ( MutableByteArray(..)
@@ -59,7 +60,7 @@ newtype IORef a = IORef MutableByteArray
59{-# INLINE newIORef #-}
60{-# INLINE newIORef #-}
60newIORef :: forall a. Unbox a => a -> IO (IORef a)
61newIORef :: forall a. Unbox a => a -> IO (IORef a)
61newIORef x = do
62newIORef x = do
62− var <- newUnpinnedBytes (sizeOf (undefined :: a))
63+ var <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
63 pokeWith var 0 x
64 pokeWith var 0 x
64 return $ IORef var
65 return $ IORef var
65
66
core/src/Streamly/Internal/Data/Parser/Chunked.hs+1
@@ -37,6 +37,7 @@ where
37#include "inline.hs"
37#include "inline.hs"
38
38
39import Control.Monad.IO.Class (MonadIO(..))
39import Control.Monad.IO.Class (MonadIO(..))
40+import Data.Proxy (Proxy(..))
40import GHC.Types (SPEC(..))
41import GHC.Types (SPEC(..))
41import Streamly.Internal.Data.Array.Type (Array(..))
42import Streamly.Internal.Data.Array.Type (Array(..))
42import Streamly.Internal.Data.Parser.Chunked.Type (ChunkParser (..))
43import Streamly.Internal.Data.Parser.Chunked.Type (ChunkParser (..))
core/src/Streamly/Internal/Data/Ring/Unboxed.hs+2−2
@@ -166,7 +166,7 @@ moveBy by Ring {..} ringHead = ringStartPtr `plusPtr` advanceFromHead
166
166
167 where
167 where
168
168
169− elemSize = SIZE_OF(a)
169+ elemSize = STORABLE_SIZE_OF(a)
170 ringStartPtr = unsafeForeignPtrToPtr ringStart
170 ringStartPtr = unsafeForeignPtrToPtr ringStart
171 lenInBytes = ringBound `minusPtr` ringStartPtr
171 lenInBytes = ringBound `minusPtr` ringStartPtr
172 offInBytes = ringHead `minusPtr` ringStartPtr
172 offInBytes = ringHead `minusPtr` ringStartPtr
@@ -384,7 +384,7 @@ asBytes = castUnsafe
384cast :: forall a b. Storable b => Ring a -> Maybe (Ring b)
384cast :: forall a b. Storable b => Ring a -> Maybe (Ring b)
385cast arr =
385cast arr =
386 let len = byteLength arr
386 let len = byteLength arr
387− r = len `mod` SIZE_OF(b)
387+ r = len `mod` STORABLE_SIZE_OF(b)
388 in if r /= 0
388 in if r /= 0
389 then Nothing
389 then Nothing
390 else Just $ castUnsafe arr
390 else Just $ castUnsafe arr
core/src/Streamly/Internal/Data/Stream/Chunked.hs+1
@@ -53,6 +53,7 @@ where
53import Data.Bifunctor (second)
53import Data.Bifunctor (second)
54import Control.Exception (assert)
54import Control.Exception (assert)
55import Control.Monad.IO.Class (MonadIO(..))
55import Control.Monad.IO.Class (MonadIO(..))
56+import Data.Proxy (Proxy(..))
56import Data.Word (Word8)
57import Data.Word (Word8)
57import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
58import Streamly.Internal.Data.Unboxed (Unbox, peekWith, sizeOf)
58import Fusion.Plugin.Types (Fuse(..))
59import Fusion.Plugin.Types (Fuse(..))
core/src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+3−2
@@ -153,6 +153,7 @@ import Control.Exception (assert)
153import Control.Monad.IO.Class (MonadIO(..))
153import Control.Monad.IO.Class (MonadIO(..))
154import Data.Bits (shiftR, shiftL, (.|.), (.&.))
154import Data.Bits (shiftR, shiftL, (.|.), (.&.))
155import Data.Functor.Identity ( Identity )
155import Data.Functor.Identity ( Identity )
156+import Data.Proxy (Proxy(..))
156import Data.Word (Word32)
157import Data.Word (Word32)
157import Foreign.Storable (Storable, peek)
158import Foreign.Storable (Storable, peek)
158import Fusion.Plugin.Types (Fuse(..))
159import Fusion.Plugin.Types (Fuse(..))
@@ -1961,7 +1962,7 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1961 pat <- liftIO $ A.unsafeIndexIO 0 patArr
1962 pat <- liftIO $ A.unsafeIndexIO 0 patArr
1962 return $ Skip $ SplitOnSeqSingle acc state pat
1963 return $ Skip $ SplitOnSeqSingle acc state pat
1963 else if SIZE_OF(a) * patLen
1964 else if SIZE_OF(a) * patLen
1964− <= sizeOf (undefined :: Word)
1965+ <= sizeOf (Proxy :: Proxy Word)
1965 then return $ Skip $ SplitOnSeqWordInit acc state
1966 then return $ Skip $ SplitOnSeqWordInit acc state
1966 else do
1967 else do
1967 (rb, rhead) <- liftIO $ RB.new patLen
1968 (rb, rhead) <- liftIO $ RB.new patLen
@@ -2294,7 +2295,7 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2294 pat <- liftIO $ A.unsafeIndexIO 0 patArr
2295 pat <- liftIO $ A.unsafeIndexIO 0 patArr
2295 skip $ SplitOnSuffixSeqSingleInit fs state pat
2296 skip $ SplitOnSuffixSeqSingleInit fs state pat
2296 else if SIZE_OF(a) * patLen
2297 else if SIZE_OF(a) * patLen
2297− <= sizeOf (undefined :: Word)
2298+ <= sizeOf (Proxy :: Proxy Word)
2298 then skip $ SplitOnSuffixSeqWordInit fs state
2299 then skip $ SplitOnSuffixSeqWordInit fs state
2299 else do
2300 else do
2300 (rb, rhead) <- liftIO $ RB.new patLen
2301 (rb, rhead) <- liftIO $ RB.new patLen
core/src/Streamly/Internal/Data/Unboxed.hs+12−13
@@ -178,7 +178,6 @@ unpin arr@(MutableByteArray marr#) =
178-- The Unbox type class
178-- The Unbox type class
179--------------------------------------------------------------------------------
179--------------------------------------------------------------------------------
180
180
181−-- XXX Use Proxy type in sizeOf method.
182-- XXX generate error if the size is < 1
181-- XXX generate error if the size is < 1
183
182
184-- In theory we could convert a type to and from a byte stream and use that
183-- In theory we could convert a type to and from a byte stream and use that
@@ -257,10 +256,10 @@ unpin arr@(MutableByteArray marr#) =
257--
256--
258class Unbox a where
257class Unbox a where
259 -- | Get the size.
258 -- | Get the size.
260− sizeOf :: a -> Int
259+ sizeOf :: Proxy a -> Int
261
260
262− default sizeOf :: (SizeOfRep (Rep a)) => a -> Int
261+ default sizeOf :: (SizeOfRep (Rep a)) => Proxy a -> Int
263− sizeOf _ = genericSizeOf (Proxy :: Proxy a)
262+ sizeOf = genericSizeOf
264
263
265 -- | Read an element of type "a" from a MutableByteArray given the byte
264 -- | Read an element of type "a" from a MutableByteArray given the byte
266 -- index.
265 -- index.
@@ -449,7 +448,7 @@ instance Unbox IoSubSystem where
449 pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
448 pokeByteIndex i arr a = pokeByteIndex i arr (fromEnum a)
450
449
451 {-# INLINE sizeOf #-}
450 {-# INLINE sizeOf #-}
452− sizeOf = sizeOf . fromEnum
451+ sizeOf _ = sizeOf (Proxy :: Proxy Int)
453#endif
452#endif
454
453
455instance Unbox Bool where
454instance Unbox Bool where
@@ -511,7 +510,7 @@ readUnsafe = Peeker (Builder step)
511 {-# INLINE step #-}
510 {-# INLINE step #-}
512 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
511 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
513 step (BoundedPtr arr pos end) = do
512 step (BoundedPtr arr pos end) = do
514− let next = pos + sizeOf (undefined :: a)
513+ let next = pos + sizeOf (Proxy :: Proxy a)
515 r <- peekByteIndex pos arr
514 r <- peekByteIndex pos arr
516 return (BoundedPtr arr next end, r)
515 return (BoundedPtr arr next end, r)
517
516
@@ -524,7 +523,7 @@ read = Peeker (Builder step)
524 {-# INLINE step #-}
523 {-# INLINE step #-}
525 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
524 step :: forall a. Unbox a => BoundedPtr -> IO (BoundedPtr, a)
526 step (BoundedPtr arr pos end) = do
525 step (BoundedPtr arr pos end) = do
527− let next = pos + sizeOf (undefined :: a)
526+ let next = pos + sizeOf (Proxy :: Proxy a)
528 when (next > end) $ error "peekObject reading beyond limit"
527 when (next > end) $ error "peekObject reading beyond limit"
529 r <- peekByteIndex pos arr
528 r <- peekByteIndex pos arr
530 return (BoundedPtr arr next end, r)
529 return (BoundedPtr arr next end, r)
@@ -557,16 +556,16 @@ runPeeker (Peeker (Builder f)) ptr = fmap snd (f ptr)
557
556
558-- Does not check writing beyond bound.
557-- Does not check writing beyond bound.
559{-# INLINE pokeBoundedPtrUnsafe #-}
558{-# INLINE pokeBoundedPtrUnsafe #-}
560−pokeBoundedPtrUnsafe :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
559+pokeBoundedPtrUnsafe :: forall a. Unbox a => a -> BoundedPtr -> IO BoundedPtr
561pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
560pokeBoundedPtrUnsafe a (BoundedPtr arr pos end) = do
562− let next = pos + sizeOf a
561+ let next = pos + sizeOf (Proxy :: Proxy a)
563 pokeByteIndex pos arr a
562 pokeByteIndex pos arr a
564 return (BoundedPtr arr next end)
563 return (BoundedPtr arr next end)
565
564
566{-# INLINE pokeBoundedPtr #-}
565{-# INLINE pokeBoundedPtr #-}
567−pokeBoundedPtr :: Unbox a => a -> BoundedPtr -> IO BoundedPtr
566+pokeBoundedPtr :: forall a. Unbox a => a -> BoundedPtr -> IO BoundedPtr
568pokeBoundedPtr a (BoundedPtr arr pos end) = do
567pokeBoundedPtr a (BoundedPtr arr pos end) = do
569− let next = pos + sizeOf a
568+ let next = pos + sizeOf (Proxy :: Proxy a)
570 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
569 when (next > end) $ error "pokeBoundedPtr writing beyond limit"
571 pokeByteIndex pos arr a
570 pokeByteIndex pos arr a
572 return (BoundedPtr arr next end)
571 return (BoundedPtr arr next end)
@@ -615,7 +614,7 @@ instance SizeOfRep f => SizeOfRep (M1 i c f) where
615-- Primitive type "a".
614-- Primitive type "a".
616instance Unbox a => SizeOfRep (K1 i a) where
615instance Unbox a => SizeOfRep (K1 i a) where
617 {-# INLINE sizeOfRep #-}
616 {-# INLINE sizeOfRep #-}
618− sizeOfRep _ = sizeOf (undefined :: a)
617+ sizeOfRep _ = sizeOf (Proxy :: Proxy a)
619
618
620-- Void: data type without constructors. Values of this type cannot exist,
619-- Void: data type without constructors. Values of this type cannot exist,
621-- therefore the size is undefined. We should never be serializing structures
620-- therefore the size is undefined. We should never be serializing structures
@@ -671,7 +670,7 @@ instance (MaxArity256 (SumArity (f :+: g)), SizeOfRepSum f, SizeOfRepSum g) =>
671 {-# INLINE sizeOfRep #-}
670 {-# INLINE sizeOfRep #-}
672 sizeOfRep _ =
671 sizeOfRep _ =
673 -- One byte for the constructor id and then the constructor value.
672 -- One byte for the constructor id and then the constructor value.
674− sizeOf (undefined :: Word8) +
673+ sizeOf (Proxy :: Proxy Word8) +
675 max (sizeOfRepSum (undefined :: f x))
674 max (sizeOfRepSum (undefined :: f x))
676 (sizeOfRepSum (undefined :: g x))
675 (sizeOfRepSum (undefined :: g x))
677
676
src/Streamly/Internal/Data/Array/ArrayMacros.h+8−7
@@ -8,22 +8,23 @@
8--
8--
9-- XXX Check the core to see if max can be statically eliminated. llvm can
9-- XXX Check the core to see if max can be statically eliminated. llvm can
10-- eliminate the comparison, but not sure if GHC NCG can.
10-- eliminate the comparison, but not sure if GHC NCG can.
11−#define SIZE_OF(a) max 1 (sizeOf (undefined :: a))
11+#define SIZE_OF(a) max 1 (sizeOf (Proxy :: Proxy a))
12+#define STORABLE_SIZE_OF(a) max 1 (sizeOf (undefined :: a))
12
13
13-- Move the pointer to ith element of specified type. Type is specified as the
14-- Move the pointer to ith element of specified type. Type is specified as the
14-- type variable in the signature of the function where this macro is used.
15-- type variable in the signature of the function where this macro is used.
15−#define PTR_NEXT(ptr,a) ptr `plusPtr` SIZE_OF(a)
16+#define PTR_NEXT(ptr,a) ptr `plusPtr` STORABLE_SIZE_OF(a)
16−#define PTR_PREV(ptr,a) ptr `plusPtr` negate (SIZE_OF(a))
17+#define PTR_PREV(ptr,a) ptr `plusPtr` negate (STORABLE_SIZE_OF(a))
17
18
18−#define PTR_INDEX(ptr,i,a) ptr `plusPtr` (SIZE_OF(a) * i)
19+#define PTR_INDEX(ptr,i,a) ptr `plusPtr` (STORABLE_SIZE_OF(a) * i)
19−#define PTR_RINDEX(ptr,i,a) ptr `plusPtr` negate (SIZE_OF(a) * (i + 1))
20+#define PTR_RINDEX(ptr,i,a) ptr `plusPtr` negate (STORABLE_SIZE_OF(a) * (i + 1))
20
21
21-- XXX If we know that the array is guaranteed to have size multiples of the
22-- XXX If we know that the array is guaranteed to have size multiples of the
22-- element size then we can use a simpler check saying "ptr < end". Since we
23-- element size then we can use a simpler check saying "ptr < end". Since we
23-- always allocate in multiples of elem we can use the simpler check and assert
24-- always allocate in multiples of elem we can use the simpler check and assert
24-- the rigorous check.
25-- the rigorous check.
25−#define PTR_VALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) <= end
26+#define PTR_VALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) <= end
26−#define PTR_INVALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) > end
27+#define PTR_INVALID(ptr,end,a) ptr `plusPtr` STORABLE_SIZE_OF(a) > end
27
28
28-------------------------------------------------------------------------------
29-------------------------------------------------------------------------------
29-- Macros to access array indices
30-- Macros to access array indices
test/Streamly/Test/Data/Array.hs+3−2
@@ -10,6 +10,7 @@ module Streamly.Test.Data.Array (main) where
10
10
11import Data.Char (isLower)
11import Data.Char (isLower)
12import Data.List (sort)
12import Data.List (sort)
13+import Data.Proxy (Proxy(..))
13import Data.Word(Word8)
14import Data.Word(Word8)
14import Foreign.Storable (peek)
15import Foreign.Storable (peek)
15import GHC.Ptr (plusPtr)
16import GHC.Ptr (plusPtr)
@@ -196,7 +197,7 @@ testAsPtrUnsafeMA = do
196
197
197 where
198 where
198
199
199− sizeOfInt = sizeOf (undefined :: Int)
200+ sizeOfInt = sizeOf (Proxy :: Proxy Int)
200
201
201 -- We need to be careful here. We assume Unboxed and Storable are compatible
202 -- We need to be careful here. We assume Unboxed and Storable are compatible
202 -- with each other. For Int, they are compatible.
203 -- with each other. For Int, they are compatible.
@@ -210,7 +211,7 @@ testAsPtrUnsafeMA = do
210reallocMA :: Property
211reallocMA :: Property
211reallocMA =
212reallocMA =
212 let len = 10000
213 let len = 10000
213− bSize = len * sizeOf (undefined :: Char)
214+ bSize = len * sizeOf (Proxy :: Proxy Char)
214 in forAll (vectorOf len (arbitrary :: Gen Char)) $ \vec ->
215 in forAll (vectorOf len (arbitrary :: Gen Char)) $ \vec ->
215 forAll (chooseInt (bSize - 2000, bSize + 2000)) $ \newBLen -> do
216 forAll (chooseInt (bSize - 2000, bSize + 2000)) $ \newBLen -> do
216 arr <- MA.fromList vec
217 arr <- MA.fromList vec
test/Streamly/Test/Data/Array/Common.hs+1−1
@@ -8,7 +8,7 @@ maxTestCount = 10
8#endif
8#endif
9
9
10allocOverhead :: Int
10allocOverhead :: Int
11−allocOverhead = 2 * sizeOf (undefined :: Int)
11+allocOverhead = 16
12
12
13-- XXX this should be in sync with the defaultChunkSize in Array code, or we
13-- XXX this should be in sync with the defaultChunkSize in Array code, or we
14-- should expose that and use that. For fast testing we could reduce the
14-- should expose that and use that. For fast testing we could reduce the
test/Streamly/Test/Data/Array/CommonImports.hs−2
@@ -1,6 +1,4 @@
1
1
2−import Streamly.Internal.Data.Unboxed (sizeOf)
3−
4import Test.Hspec.QuickCheck
2import Test.Hspec.QuickCheck
5import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
3import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
6import Test.QuickCheck.Monadic (monadicIO, assert, run)
4import Test.QuickCheck.Monadic (monadicIO, assert, run)
test/Streamly/Test/Data/Unbox.hs+1−1
@@ -48,7 +48,7 @@ test v1 v2 = monadicIO $ do
48
48
49checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> Property
49checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> Property
50checkSizeOf _ size = monadicIO $
50checkSizeOf _ size = monadicIO $
51− assert (sizeOf (undefined :: a) == size)
51+ assert (sizeOf (Proxy :: Proxy a) == size)
52
52
53moduleName :: String
53moduleName :: String
54moduleName = "Data.Unbox"
54moduleName = "Data.Unbox"
test/Streamly/Test/FileSystem/Handle.hs+2−1
@@ -9,6 +9,7 @@
9module Streamly.Test.FileSystem.Handle (main) where
9module Streamly.Test.FileSystem.Handle (main) where
10
10
11import Data.Functor.Identity (runIdentity)
11import Data.Functor.Identity (runIdentity)
12+import Data.Proxy (Proxy(..))
12import Data.Word (Word8)
13import Data.Word (Word8)
13import Streamly.Internal.Data.Unboxed (sizeOf)
14import Streamly.Internal.Data.Unboxed (sizeOf)
14import Streamly.Internal.Data.Stream (Stream)
15import Streamly.Internal.Data.Stream (Stream)
@@ -36,7 +37,7 @@ import Test.Hspec as H
36import Test.Hspec.QuickCheck
37import Test.Hspec.QuickCheck
37
38
38allocOverhead :: Int
39allocOverhead :: Int
39−allocOverhead = 2 * sizeOf (undefined :: Int)
40+allocOverhead = 2 * sizeOf (Proxy :: Proxy Int)
40
41
41defaultChunkSize :: Int
42defaultChunkSize :: Int
42defaultChunkSize = 32 * k - allocOverhead
43defaultChunkSize = 32 * k - allocOverhead