2-- Module : Streamly.Internal.System.IOVec
3-- Copyright : (c) 2019 Composewell Technologies
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
10-- Low level IO routines interfacing the operating system.
13module Streamly.Internal.System.IOVec
17#if !defined(mingw32_HOST_OS)
26#if !defined(mingw32_HOST_OS)
27import Control.Monad (when)
28import Control.Monad.IO.Class (MonadIO(..))
29import Foreign.Ptr (castPtr)
30import Streamly.Internal.Data.Array.Foreign.Mut.Type (length)
31import Streamly.Internal.Data.SVar.Type (adaptState)
32import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array(..))
34import qualified Streamly.Internal.Data.Array.Foreign.Type as Array
35import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MArray
36import qualified Streamly.Internal.Data.Stream.StreamD as D
39import Streamly.Internal.System.IOVec.Type
41import Prelude hiding (length)
43#if !defined(mingw32_HOST_OS)
46 | GatherBuffering s arr Int
47 | GatherYielding arr (GatherState s arr)
50-- | @groupIOVecsOf maxBytes maxEntries@ groups arrays in the incoming stream
51-- to create a stream of 'IOVec' arrays with a maximum of @maxBytes@ bytes in
52-- each array and a maximum of @maxEntries@ entries in each array.
55{-# INLINE_NORMAL groupIOVecsOfMut #-}
56groupIOVecsOfMut :: MonadIO m
57 => Int -> Int -> D.Stream m (Array a) -> D.Stream m (Array IOVec)
58groupIOVecsOfMut n maxIOVLen (D.Stream step state) =
59 D.Stream step' (GatherInitial state)
63 {-# INLINE_LATE step' #-}
64 step' gst (GatherInitial st) = do
66 -- XXX we can pass the module string from the higher level API
67 error $ "Streamly.Internal.Data.Array.Foreign.Mut.Type.groupIOVecsOf: the size of "
68 ++ "groups [" ++ show n ++ "] must be a natural number"
69 when (maxIOVLen <= 0) $
70 -- XXX we can pass the module string from the higher level API
71 error $ "Streamly.Internal.Data.Array.Foreign.Mut.Type.groupIOVecsOf: the number of "
72 ++ "IOVec entries [" ++ show n ++ "] must be a natural number"
73 r <- step (adaptState gst) st
77 len = MArray.byteLength arr
78 iov <- liftIO $ MArray.newArray maxIOVLen
79 iov' <- liftIO $ MArray.snocUnsafe iov (IOVec (castPtr p)
82 then return $ D.Skip (GatherYielding iov' (GatherInitial s))
83 else return $ D.Skip (GatherBuffering s iov' len)
84 D.Skip s -> return $ D.Skip (GatherInitial s)
85 D.Stop -> return D.Stop
87 step' gst (GatherBuffering st iov len) = do
88 r <- step (adaptState gst) st
92 alen = MArray.byteLength arr
94 if len' > n || length iov >= maxIOVLen
96 iov' <- liftIO $ MArray.newArray maxIOVLen
97 iov'' <- liftIO $ MArray.snocUnsafe iov' (IOVec (castPtr p)
99 return $ D.Skip (GatherYielding iov
100 (GatherBuffering s iov'' alen))
102 iov' <- liftIO $ MArray.snocUnsafe iov (IOVec (castPtr p)
104 return $ D.Skip (GatherBuffering s iov' len')
105 D.Skip s -> return $ D.Skip (GatherBuffering s iov len)
106 D.Stop -> return $ D.Skip (GatherYielding iov GatherFinish)
108 step' _ GatherFinish = return D.Stop
110 step' _ (GatherYielding iov next) = return $ D.Yield iov next
112-- | @groupIOVecsOf maxBytes maxEntries@ groups arrays in the incoming stream
113-- to create a stream of 'IOVec' arrays with a maximum of @maxBytes@ bytes in
114-- each array and a maximum of @maxEntries@ entries in each array.
117{-# INLINE_NORMAL groupIOVecsOf #-}
118groupIOVecsOf :: MonadIO m
121 -> D.Stream m (Array.Array a) -> D.Stream m (Array.Array IOVec)
122groupIOVecsOf n maxIOVLen str =
123 D.map Array.unsafeFreeze
124 $ groupIOVecsOfMut n maxIOVLen
125 $ D.map Array.unsafeThaw str
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list