on d7d42be help sign in
80 lines 2.2 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, Word64)
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 , iovLen :: {-# UNPACK #-} !Word64
40 } deriving (Eq, Show)
42#if !defined(mingw32_HOST_OS)
44#include <sys/uio.h>
46#if __GLASGOW_HASKELL__ < 800
47#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
48#endif
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