@@ -1,5 +1,3 @@
1−{-# LANGUAGE UnboxedTuples #-}
4-- Module : Streamly.Internal.Data.Array.Foreign.Mut.Type
2-- Module : Streamly.Internal.Data.Array.Foreign.Mut.Type
5-- Copyright : (c) 2020 Composewell Technologies
3-- Copyright : (c) 2020 Composewell Technologies
@@ -92,13 +90,10 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
92 , writeChunks
90 , writeChunks
94 -- * Utilities
92 -- * Utilities
98 , bytesToElemCount
93 , bytesToElemCount
@@ -118,10 +113,10 @@ import Foreign.ForeignPtr (touchForeignPtr)
118import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
113import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
119import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
114import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
120import Foreign.Storable (Storable(..))
115import Foreign.Storable (Storable(..))
121−import GHC.Base (nullAddr#, realWorld#, build)
116+import GHC.Base (nullAddr#, build)
122import GHC.Exts (IsList, IsString(..))
117import GHC.Exts (IsList, IsString(..))
123import GHC.ForeignPtr (ForeignPtr(..))
118import GHC.ForeignPtr (ForeignPtr(..))
124−import GHC.IO (IO(IO), unsafePerformIO)
119+import GHC.IO (unsafePerformIO)
125import GHC.Ptr (Ptr(..))
120import GHC.Ptr (Ptr(..))
127import Streamly.Internal.BaseCompat
122import Streamly.Internal.BaseCompat
@@ -131,6 +126,9 @@ import Streamly.Internal.Data.SVar (adaptState)
131import Streamly.Internal.Data.Unfold.Type (Unfold(..))
126import Streamly.Internal.Data.Unfold.Type (Unfold(..))
132import Text.Read (readPrec, readListPrec, readListPrecDefault)
127import Text.Read (readPrec, readListPrec, readListPrecDefault)
129+import Streamly.Internal.System.IO
130+ (defaultChunkSize, mkChunkSize, unsafeInlineIO)
134#ifdef DEVBUILD
132#ifdef DEVBUILD
135import qualified Data.Foldable as F
133import qualified Data.Foldable as F
@@ -156,6 +154,37 @@ plusForeignPtr (ForeignPtr addr c) (I# d) = ForeignPtr (plusAddr# addr d) c
156-- Array Data Type
154-- Array Data Type
157-------------------------------------------------------------------------------
155-------------------------------------------------------------------------------
157+foreign import ccall unsafe "string.h memcpy" c_memcpy
158+ :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)
160+foreign import ccall unsafe "string.h memchr" c_memchr
161+ :: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)
163+foreign import ccall unsafe "string.h memcmp" c_memcmp
164+ :: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
166+{-# INLINE bytesToElemCount #-}
167+bytesToElemCount :: Storable a => a -> Int -> Int
168+bytesToElemCount x n =
169+ let elemSize = sizeOf x
170+ in n + elemSize - 1 `div` elemSize
172+-- XXX we are converting Int to CSize
173+memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
174+memcpy dst src len = void (c_memcpy dst src (fromIntegral len))
176+-- XXX we are converting Int to CSize
177+-- return True if the memory locations have identical contents
178+{-# INLINE memcmp #-}
179+memcmp :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
180+memcmp p1 p2 len = do
181+ r <- c_memcmp p1 p2 (fromIntegral len)
184+-------------------------------------------------------------------------------
186+-------------------------------------------------------------------------------
159-- $arrayNotes
188-- $arrayNotes
161-- We can use a 'Storable' constraint in the Array type and the constraint can
190-- We can use a 'Storable' constraint in the Array type and the constraint can
@@ -183,59 +212,6 @@ mutableArray ::
183 ForeignPtr a -> Ptr a -> Ptr a -> Array a
212 ForeignPtr a -> Ptr a -> Ptr a -> Array a
184mutableArray = Array
213mutableArray = Array
186−-------------------------------------------------------------------------------
188−-------------------------------------------------------------------------------
190−foreign import ccall unsafe "string.h memcpy" c_memcpy
191− :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)
193−foreign import ccall unsafe "string.h memchr" c_memchr
194− :: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)
196−-- XXX we are converting Int to CSize
197−memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
198−memcpy dst src len = void (c_memcpy dst src (fromIntegral len))
200−foreign import ccall unsafe "string.h memcmp" c_memcmp
201− :: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
203−-- XXX we are converting Int to CSize
204−-- return True if the memory locations have identical contents
205−{-# INLINE memcmp #-}
206−memcmp :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
207−memcmp p1 p2 len = do
208− r <- c_memcmp p1 p2 (fromIntegral len)
211−{-# INLINE unsafeInlineIO #-}
212−unsafeInlineIO :: IO a -> a
213−unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
215−{-# INLINE bytesToElemCount #-}
216−bytesToElemCount :: Storable a => a -> Int -> Int
217−bytesToElemCount x n =
218− let elemSize = sizeOf x
219− in n + elemSize - 1 `div` elemSize
222−-- | GHC memory management allocation header overhead
224−allocOverhead = 2 * sizeOf (undefined :: Int)
226−mkChunkSize :: Int -> Int
227−mkChunkSize n = let size = n - allocOverhead in max size 0
229−mkChunkSizeKB :: Int -> Int
230−mkChunkSizeKB n = mkChunkSize (n * k)
233−-- | Default maximum buffer size in bytes, for reading from and writing to IO
234−-- devices, the value is 32KB minus GHC allocation overhead, which is a few
235−-- bytes, so that the actual allocation is 32KB.
236−defaultChunkSize :: Int
237−defaultChunkSize = mkChunkSizeKB 32
239-------------------------------------------------------------------------------
215-------------------------------------------------------------------------------
240-- Construction
216-- Construction
241-------------------------------------------------------------------------------
217-------------------------------------------------------------------------------