@@ -127,7 +127,7 @@ import Data.Semigroup (Semigroup(..))
127127#endif
128128import Data.Word (Word8)
129129import Foreign.C.Types (CSize(..), CInt(..))
130−import Foreign.ForeignPtr (withForeignPtr, touchForeignPtr)
130+import Foreign.ForeignPtr (touchForeignPtr)
131131import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
132132import Foreign.Ptr (plusPtr, minusPtr, castPtr, nullPtr)
133133import Foreign.Storable (Storable(..))
@@ -136,6 +136,8 @@ import GHC.Exts (IsList, IsString(..))
136136import GHC.ForeignPtr (ForeignPtr(..))
137137import GHC.IO (IO(IO), unsafePerformIO)
138138import GHC.Ptr (Ptr(..))
139+
140+import Streamly.Internal.BaseCompat
139141import Streamly.Internal.Data.Fold.Type (Fold(..))
140142import Streamly.Internal.Data.Producer.Type (Producer (..))
141143import Streamly.Internal.Data.SVar (adaptState)
@@ -308,7 +310,7 @@ newArray = newArrayAligned (alignment (undefined :: a))
308310withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
309311withNewArray count f = do
310312 arr <- newArray count
311− withForeignPtr (aStart arr) $ \p -> f p >> return arr
313+ unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr
312314
313315-------------------------------------------------------------------------------
314316-- snoc
@@ -317,7 +319,7 @@ withNewArray count f = do
317319{-# INLINE unsafeWriteIndex #-}
318320unsafeWriteIndex :: forall a. Storable a => Array a -> Int -> a -> IO (Array a)
319321unsafeWriteIndex arr@Array {..} i x =
320− withForeignPtr aStart
322+ unsafeWithForeignPtr aStart
321323 $ \begin -> do
322324 poke (begin `plusPtr` (i * sizeOf (undefined :: a))) x
323325 return arr
@@ -347,7 +349,7 @@ snoc arr@Array {..} x =
347349 Malloc.mallocForeignPtrAlignedBytes
348350 newSize
349351 (alignment (undefined :: a))
350− withForeignPtr newPtr $ \pNew -> do
352+ unsafeWithForeignPtr newPtr $ \pNew -> do
351353 memcpy (castPtr pNew) (castPtr oldStart) size
352354 poke (pNew `plusPtr` size) x
353355 touchForeignPtr aStart
@@ -375,7 +377,7 @@ reallocAligned alignSize newSize Array{..} = do
375377 let oldStart = unsafeForeignPtrToPtr aStart
376378 let size = aEnd `minusPtr` oldStart
377379 newPtr <- Malloc.mallocForeignPtrAlignedBytes newSize alignSize
378− withForeignPtr newPtr $ \pNew -> do
380+ unsafeWithForeignPtr newPtr $ \pNew -> do
379381 memcpy (castPtr pNew) (castPtr oldStart) size
380382 touchForeignPtr aStart
381383 return $ Array
@@ -399,7 +401,7 @@ realloc = reallocAligned (alignment (undefined :: a))
399401{-# INLINE_NORMAL unsafeIndexIO #-}
400402unsafeIndexIO :: forall a. Storable a => Array a -> Int -> IO a
401403unsafeIndexIO Array {..} i =
402− withForeignPtr aStart $ \p -> do
404+ unsafeWithForeignPtr aStart $ \p -> do
403405 let elemSize = sizeOf (undefined :: a)
404406 elemOff = p `plusPtr` (elemSize * i)
405407 assert (i >= 0 && elemOff `plusPtr` elemSize <= aEnd)
@@ -1041,8 +1043,8 @@ spliceWith dst@(Array _ end bound) src =
10411043 if end `plusPtr` srcLen > bound
10421044 then error
10431045 "Bug: spliceWith: Not enough space in the target array"
1044− else withForeignPtr (aStart dst) $ \_ ->
1045− withForeignPtr (aStart src) $ \psrc -> do
1046+ else unsafeWithForeignPtr (aStart dst) $ \_ ->
1047+ unsafeWithForeignPtr (aStart src) $ \psrc -> do
10461048 let pdst = aEnd dst
10471049 memcpy (castPtr pdst) (castPtr psrc) srcLen
10481050 return $ dst {aEnd = pdst `plusPtr` srcLen}