{-# LANGUAGE ScopedTypeVariables, CPP, BangPatterns, RankNTypes, TupleSections #-}
{-# LANGUAGE Unsafe #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.ByteString.Builder.Internal (
Buffer(..)
, BufferRange(..)
, newBuffer
, bufferSize
, byteStringFromBuffer
, ChunkIOStream(..)
, buildStepToCIOS
, ciosUnitToLazyByteString
, ciosToLazyByteString
, BuildSignal
, BuildStep
, finalBuildStep
, done
, bufferFull
, insertChunk
, fillWithBuildStep
, Builder
, builder
, runBuilder
, runBuilderWith
, empty
, append
, flush
, ensureFree
, byteStringCopy
, byteStringInsert
, byteStringThreshold
, lazyByteStringCopy
, lazyByteStringInsert
, lazyByteStringThreshold
, shortByteString
, maximalCopySize
, byteString
, lazyByteString
, toLazyByteStringWith
, AllocationStrategy
, safeStrategy
, untrimmedStrategy
, customStrategy
, L.smallChunkSize
, L.defaultChunkSize
, L.chunkOverhead
, Put
, put
, runPut
, putToLazyByteString
, putToLazyByteStringWith
, hPut
, putBuilder
, fromPut
) where
import Control.Arrow (second)
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup((<>)))
#endif
import qualified Data.ByteString as S
import qualified Data.ByteString.Internal.Type as S
import qualified Data.ByteString.Lazy.Internal as L
import qualified Data.ByteString.Short.Internal as Sh
import qualified GHC.IO.Buffer as IO (Buffer(..), newByteBuffer)
import GHC.IO.Handle.Internals (wantWritableHandle, flushWriteBuffer)
import GHC.IO.Handle.Types (Handle__, haByteBuffer, haBufferMode)
import System.IO (hFlush, BufferMode(..), Handle)
import Data.IORef
import Foreign
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)
data BufferRange = BufferRange {-# UNPACK #-} !(Ptr Word8)
{-# UNPACK #-} !(Ptr Word8)
data Buffer = Buffer {-# UNPACK #-} !(ForeignPtr Word8)
{-# UNPACK #-} !BufferRange
{-# INLINE bufferSize #-}
bufferSize :: Buffer -> Int
bufferSize :: Buffer -> Int
bufferSize (Buffer ForeignPtr Word8
fpbuf (BufferRange Ptr Word8
_ Ptr Word8
ope)) =
Ptr Word8
ope forall a b. Ptr a -> Ptr b -> Int
`minusPtr` forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fpbuf
{-# INLINE newBuffer #-}
newBuffer :: Int -> IO Buffer
newBuffer :: Int -> IO Buffer
newBuffer Int
size = do
ForeignPtr Word8
fpbuf <- forall a. Int -> IO (ForeignPtr a)
S.mallocByteString Int
size
let pbuf :: Ptr Word8
pbuf = forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fpbuf
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! ForeignPtr Word8 -> BufferRange -> Buffer
Buffer ForeignPtr Word8
fpbuf (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
pbuf (Ptr Word8
pbuf forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size))
{-# INLINE byteStringFromBuffer #-}
byteStringFromBuffer :: Buffer -> S.ByteString
byteStringFromBuffer :: Buffer -> ByteString
byteStringFromBuffer (Buffer ForeignPtr Word8
fpbuf (BufferRange Ptr Word8
op Ptr Word8
_)) =
ForeignPtr Word8 -> Int -> ByteString
S.BS ForeignPtr Word8
fpbuf (Ptr Word8
op forall a b. Ptr a -> Ptr b -> Int
`minusPtr` forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fpbuf)
{-# INLINE trimmedChunkFromBuffer #-}
trimmedChunkFromBuffer :: AllocationStrategy -> Buffer
-> L.ByteString -> L.ByteString
trimmedChunkFromBuffer :: AllocationStrategy -> Buffer -> ByteString -> ByteString
trimmedChunkFromBuffer (AllocationStrategy Maybe (Buffer, Int) -> IO Buffer
_ Int
_ Int -> Int -> Bool
trim) Buffer
buf ByteString
k
| ByteString -> Bool
S.null ByteString
bs = ByteString
k
| Int -> Int -> Bool
trim (ByteString -> Int
S.length ByteString
bs) (Buffer -> Int
bufferSize Buffer
buf) = ByteString -> ByteString -> ByteString
L.Chunk (ByteString -> ByteString
S.copy ByteString
bs) ByteString
k
| Bool
otherwise = ByteString -> ByteString -> ByteString
L.Chunk ByteString
bs ByteString
k
where
bs :: ByteString
bs = Buffer -> ByteString
byteStringFromBuffer Buffer
buf
data ChunkIOStream a =
Finished Buffer a
| Yield1 S.ByteString (IO (ChunkIOStream a))
{-# INLINE yield1 #-}
yield1 :: S.ByteString -> IO (ChunkIOStream a) -> IO (ChunkIOStream a)
yield1 :: forall a.
ByteString -> IO (ChunkIOStream a) -> IO (ChunkIOStream a)
yield1 ByteString
bs IO (ChunkIOStream a)
cios | ByteString -> Bool
S.null ByteString
bs = IO (ChunkIOStream a)
cios
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. ByteString -> IO (ChunkIOStream a) -> ChunkIOStream a
Yield1 ByteString
bs IO (ChunkIOStream a)
cios
{-# INLINE ciosUnitToLazyByteString #-}
ciosUnitToLazyByteString :: AllocationStrategy
-> L.ByteString -> ChunkIOStream () -> L.ByteString
ciosUnitToLazyByteString :: AllocationStrategy -> ByteString -> ChunkIOStream () -> ByteString
ciosUnitToLazyByteString AllocationStrategy
strategy ByteString
k = forall {a}. ChunkIOStream a -> ByteString
go
where
go :: ChunkIOStream a -> ByteString
go (Finished Buffer
buf a
_) = AllocationStrategy -> Buffer -> ByteString -> ByteString
trimmedChunkFromBuffer AllocationStrategy
strategy Buffer
buf ByteString
k
go (Yield1 ByteString
bs IO (ChunkIOStream a)
io) = ByteString -> ByteString -> ByteString
L.Chunk ByteString
bs forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO (ChunkIOStream a -> ByteString
go forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (ChunkIOStream a)
io)
{-# INLINE ciosToLazyByteString #-}
ciosToLazyByteString :: AllocationStrategy
-> (a -> (b, L.ByteString))
-> ChunkIOStream a
-> (b, L.ByteString)
ciosToLazyByteString :: forall a b.
AllocationStrategy
-> (a -> (b, ByteString)) -> ChunkIOStream a -> (b, ByteString)
ciosToLazyByteString AllocationStrategy
strategy a -> (b, ByteString)
k =
ChunkIOStream a -> (b, ByteString)
go
where
go :: ChunkIOStream a -> (b, ByteString)
go (Finished Buffer
buf a
x) =
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (AllocationStrategy -> Buffer -> ByteString -> ByteString
trimmedChunkFromBuffer AllocationStrategy
strategy Buffer
buf) forall a b. (a -> b) -> a -> b
$ a -> (b, ByteString)
k a
x
go (Yield1 ByteString
bs IO (ChunkIOStream a)
io) = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (ByteString -> ByteString -> ByteString
L.Chunk ByteString
bs) forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO (ChunkIOStream a -> (b, ByteString)
go forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (ChunkIOStream a)
io)
type BuildStep a = BufferRange -> IO (BuildSignal a)
data BuildSignal a =
Done {-# UNPACK #-} !(Ptr Word8) a
| BufferFull
{-# UNPACK #-} !Int
{-# UNPACK #-} !(Ptr Word8)
(BuildStep a)
| InsertChunk
{-# UNPACK #-} !(Ptr Word8)
S.ByteString
(BuildStep a)
{-# INLINE done #-}
done :: Ptr Word8
-> a
-> BuildSignal a
done :: forall a. Ptr Word8 -> a -> BuildSignal a
done = forall a. Ptr Word8 -> a -> BuildSignal a
Done
{-# INLINE bufferFull #-}
bufferFull :: Int
-> Ptr Word8
-> BuildStep a
-> BuildSignal a
bufferFull :: forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull = forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
BufferFull
{-# INLINE insertChunk #-}
insertChunk :: Ptr Word8
-> S.ByteString
-> BuildStep a
-> BuildSignal a
insertChunk :: forall a. Ptr Word8 -> ByteString -> BuildStep a -> BuildSignal a
insertChunk = forall a. Ptr Word8 -> ByteString -> BuildStep a -> BuildSignal a
InsertChunk
{-# INLINE fillWithBuildStep #-}
fillWithBuildStep
:: BuildStep a
-> (Ptr Word8 -> a -> IO b)
-> (Ptr Word8 -> Int -> BuildStep a -> IO b)
-> (Ptr Word8 -> S.ByteString -> BuildStep a -> IO b)
-> BufferRange
-> IO b
fillWithBuildStep :: forall a b.
BuildStep a
-> (Ptr Word8 -> a -> IO b)
-> (Ptr Word8 -> Int -> BuildStep a -> IO b)
-> (Ptr Word8 -> ByteString -> BuildStep a -> IO b)
-> BufferRange
-> IO b
fillWithBuildStep BuildStep a
step Ptr Word8 -> a -> IO b
fDone Ptr Word8 -> Int -> BuildStep a -> IO b
fFull Ptr Word8 -> ByteString -> BuildStep a -> IO b
fChunk !BufferRange
br = do
BuildSignal a
signal <- BuildStep a
step BufferRange
br
case BuildSignal a
signal of
Done Ptr Word8
op a
x -> Ptr Word8 -> a -> IO b
fDone Ptr Word8
op a
x
BufferFull Int
minSize Ptr Word8
op BuildStep a
nextStep -> Ptr Word8 -> Int -> BuildStep a -> IO b
fFull Ptr Word8
op Int
minSize BuildStep a
nextStep
InsertChunk Ptr Word8
op ByteString
bs BuildStep a
nextStep -> Ptr Word8 -> ByteString -> BuildStep a -> IO b
fChunk Ptr Word8
op ByteString
bs BuildStep a
nextStep
newtype Builder = Builder (forall r. BuildStep r -> BuildStep r)
{-# INLINE builder #-}
builder :: (forall r. BuildStep r -> BuildStep r)
-> Builder
builder :: (forall r. BuildStep r -> BuildStep r) -> Builder
builder = (forall r. BuildStep r -> BuildStep r) -> Builder
Builder
finalBuildStep :: BuildStep ()
finalBuildStep :: BuildStep ()
finalBuildStep (BufferRange Ptr Word8
op Ptr Word8
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr Word8 -> a -> BuildSignal a
Done Ptr Word8
op ()
{-# INLINE runBuilder #-}
runBuilder :: Builder
-> BuildStep ()
runBuilder :: Builder -> BuildStep ()
runBuilder Builder
b = forall a. Builder -> BuildStep a -> BuildStep a
runBuilderWith Builder
b BuildStep ()
finalBuildStep
{-# INLINE runBuilderWith #-}
runBuilderWith :: Builder
-> BuildStep a
-> BuildStep a
runBuilderWith :: forall a. Builder -> BuildStep a -> BuildStep a
runBuilderWith (Builder forall r. BuildStep r -> BuildStep r
b) = forall r. BuildStep r -> BuildStep r
b
{-# INLINE[1] empty #-}
empty :: Builder
empty :: Builder
empty = (forall r. BuildStep r -> BuildStep r) -> Builder
Builder forall a b. (a -> b) -> a -> b
($)
{-# INLINE[1] append #-}
append :: Builder -> Builder -> Builder
append :: Builder -> Builder -> Builder
append (Builder forall r. BuildStep r -> BuildStep r
b1) (Builder forall r. BuildStep r -> BuildStep r
b2) = (forall r. BuildStep r -> BuildStep r) -> Builder
Builder forall a b. (a -> b) -> a -> b
$ forall r. BuildStep r -> BuildStep r
b1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. BuildStep r -> BuildStep r
b2
instance Semigroup Builder where
{-# INLINE (<>) #-}
<> :: Builder -> Builder -> Builder
(<>) = Builder -> Builder -> Builder
append
instance Monoid Builder where
{-# INLINE mempty #-}
mempty :: Builder
mempty = Builder
empty
{-# INLINE mappend #-}
mappend :: Builder -> Builder -> Builder
mappend = forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mconcat #-}
mconcat :: [Builder] -> Builder
mconcat = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a
mempty
{-# INLINE flush #-}
flush :: Builder
flush :: Builder
flush = (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall {m :: * -> *} {a}.
Monad m =>
BuildStep a -> BufferRange -> m (BuildSignal a)
step
where
step :: BuildStep a -> BufferRange -> m (BuildSignal a)
step BuildStep a
k (BufferRange Ptr Word8
op Ptr Word8
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr Word8 -> ByteString -> BuildStep a -> BuildSignal a
insertChunk Ptr Word8
op ByteString
S.empty BuildStep a
k
newtype Put a = Put { forall a. Put a -> forall r. (a -> BuildStep r) -> BuildStep r
unPut :: forall r. (a -> BuildStep r) -> BuildStep r }
{-# INLINE put #-}
put :: (forall r. (a -> BuildStep r) -> BuildStep r)
-> Put a
put :: forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
put = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put
{-# INLINE runPut #-}
runPut :: Put a
-> BuildStep a
runPut :: forall a. Put a -> BuildStep a
runPut (Put forall r. (a -> BuildStep r) -> BuildStep r
p) = forall r. (a -> BuildStep r) -> BuildStep r
p forall a b. (a -> b) -> a -> b
$ \a
x (BufferRange Ptr Word8
op Ptr Word8
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr Word8 -> a -> BuildSignal a
Done Ptr Word8
op a
x
instance Functor Put where
fmap :: forall a b. (a -> b) -> Put a -> Put b
fmap a -> b
f Put a
p = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \b -> BuildStep r
k -> forall a. Put a -> forall r. (a -> BuildStep r) -> BuildStep r
unPut Put a
p (b -> BuildStep r
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
{-# INLINE fmap #-}
{-# INLINE[1] ap_l #-}
ap_l :: Put a -> Put b -> Put a
ap_l :: forall a b. Put a -> Put b -> Put a
ap_l (Put forall r. (a -> BuildStep r) -> BuildStep r
a) (Put forall r. (b -> BuildStep r) -> BuildStep r
b) = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \a -> BuildStep r
k -> forall r. (a -> BuildStep r) -> BuildStep r
a (\a
a' -> forall r. (b -> BuildStep r) -> BuildStep r
b (\b
_ -> a -> BuildStep r
k a
a'))
{-# INLINE[1] ap_r #-}
ap_r :: Put a -> Put b -> Put b
ap_r :: forall a b. Put a -> Put b -> Put b
ap_r (Put forall r. (a -> BuildStep r) -> BuildStep r
a) (Put forall r. (b -> BuildStep r) -> BuildStep r
b) = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \b -> BuildStep r
k -> forall r. (a -> BuildStep r) -> BuildStep r
a (\a
_ -> forall r. (b -> BuildStep r) -> BuildStep r
b b -> BuildStep r
k)
instance Applicative Put where
{-# INLINE pure #-}
pure :: forall a. a -> Put a
pure a
x = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \a -> BuildStep r
k -> a -> BuildStep r
k a
x
{-# INLINE (<*>) #-}
Put forall r. ((a -> b) -> BuildStep r) -> BuildStep r
f <*> :: forall a b. Put (a -> b) -> Put a -> Put b
<*> Put forall r. (a -> BuildStep r) -> BuildStep r
a = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \b -> BuildStep r
k -> forall r. ((a -> b) -> BuildStep r) -> BuildStep r
f (\a -> b
f' -> forall r. (a -> BuildStep r) -> BuildStep r
a (b -> BuildStep r
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f'))
{-# INLINE (<*) #-}
<* :: forall a b. Put a -> Put b -> Put a
(<*) = forall a b. Put a -> Put b -> Put a
ap_l
{-# INLINE (*>) #-}
*> :: forall a b. Put a -> Put b -> Put b
(*>) = forall a b. Put a -> Put b -> Put b
ap_r
instance Monad Put where
{-# INLINE return #-}
return :: forall a. a -> Put a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE (>>=) #-}
Put forall r. (a -> BuildStep r) -> BuildStep r
m >>= :: forall a b. Put a -> (a -> Put b) -> Put b
>>= a -> Put b
f = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \b -> BuildStep r
k -> forall r. (a -> BuildStep r) -> BuildStep r
m (\a
m' -> forall a. Put a -> forall r. (a -> BuildStep r) -> BuildStep r
unPut (a -> Put b
f a
m') b -> BuildStep r
k)
{-# INLINE (>>) #-}
>> :: forall a b. Put a -> Put b -> Put b
(>>) = forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
{-# INLINE[1] putBuilder #-}
putBuilder :: Builder -> Put ()
putBuilder :: Builder -> Put ()
putBuilder (Builder forall r. BuildStep r -> BuildStep r
b) = forall a. (forall r. (a -> BuildStep r) -> BuildStep r) -> Put a
Put forall a b. (a -> b) -> a -> b
$ \() -> BuildStep r
k -> forall r. BuildStep r -> BuildStep r
b (() -> BuildStep r
k ())
{-# INLINE fromPut #-}
fromPut :: Put () -> Builder
fromPut :: Put () -> Builder
fromPut (Put forall r. (() -> BuildStep r) -> BuildStep r
p) = (forall r. BuildStep r -> BuildStep r) -> Builder
Builder forall a b. (a -> b) -> a -> b
$ \BuildStep r
k -> forall r. (() -> BuildStep r) -> BuildStep r
p (forall a b. a -> b -> a
const BuildStep r
k)
{-# RULES
"ap_l/putBuilder" forall b1 b2.
ap_l (putBuilder b1) (putBuilder b2)
= putBuilder (append b1 b2)
"ap_l/putBuilder/assoc_r" forall b1 b2 (p :: Put a).
ap_l (putBuilder b1) (ap_l (putBuilder b2) p)
= ap_l (putBuilder (append b1 b2)) p
"ap_l/putBuilder/assoc_l" forall (p :: Put a) b1 b2.
ap_l (ap_l p (putBuilder b1)) (putBuilder b2)
= ap_l p (putBuilder (append b1 b2))
#-}
{-# RULES
"ap_r/putBuilder" forall b1 b2.
ap_r (putBuilder b1) (putBuilder b2)
= putBuilder (append b1 b2)
"ap_r/putBuilder/assoc_r" forall b1 b2 (p :: Put a).
ap_r (putBuilder b1) (ap_r (putBuilder b2) p)
= ap_r (putBuilder (append b1 b2)) p
"ap_r/putBuilder/assoc_l" forall (p :: Put a) b1 b2.
ap_r (ap_r p (putBuilder b1)) (putBuilder b2)
= ap_r p (putBuilder (append b1 b2))
#-}
{-# RULES
"ap_l/ap_r/putBuilder/assoc_r" forall b1 b2 (p :: Put a).
ap_l (putBuilder b1) (ap_r (putBuilder b2) p)
= ap_l (putBuilder (append b1 b2)) p
"ap_r/ap_l/putBuilder/assoc_r" forall b1 b2 (p :: Put a).
ap_r (putBuilder b1) (ap_l (putBuilder b2) p)
= ap_l (putBuilder (append b1 b2)) p
"ap_l/ap_r/putBuilder/assoc_l" forall (p :: Put a) b1 b2.
ap_l (ap_r p (putBuilder b1)) (putBuilder b2)
= ap_r p (putBuilder (append b1 b2))
"ap_r/ap_l/putBuilder/assoc_l" forall (p :: Put a) b1 b2.
ap_r (ap_l p (putBuilder b1)) (putBuilder b2)
= ap_r p (putBuilder (append b1 b2))
#-}
hPut :: forall a. Handle -> Put a -> IO a
hPut :: forall a. Handle -> Put a -> IO a
hPut Handle
h Put a
p = do
Int -> BuildStep a -> IO a
fillHandle Int
1 (forall a. Put a -> BuildStep a
runPut Put a
p)
where
fillHandle :: Int -> BuildStep a -> IO a
fillHandle :: Int -> BuildStep a -> IO a
fillHandle !Int
minFree BuildStep a
step = do
IO a
next <- forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle String
"hPut" Handle
h Handle__ -> IO (IO a)
fillHandle_
IO a
next
where
fillHandle_ :: Handle__ -> IO (IO a)
fillHandle_ :: Handle__ -> IO (IO a)
fillHandle_ Handle__
h_ = do
forall {e}. Buffer e -> IO ()
makeSpace forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
refBuf
Buffer Word8 -> IO (IO a)
fillBuffer forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
refBuf
where
refBuf :: IORef (Buffer Word8)
refBuf = Handle__ -> IORef (Buffer Word8)
haByteBuffer Handle__
h_
freeSpace :: Buffer e -> Int
freeSpace Buffer e
buf = forall e. Buffer e -> Int
IO.bufSize Buffer e
buf forall a. Num a => a -> a -> a
- forall e. Buffer e -> Int
IO.bufR Buffer e
buf
makeSpace :: Buffer e -> IO ()
makeSpace Buffer e
buf
| forall e. Buffer e -> Int
IO.bufSize Buffer e
buf forall a. Ord a => a -> a -> Bool
< Int
minFree = do
Handle__ -> IO ()
flushWriteBuffer Handle__
h_
BufferState
s <- forall e. Buffer e -> BufferState
IO.bufState forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
refBuf
Int -> BufferState -> IO (Buffer Word8)
IO.newByteBuffer Int
minFree BufferState
s forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
refBuf
| forall e. Buffer e -> Int
freeSpace Buffer e
buf forall a. Ord a => a -> a -> Bool
< Int
minFree = Handle__ -> IO ()
flushWriteBuffer Handle__
h_
| Bool
otherwise =
forall (m :: * -> *) a. Monad m => a -> m a
return ()
fillBuffer :: Buffer Word8 -> IO (IO a)
fillBuffer Buffer Word8
buf
| forall e. Buffer e -> Int
freeSpace Buffer Word8
buf forall a. Ord a => a -> a -> Bool
< Int
minFree =
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
[ String
"Data.ByteString.Builder.Internal.hPut: internal error."
, String
" Not enough space after flush."
, String
" required: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
minFree
, String
" free: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall e. Buffer e -> Int
freeSpace Buffer Word8
buf)
]
| Bool
otherwise = do
let !br :: BufferRange
br = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange forall {b}. Ptr b
op (Ptr Word8
pBuf forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
IO.bufSize Buffer Word8
buf)
IO a
res <- forall a b.
BuildStep a
-> (Ptr Word8 -> a -> IO b)
-> (Ptr Word8 -> Int -> BuildStep a -> IO b)
-> (Ptr Word8 -> ByteString -> BuildStep a -> IO b)
-> BufferRange
-> IO b
fillWithBuildStep BuildStep a
step forall {a} {a}. Ptr a -> a -> IO (IO a)
doneH forall {a}. Ptr a -> Int -> BuildStep a -> IO (IO a)
fullH forall {a}. Ptr a -> ByteString -> BuildStep a -> IO (IO a)
insertChunkH BufferRange
br
forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
fpBuf
forall (m :: * -> *) a. Monad m => a -> m a
return IO a
res
where
fpBuf :: ForeignPtr Word8
fpBuf = forall e. Buffer e -> RawBuffer e
IO.bufRaw Buffer Word8
buf
pBuf :: Ptr Word8
pBuf = forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fpBuf
op :: Ptr b
op = Ptr Word8
pBuf forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
IO.bufR Buffer Word8
buf
{-# INLINE updateBufR #-}
updateBufR :: Ptr a -> IO ()
updateBufR Ptr a
op' = do
let !off' :: Int
off' = Ptr a
op' forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
pBuf
!buf' :: Buffer Word8
buf' = Buffer Word8
buf {bufR :: Int
IO.bufR = Int
off'}
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
refBuf Buffer Word8
buf'
doneH :: Ptr a -> a -> IO (IO a)
doneH Ptr a
op' a
x = do
forall {a}. Ptr a -> IO ()
updateBufR Ptr a
op'
case Handle__ -> BufferMode
haBufferMode Handle__
h_ of
BlockBuffering Maybe Int
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return a
x
BufferMode
_line_or_no_buffering -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
h forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return a
x
fullH :: Ptr a -> Int -> BuildStep a -> IO (IO a)
fullH Ptr a
op' Int
minSize BuildStep a
nextStep = do
forall {a}. Ptr a -> IO ()
updateBufR Ptr a
op'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Int -> BuildStep a -> IO a
fillHandle Int
minSize BuildStep a
nextStep
insertChunkH :: Ptr a -> ByteString -> BuildStep a -> IO (IO a)
insertChunkH Ptr a
op' ByteString
bs BuildStep a
nextStep = do
forall {a}. Ptr a -> IO ()
updateBufR Ptr a
op'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ do
Handle -> ByteString -> IO ()
S.hPut Handle
h ByteString
bs
Int -> BuildStep a -> IO a
fillHandle Int
1 BuildStep a
nextStep
{-# NOINLINE putToLazyByteString #-}
putToLazyByteString
:: Put a
-> (a, L.ByteString)
putToLazyByteString :: forall a. Put a -> (a, ByteString)
putToLazyByteString = forall a b.
AllocationStrategy
-> (a -> (b, ByteString)) -> Put a -> (b, ByteString)
putToLazyByteStringWith
(Int -> Int -> AllocationStrategy
safeStrategy Int
L.smallChunkSize Int
L.defaultChunkSize) (, ByteString
L.Empty)
{-# INLINE putToLazyByteStringWith #-}
putToLazyByteStringWith
:: AllocationStrategy
-> (a -> (b, L.ByteString))
-> Put a
-> (b, L.ByteString)
putToLazyByteStringWith :: forall a b.
AllocationStrategy
-> (a -> (b, ByteString)) -> Put a -> (b, ByteString)
putToLazyByteStringWith AllocationStrategy
strategy a -> (b, ByteString)
k Put a
p =
forall a b.
AllocationStrategy
-> (a -> (b, ByteString)) -> ChunkIOStream a -> (b, ByteString)
ciosToLazyByteString AllocationStrategy
strategy a -> (b, ByteString)
k forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
forall a. AllocationStrategy -> BuildStep a -> IO (ChunkIOStream a)
buildStepToCIOS AllocationStrategy
strategy (forall a. Put a -> BuildStep a
runPut Put a
p)
{-# INLINE ensureFree #-}
ensureFree :: Int -> Builder
ensureFree :: Int -> Builder
ensureFree Int
minFree =
(forall r. BuildStep r -> BuildStep r) -> Builder
builder forall r. BuildStep r -> BuildStep r
step
where
step :: BuildStep a -> BuildStep a
step BuildStep a
k br :: BufferRange
br@(BufferRange Ptr Word8
op Ptr Word8
ope)
| Ptr Word8
ope forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
op forall a. Ord a => a -> a -> Bool
< Int
minFree = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
minFree Ptr Word8
op BuildStep a
k
| Bool
otherwise = BuildStep a
k BufferRange
br
wrappedBytesCopyStep :: BufferRange
-> BuildStep a -> BuildStep a
wrappedBytesCopyStep :: forall a. BufferRange -> BuildStep a -> BuildStep a
wrappedBytesCopyStep (BufferRange Ptr Word8
ip0 Ptr Word8
ipe) BuildStep a
k =
Ptr Word8 -> BuildStep a
go Ptr Word8
ip0
where
go :: Ptr Word8 -> BuildStep a
go !Ptr Word8
ip (BufferRange Ptr Word8
op Ptr Word8
ope)
| Int
inpRemaining forall a. Ord a => a -> a -> Bool
<= Int
outRemaining = do
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr Word8
op Ptr Word8
ip Int
inpRemaining
let !br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange (Ptr Word8
op forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
inpRemaining) Ptr Word8
ope
BuildStep a
k BufferRange
br'
| Bool
otherwise = do
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr Word8
op Ptr Word8
ip Int
outRemaining
let !ip' :: Ptr b
ip' = Ptr Word8
ip forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
outRemaining
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
1 Ptr Word8
ope (Ptr Word8 -> BuildStep a
go forall {b}. Ptr b
ip')
where
outRemaining :: Int
outRemaining = Ptr Word8
ope forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
op
inpRemaining :: Int
inpRemaining = Ptr Word8
ipe forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
ip
{-# INLINE byteStringThreshold #-}
byteStringThreshold :: Int -> S.ByteString -> Builder
byteStringThreshold :: Int -> ByteString -> Builder
byteStringThreshold Int
maxCopySize =
\ByteString
bs -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall a b. (a -> b) -> a -> b
$ forall {a}. ByteString -> BuildStep a -> BuildStep a
step ByteString
bs
where
step :: ByteString -> BuildStep a -> BuildStep a
step bs :: ByteString
bs@(S.BS ForeignPtr Word8
_ Int
len) !BuildStep a
k br :: BufferRange
br@(BufferRange !Ptr Word8
op Ptr Word8
_)
| Int
len forall a. Ord a => a -> a -> Bool
<= Int
maxCopySize = forall {a}. ByteString -> BuildStep a -> BuildStep a
byteStringCopyStep ByteString
bs BuildStep a
k BufferRange
br
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr Word8 -> ByteString -> BuildStep a -> BuildSignal a
insertChunk Ptr Word8
op ByteString
bs BuildStep a
k
{-# INLINE byteStringCopy #-}
byteStringCopy :: S.ByteString -> Builder
byteStringCopy :: ByteString -> Builder
byteStringCopy = \ByteString
bs -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall a b. (a -> b) -> a -> b
$ forall {a}. ByteString -> BuildStep a -> BuildStep a
byteStringCopyStep ByteString
bs
{-# INLINE byteStringCopyStep #-}
byteStringCopyStep :: S.ByteString -> BuildStep a -> BuildStep a
byteStringCopyStep :: forall {a}. ByteString -> BuildStep a -> BuildStep a
byteStringCopyStep (S.BS ForeignPtr Word8
ifp Int
isize) !BuildStep a
k0 br0 :: BufferRange
br0@(BufferRange Ptr Word8
op Ptr Word8
ope)
| forall {b}. Ptr b
op' forall a. Ord a => a -> a -> Bool
<= Ptr Word8
ope = do forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr Word8
op Ptr Word8
ip Int
isize
forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
ifp
BuildStep a
k0 (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange forall {b}. Ptr b
op' Ptr Word8
ope)
| Bool
otherwise = forall a. BufferRange -> BuildStep a -> BuildStep a
wrappedBytesCopyStep (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
ip forall {b}. Ptr b
ipe) BuildStep a
k BufferRange
br0
where
op' :: Ptr b
op' = Ptr Word8
op forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
isize
ip :: Ptr Word8
ip = forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
ifp
ipe :: Ptr b
ipe = Ptr Word8
ip forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
isize
k :: BuildStep a
k BufferRange
br = do forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
ifp
BuildStep a
k0 BufferRange
br
{-# INLINE byteStringInsert #-}
byteStringInsert :: S.ByteString -> Builder
byteStringInsert :: ByteString -> Builder
byteStringInsert =
\ByteString
bs -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall a b. (a -> b) -> a -> b
$ \BuildStep r
k (BufferRange Ptr Word8
op Ptr Word8
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ptr Word8 -> ByteString -> BuildStep a -> BuildSignal a
insertChunk Ptr Word8
op ByteString
bs BuildStep r
k
{-# INLINE shortByteString #-}
shortByteString :: Sh.ShortByteString -> Builder
shortByteString :: ShortByteString -> Builder
shortByteString = \ShortByteString
sbs -> (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall a b. (a -> b) -> a -> b
$ forall a. ShortByteString -> BuildStep a -> BuildStep a
shortByteStringCopyStep ShortByteString
sbs
{-# INLINE shortByteStringCopyStep #-}
shortByteStringCopyStep :: Sh.ShortByteString
-> BuildStep a -> BuildStep a
shortByteStringCopyStep :: forall a. ShortByteString -> BuildStep a -> BuildStep a
shortByteStringCopyStep !ShortByteString
sbs BuildStep a
k =
Int -> Int -> BuildStep a
go Int
0 (ShortByteString -> Int
Sh.length ShortByteString
sbs)
where
go :: Int -> Int -> BuildStep a
go !Int
ip !Int
ipe (BufferRange Ptr Word8
op Ptr Word8
ope)
| Int
inpRemaining forall a. Ord a => a -> a -> Bool
<= Int
outRemaining = do
forall a. ShortByteString -> Int -> Ptr a -> Int -> IO ()
Sh.copyToPtr ShortByteString
sbs Int
ip Ptr Word8
op Int
inpRemaining
let !br' :: BufferRange
br' = Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange (Ptr Word8
op forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
inpRemaining) Ptr Word8
ope
BuildStep a
k BufferRange
br'
| Bool
otherwise = do
forall a. ShortByteString -> Int -> Ptr a -> Int -> IO ()
Sh.copyToPtr ShortByteString
sbs Int
ip Ptr Word8
op Int
outRemaining
let !ip' :: Int
ip' = Int
ip forall a. Num a => a -> a -> a
+ Int
outRemaining
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> Ptr Word8 -> BuildStep a -> BuildSignal a
bufferFull Int
1 Ptr Word8
ope (Int -> Int -> BuildStep a
go Int
ip' Int
ipe)
where
outRemaining :: Int
outRemaining = Ptr Word8
ope forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
op
inpRemaining :: Int
inpRemaining = Int
ipe forall a. Num a => a -> a -> a
- Int
ip
{-# INLINE lazyByteStringThreshold #-}
lazyByteStringThreshold :: Int -> L.ByteString -> Builder
lazyByteStringThreshold :: Int -> ByteString -> Builder
lazyByteStringThreshold Int
maxCopySize =
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
L.foldrChunks (\ByteString
bs Builder
b -> Int -> ByteString -> Builder
byteStringThreshold Int
maxCopySize ByteString
bs forall a. Monoid a => a -> a -> a
`mappend` Builder
b) forall a. Monoid a => a
mempty
{-# INLINE lazyByteStringCopy #-}
lazyByteStringCopy :: L.ByteString -> Builder
lazyByteStringCopy :: ByteString -> Builder
lazyByteStringCopy =
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
L.foldrChunks (\ByteString
bs Builder
b -> ByteString -> Builder
byteStringCopy ByteString
bs forall a. Monoid a => a -> a -> a
`mappend` Builder
b) forall a. Monoid a => a
mempty
{-# INLINE lazyByteStringInsert #-}
lazyByteStringInsert :: L.ByteString -> Builder
lazyByteStringInsert :: ByteString -> Builder
lazyByteStringInsert =
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
L.foldrChunks (\ByteString
bs Builder
b -> ByteString -> Builder
byteStringInsert ByteString
bs forall a. Monoid a => a -> a -> a
`mappend` Builder
b) forall a. Monoid a => a
mempty
{-# INLINE byteString #-}
byteString :: S.ByteString -> Builder
byteString :: ByteString -> Builder
byteString = Int -> ByteString -> Builder
byteStringThreshold Int
maximalCopySize
{-# INLINE lazyByteString #-}
lazyByteString :: L.ByteString -> Builder
lazyByteString :: ByteString -> Builder
lazyByteString = Int -> ByteString -> Builder
lazyByteStringThreshold Int
maximalCopySize
maximalCopySize :: Int
maximalCopySize :: Int
maximalCopySize = Int
2 forall a. Num a => a -> a -> a
* Int
L.smallChunkSize
data AllocationStrategy = AllocationStrategy
(Maybe (Buffer, Int) -> IO Buffer)
{-# UNPACK #-} !Int
(Int -> Int -> Bool)
{-# INLINE customStrategy #-}
customStrategy
:: (Maybe (Buffer, Int) -> IO Buffer)
-> Int
-> (Int -> Int -> Bool)
-> AllocationStrategy
customStrategy :: (Maybe (Buffer, Int) -> IO Buffer)
-> Int -> (Int -> Int -> Bool) -> AllocationStrategy
customStrategy = (Maybe (Buffer, Int) -> IO Buffer)
-> Int -> (Int -> Int -> Bool) -> AllocationStrategy
AllocationStrategy
{-# INLINE sanitize #-}
sanitize :: Int -> Int
sanitize :: Int -> Int
sanitize = forall a. Ord a => a -> a -> a
max (forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Int))
{-# INLINE untrimmedStrategy #-}
untrimmedStrategy :: Int
-> Int
-> AllocationStrategy
untrimmedStrategy :: Int -> Int -> AllocationStrategy
untrimmedStrategy Int
firstSize Int
bufSize =
(Maybe (Buffer, Int) -> IO Buffer)
-> Int -> (Int -> Int -> Bool) -> AllocationStrategy
AllocationStrategy forall {a}. Maybe (a, Int) -> IO Buffer
nextBuffer (Int -> Int
sanitize Int
bufSize) (\Int
_ Int
_ -> Bool
False)
where
{-# INLINE nextBuffer #-}
nextBuffer :: Maybe (a, Int) -> IO Buffer
nextBuffer Maybe (a, Int)
Nothing = Int -> IO Buffer
newBuffer forall a b. (a -> b) -> a -> b
$ Int -> Int
sanitize Int
firstSize
nextBuffer (Just (a
_, Int
minSize)) = Int -> IO Buffer
newBuffer Int
minSize
{-# INLINE safeStrategy #-}
safeStrategy :: Int
-> Int
-> AllocationStrategy
safeStrategy :: Int -> Int -> AllocationStrategy
safeStrategy Int
firstSize Int
bufSize =
(Maybe (Buffer, Int) -> IO Buffer)
-> Int -> (Int -> Int -> Bool) -> AllocationStrategy
AllocationStrategy forall {a}. Maybe (a, Int) -> IO Buffer
nextBuffer (Int -> Int
sanitize Int
bufSize) forall {a}. (Ord a, Num a) => a -> a -> Bool
trim
where
trim :: a -> a -> Bool
trim a
used a
size = a
2 forall a. Num a => a -> a -> a
* a
used forall a. Ord a => a -> a -> Bool
< a
size
{-# INLINE nextBuffer #-}
nextBuffer :: Maybe (a, Int) -> IO Buffer
nextBuffer Maybe (a, Int)
Nothing = Int -> IO Buffer
newBuffer forall a b. (a -> b) -> a -> b
$ Int -> Int
sanitize Int
firstSize
nextBuffer (Just (a
_, Int
minSize)) = Int -> IO Buffer
newBuffer Int
minSize
{-# INLINE toLazyByteStringWith #-}
toLazyByteStringWith
:: AllocationStrategy
-> L.ByteString
-> Builder
-> L.ByteString
toLazyByteStringWith :: AllocationStrategy -> ByteString -> Builder -> ByteString
toLazyByteStringWith AllocationStrategy
strategy ByteString
k Builder
b =
AllocationStrategy -> ByteString -> ChunkIOStream () -> ByteString
ciosUnitToLazyByteString AllocationStrategy
strategy ByteString
k forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeDupablePerformIO forall a b. (a -> b) -> a -> b
$
forall a. AllocationStrategy -> BuildStep a -> IO (ChunkIOStream a)
buildStepToCIOS AllocationStrategy
strategy (Builder -> BuildStep ()
runBuilder Builder
b)
{-# INLINE buildStepToCIOS #-}
buildStepToCIOS
:: forall a.
AllocationStrategy
-> BuildStep a
-> IO (ChunkIOStream a)
buildStepToCIOS :: forall a. AllocationStrategy -> BuildStep a -> IO (ChunkIOStream a)
buildStepToCIOS (AllocationStrategy Maybe (Buffer, Int) -> IO Buffer
nextBuffer Int
bufSize Int -> Int -> Bool
trim) =
\BuildStep a
step -> Maybe (Buffer, Int) -> IO Buffer
nextBuffer forall a. Maybe a
Nothing forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill BuildStep a
step
where
fill :: BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill :: BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill !BuildStep a
step buf :: Buffer
buf@(Buffer ForeignPtr Word8
fpbuf br :: BufferRange
br@(BufferRange Ptr Word8
_ Ptr Word8
pe)) = do
ChunkIOStream a
res <- forall a b.
BuildStep a
-> (Ptr Word8 -> a -> IO b)
-> (Ptr Word8 -> Int -> BuildStep a -> IO b)
-> (Ptr Word8 -> ByteString -> BuildStep a -> IO b)
-> BufferRange
-> IO b
fillWithBuildStep BuildStep a
step Ptr Word8 -> a -> IO (ChunkIOStream a)
doneH Ptr Word8 -> Int -> BuildStep a -> IO (ChunkIOStream a)
fullH Ptr Word8 -> ByteString -> BuildStep a -> IO (ChunkIOStream a)
insertChunkH BufferRange
br
forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
fpbuf
forall (m :: * -> *) a. Monad m => a -> m a
return ChunkIOStream a
res
where
pbuf :: Ptr Word8
pbuf :: Ptr Word8
pbuf = forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fpbuf
doneH :: Ptr Word8 -> a -> IO (ChunkIOStream a)
doneH :: Ptr Word8 -> a -> IO (ChunkIOStream a)
doneH Ptr Word8
op' a
x = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
forall a. Buffer -> a -> ChunkIOStream a
Finished (ForeignPtr Word8 -> BufferRange -> Buffer
Buffer ForeignPtr Word8
fpbuf (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
op' Ptr Word8
pe)) a
x
fullH :: Ptr Word8 -> Int -> BuildStep a -> IO (ChunkIOStream a)
fullH :: Ptr Word8 -> Int -> BuildStep a -> IO (ChunkIOStream a)
fullH Ptr Word8
op' Int
minSize BuildStep a
nextStep =
Ptr Word8 -> (Bool -> IO (ChunkIOStream a)) -> IO (ChunkIOStream a)
wrapChunk Ptr Word8
op' forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$
Maybe (Buffer, Int) -> IO Buffer
nextBuffer (forall a. a -> Maybe a
Just (Buffer
buf, forall a. Ord a => a -> a -> a
max Int
minSize Int
bufSize)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill BuildStep a
nextStep
insertChunkH :: Ptr Word8 -> S.ByteString -> BuildStep a -> IO (ChunkIOStream a)
insertChunkH :: Ptr Word8 -> ByteString -> BuildStep a -> IO (ChunkIOStream a)
insertChunkH Ptr Word8
op' ByteString
bs BuildStep a
nextStep =
Ptr Word8 -> (Bool -> IO (ChunkIOStream a)) -> IO (ChunkIOStream a)
wrapChunk Ptr Word8
op' forall a b. (a -> b) -> a -> b
$ \Bool
isEmpty -> forall a.
ByteString -> IO (ChunkIOStream a) -> IO (ChunkIOStream a)
yield1 ByteString
bs forall a b. (a -> b) -> a -> b
$
if Bool
isEmpty
then BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill BuildStep a
nextStep (ForeignPtr Word8 -> BufferRange -> Buffer
Buffer ForeignPtr Word8
fpbuf (Ptr Word8 -> Ptr Word8 -> BufferRange
BufferRange Ptr Word8
pbuf Ptr Word8
pe))
else do Buffer
buf' <- Maybe (Buffer, Int) -> IO Buffer
nextBuffer (forall a. a -> Maybe a
Just (Buffer
buf, Int
bufSize))
BuildStep a -> Buffer -> IO (ChunkIOStream a)
fill BuildStep a
nextStep Buffer
buf'
{-# INLINE wrapChunk #-}
wrapChunk :: Ptr Word8 -> (Bool -> IO (ChunkIOStream a)) -> IO (ChunkIOStream a)
wrapChunk :: Ptr Word8 -> (Bool -> IO (ChunkIOStream a)) -> IO (ChunkIOStream a)
wrapChunk !Ptr Word8
op' Bool -> IO (ChunkIOStream a)
mkCIOS
| Int
chunkSize forall a. Eq a => a -> a -> Bool
== Int
0 = Bool -> IO (ChunkIOStream a)
mkCIOS Bool
True
| Int -> Int -> Bool
trim Int
chunkSize Int
size = do
ByteString
bs <- Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteString
S.createFp Int
chunkSize forall a b. (a -> b) -> a -> b
$ \ForeignPtr Word8
fpbuf' ->
ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()
S.memcpyFp ForeignPtr Word8
fpbuf' ForeignPtr Word8
fpbuf Int
chunkSize
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. ByteString -> IO (ChunkIOStream a) -> ChunkIOStream a
Yield1 ByteString
bs (Bool -> IO (ChunkIOStream a)
mkCIOS Bool
True)
| Bool
otherwise =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. ByteString -> IO (ChunkIOStream a) -> ChunkIOStream a
Yield1 (ForeignPtr Word8 -> Int -> ByteString
S.BS ForeignPtr Word8
fpbuf Int
chunkSize) (Bool -> IO (ChunkIOStream a)
mkCIOS Bool
False)
where
chunkSize :: Int
chunkSize = Ptr Word8
op' forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
pbuf
size :: Int
size = Ptr Word8
pe forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
pbuf