Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed Jun 17, 2021
1 parent 4f6980c commit 79628bd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/Streamly/Internal/Data/Array/Foreign.hs
Expand Up @@ -129,9 +129,9 @@ module Streamly.Internal.Data.Array.Foreign
-- * Casting
, cast
, unsafeCast
, asPtr
, unsafeAsPtr
, asBytes
, asCString
, unsafeAsCString

-- * Folding Arrays
, streamFold
Expand Down Expand Up @@ -579,8 +579,8 @@ cast arr =
--
-- /Pre-release/
--
asPtr :: Array a -> (Ptr b -> IO c) -> IO c
asPtr Array{..} act = do
unsafeAsPtr :: Array a -> (Ptr b -> IO c) -> IO c
unsafeAsPtr Array{..} act = do
unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)

-- | Convert an array of any type into a null terminated CString Ptr.
Expand All @@ -591,8 +591,8 @@ asPtr Array{..} act = do
--
-- /Pre-release/
--
asCString :: Array a -> (CString -> IO b) -> IO b
asCString arr act = do
unsafeAsCString :: Array a -> (CString -> IO b) -> IO b
unsafeAsCString arr act = do
let Array{..} = asBytes arr <> A.fromList [0]
unsafeWithForeignPtr aStart $ \ptr -> act (castPtr ptr)

Expand Down
8 changes: 4 additions & 4 deletions src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs
Expand Up @@ -30,7 +30,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type

-- * Construction
, mutableArray
, withNewArray
, unsafeWithNewArray
, newArray
, newArrayAligned
, newArrayAlignedUnmanaged
Expand Down Expand Up @@ -306,9 +306,9 @@ newArray = newArrayAligned (alignment (undefined :: a))

-- | Allocate an Array of the given size and run an IO action passing the array
-- start pointer.
{-# INLINE withNewArray #-}
withNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
withNewArray count f = do
{-# INLINE unsafeWithNewArray #-}
unsafeWithNewArray :: forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Array a)
unsafeWithNewArray count f = do
arr <- newArray count
unsafeWithForeignPtr (aStart arr) $ \p -> f p >> return arr

Expand Down
2 changes: 1 addition & 1 deletion src/Streamly/Internal/FileSystem/Event/Darwin.hs
Expand Up @@ -417,7 +417,7 @@ openWatch Config{..} paths = do

withPathName :: Array Word8 -> (PathName -> IO a) -> IO a
withPathName arr act = do
A.asPtr arr $ \ptr ->
A.unsafeAsPtr arr $ \ptr ->
let pname = PathName (castPtr ptr) (fromIntegral (A.length arr))
in act pname

Expand Down
4 changes: 2 additions & 2 deletions src/Streamly/Internal/FileSystem/Event/Linux.hs
Expand Up @@ -668,7 +668,7 @@ addToWatch cfg@Config{..} watch@(Watch handle wdMap) root0 path0 = do
--
-- XXX The file may have even got deleted and then recreated which we will
-- never get to know, document this.
wd <- A.asCString absPath $ \pathPtr ->
wd <- A.unsafeAsCString absPath $ \pathPtr ->
throwErrnoIfMinus1 ("addToWatch: " ++ utf8ToString absPath) $
c_inotify_add_watch (fdFD fd) pathPtr (CUInt createFlags)

Expand Down Expand Up @@ -774,7 +774,7 @@ readOneEvent :: Config -> Watch -> Parser IO Word8 Event
readOneEvent cfg wt@(Watch _ wdMap) = do
let headerLen = (sizeOf (undefined :: CInt)) + 12
arr <- PR.takeEQ headerLen (A.writeN headerLen)
(ewd, eflags, cookie, pathLen) <- PR.fromEffect $ A.asPtr arr readHeader
(ewd, eflags, cookie, pathLen) <- PR.fromEffect $ A.unsafeAsPtr arr readHeader
-- XXX need the "initial" in parsers to return a step type so that "take 0"
-- can return without an input. otherwise if pathLen is 0 we will keep
-- waiting to read one more char before we return this event.
Expand Down
9 changes: 5 additions & 4 deletions src/Streamly/Internal/FileSystem/FD.hs
Expand Up @@ -118,6 +118,8 @@ where

import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import Foreign.ForeignPtr (withForeignPtr)
-- import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import Foreign.Ptr (plusPtr, castPtr)
import Foreign.Storable (Storable(..))
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
Expand All @@ -128,7 +130,6 @@ import Prelude hiding (read)
import qualified GHC.IO.FD as FD
import qualified GHC.IO.Device as RawIO

import Streamly.Internal.BaseCompat
import Streamly.Internal.Data.Array.Foreign.Type (Array(..), byteLength, defaultChunkSize, unsafeFreeze)
import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)

Expand Down Expand Up @@ -214,7 +215,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
readArrayUpto size (Handle fd) = do
ptr <- mallocPlainForeignPtrBytes size
-- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
unsafeWithForeignPtr ptr $ \p -> do
withForeignPtr ptr $ \p -> do
-- n <- hGetBufSome h p size
#if MIN_VERSION_base(4,15,0)
n <- RawIO.read fd p 0 size
Expand All @@ -236,7 +237,7 @@ readArrayUpto size (Handle fd) = do
{-# INLINABLE writeArray #-}
writeArray :: Storable a => Handle -> Array a -> IO ()
writeArray _ arr | A.length arr == 0 = return ()
writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
writeArray (Handle fd) arr = withForeignPtr (aStart arr) $ \p ->
-- RawIO.writeAll fd (castPtr p) aLen
#if MIN_VERSION_base(4,15,0)
RawIO.write fd (castPtr p) 0 aLen
Expand All @@ -261,7 +262,7 @@ writeArray (Handle fd) arr = unsafeWithForeignPtr (aStart arr) $ \p ->
writeIOVec :: Handle -> Array RawIO.IOVec -> IO ()
writeIOVec _ iov | A.length iov == 0 = return ()
writeIOVec (Handle fd) iov =
unsafeWithForeignPtr (aStart iov) $ \p ->
withForeignPtr (aStart iov) $ \p ->
RawIO.writevAll fd p (A.length iov)
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/Streamly/Internal/FileSystem/Handle.hs
Expand Up @@ -96,6 +96,7 @@ where
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr (minusPtr, plusPtr)
import Foreign.Storable (Storable(..))
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
Expand Down Expand Up @@ -148,7 +149,7 @@ readArrayUpto :: Int -> Handle -> IO (Array Word8)
readArrayUpto size h = do
ptr <- mallocPlainForeignPtrBytes size
-- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
unsafeWithForeignPtr ptr $ \p -> do
withForeignPtr ptr $ \p -> do
n <- hGetBufSome h p size
-- XXX shrink only if the diff is significant
return $
Expand Down

0 comments on commit 79628bd

Please sign in to comment.