on master help sign in
Fix review comments
e503ca6Ranjeet Kumar Ranjan
src/Streamly/Internal/Data/Array/Foreign.hs+6−6
@@ -129,9 +129,9 @@ module Streamly.Internal.Data.Array.Foreign
129 -- * Casting
129 -- * Casting
130 , cast
130 , cast
131 , unsafeCast
131 , unsafeCast
132− , asPtr
132+ , unsafeAsPtr
133 , asBytes
133 , asBytes
134− , asCString
134+ , unsafeAsCString
135
135
136 -- * Folding Arrays
136 -- * Folding Arrays
137 , streamFold
137 , streamFold
@@ -579,8 +579,8 @@ cast arr =
579--
579--
580-- /Pre-release/
580-- /Pre-release/
581--
581--
582−asPtr :: Array a -> (Ptr b -> IO c) -> IO c
582+unsafeAsPtr :: Array a -> (Ptr b -> IO c) -> IO c
583−asPtr Array{..} act = do
583+unsafeAsPtr Array{..} act = do
584 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
584 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
585
585
586-- | 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.
@@ -591,8 +591,8 @@ asPtr Array{..} act = do
591--
591--
592-- /Pre-release/
592-- /Pre-release/
593--
593--
594−asCString :: Array a -> (CString -> IO b) -> IO b
594+unsafeAsCString :: Array a -> (CString -> IO b) -> IO b
595−asCString arr act = do
595+unsafeAsCString arr act = do
596 let Array{..} = asBytes arr <> A.fromList [0]
596 let Array{..} = asBytes arr <> A.fromList [0]
597 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
597 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
598
598
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4−4
@@ -30,7 +30,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
30
30
31 -- * Construction
31 -- * Construction
32 , mutableArray
32 , mutableArray
33− , withNewArray
33+ , unsafeWithNewArray
34 , newArray
34 , newArray
35 , newArrayAligned
35 , newArrayAligned
36 , newArrayAlignedUnmanaged
36 , newArrayAlignedUnmanaged
@@ -306,9 +306,9 @@ newArray = newArrayAligned (alignment (undefined :: a))
306
306
307-- | Allocate an Array of the given size and run an IO action passing the array
307-- | Allocate an Array of the given size and run an IO action passing the array
308-- start pointer.
308-- start pointer.
309−{-# INLINE withNewArray #-}
309+{-# INLINE unsafeWithNewArray #-}
310−withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
310+unsafeWithNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
311−withNewArray count f = do
311+unsafeWithNewArray count f = do
312 arr <- newArray count
312 arr <- newArray count
313 unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr
313 unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr
314
314
src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1
@@ -417,7 +417,7 @@ openWatch Config{..} paths = do
417
417
418 withPathName :: Array Word8 -> (PathName -> IO a) -> IO a
418 withPathName :: Array Word8 -> (PathName -> IO a) -> IO a
419 withPathName arr act = do
419 withPathName arr act = do
420− A.asPtr arr $ \ptr ->
420+ A.unsafeAsPtr arr $ \ptr ->
421 let pname = PathName (castPtr ptr) (fromIntegral (A.length arr))
421 let pname = PathName (castPtr ptr) (fromIntegral (A.length arr))
422 in act pname
422 in act pname
423
423
src/Streamly/Internal/FileSystem/Event/Linux.hs+2−2
@@ -668,7 +668,7 @@ addToWatch cfg@Config{..} watch@(Watch handle wdMap) root0 path0 = do
668 --
668 --
669 -- XXX The file may have even got deleted and then recreated which we will
669 -- XXX The file may have even got deleted and then recreated which we will
670 -- never get to know, document this.
670 -- never get to know, document this.
671− wd <- A.asCString absPath $ \pathPtr ->
671+ wd <- A.unsafeAsCString absPath $ \pathPtr ->
672 throwErrnoIfMinus1 ("addToWatch: " ++ utf8ToString absPath) $
672 throwErrnoIfMinus1 ("addToWatch: " ++ utf8ToString absPath) $
673 c_inotify_add_watch (fdFD fd) pathPtr (CUInt createFlags)
673 c_inotify_add_watch (fdFD fd) pathPtr (CUInt createFlags)
674
674
@@ -774,7 +774,7 @@ readOneEvent :: Config -> Watch -> Parser IO Word8 Event
774readOneEvent cfg wt@(Watch _ wdMap) = do
774readOneEvent cfg wt@(Watch _ wdMap) = do
775 let headerLen = (sizeOf (undefined :: CInt)) + 12
775 let headerLen = (sizeOf (undefined :: CInt)) + 12
776 arr <- PR.takeEQ headerLen (A.writeN headerLen)
776 arr <- PR.takeEQ headerLen (A.writeN headerLen)
777− (ewd, eflags, cookie, pathLen) <- PR.fromEffect $ A.asPtr arr readHeader
777+ (ewd, eflags, cookie, pathLen) <- PR.fromEffect $ A.unsafeAsPtr arr readHeader
778 -- XXX need the "initial" in parsers to return a step type so that "take 0"
778 -- XXX need the "initial" in parsers to return a step type so that "take 0"
779 -- can return without an input. otherwise if pathLen is 0 we will keep
779 -- can return without an input. otherwise if pathLen is 0 we will keep
780 -- waiting to read one more char before we return this event.
780 -- waiting to read one more char before we return this event.
src/Streamly/Internal/FileSystem/FD.hs+5−4
@@ -118,6 +118,8 @@ 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)
121import Foreign.Ptr (plusPtr, castPtr)
123import Foreign.Ptr (plusPtr, castPtr)
122import Foreign.Storable (Storable(..))
124import Foreign.Storable (Storable(..))
123import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
125import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -128,7 +130,6 @@ import Prelude hiding (read)
128import qualified GHC.IO.FD as FD
130import qualified GHC.IO.FD as FD
129import qualified GHC.IO.Device as RawIO
131import qualified GHC.IO.Device as RawIO
130
132
131−import Streamly.Internal.BaseCompat
132import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
133import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
133import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
134import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
134
135
@@ -214,7 +215,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
214readArrayUpto size (Handle fd) = do
215readArrayUpto size (Handle fd) = do
215 ptr <- mallocPlainForeignPtrBytes size
216 ptr <- mallocPlainForeignPtrBytes size
216 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
217 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
217− unsafeWithForeignPtr ptr $ \p -> do
218+ withForeignPtr ptr $ \p -> do
218 -- n <- hGetBufSome h p size
219 -- n <- hGetBufSome h p size
219#if MIN_VERSION_base(4,15,0)
220#if MIN_VERSION_base(4,15,0)
220 n <- RawIO.read fd p 0 size
221 n <- RawIO.read fd p 0 size
@@ -236,7 +237,7 @@ readArrayUpto size (Handle fd) = do
236{-# INLINABLE writeArray #-}
237{-# INLINABLE writeArray #-}
237writeArray :: Storable a => Handle -> Array a -> IO ()
238writeArray :: Storable a => Handle -> Array a -> IO ()
238writeArray _ arr | A.length arr == 0 = return ()
239writeArray _ arr | A.length arr == 0 = return ()
239−writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
240+writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
240 -- RawIO.writeAll fd (castPtr p) aLen
241 -- RawIO.writeAll fd (castPtr p) aLen
241#if MIN_VERSION_base(4,15,0)
242#if MIN_VERSION_base(4,15,0)
242 RawIO.write fd (castPtr p) 0 aLen
243 RawIO.write fd (castPtr p) 0 aLen
@@ -261,7 +262,7 @@ writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
261writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
262writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
262writeIOVec _ iov | A.length iov == 0 = return ()
263writeIOVec _ iov | A.length iov == 0 = return ()
263writeIOVec (Handle fd) iov =
264writeIOVec (Handle fd) iov =
264− unsafeWithForeignPtr (aStart iov) $ \p ->
265+ withForeignPtr (aStart iov) $ \p ->
265 RawIO.writevAll fd p (A.length iov)
266 RawIO.writevAll fd p (A.length iov)
266#endif
267#endif
267
268
src/Streamly/Internal/FileSystem/Handle.hs+2−1
@@ -96,6 +96,7 @@ where
96import Control.Monad.IO.Class (MonadIO(..))
96import Control.Monad.IO.Class (MonadIO(..))
97import Data.Word (Word8)
97import Data.Word (Word8)
98import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
98import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
99+import Foreign.ForeignPtr (withForeignPtr)
99import Foreign.Ptr (minusPtr, plusPtr)
100import Foreign.Ptr (minusPtr, plusPtr)
100import Foreign.Storable (Storable(..))
101import Foreign.Storable (Storable(..))
101import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
102import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -148,7 +149,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
148readArrayUpto size h = do
149readArrayUpto size h = do
149 ptr <- mallocPlainForeignPtrBytes size
150 ptr <- mallocPlainForeignPtrBytes size
150 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
151 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
151− unsafeWithForeignPtr ptr $ \p -> do
152+ withForeignPtr ptr $ \p -> do
152 n <- hGetBufSome h p size
153 n <- hGetBufSome h p size
153 -- XXX shrink only if the diff is significant
154 -- XXX shrink only if the diff is significant
154 return $
155 return $