@@ -1,23 +1,22 @@
11module Network.Wai.Handler.Warp.Buffer where
22
33import Control.Monad (when)
4−import qualified Data.Streaming.ByteString.Builder.Buffer as B (Buffer (..))
5−import Data.Word (Word8)
6−import Data.IORef (IORef, newIORef, readIORef, writeIORef)
74import Data.ByteString (ByteString, empty, length)
85import Data.ByteString.Internal (ByteString(PS), mallocByteString)
96import Data.ByteString.Unsafe (unsafeTake, unsafeDrop)
7+import Data.IORef (newIORef, readIORef, writeIORef)
8+import qualified Data.Streaming.ByteString.Builder.Buffer as B (Buffer (..))
109import Foreign.ForeignPtr (newForeignPtr, newForeignPtr_, withForeignPtr)
1110import Foreign.Marshal.Alloc (mallocBytes, free, finalizerFree)
12−import Foreign.Ptr (Ptr, castPtr, plusPtr)
11+import Foreign.Ptr (castPtr, plusPtr)
12+import Network.Wai.Handler.Warp.Types
1313
1414largeBufferSize :: Int
1515largeBufferSize = 16384
16+
1617minBufferSize :: Int
1718minBufferSize = 2048
1819
19−type BufferPool = IORef ByteString
20−
2120newBufferPool :: IO BufferPool
2221newBufferPool = newIORef Data.ByteString.empty
2322
@@ -48,11 +47,11 @@ putBuffer :: BufferPool -> ByteString -> IO ()
4847putBuffer pool buffer = when (usefulBuffer buffer) $ writeIORef pool buffer
4948{-# INLINE putBuffer #-}
5049
51−withForeignBuffer :: ByteString -> ((Ptr Word8, Int) -> IO Int) -> IO Int
50+withForeignBuffer :: ByteString -> ((Buffer, BufSize) -> IO Int) -> IO Int
5251withForeignBuffer (PS ps s l) f = withForeignPtr ps $ \p -> f (castPtr p `plusPtr` s, l)
5352{-# INLINE withForeignBuffer #-}
5453
55−withBufferPool :: BufferPool -> ((Ptr Word8, Int) -> IO Int) -> IO ByteString
54+withBufferPool :: BufferPool -> ((Buffer, BufSize) -> IO Int) -> IO ByteString
5655withBufferPool pool f = do
5756 buffer <- getBuffer pool
5857 consumed <- withForeignBuffer buffer f
@@ -60,9 +59,6 @@ withBufferPool pool f = do
6059 return $! unsafeTake consumed buffer
6160{-# INLINE withBufferPool #-}
6261
63−type Buffer = Ptr Word8
64−type BufSize = Int
65−
6662-- 2^14 = 1024 * 16
6763-- The maximum size of TLS record
6864-- The maximum size of HTTP/2 frame payload (excluding frame header)