on 6006084 help sign in
52 lines 1.5 kB history raw
1{-# LANGUAGE UnboxedTuples #-}
3-- |
4-- Module : Streamly.Internal.System.IO
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
12module Streamly.Internal.System.IO
13 ( defaultChunkSize
14 , mkChunkSize
15 , mkChunkSizeKB
16 , unsafeInlineIO
17 )
19where
21-------------------------------------------------------------------------------
22-- Imports
23-------------------------------------------------------------------------------
25import Foreign.Storable (Storable(sizeOf))
26import GHC.Base (realWorld#)
27import GHC.IO (IO(IO))
29-------------------------------------------------------------------------------
30-- API
31-------------------------------------------------------------------------------
33{-# INLINE unsafeInlineIO #-}
34unsafeInlineIO :: IO a -> a
35unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
37-- | GHC memory management allocation header overhead
38allocOverhead :: Int
39allocOverhead = 2 * sizeOf (undefined :: Int)
41mkChunkSize :: Int -> Int
42mkChunkSize n = let size = n - allocOverhead in max size 0
44mkChunkSizeKB :: Int -> Int
45mkChunkSizeKB n = mkChunkSize (n * k)
46 where k = 1024
48-- | Default maximum buffer size in bytes, for reading from and writing to IO
49-- devices, the value is 32KB minus GHC allocation overhead, which is a few
50-- bytes, so that the actual allocation is 32KB.
51defaultChunkSize :: Int
52defaultChunkSize = mkChunkSizeKB 32