{-# LINE 1 "System/Posix/Resource.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LINE 5 "System/Posix/Resource.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 7 "System/Posix/Resource.hsc" #-}
module System.Posix.Resource (
ResourceLimit(..), ResourceLimits(..), Resource(..),
getResourceLimit,
setResourceLimit,
) where
import System.Posix.Types
import Foreign
import Foreign.C
{-# LINE 39 "System/Posix/Resource.hsc" #-}
{-# LINE 42 "System/Posix/Resource.hsc" #-}
data Resource
= ResourceCoreFileSize
| ResourceCPUTime
| ResourceDataSize
| ResourceFileSize
| ResourceOpenFiles
| ResourceStackSize
{-# LINE 54 "System/Posix/Resource.hsc" #-}
| ResourceTotalMemory
{-# LINE 56 "System/Posix/Resource.hsc" #-}
deriving (Eq, Show)
data ResourceLimits
= ResourceLimits { softLimit, hardLimit :: ResourceLimit }
deriving (Eq, Show)
data ResourceLimit
= ResourceLimitInfinity
| ResourceLimitUnknown
| ResourceLimit Integer
deriving (Eq, Show)
{-# LINE 81 "System/Posix/Resource.hsc" #-}
data {-# CTYPE "struct rlimit" #-} RLimit
foreign import capi unsafe "HsUnix.h getrlimit"
c_getrlimit :: CInt -> Ptr RLimit -> IO CInt
foreign import capi unsafe "HsUnix.h setrlimit"
c_setrlimit :: CInt -> Ptr RLimit -> IO CInt
getResourceLimit :: Resource -> IO ResourceLimits
getResourceLimit res = do
allocaBytes (16) $ \p_rlimit -> do
{-# LINE 93 "System/Posix/Resource.hsc" #-}
throwErrnoIfMinus1_ "getResourceLimit" $
c_getrlimit (packResource res) p_rlimit
soft <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p_rlimit
{-# LINE 96 "System/Posix/Resource.hsc" #-}
hard <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p_rlimit
{-# LINE 97 "System/Posix/Resource.hsc" #-}
return (ResourceLimits {
softLimit = unpackRLimit soft,
hardLimit = unpackRLimit hard
})
setResourceLimit :: Resource -> ResourceLimits -> IO ()
setResourceLimit res ResourceLimits{softLimit=soft,hardLimit=hard} = do
allocaBytes (16) $ \p_rlimit -> do
{-# LINE 105 "System/Posix/Resource.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p_rlimit (packRLimit soft True)
{-# LINE 106 "System/Posix/Resource.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p_rlimit (packRLimit hard False)
{-# LINE 107 "System/Posix/Resource.hsc" #-}
throwErrnoIfMinus1_ "setResourceLimit" $
c_setrlimit (packResource res) p_rlimit
return ()
packResource :: Resource -> CInt
packResource ResourceCoreFileSize = (4)
{-# LINE 113 "System/Posix/Resource.hsc" #-}
packResource ResourceCPUTime = (0)
{-# LINE 114 "System/Posix/Resource.hsc" #-}
packResource ResourceDataSize = (2)
{-# LINE 115 "System/Posix/Resource.hsc" #-}
packResource ResourceFileSize = (1)
{-# LINE 116 "System/Posix/Resource.hsc" #-}
packResource ResourceOpenFiles = (7)
{-# LINE 117 "System/Posix/Resource.hsc" #-}
packResource ResourceStackSize = (3)
{-# LINE 118 "System/Posix/Resource.hsc" #-}
{-# LINE 119 "System/Posix/Resource.hsc" #-}
packResource ResourceTotalMemory = (9)
{-# LINE 120 "System/Posix/Resource.hsc" #-}
{-# LINE 121 "System/Posix/Resource.hsc" #-}
unpackRLimit :: CRLim -> ResourceLimit
unpackRLimit (18446744073709551615) = ResourceLimitInfinity
{-# LINE 124 "System/Posix/Resource.hsc" #-}
unpackRLimit other
{-# LINE 126 "System/Posix/Resource.hsc" #-}
| ((18446744073709551615) :: CRLim) /= (18446744073709551615)
{-# LINE 127 "System/Posix/Resource.hsc" #-}
, other == (18446744073709551615)
{-# LINE 128 "System/Posix/Resource.hsc" #-}
= ResourceLimitUnknown
{-# LINE 130 "System/Posix/Resource.hsc" #-}
{-# LINE 131 "System/Posix/Resource.hsc" #-}
| ((18446744073709551615) :: CRLim) /= (18446744073709551615)
{-# LINE 132 "System/Posix/Resource.hsc" #-}
, other == (18446744073709551615)
{-# LINE 133 "System/Posix/Resource.hsc" #-}
{-# LINE 136 "System/Posix/Resource.hsc" #-}
= ResourceLimitUnknown
{-# LINE 141 "System/Posix/Resource.hsc" #-}
| otherwise = ResourceLimit (fromIntegral other)
packRLimit :: ResourceLimit -> Bool -> CRLim
packRLimit ResourceLimitInfinity _ = (18446744073709551615)
{-# LINE 145 "System/Posix/Resource.hsc" #-}
{-# LINE 146 "System/Posix/Resource.hsc" #-}
packRLimit ResourceLimitUnknown True = (18446744073709551615)
{-# LINE 147 "System/Posix/Resource.hsc" #-}
{-# LINE 148 "System/Posix/Resource.hsc" #-}
{-# LINE 149 "System/Posix/Resource.hsc" #-}
packRLimit ResourceLimitUnknown False = (18446744073709551615)
{-# LINE 150 "System/Posix/Resource.hsc" #-}
{-# LINE 151 "System/Posix/Resource.hsc" #-}
{-# LINE 157 "System/Posix/Resource.hsc" #-}
packRLimit (ResourceLimit other) _ = fromIntegral other
{-# LINE 200 "System/Posix/Resource.hsc" #-}