@@ -127,7 +127,7 @@ import Data.Semigroup (Semigroup(..))
128import Data.Word (Word8)
128import Data.Word (Word8)
129import Foreign.C.Types (CSize(..), CInt(..))
129import Foreign.C.Types (CSize(..), CInt(..))
130−import Foreign.ForeignPtr (withForeignPtr, touchForeignPtr)
130+import Foreign.ForeignPtr (touchForeignPtr)
131import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
131import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
132import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
132import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
133import Foreign.Storable (Storable(..))
133import Foreign.Storable (Storable(..))
@@ -136,6 +136,8 @@ import GHC.Exts (IsList, IsString(..))
136import GHC.ForeignPtr (ForeignPtr(..))
136import GHC.ForeignPtr (ForeignPtr(..))
137import GHC.IO (IO(IO), unsafePerformIO)
137import GHC.IO (IO(IO), unsafePerformIO)
138import GHC.Ptr (Ptr(..))
138import GHC.Ptr (Ptr(..))
140+import Streamly.Internal.BaseCompat
139import Streamly.Internal.Data.Fold.Type (Fold(..))
141import Streamly.Internal.Data.Fold.Type (Fold(..))
140import Streamly.Internal.Data.Producer.Type (Producer (..))
142import Streamly.Internal.Data.Producer.Type (Producer (..))
141import Streamly.Internal.Data.SVar (adaptState)
143import Streamly.Internal.Data.SVar (adaptState)
@@ -308,7 +310,7 @@ newArray = newArrayAligned (alignment (undefined :: a))
308withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
310withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
309withNewArray count f = do
311withNewArray count f = do
310 arr <- newArray count
312 arr <- newArray count
311− withForeignPtr (aStart arr) $ \p -> f p >> return arr
313+ unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr
313-------------------------------------------------------------------------------
315-------------------------------------------------------------------------------
@@ -317,7 +319,7 @@ withNewArray count f = do
317{-# INLINE unsafeWriteIndex #-}
319{-# INLINE unsafeWriteIndex #-}
318unsafeWriteIndex :: forall a. Storable a => Array a -> Int -> a -> IO (Array a)
320unsafeWriteIndex :: forall a. Storable a => Array a -> Int -> a -> IO (Array a)
319unsafeWriteIndex arr@Array {..} i x =
321unsafeWriteIndex arr@Array {..} i x =
320− withForeignPtr aStart
322+ unsafeWithForeignPtr aStart
321 $ \begin -> do
323 $ \begin -> do
322 poke (begin `plusPtr` (i * sizeOf (undefined :: a))) x
324 poke (begin `plusPtr` (i * sizeOf (undefined :: a))) x
323 return arr
325 return arr
@@ -347,7 +349,7 @@ snoc arr@Array {..} x =
347 Malloc.mallocForeignPtrAlignedBytes
349 Malloc.mallocForeignPtrAlignedBytes
349 (alignment (undefined :: a))
351 (alignment (undefined :: a))
350− withForeignPtr newPtr $ \pNew -> do
352+ unsafeWithForeignPtr newPtr $ \pNew -> do
351 memcpy (castPtr pNew) (castPtr oldStart) size
353 memcpy (castPtr pNew) (castPtr oldStart) size
352 poke (pNew `plusPtr` size) x
354 poke (pNew `plusPtr` size) x
353 touchForeignPtr aStart
355 touchForeignPtr aStart
@@ -375,7 +377,7 @@ reallocAligned alignSize newSize Array{..} = do
375 let oldStart = unsafeForeignPtrToPtr aStart
377 let oldStart = unsafeForeignPtrToPtr aStart
376 let size = aEnd `minusPtr` oldStart
378 let size = aEnd `minusPtr` oldStart
377 newPtr <- Malloc.mallocForeignPtrAlignedBytes newSize alignSize
379 newPtr <- Malloc.mallocForeignPtrAlignedBytes newSize alignSize
378− withForeignPtr newPtr $ \pNew -> do
380+ unsafeWithForeignPtr newPtr $ \pNew -> do
379 memcpy (castPtr pNew) (castPtr oldStart) size
381 memcpy (castPtr pNew) (castPtr oldStart) size
380 touchForeignPtr aStart
382 touchForeignPtr aStart
381 return $ Array
383 return $ Array
@@ -399,7 +401,7 @@ realloc = reallocAligned (alignment (undefined :: a))
399{-# INLINE_NORMAL unsafeIndexIO #-}
401{-# INLINE_NORMAL unsafeIndexIO #-}
400unsafeIndexIO :: forall a. Storable a => Array a -> Int -> IO a
402unsafeIndexIO :: forall a. Storable a => Array a -> Int -> IO a
401unsafeIndexIO Array {..} i =
403unsafeIndexIO Array {..} i =
402− withForeignPtr aStart $ \p -> do
404+ unsafeWithForeignPtr aStart $ \p -> do
403 let elemSize = sizeOf (undefined :: a)
405 let elemSize = sizeOf (undefined :: a)
404 elemOff = p `plusPtr` (elemSize * i)
406 elemOff = p `plusPtr` (elemSize * i)
405 assert (i >= 0 && elemOff `plusPtr` elemSize <= aEnd)
407 assert (i >= 0 && elemOff `plusPtr` elemSize <= aEnd)
@@ -1041,8 +1043,8 @@ spliceWith dst@(Array _ end bound) src =
1041 if end `plusPtr` srcLen > bound
1043 if end `plusPtr` srcLen > bound
1042 then error
1044 then error
1043 "Bug: spliceWith: Not enough space in the target array"
1045 "Bug: spliceWith: Not enough space in the target array"
1044− else withForeignPtr (aStart dst) $ \_ ->
1046+ else unsafeWithForeignPtr (aStart dst) $ \_ ->
1045− withForeignPtr (aStart src) $ \psrc -> do
1047+ unsafeWithForeignPtr (aStart src) $ \psrc -> do
1046 let pdst = aEnd dst
1048 let pdst = aEnd dst
1047 memcpy (castPtr pdst) (castPtr psrc) srcLen
1049 memcpy (castPtr pdst) (castPtr psrc) srcLen
1048 return $ dst {aEnd = pdst `plusPtr` srcLen}
1050 return $ dst {aEnd = pdst `plusPtr` srcLen}