on master help sign in
Replace withForeignPtr by unsafeWithForeignPtr

Fix review comments Fix ghc 9.1 build issue Fix ghc 9.1 build issue

commitdcaa9ef2d7e81d3b98d53f85b201ba8ef79942e8 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateWed 9 Jun 2021, 13:42 (5 years ago) parent5953313
8 files changed+50−30
src/Streamly/Internal/BaseCompat.hs+15 src/Streamly/Internal/Data/Array/Foreign.hs+6−5 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−8 src/Streamly/Internal/Data/Array/Stream/Foreign.hs+5−3 src/Streamly/Internal/Data/Stream/IsStream/Transform.hs+3−3 src/Streamly/Internal/FileSystem/FD.hs+4−5 src/Streamly/Internal/FileSystem/Handle.hs+3−3 src/Streamly/Internal/Network/Socket.hs+4−3
src/Streamly/Internal/BaseCompat.hs+15
@@ -13,6 +13,7 @@ module Streamly.Internal.BaseCompat
1313 , errorWithoutStackTrace
1414 , fromLeft
1515 , fromRight
16+ , unsafeWithForeignPtr
1617 )
1718where
1819
@@ -20,6 +21,13 @@ import Data.Coerce (Coercible, coerce)
2021#if MIN_VERSION_base(4,10,0)
2122import Data.Either (fromRight, fromLeft)
2223#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
2331
2432{-# INLINE (#.) #-}
2533(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
@@ -40,3 +48,10 @@ fromRight :: b -> Either a b -> b
4048fromRight _ (Right b) = b
4149fromRight b _ = b
4250#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 ((<>))
147147import Data.Word (Word8)
148148-- import Data.Functor.Identity (Identity)
149149import Foreign.C.String (CString)
150−import Foreign.ForeignPtr (withForeignPtr, castForeignPtr)
150+import Foreign.ForeignPtr (castForeignPtr)
151151import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
152152import Foreign.Ptr (plusPtr, castPtr)
153153import Foreign.Storable (Storable(..))
@@ -158,6 +158,7 @@ import GHC.Ptr (Ptr(..))
158158import GHC.Prim (touch#)
159159import GHC.IO (IO(..))
160160
161+import Streamly.Internal.BaseCompat
161162import Streamly.Internal.Data.Array.Foreign.Type (Array(..), length)
162163import Streamly.Internal.Data.Fold.Type (Fold(..))
163164import Streamly.Internal.Data.Producer.Type (Producer)
@@ -440,7 +441,7 @@ getIndex arr i =
440441 if i < 0 || i > length arr - 1
441442 then Nothing
442443 else A.unsafeInlineIO $
443− withForeignPtr (aStart arr) $ \p -> Just <$> peekElemOff p i
444+ unsafeWithForeignPtr (aStart arr) $ \p -> Just <$> peekElemOff p i
444445
445446{-
446447-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
@@ -478,7 +479,7 @@ writeIndex arr i a = do
478479 then error $ "writeIndex: specified array index " ++ show i
479480 ++ " is beyond the maximum index " ++ show maxIndex
480481 else
481− liftIO $ withForeignPtr (aStart arr) $ \p ->
482+ liftIO $ unsafeWithForeignPtr (aStart arr) $ \p ->
482483 pokeElemOff p i a
483484
484485{-
@@ -580,7 +581,7 @@ cast arr =
580581--
581582asPtr :: Array a -> (Ptr b -> IO c) -> IO c
582583asPtr Array{..} act = do
583− withForeignPtr aStart $ \ptr -> act (castPtr ptr)
584+ unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
584585
585586-- | Convert an array of any type into a null terminated CString Ptr.
586587--
@@ -593,7 +594,7 @@ asPtr Array{..} act = do
593594asCString :: Array a -> (CString -> IO b) -> IO b
594595asCString arr act = do
595596 let Array{..} = asBytes arr <> A.fromList [0]
596− withForeignPtr aStart $ \ptr -> act (castPtr ptr)
597+ unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
597598
598599-------------------------------------------------------------------------------
599600-- Folds
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+10−8
@@ -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}
src/Streamly/Internal/Data/Array/Stream/Foreign.hs+5−3
@@ -49,7 +49,7 @@ import Control.Exception (assert)
4949import Control.Monad.Catch (MonadThrow, throwM)
5050import Control.Monad.IO.Class (MonadIO(..))
5151import Data.Word (Word8)
52−import Foreign.ForeignPtr (withForeignPtr, touchForeignPtr)
52+import Foreign.ForeignPtr (touchForeignPtr)
5353import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
5454import Foreign.Ptr (minusPtr, plusPtr, castPtr)
5555import Foreign.Storable (Storable(..))
@@ -63,6 +63,8 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
6363#if !defined(mingw32_HOST_OS)
6464import Streamly.Internal.FileSystem.FDIO (IOVec(..))
6565#endif
66+
67+import Streamly.Internal.BaseCompat
6668import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
6769import Streamly.Internal.Data.Fold.Type (Fold(..))
6870import Streamly.Internal.Data.Parser (ParseError(..))
@@ -382,7 +384,7 @@ spliceArraysLenUnsafe len buffered = do
382384 where
383385
384386 writeArr dst (MA.Array as ae _) =
385− liftIO $ withForeignPtr as $ \src -> do
387+ liftIO $ unsafeWithForeignPtr as $ \src -> do
386388 let count = ae `minusPtr` src
387389 A.memcpy (castPtr dst) (castPtr src) count
388390 return $ dst `plusPtr` count
@@ -400,7 +402,7 @@ _spliceArrays s = do
400402 where
401403
402404 writeArr dst (Array as ae) =
403− liftIO $ withForeignPtr as $ \src -> do
405+ liftIO $ unsafeWithForeignPtr as $ \src -> do
404406 let count = ae `minusPtr` src
405407 A.memcpy (castPtr dst) (castPtr src) count
406408 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)
352352-- >>> drain $ Stream.mapM putStr $ Stream.fromList ["a", "b", "c"]
353353-- abc
354354--
355−-- >>> :{
356−-- drain $ Stream.replicateM 10 (return 1)
355+-- >>> :{
356+-- drain $ Stream.replicateM 10 (return 1)
357357-- & (fromSerial . Stream.mapM (\x -> threadDelay 1000000 >> print x))
358358-- :}
359359-- 1
360360-- ...
361361-- 1
362362--
363−-- > drain $ Stream.replicateM 10 (return 1)
363+-- > drain $ Stream.replicateM 10 (return 1)
364364-- & (fromAsync . Stream.mapM (\x -> threadDelay 1000000 >> print x))
365365-- @
366366--
src/Streamly/Internal/FileSystem/FD.hs+4−5
@@ -118,8 +118,6 @@ where
118118
119119import Control.Monad.IO.Class (MonadIO(..))
120120import Data.Word (Word8)
121−import Foreign.ForeignPtr (withForeignPtr)
122−-- import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
123121import Foreign.Ptr (plusPtr, castPtr)
124122import Foreign.Storable (Storable(..))
125123import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -130,6 +128,7 @@ import Prelude hiding (read)
130128import qualified GHC.IO.FD as FD
131129import qualified GHC.IO.Device as RawIO
132130
131+import Streamly.Internal.BaseCompat
133132import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
134133import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
135134
@@ -215,7 +214,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
215214readArrayUpto size (Handle fd) = do
216215 ptr <- mallocPlainForeignPtrBytes size
217216 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
218− withForeignPtr ptr $ \p -> do
217+ unsafeWithForeignPtr ptr $ \p -> do
219218 -- n <- hGetBufSome h p size
220219#if MIN_VERSION_base(4,15,0)
221220 n <- RawIO.read fd p 0 size
@@ -237,7 +236,7 @@ readArrayUpto size (Handle fd) = do
237236{-# INLINABLE writeArray #-}
238237writeArray :: Storable a => Handle -> Array a -> IO ()
239238writeArray _ arr | A.length arr == 0 = return ()
240−writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
239+writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
241240 -- RawIO.writeAll fd (castPtr p) aLen
242241#if MIN_VERSION_base(4,15,0)
243242 RawIO.write fd (castPtr p) 0 aLen
@@ -262,7 +261,7 @@ writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
262261writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
263262writeIOVec _ iov | A.length iov == 0 = return ()
264263writeIOVec (Handle fd) iov =
265− withForeignPtr (aStart iov) $ \p ->
264+ unsafeWithForeignPtr (aStart iov) $ \p ->
266265 RawIO.writevAll fd p (A.length iov)
267266#endif
268267
src/Streamly/Internal/FileSystem/Handle.hs+3−3
@@ -95,7 +95,6 @@ where
9595
9696import Control.Monad.IO.Class (MonadIO(..))
9797import Data.Word (Word8)
98−import Foreign.ForeignPtr (withForeignPtr)
9998import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
10099import Foreign.Ptr (minusPtr, plusPtr)
101100import Foreign.Storable (Storable(..))
@@ -103,6 +102,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
103102import System.IO (Handle, hGetBufSome, hPutBuf)
104103import Prelude hiding (read)
105104
105+import Streamly.Internal.BaseCompat
106106import Streamly.Data.Fold (Fold)
107107import Streamly.Internal.Data.Fold.Type (Fold2(..))
108108import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -148,7 +148,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
148148readArrayUpto size h = do
149149 ptr <- mallocPlainForeignPtrBytes size
150150 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
151− withForeignPtr ptr $ \p -> do
151+ unsafeWithForeignPtr ptr $ \p -> do
152152 n <- hGetBufSome h p size
153153 -- XXX shrink only if the diff is significant
154154 return $
@@ -292,7 +292,7 @@ toBytes = AS.concat . toChunks
292292{-# INLINABLE writeArray #-}
293293writeArray :: Storable a => Handle -> Array a -> IO ()
294294writeArray _ 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
296296 where
297297 aLen =
298298 let p = unsafeForeignPtrToPtr aStart
src/Streamly/Internal/Network/Socket.hs+4−3
@@ -67,7 +67,7 @@ import Control.Monad.Catch (MonadCatch, finally, MonadMask)
6767import Control.Monad.IO.Class (MonadIO(..))
6868import Control.Monad (forM_, when)
6969import Data.Word (Word8)
70−import Foreign.ForeignPtr (withForeignPtr)
70+
7171import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
7272import Foreign.Ptr (minusPtr, plusPtr, Ptr, castPtr)
7373import Foreign.Storable (Storable(..))
@@ -85,6 +85,7 @@ import Prelude hiding (read)
8585
8686import qualified Network.Socket as Net
8787
88+import Streamly.Internal.BaseCompat
8889import Streamly.Internal.Data.Unfold.Type (Unfold(..))
8990import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
9091import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
@@ -253,7 +254,7 @@ readArrayUptoWith
253254readArrayUptoWith f size h = do
254255 ptr <- mallocPlainForeignPtrBytes size
255256 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
256− withForeignPtr ptr $ \p -> do
257+ unsafeWithForeignPtr ptr $ \p -> do
257258 n <- f h p size
258259 let v = A.unsafeFreeze
259260 $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
@@ -302,7 +303,7 @@ writeArrayWith :: Storable a
302303 -> Array a
303304 -> IO ()
304305writeArrayWith _ _ arr | A.length arr == 0 = return ()
305−writeArrayWith f h Array{..} = withForeignPtr aStart $ \p ->
306+writeArrayWith f h Array{..} = unsafeWithForeignPtr aStart $ \p ->
306307 f h (castPtr p) aLen
307308 where
308309 aLen =