on 63c646a help sign in
89 lines 2.4 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE CApiFFI #-}
4{-# LANGUAGE ScopedTypeVariables #-}
6-- |
7-- Module : Streamly.FileSystem.IOVec
8-- Copyright : (c) 2019 Composewell Technologies
9--
10-- License : BSD3
11-- Maintainer : streamly@composewell.com
12-- Stability : experimental
13-- Portability : GHC
14--
15-- Low level IO routines interfacing the operating system.
16--
18module Streamly.FileSystem.IOVec
19 ( IOVec(..)
20 , c_writev
21 , c_safe_writev
22 )
23where
25import Data.Word (Word8)
26#if defined(i386_HOST_ARCH)
27import Data.Word (Word32)
28#else
29import Data.Word (Word64)
30#endif
31import Foreign.C.Types (CInt(..))
32import Foreign.Ptr (Ptr)
33import System.Posix.Types (CSsize(..))
34#if !defined(mingw32_HOST_OS)
35import Foreign.Storable (Storable(..))
36#endif
38-------------------------------------------------------------------------------
39-- IOVec
40-------------------------------------------------------------------------------
42data IOVec = IOVec
43 { iovBase :: {-# UNPACK #-} !(Ptr Word8)
44#if defined(i386_HOST_ARCH)
45 , iovLen :: {-# UNPACK #-} !Word32
46#else
47 , iovLen :: {-# UNPACK #-} !Word64
48#endif
49 } deriving (Eq, Show)
51#if !defined(mingw32_HOST_OS)
53#include <sys/uio.h>
55#if __GLASGOW_HASKELL__ < 800
56#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
57#endif
59instance Storable IOVec where
60 sizeOf _ = #{size struct iovec}
61 alignment _ = #{alignment struct iovec}
62 peek ptr = do
63 base <- #{peek struct iovec, iov_base} ptr
64 len :: #{type size_t} <- #{peek struct iovec, iov_len} ptr
65 return $ IOVec base len
66 poke ptr vec = do
67 let base = iovBase vec
68 len :: #{type size_t} = iovLen vec
69 #{poke struct iovec, iov_base} ptr base
70 #{poke struct iovec, iov_len} ptr len
71#endif
73-- capi calling convention does not work without -fobject-code option with GHCi
74-- so using this in DEVBUILD only for now.
75--
76#if !defined(mingw32_HOST_OS) && defined DEVBUILD
77foreign import capi unsafe "sys/uio.h writev"
78 c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
80foreign import capi safe "sys/uio.h writev"
81 c_safe_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
83#else
84c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
85c_writev = error "writev not implemented"
87c_safe_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
88c_safe_writev = error "writev not implemented"
89#endif