{-# LINE 1 "Z/IO/UV/FFI.hsc" #-}
{-|
Module      : Z.IO.UV
Description : libuv operations
Copyright   : (c) Winterland, 2017-2018
License     : BSD
Maintainer  : drkoster@qq.com
Stability   : experimental
Portability : non-portable

INTERNAL MODULE, provides all libuv side operations.

-}

module Z.IO.UV.FFI where

import           Data.Bits
import           Data.Int
import           Data.Word
import           Foreign.C.String
import           Foreign.C.Types
import           Foreign.Ptr
import           Foreign.Storable
import           Z.Data.Array.Unaligned
import qualified Z.Data.Array  as A
import qualified Z.Data.Text   as T
import           Z.Data.Text.ShowT   (ShowT(..))
import           Z.Data.JSON         (EncodeJSON, ToValue, FromValue)
import           Z.Data.CBytes
import           Z.Foreign
import           Z.IO.Exception (throwUVIfMinus_)
import           Z.IO.Network.SocketAddr    (SocketAddr)
import           System.Posix.Types (CSsize (..))
import           GHC.Generics



{-# LINE 37 "Z/IO/UV/FFI.hsc" #-}


{-# LINE 39 "Z/IO/UV/FFI.hsc" #-}

--------------------------------------------------------------------------------
-- libuv version
foreign import ccall unsafe uv_version :: IO CUInt
foreign import ccall unsafe uv_version_string :: IO CString

--------------------------------------------------------------------------------
-- Type alias
type UVSlot = Int
-- | UVSlotUnsafe wrap a slot which may not have a 'MVar' in blocking table, 
--   i.e. the blocking table need to be resized.
newtype UVSlotUnsafe = UVSlotUnsafe { unsafeGetSlot :: UVSlot }
type UVFD = Int32

--------------------------------------------------------------------------------
-- CONSTANT

pattern SO_REUSEPORT_LOAD_BALANCE :: Int
pattern SO_REUSEPORT_LOAD_BALANCE = 1
{-# LINE 58 "Z/IO/UV/FFI.hsc" #-}
pattern INIT_LOOP_SIZE :: Int
pattern INIT_LOOP_SIZE = 128
{-# LINE 60 "Z/IO/UV/FFI.hsc" #-}

--------------------------------------------------------------------------------
-- loop
data UVLoop
data UVLoopData

peekUVEventQueue :: Ptr UVLoopData -> IO (Int, Ptr Int)
peekUVEventQueue p = (,)
    <$> ((\hsc_ptr -> peekByteOff hsc_ptr 0) p)
{-# LINE 69 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 8) p)
{-# LINE 70 "Z/IO/UV/FFI.hsc" #-}

clearUVEventCounter :: Ptr UVLoopData -> IO ()
clearUVEventCounter p = do
    (\hsc_ptr -> pokeByteOff hsc_ptr 0) p $ (0 :: Int)
{-# LINE 74 "Z/IO/UV/FFI.hsc" #-}

peekUVBufferTable :: Ptr UVLoopData -> IO (Ptr (Ptr Word8), Ptr CSsize)
peekUVBufferTable p = (,)
    <$> ((\hsc_ptr -> peekByteOff hsc_ptr 16) p)
{-# LINE 78 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 24) p)
{-# LINE 79 "Z/IO/UV/FFI.hsc" #-}

newtype UVRunMode = UVRunMode CInt deriving (Show, Eq, Ord, Generic)
                                   deriving anyclass ShowT

pattern UV_RUN_DEFAULT :: UVRunMode
pattern UV_RUN_DEFAULT = UVRunMode 0
{-# LINE 85 "Z/IO/UV/FFI.hsc" #-}
pattern UV_RUN_ONCE :: UVRunMode
pattern UV_RUN_ONCE    = UVRunMode 1
{-# LINE 87 "Z/IO/UV/FFI.hsc" #-}
pattern UV_RUN_NOWAIT :: UVRunMode
pattern UV_RUN_NOWAIT  = UVRunMode 2
{-# LINE 89 "Z/IO/UV/FFI.hsc" #-}

-- | Peek loop data pointer from uv loop  pointer.
peekUVLoopData :: Ptr UVLoop -> IO (Ptr UVLoopData)
peekUVLoopData p = (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 93 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_loop_init      :: Int -> IO (Ptr UVLoop)
foreign import ccall unsafe hs_uv_loop_close     :: Ptr UVLoop -> IO ()

-- | uv_run with usafe FFI.
foreign import ccall unsafe "hs_uv_run" uv_run    :: Ptr UVLoop -> UVRunMode -> IO CInt

-- | uv_run with safe FFI.
foreign import ccall safe "hs_uv_run" uv_run_safe :: Ptr UVLoop -> UVRunMode -> IO CInt

foreign import ccall unsafe uv_loop_alive :: Ptr UVLoop -> IO CInt

--------------------------------------------------------------------------------
-- thread safe wake up

foreign import ccall unsafe hs_uv_wake_up_timer :: Ptr UVLoopData -> IO CInt
foreign import ccall unsafe hs_uv_wake_up_async :: Ptr UVLoopData -> IO CInt

--------------------------------------------------------------------------------
-- handle
data UVHandle

peekUVHandleData :: Ptr UVHandle -> IO UVSlotUnsafe
peekUVHandleData p =  UVSlotUnsafe <$> ((\hsc_ptr -> peekByteOff hsc_ptr 0) p :: IO Int)
{-# LINE 117 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_fileno :: Ptr UVHandle -> IO UVFD
foreign import ccall unsafe hs_uv_handle_alloc :: Ptr UVLoop -> IO (Ptr UVHandle)
foreign import ccall unsafe hs_uv_handle_free  :: Ptr UVHandle -> IO ()
foreign import ccall unsafe hs_uv_handle_close :: Ptr UVHandle -> IO ()

--------------------------------------------------------------------------------
-- request

foreign import ccall unsafe hs_uv_cancel :: Ptr UVLoop -> UVSlot -> IO ()

--------------------------------------------------------------------------------
-- stream

foreign import ccall unsafe hs_uv_listen  :: Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe hs_uv_listen_resume :: Ptr UVHandle -> IO ()

foreign import ccall unsafe hs_uv_read_start :: Ptr UVHandle -> IO CInt
foreign import ccall unsafe uv_read_stop :: Ptr UVHandle -> IO CInt
foreign import ccall unsafe hs_uv_write :: Ptr UVHandle -> Ptr Word8 -> Int -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_accept_check_alloc :: Ptr UVHandle -> IO (Ptr UVHandle)
foreign import ccall unsafe hs_uv_accept_check_init :: Ptr UVHandle -> IO CInt
foreign import ccall unsafe hs_uv_accept_check_close :: Ptr UVHandle -> IO ()

--------------------------------------------------------------------------------
-- tcp & pipe
foreign import ccall unsafe hs_uv_tcp_open :: Ptr UVHandle -> UVFD -> IO CInt
foreign import ccall unsafe uv_tcp_init :: Ptr UVLoop -> Ptr UVHandle -> IO CInt
foreign import ccall unsafe uv_tcp_init_ex :: Ptr UVLoop -> Ptr UVHandle -> CUInt -> IO CInt
foreign import ccall unsafe uv_tcp_nodelay :: Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_tcp_keepalive :: Ptr UVHandle -> CInt -> CUInt -> IO CInt
foreign import ccall unsafe uv_tcp_getsockname :: Ptr UVHandle -> MBA# SocketAddr -> MBA# CInt -> IO CInt
foreign import ccall unsafe uv_tcp_getpeername :: Ptr UVHandle -> MBA# SocketAddr -> MBA# CInt -> IO CInt

uV_TCP_IPV6ONLY :: CUInt
uV_TCP_IPV6ONLY = 1
{-# LINE 154 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe uv_tcp_bind :: Ptr UVHandle -> MBA# SocketAddr -> CUInt -> IO CInt
foreign import ccall unsafe hs_uv_tcp_connect :: Ptr UVHandle -> MBA# SocketAddr -> IO UVSlotUnsafe
foreign import ccall unsafe hs_set_socket_reuse :: Ptr UVHandle -> IO CInt

foreign import ccall unsafe hs_uv_pipe_open :: Ptr UVHandle -> UVFD -> IO CInt
foreign import ccall unsafe uv_pipe_init :: Ptr UVLoop -> Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_pipe_bind :: Ptr UVHandle -> BA# Word8 -> IO CInt
foreign import ccall unsafe hs_uv_pipe_connect :: Ptr UVHandle -> BA# Word8 -> IO UVSlotUnsafe

--------------------------------------------------------------------------------
-- udp
foreign import ccall unsafe uv_udp_init :: Ptr UVLoop -> Ptr UVHandle -> IO CInt
foreign import ccall unsafe uv_udp_init_ex :: Ptr UVLoop -> Ptr UVHandle -> CUInt -> IO CInt
foreign import ccall unsafe uv_udp_open :: Ptr UVHandle -> UVFD -> IO CInt
foreign import ccall unsafe uv_udp_bind :: Ptr UVHandle -> MBA# SocketAddr -> UDPFlag -> IO CInt

newtype Membership = Membership CInt deriving (Show, Eq, Ord, Generic)
                                        deriving newtype (EncodeJSON, ToValue, FromValue)
                                        deriving anyclass ShowT

pattern LEAVE_GROUP :: Membership
pattern LEAVE_GROUP = Membership 0
{-# LINE 177 "Z/IO/UV/FFI.hsc" #-}
pattern JOIN_GROUP :: Membership
pattern JOIN_GROUP = Membership 1
{-# LINE 179 "Z/IO/UV/FFI.hsc" #-}

newtype UDPFlag = UDPFlag CInt deriving (Show, Eq, Ord, Generic)
                                deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
                                deriving anyclass ShowT

pattern UDP_DEFAULT        :: UDPFlag
pattern UDP_DEFAULT         = UDPFlag 0
pattern UDP_IPV6ONLY       :: UDPFlag
pattern UDP_IPV6ONLY        = UDPFlag 1
{-# LINE 188 "Z/IO/UV/FFI.hsc" #-}
pattern UDP_REUSEADDR      :: UDPFlag
pattern UDP_REUSEADDR       = UDPFlag 4
{-# LINE 190 "Z/IO/UV/FFI.hsc" #-}

pattern UV_UDP_PARTIAL     :: Int32
pattern UV_UDP_PARTIAL      = 2
{-# LINE 193 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe uv_udp_connect
    :: Ptr UVHandle -> MBA# SocketAddr -> IO CInt
-- | Just pass null pointer as SocketAddr to disconnect
foreign import ccall unsafe "uv_udp_connect" uv_udp_disconnect
    :: Ptr UVHandle -> Ptr SocketAddr -> IO CInt

foreign import ccall unsafe uv_udp_set_membership ::
    Ptr UVHandle -> BA# Word8 -> BA# Word8 -> Membership -> IO CInt
foreign import ccall unsafe uv_udp_set_source_membership ::
    Ptr UVHandle -> BA# Word8 -> BA# Word8 -> BA# Word8 -> Membership -> IO CInt

foreign import ccall unsafe uv_udp_set_multicast_loop :: Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_udp_set_multicast_ttl :: Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_udp_set_multicast_interface :: Ptr UVHandle -> BA# Word8 -> IO CInt
foreign import ccall unsafe uv_udp_set_broadcast :: Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_udp_set_ttl :: Ptr UVHandle -> CInt -> IO CInt

foreign import ccall unsafe hs_uv_udp_recv_start :: Ptr UVHandle -> IO CInt
foreign import ccall unsafe uv_udp_recv_stop :: Ptr UVHandle -> IO CInt

foreign import ccall unsafe hs_uv_udp_check_alloc :: Ptr UVHandle -> IO (Ptr UVHandle)
foreign import ccall unsafe hs_uv_udp_check_init :: Ptr UVHandle -> IO CInt
foreign import ccall unsafe hs_uv_udp_check_close :: Ptr UVHandle -> IO ()

foreign import ccall unsafe hs_uv_udp_send
    :: Ptr UVHandle -> MBA# SocketAddr -> Ptr Word8 -> Int -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_udp_send_connected
    :: Ptr UVHandle -> Ptr Word8 -> Int -> IO UVSlotUnsafe
foreign import ccall unsafe uv_udp_getsockname
    :: Ptr UVHandle -> MBA# SocketAddr -> MBA# CInt -> IO CInt
foreign import ccall unsafe uv_udp_getpeername
    :: Ptr UVHandle -> MBA# SocketAddr -> MBA# CInt -> IO CInt


--------------------------------------------------------------------------------
-- tty

-- | Terminal mode.
--
-- When in 'UV_TTY_MODE_RAW' mode, input is always available character-by-character,
-- not including modifiers. Additionally, all special processing of characters by the terminal is disabled, 
-- including echoing input characters. Note that CTRL+C will no longer cause a SIGINT when in this mode.
newtype TTYMode = TTYMode CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

pattern TTY_MODE_NORMAL :: TTYMode
pattern TTY_MODE_NORMAL = TTYMode 0
{-# LINE 243 "Z/IO/UV/FFI.hsc" #-}
pattern TTY_MODE_RAW :: TTYMode
pattern TTY_MODE_RAW = TTYMode 1
{-# LINE 245 "Z/IO/UV/FFI.hsc" #-}
pattern TTY_MODE_IO :: TTYMode
pattern TTY_MODE_IO = TTYMode 2
{-# LINE 247 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe uv_tty_init :: Ptr UVLoop -> Ptr UVHandle -> CInt -> IO CInt
foreign import ccall unsafe uv_tty_set_mode :: Ptr UVHandle -> TTYMode -> IO CInt
foreign import ccall unsafe uv_tty_get_winsize :: Ptr UVHandle -> MBA# CInt -> MBA# CInt -> IO CInt

--------------------------------------------------------------------------------
-- fs

newtype FileMode = FileMode CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

-- | 00700 user (file owner) has read, write and execute permission
pattern S_IRWXU :: FileMode
pattern S_IRWXU = FileMode 448
{-# LINE 263 "Z/IO/UV/FFI.hsc" #-}

-- | 00400 user has read permission
pattern S_IRUSR :: FileMode
pattern S_IRUSR = FileMode 256
{-# LINE 267 "Z/IO/UV/FFI.hsc" #-}

-- | 00200 user has write permission
pattern S_IWUSR :: FileMode
pattern S_IWUSR = FileMode 128
{-# LINE 271 "Z/IO/UV/FFI.hsc" #-}

-- | 00100 user has execute permission
pattern S_IXUSR :: FileMode
pattern S_IXUSR = FileMode 64
{-# LINE 275 "Z/IO/UV/FFI.hsc" #-}

-- | 00070 group has read, write and execute permission
pattern S_IRWXG :: FileMode
pattern S_IRWXG = FileMode 56
{-# LINE 279 "Z/IO/UV/FFI.hsc" #-}

-- | 00040 group has read permission
pattern S_IRGRP :: FileMode
pattern S_IRGRP = FileMode 32
{-# LINE 283 "Z/IO/UV/FFI.hsc" #-}

-- | 00020 group has write permission
pattern S_IWGRP :: FileMode
pattern S_IWGRP = FileMode 16
{-# LINE 287 "Z/IO/UV/FFI.hsc" #-}

-- | 00010 group has execute permission
pattern S_IXGRP :: FileMode
pattern S_IXGRP = FileMode 8
{-# LINE 291 "Z/IO/UV/FFI.hsc" #-}

-- | 00007 others have read, write and execute permission
pattern S_IRWXO :: FileMode
pattern S_IRWXO = FileMode 7
{-# LINE 295 "Z/IO/UV/FFI.hsc" #-}

-- | 00004 others have read permission
pattern S_IROTH :: FileMode
pattern S_IROTH = FileMode 4
{-# LINE 299 "Z/IO/UV/FFI.hsc" #-}

-- | 00002 others have write permission
pattern S_IWOTH :: FileMode
pattern S_IWOTH = FileMode 2
{-# LINE 303 "Z/IO/UV/FFI.hsc" #-}

-- | 00001 others have execute permission
pattern S_IXOTH :: FileMode
pattern S_IXOTH = FileMode 1
{-# LINE 307 "Z/IO/UV/FFI.hsc" #-}

-- | Default mode for open, 0o666(readable and writable).
pattern DEFAULT_MODE :: FileMode
pattern DEFAULT_MODE = FileMode 0o666

-- non-threaded functions
foreign import ccall unsafe hs_uv_fs_open    :: BA# Word8 -> FileFlag -> FileMode -> IO UVFD
foreign import ccall unsafe hs_uv_fs_close   :: UVFD -> IO Int
foreign import ccall unsafe hs_uv_fs_read    :: UVFD -> Ptr Word8 -> Int -> Int64 -> IO Int
foreign import ccall unsafe hs_uv_fs_write   :: UVFD -> Ptr Word8 -> Int -> Int64 -> IO Int
foreign import ccall unsafe hs_uv_fs_unlink  :: BA# Word8 -> IO Int
foreign import ccall unsafe hs_uv_fs_mkdir   :: BA# Word8 -> FileMode -> IO Int
foreign import ccall unsafe hs_uv_fs_rmdir   :: BA# Word8 -> IO Int
foreign import ccall unsafe hs_uv_fs_mkdtemp :: BA# Word8 -> Int -> MBA# Word8 -> IO Int

-- threaded functions
foreign import ccall unsafe hs_uv_fs_open_threaded
    :: BA# Word8 -> FileFlag -> FileMode -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_close_threaded
    :: UVFD -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_read_threaded
    :: UVFD -> Ptr Word8 -> Int -> Int64 -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_write_threaded
    :: UVFD -> Ptr Word8 -> Int -> Int64 -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_unlink_threaded
    :: BA# Word8 -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_mkdir_threaded
    :: BA# Word8 -> FileMode -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_rmdir_threaded
    :: BA# Word8 -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_mkdtemp_threaded
    :: BA# Word8 -> Int -> MBA# Word8 -> Ptr UVLoop -> IO UVSlotUnsafe

newtype FileFlag = FileFlag CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

-- | The file is opened in append mode. Before each write, the file offset is positioned at the end of the file.
pattern O_APPEND :: FileFlag
pattern O_APPEND = FileFlag 1024
{-# LINE 348 "Z/IO/UV/FFI.hsc" #-}

-- | The file is created if it does not already exist.
pattern O_CREAT :: FileFlag
pattern O_CREAT = FileFlag 64
{-# LINE 352 "Z/IO/UV/FFI.hsc" #-}

-- | File IO is done directly to and from user-space buffers, which must be aligned. Buffer size and address should be a multiple of the physical sector size of the block device, (DO NOT USE WITH Z-IO's @BufferedIO@)
pattern O_DIRECT :: FileFlag
pattern O_DIRECT = FileFlag 16384
{-# LINE 356 "Z/IO/UV/FFI.hsc" #-}

-- | If the path is not a directory, fail the open. (Not useful on regular file)
--
-- Note 'o_DIRECTORY' is not supported on Windows.
pattern O_DIRECTORY :: FileFlag
pattern O_DIRECTORY = FileFlag 65536
{-# LINE 362 "Z/IO/UV/FFI.hsc" #-}

-- |The file is opened for synchronous IO. Write operations will complete once all data and a minimum of metadata are flushed to disk.
--
-- Note 'o_DSYNC' is supported on Windows via @FILE_FLAG_WRITE_THROUGH@.
pattern O_DSYNC :: FileFlag
pattern O_DSYNC = FileFlag 4096
{-# LINE 368 "Z/IO/UV/FFI.hsc" #-}

-- | If the 'o_CREAT' flag is set and the file already exists, fail the open.
--
-- Note In general, the behavior of 'o_EXCL' is undefined if it is used without 'o_CREAT'. There is one exception: on 
-- Linux 2.6 and later, 'o_EXCL' can be used without 'o_CREAT' if pathname refers to a block device. If the block 
-- device is in use by the system (e.g., mounted), the open will fail with the error @EBUSY@.
pattern O_EXCL :: FileFlag
pattern O_EXCL = FileFlag 128
{-# LINE 376 "Z/IO/UV/FFI.hsc" #-}

-- | Atomically obtain an exclusive lock.
--
-- Note UV_FS_O_EXLOCK is only supported on macOS and Windows.
-- (libuv: Changed in version 1.17.0: support is added for Windows.)
pattern O_EXLOCK :: FileFlag
pattern O_EXLOCK = FileFlag 0
{-# LINE 383 "Z/IO/UV/FFI.hsc" #-}

-- | Do not update the file access time when the file is read.
-- 
-- Note 'o_NOATIME' is not supported on Windows.
pattern O_NOATIME :: FileFlag
pattern O_NOATIME = FileFlag 262144
{-# LINE 389 "Z/IO/UV/FFI.hsc" #-}

-- | If the path identifies a terminal device, opening the path will not cause that terminal to become the controlling terminal for the process (if the process does not already have one). (Not sure if this flag is useful)
--
-- Note 'o_NOCTTY' is not supported on Windows.
pattern O_NOCTTY :: FileFlag
pattern O_NOCTTY = FileFlag 256
{-# LINE 395 "Z/IO/UV/FFI.hsc" #-}

-- | If the path is a symbolic link, fail the open.
--
-- Note 'o_NOFOLLOW' is not supported on Windows.
pattern O_NOFOLLOW :: FileFlag
pattern O_NOFOLLOW = FileFlag 131072
{-# LINE 401 "Z/IO/UV/FFI.hsc" #-}

-- | Open the file in nonblocking mode if possible. (Definitely not useful in Z-IO)
--
-- Note 'o_NONBLOCK' is not supported on Windows. (Not useful on regular file anyway)
pattern O_NONBLOCK :: FileFlag
pattern O_NONBLOCK = FileFlag 2048
{-# LINE 407 "Z/IO/UV/FFI.hsc" #-}

-- | Access is intended to be random. The system can use this as a hint to optimize file caching.
-- 
-- Note 'o_RANDOM' is only supported on Windows via @FILE_FLAG_RANDOM_ACCESS@.
pattern O_RANDOM :: FileFlag
pattern O_RANDOM = FileFlag 0
{-# LINE 413 "Z/IO/UV/FFI.hsc" #-}

-- | Open the file for read-only access.
pattern O_RDONLY :: FileFlag
pattern O_RDONLY = FileFlag 0
{-# LINE 417 "Z/IO/UV/FFI.hsc" #-}

-- | Open the file for read-write access.
pattern O_RDWR :: FileFlag
pattern O_RDWR = FileFlag 2
{-# LINE 421 "Z/IO/UV/FFI.hsc" #-}


-- | Access is intended to be sequential from beginning to end. The system can use this as a hint to optimize file caching.
-- 
-- Note 'o_SEQUENTIAL' is only supported on Windows via @FILE_FLAG_SEQUENTIAL_SCAN@.
pattern O_SEQUENTIAL :: FileFlag
pattern O_SEQUENTIAL = FileFlag 0
{-# LINE 428 "Z/IO/UV/FFI.hsc" #-}

-- | The file is temporary and should not be flushed to disk if possible.
--
-- Note 'o_SHORT_LIVED' is only supported on Windows via @FILE_ATTRIBUTE_TEMPORARY@.
pattern O_SHORT_LIVED :: FileFlag
pattern O_SHORT_LIVED = FileFlag 0
{-# LINE 434 "Z/IO/UV/FFI.hsc" #-}

-- | Open the symbolic link itself rather than the resource it points to.
pattern O_SYMLINK :: FileFlag
pattern O_SYMLINK = FileFlag 0
{-# LINE 438 "Z/IO/UV/FFI.hsc" #-}

-- | The file is opened for synchronous IO. Write operations will complete once all data and all metadata are flushed to disk.
--
-- Note 'o_SYNC' is supported on Windows via @FILE_FLAG_WRITE_THROUGH@.
pattern O_SYNC :: FileFlag
pattern O_SYNC = FileFlag 1052672
{-# LINE 444 "Z/IO/UV/FFI.hsc" #-}

-- | The file is temporary and should not be flushed to disk if possible.
--
-- Note 'o_TEMPORARY' is only supported on Windows via @FILE_ATTRIBUTE_TEMPORARY@.
pattern O_TEMPORARY :: FileFlag
pattern O_TEMPORARY = FileFlag 0
{-# LINE 450 "Z/IO/UV/FFI.hsc" #-}

-- | If the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero.
pattern O_TRUNC :: FileFlag
pattern O_TRUNC = FileFlag 512
{-# LINE 454 "Z/IO/UV/FFI.hsc" #-}

-- | Open the file for write-only access.
pattern O_WRONLY :: FileFlag
pattern O_WRONLY = FileFlag 1
{-# LINE 458 "Z/IO/UV/FFI.hsc" #-}


{-# LINE 462 "Z/IO/UV/FFI.hsc" #-}
newtype UVDirEntType = UVDirEntType CChar

{-# LINE 464 "Z/IO/UV/FFI.hsc" #-}
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

data DirEntType
    = DirEntUnknown
    | DirEntFile
    | DirEntDir
    | DirEntLink
    | DirEntFIFO
    | DirEntSocket
    | DirEntChar
    | DirEntBlock
  deriving (Read, Show, Eq, Ord, Generic)
    deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

fromUVDirEntType :: UVDirEntType -> DirEntType
fromUVDirEntType t
    | t == uV__DT_FILE   = DirEntFile
    | t == uV__DT_DIR    = DirEntDir
    | t == uV__DT_LINK   = DirEntLink
    | t == uV__DT_FIFO   = DirEntFIFO
    | t == uV__DT_SOCKET = DirEntSocket
    | t == uV__DT_CHAR   = DirEntChar
    | t == uV__DT_BLOCK  = DirEntBlock
    | otherwise          = DirEntUnknown

uV__DT_FILE     :: UVDirEntType
uV__DT_FILE     = UVDirEntType 8
uV__DT_DIR      :: UVDirEntType
uV__DT_DIR      = UVDirEntType 4
uV__DT_LINK     :: UVDirEntType
uV__DT_LINK     = UVDirEntType 10
uV__DT_FIFO     :: UVDirEntType
uV__DT_FIFO     = UVDirEntType 1
uV__DT_SOCKET   :: UVDirEntType
uV__DT_SOCKET   = UVDirEntType 12
uV__DT_CHAR     :: UVDirEntType
uV__DT_CHAR     = UVDirEntType 2
uV__DT_BLOCK    :: UVDirEntType
uV__DT_BLOCK    = UVDirEntType 6

{-# LINE 499 "Z/IO/UV/FFI.hsc" #-}

data UVDirEnt

peekUVDirEnt :: Ptr UVDirEnt -> IO (CString, UVDirEntType)

{-# LINE 504 "Z/IO/UV/FFI.hsc" #-}
peekUVDirEnt p = (,) ((\hsc_ptr -> hsc_ptr `plusPtr` 19) p) <$> ((\hsc_ptr -> peekByteOff hsc_ptr 18) p)
{-# LINE 505 "Z/IO/UV/FFI.hsc" #-}

{-# LINE 508 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_fs_scandir_cleanup
    :: Ptr (Ptr UVDirEnt) -> Int -> IO ()
foreign import ccall unsafe hs_uv_fs_scandir
    :: BA# Word8 -> MBA# (Ptr UVDirEnt) -> IO Int
foreign import ccall unsafe hs_uv_fs_scandir_extra_cleanup
    :: Ptr (Ptr (Ptr UVDirEnt)) -> Int -> IO ()
foreign import ccall unsafe hs_uv_fs_scandir_threaded
    :: BA# Word8 -> Ptr (Ptr (Ptr UVDirEnt)) -> Ptr UVLoop -> IO UVSlotUnsafe

data UVTimeSpec = UVTimeSpec
    { uvtSecond     :: {-# UNPACK #-} !CLong
    , uvtNanoSecond :: {-# UNPACK #-} !CLong
    } deriving (Show, Read, Eq, Ord, Generic)
        deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

instance Storable UVTimeSpec where
    sizeOf _  = (16)
{-# LINE 526 "Z/IO/UV/FFI.hsc" #-}
    alignment _ = 8
{-# LINE 527 "Z/IO/UV/FFI.hsc" #-}
    peek p = UVTimeSpec <$> ((\hsc_ptr -> peekByteOff hsc_ptr 0) p)
{-# LINE 528 "Z/IO/UV/FFI.hsc" #-}
                        <*> ((\hsc_ptr -> peekByteOff hsc_ptr 8) p)
{-# LINE 529 "Z/IO/UV/FFI.hsc" #-}
    poke p (UVTimeSpec sec nsec) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0) p sec)
{-# LINE 531 "Z/IO/UV/FFI.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8) p nsec)
{-# LINE 532 "Z/IO/UV/FFI.hsc" #-}

data FStat = FStat
    { stDev      :: {-# UNPACK #-} !Word64
    , stMode     :: {-# UNPACK #-} !Word64
    , stNlink    :: {-# UNPACK #-} !Word64
    , stUid      :: {-# UNPACK #-} !Word64
    , stGid      :: {-# UNPACK #-} !Word64
    , stRdev     :: {-# UNPACK #-} !Word64
    , stIno      :: {-# UNPACK #-} !Word64
    , stSize     :: {-# UNPACK #-} !Word64
    , stBlksize  :: {-# UNPACK #-} !Word64
    , stBlocks   :: {-# UNPACK #-} !Word64
    , stFlags    :: {-# UNPACK #-} !Word64
    , stGen      :: {-# UNPACK #-} !Word64
    , stAtim     :: {-# UNPACK #-} !UVTimeSpec
    , stMtim     :: {-# UNPACK #-} !UVTimeSpec
    , stCtim     :: {-# UNPACK #-} !UVTimeSpec
    , stBirthtim :: {-# UNPACK #-} !UVTimeSpec
    } deriving (Show, Read, Eq, Ord, Generic)
        deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

uvStatSize :: Int
uvStatSize = (160)
{-# LINE 555 "Z/IO/UV/FFI.hsc" #-}

peekUVStat :: Ptr FStat -> IO FStat
peekUVStat p = FStat
    <$> ((\hsc_ptr -> peekByteOff hsc_ptr 0) p)
{-# LINE 559 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 8) p)
{-# LINE 560 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 16) p)
{-# LINE 561 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 24) p)
{-# LINE 562 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 32) p)
{-# LINE 563 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 40) p)
{-# LINE 564 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 48) p)
{-# LINE 565 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 56) p)
{-# LINE 566 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 64) p)
{-# LINE 567 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 72) p)
{-# LINE 568 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 80) p)
{-# LINE 569 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 88) p)
{-# LINE 570 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 96) p)
{-# LINE 571 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 112) p)
{-# LINE 572 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 128) p)
{-# LINE 573 "Z/IO/UV/FFI.hsc" #-}
    <*> ((\hsc_ptr -> peekByteOff hsc_ptr 144) p)
{-# LINE 574 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_fs_stat :: BA# Word8 -> Ptr FStat -> IO Int
foreign import ccall unsafe hs_uv_fs_fstat :: UVFD -> Ptr FStat -> IO Int
foreign import ccall unsafe hs_uv_fs_lstat :: BA# Word8 -> Ptr FStat -> IO Int
foreign import ccall unsafe hs_uv_fs_rename :: BA# Word8 -> BA# Word8 -> IO Int
foreign import ccall unsafe hs_uv_fs_fsync :: UVFD -> IO Int
foreign import ccall unsafe hs_uv_fs_fdatasync :: UVFD -> IO Int
foreign import ccall unsafe hs_uv_fs_ftruncate :: UVFD -> Int64 -> IO Int

foreign import ccall unsafe hs_uv_fs_stat_threaded
    :: BA# Word8 -> Ptr FStat -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_fstat_threaded
    :: UVFD -> Ptr FStat -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_lstat_threaded
    :: BA# Word8 -> Ptr FStat -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_rename_threaded
    :: BA# Word8 -> BA# Word8 -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_fsync_threaded
    :: UVFD -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_fdatasync_threaded
    :: UVFD -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_ftruncate_threaded
    :: UVFD -> Int64 -> Ptr UVLoop -> IO UVSlotUnsafe

-- | Flags control copying.
-- 
--   * 'COPYFILE_EXCL': If present, uv_fs_copyfile() will fail with UV_EEXIST if the destination path already exists. The default behavior is to overwrite the destination if it exists.
--   * 'COPYFILE_FICLONE': If present, uv_fs_copyfile() will attempt to create a copy-on-write reflink. If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
-- 
newtype CopyFileFlag = CopyFileFlag CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

pattern COPYFILE_DEFAULT :: CopyFileFlag
pattern COPYFILE_DEFAULT = CopyFileFlag 0

pattern COPYFILE_EXCL :: CopyFileFlag
pattern COPYFILE_EXCL = CopyFileFlag 1
{-# LINE 613 "Z/IO/UV/FFI.hsc" #-}

pattern COPYFILE_FICLONE :: CopyFileFlag

{-# LINE 616 "Z/IO/UV/FFI.hsc" #-}
pattern COPYFILE_FICLONE = CopyFileFlag 2
{-# LINE 617 "Z/IO/UV/FFI.hsc" #-}

{-# LINE 620 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_fs_copyfile :: BA# Word8 -> BA# Word8 -> CopyFileFlag -> IO Int
foreign import ccall unsafe hs_uv_fs_copyfile_threaded
    :: BA# Word8 -> BA# Word8 -> CopyFileFlag -> Ptr UVLoop -> IO UVSlotUnsafe

newtype AccessMode = AccessMode CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

pattern F_OK :: AccessMode
pattern F_OK = AccessMode 0
{-# LINE 632 "Z/IO/UV/FFI.hsc" #-}
pattern R_OK :: AccessMode
pattern R_OK = AccessMode 4
{-# LINE 634 "Z/IO/UV/FFI.hsc" #-}
pattern W_OK :: AccessMode
pattern W_OK = AccessMode 2
{-# LINE 636 "Z/IO/UV/FFI.hsc" #-}
pattern X_OK :: AccessMode
pattern X_OK = AccessMode 1
{-# LINE 638 "Z/IO/UV/FFI.hsc" #-}

data AccessResult = NoExistence | NoPermission | AccessOK deriving (Show, Eq, Ord, Generic)
                                                          deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

foreign import ccall unsafe hs_uv_fs_access :: BA# Word8 -> AccessMode -> IO Int
foreign import ccall unsafe hs_uv_fs_access_threaded
    :: BA# Word8 -> AccessMode -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_chmod :: BA# Word8 -> FileMode -> IO Int
foreign import ccall unsafe hs_uv_fs_chmod_threaded
    :: BA# Word8 -> FileMode -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_fchmod :: UVFD -> FileMode -> IO Int
foreign import ccall unsafe hs_uv_fs_fchmod_threaded
    :: UVFD -> FileMode -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_utime :: BA# Word8 -> Double -> Double -> IO Int
foreign import ccall unsafe hs_uv_fs_utime_threaded
    :: BA# Word8 -> Double -> Double -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_futime :: UVFD -> Double -> Double -> IO Int
foreign import ccall unsafe hs_uv_fs_futime_threaded
    :: UVFD -> Double -> Double -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_lutime :: BA# Word8 -> Double -> Double -> IO Int
foreign import ccall unsafe hs_uv_fs_lutime_threaded
    :: BA# Word8 -> Double -> Double -> Ptr UVLoop -> IO UVSlotUnsafe

newtype SymlinkFlag = SymlinkFlag CInt
    deriving (Eq, Ord, Show, Generic)
    deriving newtype (Storable, Unaligned, Bits, FiniteBits, Num, EncodeJSON, ToValue, FromValue)
    deriving anyclass ShowT

pattern SYMLINK_DEFAULT :: SymlinkFlag
pattern SYMLINK_DEFAULT = SymlinkFlag 0

pattern SYMLINK_DIR :: SymlinkFlag
pattern SYMLINK_DIR = SymlinkFlag 1
{-# LINE 676 "Z/IO/UV/FFI.hsc" #-}

pattern SYMLINK_JUNCTION :: SymlinkFlag
pattern SYMLINK_JUNCTION = SymlinkFlag 2
{-# LINE 679 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe hs_uv_fs_link :: BA# Word8 -> BA# Word8 -> IO Int
foreign import ccall unsafe hs_uv_fs_link_threaded
    :: BA# Word8 -> BA# Word8 -> Ptr UVLoop -> IO UVSlotUnsafe

foreign import ccall unsafe hs_uv_fs_symlink :: BA# Word8 -> BA# Word8 -> SymlinkFlag -> IO Int
foreign import ccall unsafe hs_uv_fs_symlink_threaded
    :: BA# Word8 -> BA# Word8 -> SymlinkFlag -> Ptr UVLoop -> IO UVSlotUnsafe

-- readlink and realpath share the same cleanup and callback
foreign import ccall unsafe hs_uv_fs_readlink_cleanup
    :: CString -> IO ()
foreign import ccall unsafe hs_uv_fs_readlink
    :: BA# Word8 -> MBA# CString -> IO Int
foreign import ccall unsafe hs_uv_fs_realpath
    :: BA# Word8  -> MBA# CString -> IO Int
foreign import ccall unsafe hs_uv_fs_readlink_extra_cleanup
    :: Ptr CString -> IO ()
foreign import ccall unsafe hs_uv_fs_readlink_threaded
    :: BA# Word8  -> Ptr CString -> Ptr UVLoop -> IO UVSlotUnsafe
foreign import ccall unsafe hs_uv_fs_realpath_threaded
    :: BA# Word8  -> Ptr CString -> Ptr UVLoop -> IO UVSlotUnsafe

--------------------------------------------------------------------------------
-- misc

newtype UVHandleType = UVHandleType CInt deriving (Eq, Ord, Show, Generic)
                                         deriving newtype (Storable, Unaligned, EncodeJSON, ToValue, FromValue)
                                         deriving anyclass ShowT

pattern UV_UNKNOWN_HANDLE :: UVHandleType
pattern UV_UNKNOWN_HANDLE = UVHandleType 0
{-# LINE 711 "Z/IO/UV/FFI.hsc" #-}
pattern UV_ASYNC :: UVHandleType
pattern UV_ASYNC = UVHandleType 1
{-# LINE 713 "Z/IO/UV/FFI.hsc" #-}
pattern UV_CHECK :: UVHandleType
pattern UV_CHECK = UVHandleType 2
{-# LINE 715 "Z/IO/UV/FFI.hsc" #-}
pattern UV_FS_EVENT :: UVHandleType
pattern UV_FS_EVENT = UVHandleType 3
{-# LINE 717 "Z/IO/UV/FFI.hsc" #-}
pattern UV_FS_POLL :: UVHandleType
pattern UV_FS_POLL = UVHandleType 4
{-# LINE 719 "Z/IO/UV/FFI.hsc" #-}
pattern UV_HANDLE :: UVHandleType
pattern UV_HANDLE = UVHandleType 5
{-# LINE 721 "Z/IO/UV/FFI.hsc" #-}
pattern UV_IDLE :: UVHandleType
pattern UV_IDLE = UVHandleType 6
{-# LINE 723 "Z/IO/UV/FFI.hsc" #-}
pattern UV_NAMED_PIPE :: UVHandleType
pattern UV_NAMED_PIPE = UVHandleType 7
{-# LINE 725 "Z/IO/UV/FFI.hsc" #-}
pattern UV_POLL :: UVHandleType
pattern UV_POLL = UVHandleType 8
{-# LINE 727 "Z/IO/UV/FFI.hsc" #-}
pattern UV_PREPARE :: UVHandleType
pattern UV_PREPARE = UVHandleType 9
{-# LINE 729 "Z/IO/UV/FFI.hsc" #-}
pattern UV_PROCESS :: UVHandleType
pattern UV_PROCESS = UVHandleType 10
{-# LINE 731 "Z/IO/UV/FFI.hsc" #-}
pattern UV_STREAM :: UVHandleType
pattern UV_STREAM = UVHandleType 11
{-# LINE 733 "Z/IO/UV/FFI.hsc" #-}
pattern UV_TCP :: UVHandleType
pattern UV_TCP = UVHandleType 12
{-# LINE 735 "Z/IO/UV/FFI.hsc" #-}
pattern UV_TIMER :: UVHandleType
pattern UV_TIMER = UVHandleType 13
{-# LINE 737 "Z/IO/UV/FFI.hsc" #-}
pattern UV_TTY :: UVHandleType
pattern UV_TTY = UVHandleType 14
{-# LINE 739 "Z/IO/UV/FFI.hsc" #-}
pattern UV_UDP :: UVHandleType
pattern UV_UDP = UVHandleType 15
{-# LINE 741 "Z/IO/UV/FFI.hsc" #-}
pattern UV_SIGNAL :: UVHandleType
pattern UV_SIGNAL = UVHandleType 16
{-# LINE 743 "Z/IO/UV/FFI.hsc" #-}
pattern UV_FILE :: UVHandleType
pattern UV_FILE = UVHandleType 17
{-# LINE 745 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe uv_guess_handle :: UVFD -> IO UVHandleType

foreign import ccall unsafe uv_resident_set_memory :: MBA# CSize -> IO CInt
foreign import ccall unsafe uv_uptime :: MBA# Double -> IO CInt
foreign import ccall unsafe uv_getrusage :: MBA# a -> IO CInt

-- | Data type for storing times.
-- typedef struct { long tv_sec; long tv_usec; } uv_timeval_t;
data TimeVal = TimeVal
    { tv_sec  :: {-# UNPACK #-} !CLong
    , tv_usec :: {-# UNPACK #-} !CLong
    } deriving (Show, Read, Eq, Ord, Generic)
      deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

-- | Data type for resource usage results.
--
-- Members marked with (X) are unsupported on Windows. 
-- See <https://man7.org/linux/man-pages/man2/getrusage.2.html getrusage(2)> for supported fields on Unix
data ResUsage = ResUsage
    { ru_utime    :: {-# UNPACK #-} !TimeVal   -- ^  user CPU time used, in microseconds
    , ru_stime    :: {-# UNPACK #-} !TimeVal   -- ^  system CPU time used, in microseconds
    , ru_maxrss   :: {-# UNPACK #-} !Word64    -- ^  maximum resident set size
    , ru_ixrss    :: {-# UNPACK #-} !Word64    -- ^  integral shared memory size (X)
    , ru_idrss    :: {-# UNPACK #-} !Word64    -- ^  integral unshared data size (X)
    , ru_isrss    :: {-# UNPACK #-} !Word64    -- ^  integral unshared stack size (X)
    , ru_minflt   :: {-# UNPACK #-} !Word64    -- ^  page reclaims (soft page faults) (X)
    , ru_majflt   :: {-# UNPACK #-} !Word64    -- ^  page faults (hard page faults)
    , ru_nswap    :: {-# UNPACK #-} !Word64    -- ^  swaps (X)
    , ru_inblock  :: {-# UNPACK #-} !Word64    -- ^  block input operations
    , ru_oublock  :: {-# UNPACK #-} !Word64    -- ^  block output operations
    , ru_msgsnd   :: {-# UNPACK #-} !Word64    -- ^  IPC messages sent (X)
    , ru_msgrcv   :: {-# UNPACK #-} !Word64    -- ^  IPC messages received (X)
    , ru_nsignals :: {-# UNPACK #-} !Word64    -- ^  signals received (X)
    , ru_nvcsw    :: {-# UNPACK #-} !Word64    -- ^  voluntary context switches (X)
    , ru_nivcsw   :: {-# UNPACK #-} !Word64    -- ^  involuntary context switches (X)
    } deriving (Show, Read, Eq, Ord, Generic)
      deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

sizeOfResUsage :: Int
sizeOfResUsage = (144)
{-# LINE 786 "Z/IO/UV/FFI.hsc" #-}

peekResUsage :: MBA# a -> IO ResUsage
peekResUsage mba = do
    utime_sec :: CLong <- peekMBA mba ((0))
{-# LINE 790 "Z/IO/UV/FFI.hsc" #-}
    utime_usec :: CLong <- peekMBA mba (((0)) + sizeOf (undefined :: CLong))
{-# LINE 791 "Z/IO/UV/FFI.hsc" #-}
    stime_sec :: CLong <- peekMBA mba ((16))
{-# LINE 792 "Z/IO/UV/FFI.hsc" #-}
    stime_usec :: CLong <- peekMBA mba (((16)) + sizeOf (undefined :: CLong))
{-# LINE 793 "Z/IO/UV/FFI.hsc" #-}
    maxrss   <- peekMBA mba ((32))
{-# LINE 794 "Z/IO/UV/FFI.hsc" #-}
    ixrss    <- peekMBA mba ((40))
{-# LINE 795 "Z/IO/UV/FFI.hsc" #-}
    idrss    <- peekMBA mba ((48))
{-# LINE 796 "Z/IO/UV/FFI.hsc" #-}
    isrss    <- peekMBA mba ((56))
{-# LINE 797 "Z/IO/UV/FFI.hsc" #-}
    minflt   <- peekMBA mba ((64))
{-# LINE 798 "Z/IO/UV/FFI.hsc" #-}
    majflt   <- peekMBA mba ((72))
{-# LINE 799 "Z/IO/UV/FFI.hsc" #-}
    nswap    <- peekMBA mba ((80))
{-# LINE 800 "Z/IO/UV/FFI.hsc" #-}
    inblock  <- peekMBA mba ((88))
{-# LINE 801 "Z/IO/UV/FFI.hsc" #-}
    oublock  <- peekMBA mba ((96))
{-# LINE 802 "Z/IO/UV/FFI.hsc" #-}
    msgsnd   <- peekMBA mba ((104))
{-# LINE 803 "Z/IO/UV/FFI.hsc" #-}
    msgrcv   <- peekMBA mba ((112))
{-# LINE 804 "Z/IO/UV/FFI.hsc" #-}
    nsignals <- peekMBA mba ((120))
{-# LINE 805 "Z/IO/UV/FFI.hsc" #-}
    nvcsw    <- peekMBA mba ((128))
{-# LINE 806 "Z/IO/UV/FFI.hsc" #-}
    nivcsw   <- peekMBA mba ((136))
{-# LINE 807 "Z/IO/UV/FFI.hsc" #-}
    return (ResUsage (TimeVal utime_sec utime_usec) (TimeVal stime_sec stime_usec)
                    maxrss ixrss idrss isrss minflt majflt nswap inblock
                    oublock msgsnd msgrcv nsignals nvcsw nivcsw)

foreign import ccall unsafe uv_os_getpid :: IO PID
foreign import ccall unsafe uv_os_getppid :: IO PID
foreign import ccall unsafe uv_os_getpriority :: PID -> MBA# CInt -> IO CInt
foreign import ccall unsafe uv_os_setpriority :: PID -> CInt -> IO CInt

newtype PID = PID CInt deriving (Eq, Ord, Show, Generic)
                       deriving newtype (Storable, Unaligned, EncodeJSON, ToValue, FromValue)
                       deriving anyclass ShowT

pattern PRIORITY_LOW          :: CInt
pattern PRIORITY_BELOW_NORMAL :: CInt
pattern PRIORITY_NORMAL       :: CInt
pattern PRIORITY_ABOVE_NORMAL :: CInt
pattern PRIORITY_HIGH         :: CInt
pattern PRIORITY_HIGHEST      :: CInt
pattern PRIORITY_LOW           = (19)
{-# LINE 827 "Z/IO/UV/FFI.hsc" #-}
pattern PRIORITY_BELOW_NORMAL  = (10)
{-# LINE 828 "Z/IO/UV/FFI.hsc" #-}
pattern PRIORITY_NORMAL        = (0)
{-# LINE 829 "Z/IO/UV/FFI.hsc" #-}
pattern PRIORITY_ABOVE_NORMAL  = (-7)
{-# LINE 830 "Z/IO/UV/FFI.hsc" #-}
pattern PRIORITY_HIGH          = (-14)
{-# LINE 831 "Z/IO/UV/FFI.hsc" #-}
pattern PRIORITY_HIGHEST       = (-20)
{-# LINE 832 "Z/IO/UV/FFI.hsc" #-}

foreign import ccall unsafe uv_hrtime :: IO Word64

foreign import ccall unsafe uv_os_environ :: MBA# (Ptr a) -> MBA# CInt -> IO CInt
foreign import ccall unsafe uv_os_free_environ :: Ptr a -> CInt -> IO ()
foreign import ccall unsafe uv_os_getenv :: BA# Word8 -> MBA# Word8 -> MBA# CSize -> IO CInt
foreign import ccall unsafe uv_os_setenv :: BA# Word8 -> BA# Word8 -> IO CInt
foreign import ccall unsafe uv_os_unsetenv :: BA# Word8 -> IO CInt

pattern UV_MAXHOSTNAMESIZE :: CSize
pattern UV_MAXHOSTNAMESIZE = 65
{-# LINE 843 "Z/IO/UV/FFI.hsc" #-}
foreign import ccall unsafe uv_os_gethostname :: MBA# Word8 -> MBA# CSize -> IO CInt

data OSName = OSName
    { os_sysname :: T.Text
    , os_release :: T.Text
    , os_version :: T.Text
    , os_machine :: T.Text
    } deriving (Eq, Ord, Show, Generic)
       deriving anyclass (ShowT, EncodeJSON, ToValue, FromValue)

getOSName :: IO OSName
getOSName = do
    (A.MutablePrimArray mba# :: A.MutablePrimArray A.RealWorld Word8) <- A.newArr ((1024))
{-# LINE 856 "Z/IO/UV/FFI.hsc" #-}
    throwUVIfMinus_ (uv_os_uname mba#)
    sn <- toText <$> peekMBA mba# ((0))
{-# LINE 858 "Z/IO/UV/FFI.hsc" #-}
    re <- toText <$> peekMBA mba# ((256))
{-# LINE 859 "Z/IO/UV/FFI.hsc" #-}
    ve <- toText <$> peekMBA mba# ((512))
{-# LINE 860 "Z/IO/UV/FFI.hsc" #-}
    ma <- toText <$> peekMBA mba#  ((768))
{-# LINE 861 "Z/IO/UV/FFI.hsc" #-}
    return (OSName sn re ve ma)

foreign import ccall unsafe uv_os_uname :: MBA# OSName -> IO CInt

foreign import ccall unsafe hs_uv_random :: MBA# Word8 -> CSize -> CInt -> IO CInt
foreign import ccall unsafe hs_uv_random_threaded :: Ptr Word8 -> CSize -> CInt -> Ptr UVLoop -> IO UVSlotUnsafe