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