on master help sign in
Replace withForeignPtr by unsafeWithForeignPtr
dcaa9efRanjeet Kumar Ranjan
src/Streamly/Internal/BaseCompat.hs+15
@@ -13,6 +13,7 @@ module Streamly.Internal.BaseCompat
13 , errorWithoutStackTrace
13 , errorWithoutStackTrace
14 , fromLeft
14 , fromLeft
15 , fromRight
15 , fromRight
16+ , unsafeWithForeignPtr
16 )
17 )
17where
18where
18
19
@@ -20,6 +21,13 @@ import Data.Coerce (Coercible, coerce)
20#if MIN_VERSION_base(4,10,0)
21#if MIN_VERSION_base(4,10,0)
21import Data.Either (fromRight, fromLeft)
22import Data.Either (fromRight, fromLeft)
22#endif
23#endif
24+import GHC.ForeignPtr (ForeignPtr(..))
25+import GHC.Ptr (Ptr(..))
26+#if MIN_VERSION_base(4,15,0)
27+import qualified GHC.ForeignPtr as GHCForeignPtr
28+#else
29+import Foreign.ForeignPtr (withForeignPtr)
30+#endif
23
31
24{-# INLINE (#.) #-}
32{-# INLINE (#.) #-}
25(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
33(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
@@ -40,3 +48,10 @@ fromRight :: b -> Either a b -> b
40fromRight _ (Right b) = b
48fromRight _ (Right b) = b
41fromRight b _ = b
49fromRight b _ = b
42#endif
50#endif
51+
52+unsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
53+#if MIN_VERSION_base(4,15,0)
54+unsafeWithForeignPtr = GHCForeignPtr.unsafeWithForeignPtr
55+#else
56+unsafeWithForeignPtr = withForeignPtr
57+#endif
src/Streamly/Internal/Data/Array/Foreign.hs+6−5
@@ -147,7 +147,7 @@ import Data.Semigroup ((<>))
147import Data.Word (Word8)
147import Data.Word (Word8)
148-- import Data.Functor.Identity (Identity)
148-- import Data.Functor.Identity (Identity)
149import Foreign.C.String (CString)
149import Foreign.C.String (CString)
150−import Foreign.ForeignPtr (withForeignPtr, castForeignPtr)
150+import Foreign.ForeignPtr (castForeignPtr)
151import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
151import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
152import Foreign.Ptr (plusPtr, castPtr)
152import Foreign.Ptr (plusPtr, castPtr)
153import Foreign.Storable (Storable(..))
153import Foreign.Storable (Storable(..))
@@ -158,6 +158,7 @@ import GHC.Ptr (Ptr(..))
158import GHC.Prim (touch#)
158import GHC.Prim (touch#)
159import GHC.IO (IO(..))
159import GHC.IO (IO(..))
160
160
161+import Streamly.Internal.BaseCompat
161import Streamly.Internal.Data.Array.Foreign.Type (Array(..), length)
162import Streamly.Internal.Data.Array.Foreign.Type (Array(..), length)
162import Streamly.Internal.Data.Fold.Type (Fold(..))
163import Streamly.Internal.Data.Fold.Type (Fold(..))
163import Streamly.Internal.Data.Producer.Type (Producer)
164import Streamly.Internal.Data.Producer.Type (Producer)
@@ -440,7 +441,7 @@ getIndex arr i =
440 if i < 0 || i > length arr - 1
441 if i < 0 || i > length arr - 1
441 then Nothing
442 then Nothing
442 else A.unsafeInlineIO $
443 else A.unsafeInlineIO $
443− withForeignPtr (aStart arr) $ \p -> Just <$> peekElemOff p i
444+ unsafeWithForeignPtr (aStart arr) $ \p -> Just <$> peekElemOff p i
444
445
445{-
446{-
446-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
447-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
@@ -478,7 +479,7 @@ writeIndex arr i a = do
478 then error $ "writeIndex: specified array index " ++ show i
479 then error $ "writeIndex: specified array index " ++ show i
479 ++ " is beyond the maximum index " ++ show maxIndex
480 ++ " is beyond the maximum index " ++ show maxIndex
480 else
481 else
481− liftIO $ withForeignPtr (aStart arr) $ \p ->
482+ liftIO $ unsafeWithForeignPtr (aStart arr) $ \p ->
482 pokeElemOff p i a
483 pokeElemOff p i a
483
484
484{-
485{-
@@ -580,7 +581,7 @@ cast arr =
580--
581--
581asPtr :: Array a -> (Ptr b -> IO c) -> IO c
582asPtr :: Array a -> (Ptr b -> IO c) -> IO c
582asPtr Array{..} act = do
583asPtr Array{..} act = do
583− withForeignPtr aStart $ \ptr -> act (castPtr ptr)
584+ unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
584
585
585-- | Convert an array of any type into a null terminated CString Ptr.
586-- | Convert an array of any type into a null terminated CString Ptr.
586--
587--
@@ -593,7 +594,7 @@ asPtr Array{..} act = do
593asCString :: Array a -> (CString -> IO b) -> IO b
594asCString :: Array a -> (CString -> IO b) -> IO b
594asCString arr act = do
595asCString arr act = do
595 let Array{..} = asBytes arr <> A.fromList [0]
596 let Array{..} = asBytes arr <> A.fromList [0]
596− withForeignPtr aStart $ \ptr -> act (castPtr ptr)
597+ unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
597
598
598-------------------------------------------------------------------------------
599-------------------------------------------------------------------------------
599-- Folds
600-- Folds
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−8
@@ -127,7 +127,7 @@ import Data.Semigroup (Semigroup(..))
127#endif
127#endif
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(..))
139+
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
312
314
313-------------------------------------------------------------------------------
315-------------------------------------------------------------------------------
314-- snoc
316-- snoc
@@ -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
348 newSize
350 newSize
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}
src/Streamly/Internal/Data/Array/Stream/Foreign.hs+5−3
@@ -49,7 +49,7 @@ import Control.Exception (assert)
49import Control.Monad.Catch (MonadThrow, throwM)
49import Control.Monad.Catch (MonadThrow, throwM)
50import Control.Monad.IO.Class (MonadIO(..))
50import Control.Monad.IO.Class (MonadIO(..))
51import Data.Word (Word8)
51import Data.Word (Word8)
52−import Foreign.ForeignPtr (withForeignPtr, touchForeignPtr)
52+import Foreign.ForeignPtr (touchForeignPtr)
53import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
53import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
54import Foreign.Ptr (minusPtr, plusPtr, castPtr)
54import Foreign.Ptr (minusPtr, plusPtr, castPtr)
55import Foreign.Storable (Storable(..))
55import Foreign.Storable (Storable(..))
@@ -63,6 +63,8 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
63#if !defined(mingw32_HOST_OS)
63#if !defined(mingw32_HOST_OS)
64import Streamly.Internal.FileSystem.FDIO (IOVec(..))
64import Streamly.Internal.FileSystem.FDIO (IOVec(..))
65#endif
65#endif
66+
67+import Streamly.Internal.BaseCompat
66import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
68import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
67import Streamly.Internal.Data.Fold.Type (Fold(..))
69import Streamly.Internal.Data.Fold.Type (Fold(..))
68import Streamly.Internal.Data.Parser (ParseError(..))
70import Streamly.Internal.Data.Parser (ParseError(..))
@@ -382,7 +384,7 @@ spliceArraysLenUnsafe len buffered = do
382 where
384 where
383
385
384 writeArr dst (MA.Array as ae _) =
386 writeArr dst (MA.Array as ae _) =
385− liftIO $ withForeignPtr as $ \src -> do
387+ liftIO $ unsafeWithForeignPtr as $ \src -> do
386 let count = ae `minusPtr` src
388 let count = ae `minusPtr` src
387 A.memcpy (castPtr dst) (castPtr src) count
389 A.memcpy (castPtr dst) (castPtr src) count
388 return $ dst `plusPtr` count
390 return $ dst `plusPtr` count
@@ -400,7 +402,7 @@ _spliceArrays s = do
400 where
402 where
401
403
402 writeArr dst (Array as ae) =
404 writeArr dst (Array as ae) =
403− liftIO $ withForeignPtr as $ \src -> do
405+ liftIO $ unsafeWithForeignPtr as $ \src -> do
404 let count = ae `minusPtr` src
406 let count = ae `minusPtr` src
405 A.memcpy (castPtr dst) (castPtr src) count
407 A.memcpy (castPtr dst) (castPtr src) count
406 return $ dst `plusPtr` count
408 return $ dst `plusPtr` count
src/Streamly/Internal/Data/Stream/IsStream/Transform.hs+3−3
@@ -352,15 +352,15 @@ foldrT f z s = S.foldrT f z (toStreamS s)
352-- >>> drain $ Stream.mapM putStr $ Stream.fromList ["a", "b", "c"]
352-- >>> drain $ Stream.mapM putStr $ Stream.fromList ["a", "b", "c"]
353-- abc
353-- abc
354--
354--
355−-- >>> :{
355+-- >>> :{
356−-- drain $ Stream.replicateM 10 (return 1)
356+-- drain $ Stream.replicateM 10 (return 1)
357-- & (fromSerial . Stream.mapM (\x -> threadDelay 1000000 >> print x))
357-- & (fromSerial . Stream.mapM (\x -> threadDelay 1000000 >> print x))
358-- :}
358-- :}
359-- 1
359-- 1
360-- ...
360-- ...
361-- 1
361-- 1
362--
362--
363−-- > drain $ Stream.replicateM 10 (return 1)
363+-- > drain $ Stream.replicateM 10 (return 1)
364-- & (fromAsync . Stream.mapM (\x -> threadDelay 1000000 >> print x))
364-- & (fromAsync . Stream.mapM (\x -> threadDelay 1000000 >> print x))
365-- @
365-- @
366--
366--
src/Streamly/Internal/FileSystem/FD.hs+4−5
@@ -118,8 +118,6 @@ where
118
118
119import Control.Monad.IO.Class (MonadIO(..))
119import Control.Monad.IO.Class (MonadIO(..))
120import Data.Word (Word8)
120import Data.Word (Word8)
121−import Foreign.ForeignPtr (withForeignPtr)
122−-- import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
123import Foreign.Ptr (plusPtr, castPtr)
121import Foreign.Ptr (plusPtr, castPtr)
124import Foreign.Storable (Storable(..))
122import Foreign.Storable (Storable(..))
125import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
123import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -130,6 +128,7 @@ import Prelude hiding (read)
130import qualified GHC.IO.FD as FD
128import qualified GHC.IO.FD as FD
131import qualified GHC.IO.Device as RawIO
129import qualified GHC.IO.Device as RawIO
132
130
131+import Streamly.Internal.BaseCompat
133import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
132import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
134import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
133import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
135
134
@@ -215,7 +214,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
215readArrayUpto size (Handle fd) = do
214readArrayUpto size (Handle fd) = do
216 ptr <- mallocPlainForeignPtrBytes size
215 ptr <- mallocPlainForeignPtrBytes size
217 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
216 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
218− withForeignPtr ptr $ \p -> do
217+ unsafeWithForeignPtr ptr $ \p -> do
219 -- n <- hGetBufSome h p size
218 -- n <- hGetBufSome h p size
220#if MIN_VERSION_base(4,15,0)
219#if MIN_VERSION_base(4,15,0)
221 n <- RawIO.read fd p 0 size
220 n <- RawIO.read fd p 0 size
@@ -237,7 +236,7 @@ readArrayUpto size (Handle fd) = do
237{-# INLINABLE writeArray #-}
236{-# INLINABLE writeArray #-}
238writeArray :: Storable a => Handle -> Array a -> IO ()
237writeArray :: Storable a => Handle -> Array a -> IO ()
239writeArray _ arr | A.length arr == 0 = return ()
238writeArray _ arr | A.length arr == 0 = return ()
240−writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
239+writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
241 -- RawIO.writeAll fd (castPtr p) aLen
240 -- RawIO.writeAll fd (castPtr p) aLen
242#if MIN_VERSION_base(4,15,0)
241#if MIN_VERSION_base(4,15,0)
243 RawIO.write fd (castPtr p) 0 aLen
242 RawIO.write fd (castPtr p) 0 aLen
@@ -262,7 +261,7 @@ writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
262writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
261writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
263writeIOVec _ iov | A.length iov == 0 = return ()
262writeIOVec _ iov | A.length iov == 0 = return ()
264writeIOVec (Handle fd) iov =
263writeIOVec (Handle fd) iov =
265− withForeignPtr (aStart iov) $ \p ->
264+ unsafeWithForeignPtr (aStart iov) $ \p ->
266 RawIO.writevAll fd p (A.length iov)
265 RawIO.writevAll fd p (A.length iov)
267#endif
266#endif
268
267
src/Streamly/Internal/FileSystem/Handle.hs+3−3
@@ -95,7 +95,6 @@ where
95
95
96import Control.Monad.IO.Class (MonadIO(..))
96import Control.Monad.IO.Class (MonadIO(..))
97import Data.Word (Word8)
97import Data.Word (Word8)
98−import Foreign.ForeignPtr (withForeignPtr)
99import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
98import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
100import Foreign.Ptr (minusPtr, plusPtr)
99import Foreign.Ptr (minusPtr, plusPtr)
101import Foreign.Storable (Storable(..))
100import Foreign.Storable (Storable(..))
@@ -103,6 +102,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
103import System.IO (Handle, hGetBufSome, hPutBuf)
102import System.IO (Handle, hGetBufSome, hPutBuf)
104import Prelude hiding (read)
103import Prelude hiding (read)
105
104
105+import Streamly.Internal.BaseCompat
106import Streamly.Data.Fold (Fold)
106import Streamly.Data.Fold (Fold)
107import Streamly.Internal.Data.Fold.Type (Fold2(..))
107import Streamly.Internal.Data.Fold.Type (Fold2(..))
108import Streamly.Internal.Data.Unfold.Type (Unfold(..))
108import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -148,7 +148,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
148readArrayUpto size h = do
148readArrayUpto size h = do
149 ptr <- mallocPlainForeignPtrBytes size
149 ptr <- mallocPlainForeignPtrBytes size
150 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
150 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
151− withForeignPtr ptr $ \p -> do
151+ unsafeWithForeignPtr ptr $ \p -> do
152 n <- hGetBufSome h p size
152 n <- hGetBufSome h p size
153 -- XXX shrink only if the diff is significant
153 -- XXX shrink only if the diff is significant
154 return $
154 return $
@@ -292,7 +292,7 @@ toBytes = AS.concat . toChunks
292{-# INLINABLE writeArray #-}
292{-# INLINABLE writeArray #-}
293writeArray :: Storable a => Handle -> Array a -> IO ()
293writeArray :: Storable a => Handle -> Array a -> IO ()
294writeArray _ arr | A.length arr == 0 = return ()
294writeArray _ arr | A.length arr == 0 = return ()
295−writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
295+writeArray h Array{..} = unsafeWithForeignPtr aStart $ \p -> hPutBuf h p aLen
296 where
296 where
297 aLen =
297 aLen =
298 let p = unsafeForeignPtrToPtr aStart
298 let p = unsafeForeignPtrToPtr aStart
src/Streamly/Internal/Network/Socket.hs+4−3
@@ -67,7 +67,7 @@ import Control.Monad.Catch (MonadCatch, finally, MonadMask)
67import Control.Monad.IO.Class (MonadIO(..))
67import Control.Monad.IO.Class (MonadIO(..))
68import Control.Monad (forM_, when)
68import Control.Monad (forM_, when)
69import Data.Word (Word8)
69import Data.Word (Word8)
70−import Foreign.ForeignPtr (withForeignPtr)
70+
71import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
71import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
72import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
72import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
73import Foreign.Storable (Storable(..))
73import Foreign.Storable (Storable(..))
@@ -85,6 +85,7 @@ import Prelude hiding (read)
85
85
86import qualified Network.Socket as Net
86import qualified Network.Socket as Net
87
87
88+import Streamly.Internal.BaseCompat
88import Streamly.Internal.Data.Unfold.Type (Unfold(..))
89import Streamly.Internal.Data.Unfold.Type (Unfold(..))
89import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
90import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
90import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
91import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
@@ -253,7 +254,7 @@ readArrayUptoWith
253readArrayUptoWith f size h = do
254readArrayUptoWith f size h = do
254 ptr <- mallocPlainForeignPtrBytes size
255 ptr <- mallocPlainForeignPtrBytes size
255 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
256 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
256− withForeignPtr ptr $ \p -> do
257+ unsafeWithForeignPtr ptr $ \p -> do
257 n <- f h p size
258 n <- f h p size
258 let v = A.unsafeFreeze
259 let v = A.unsafeFreeze
259 $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
260 $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
@@ -302,7 +303,7 @@ writeArrayWith :: Storable a
302 -> Array a
303 -> Array a
303 -> IO ()
304 -> IO ()
304writeArrayWith _ _ arr | A.length arr == 0 = return ()
305writeArrayWith _ _ arr | A.length arr == 0 = return ()
305−writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
306+writeArrayWith f h Array{..} = unsafeWithForeignPtr aStart $ \p ->
306 f h (castPtr p) aLen
307 f h (castPtr p) aLen
307 where
308 where
308 aLen =
309 aLen =