on edbb7b8 help sign in
83 lines 2.2 kB history raw
1#include "MachDeps.h"
3-- |
4-- Module : Streamly.Internal.FileSystem.IOVec
5-- Copyright : (c) 2019 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12-- Low level IO routines interfacing the operating system.
13--
15module Streamly.Internal.FileSystem.IOVec
16 ( IOVec(..)
17 , c_writev
18 , c_safe_writev
19 )
20where
23import Data.Word (Word8)
24#if (WORD_SIZE_IN_BITS == 32)
25import Data.Word (Word32)
26#else
27import Data.Word (Word64)
28#endif
29import Foreign.C.Types (CInt(..))
30import Foreign.Ptr (Ptr)
31import System.Posix.Types (CSsize(..))
32#if !defined(mingw32_HOST_OS)
33import Foreign.Storable (Storable(..))
34#endif
36-------------------------------------------------------------------------------
37-- IOVec
38-------------------------------------------------------------------------------
40data IOVec = IOVec
41 { iovBase :: {-# UNPACK #-} !(Ptr Word8)
42#if (WORD_SIZE_IN_BITS == 32)
43 , iovLen :: {-# UNPACK #-} !Word32
44#else
45 , iovLen :: {-# UNPACK #-} !Word64
46#endif
47 } deriving (Eq, Show)
49#if !defined(mingw32_HOST_OS)
51#include <sys/uio.h>
53instance Storable IOVec where
54 sizeOf _ = #{size struct iovec}
55 alignment _ = #{alignment struct iovec}
56 peek ptr = do
57 base <- #{peek struct iovec, iov_base} ptr
58 len :: #{type size_t} <- #{peek struct iovec, iov_len} ptr
59 return $ IOVec base len
60 poke ptr vec = do
61 let base = iovBase vec
62 len :: #{type size_t} = iovLen vec
63 #{poke struct iovec, iov_base} ptr base
64 #{poke struct iovec, iov_len} ptr len
65#endif
67-- capi calling convention does not work without -fobject-code option with GHCi
68-- so using this in DEVBUILD only for now.
69--
70#if !defined(mingw32_HOST_OS) && defined DEVBUILD
71foreign import capi unsafe "sys/uio.h writev"
72 c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
74foreign import capi safe "sys/uio.h writev"
75 c_safe_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
77#else
78c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
79c_writev = error "writev not implemented"
81c_safe_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
82c_safe_writev = error "writev not implemented"
83#endif