on master help sign in
Fix review comments
commite503ca640689a4319e8d5ea3c86663d2f12c3053 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateThu 17 Jun 2021, 20:02 (5 years ago) parentdcaa9ef
6 files changed+20−18
src/Streamly/Internal/Data/Array/Foreign.hs+6−6 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4−4 src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1 src/Streamly/Internal/FileSystem/Event/Linux.hs+2−2 src/Streamly/Internal/FileSystem/FD.hs+5−4 src/Streamly/Internal/FileSystem/Handle.hs+2−1
src/Streamly/Internal/Data/Array/Foreign.hs+6−6
@@ -129,9 +129,9 @@ module Streamly.Internal.Data.Array.Foreign
129129 -- * Casting
130130 , cast
131131 , unsafeCast
132− , asPtr
132+ , unsafeAsPtr
133133 , asBytes
134− , asCString
134+ , unsafeAsCString
135135
136136 -- * Folding Arrays
137137 , streamFold
@@ -579,8 +579,8 @@ cast arr =
579579--
580580-- /Pre-release/
581581--
582−asPtr :: Array a -> (Ptr b -> IO c) -> IO c
583−asPtr Array{..} act = do
582+unsafeAsPtr :: Array a -> (Ptr b -> IO c) -> IO c
583+unsafeAsPtr Array{..} act = do
584584 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
585585
586586-- | Convert an array of any type into a null terminated CString Ptr.
@@ -591,8 +591,8 @@ asPtr Array{..} act = do
591591--
592592-- /Pre-release/
593593--
594−asCString :: Array a -> (CString -> IO b) -> IO b
595−asCString arr act = do
594+unsafeAsCString :: Array a -> (CString -> IO b) -> IO b
595+unsafeAsCString arr act = do
596596 let Array{..} = asBytes arr <> A.fromList [0]
597597 unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)
598598
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4−4
@@ -30,7 +30,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
3030
3131 -- * Construction
3232 , mutableArray
33− , withNewArray
33+ , unsafeWithNewArray
3434 , newArray
3535 , newArrayAligned
3636 , newArrayAlignedUnmanaged
@@ -306,9 +306,9 @@ newArray = newArrayAligned (alignment (undefined :: a))
306306
307307-- | Allocate an Array of the given size and run an IO action passing the array
308308-- start pointer.
309−{-# INLINE withNewArray #-}
310−withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
311−withNewArray count f = do
309+{-# INLINE unsafeWithNewArray #-}
310+unsafeWithNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
311+unsafeWithNewArray count f = do
312312 arr <- newArray count
313313 unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr
314314
src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1
@@ -417,7 +417,7 @@ openWatch Config{..} paths = do
417417
418418 withPathName :: Array Word8 -> (PathName -> IO a) -> IO a
419419 withPathName arr act = do
420− A.asPtr arr $ \ptr ->
420+ A.unsafeAsPtr arr $ \ptr ->
421421 let pname = PathName (castPtr ptr) (fromIntegral (A.length arr))
422422 in act pname
423423
src/Streamly/Internal/FileSystem/Event/Linux.hs+2−2
@@ -668,7 +668,7 @@ addToWatch cfg@Config{..} watch@(Watch handle wdMap) root0 path0 = do
668668 --
669669 -- XXX The file may have even got deleted and then recreated which we will
670670 -- never get to know, document this.
671− wd <- A.asCString absPath $ \pathPtr ->
671+ wd <- A.unsafeAsCString absPath $ \pathPtr ->
672672 throwErrnoIfMinus1 ("addToWatch: " ++ utf8ToString absPath) $
673673 c_inotify_add_watch (fdFD fd) pathPtr (CUInt createFlags)
674674
@@ -774,7 +774,7 @@ readOneEvent :: Config -> Watch -> Parser IO Word8 Event
774774readOneEvent cfg wt@(Watch _ wdMap) = do
775775 let headerLen = (sizeOf (undefined :: CInt)) + 12
776776 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
778778 -- XXX need the "initial" in parsers to return a step type so that "take 0"
779779 -- can return without an input. otherwise if pathLen is 0 we will keep
780780 -- waiting to read one more char before we return this event.
src/Streamly/Internal/FileSystem/FD.hs+5−4
@@ -118,6 +118,8 @@ where
118118
119119import Control.Monad.IO.Class (MonadIO(..))
120120import Data.Word (Word8)
121+import Foreign.ForeignPtr (withForeignPtr)
122+-- import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
121123import Foreign.Ptr (plusPtr, castPtr)
122124import Foreign.Storable (Storable(..))
123125import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -128,7 +130,6 @@ import Prelude hiding (read)
128130import qualified GHC.IO.FD as FD
129131import qualified GHC.IO.Device as RawIO
130132
131−import Streamly.Internal.BaseCompat
132133import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
133134import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
134135
@@ -214,7 +215,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
214215readArrayUpto size (Handle fd) = do
215216 ptr <- mallocPlainForeignPtrBytes size
216217 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
217− unsafeWithForeignPtr ptr $ \p -> do
218+ withForeignPtr ptr $ \p -> do
218219 -- n <- hGetBufSome h p size
219220#if MIN_VERSION_base(4,15,0)
220221 n <- RawIO.read fd p 0 size
@@ -236,7 +237,7 @@ readArrayUpto size (Handle fd) = do
236237{-# INLINABLE writeArray #-}
237238writeArray :: Storable a => Handle -> Array a -> IO ()
238239writeArray _ arr | A.length arr == 0 = return ()
239−writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
240+writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
240241 -- RawIO.writeAll fd (castPtr p) aLen
241242#if MIN_VERSION_base(4,15,0)
242243 RawIO.write fd (castPtr p) 0 aLen
@@ -261,7 +262,7 @@ writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
261262writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
262263writeIOVec _ iov | A.length iov == 0 = return ()
263264writeIOVec (Handle fd) iov =
264− unsafeWithForeignPtr (aStart iov) $ \p ->
265+ withForeignPtr (aStart iov) $ \p ->
265266 RawIO.writevAll fd p (A.length iov)
266267#endif
267268
src/Streamly/Internal/FileSystem/Handle.hs+2−1
@@ -96,6 +96,7 @@ where
9696import Control.Monad.IO.Class (MonadIO(..))
9797import Data.Word (Word8)
9898import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
99+import Foreign.ForeignPtr (withForeignPtr)
99100import Foreign.Ptr (minusPtr, plusPtr)
100101import Foreign.Storable (Storable(..))
101102import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -148,7 +149,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
148149readArrayUpto size h = do
149150 ptr <- mallocPlainForeignPtrBytes size
150151 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
151− unsafeWithForeignPtr ptr $ \p -> do
152+ withForeignPtr ptr $ \p -> do
152153 n <- hGetBufSome h p size
153154 -- XXX shrink only if the diff is significant
154155 return $