#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
#[repr(i32)]
pub enum LockAllMemory
{
AllCurrentMemory = MCL_CURRENT,
AllCurrentMemoryOnFault = MCL_CURRENT | MCL_ONFAULT,
AllFutureMemory = MCL_FUTURE,
AllFutureMemoryOnFault = MCL_FUTURE | MCL_ONFAULT,
AllCurrentAndFutureMemory = MCL_CURRENT | MCL_FUTURE,
AllCurrentAndFutureMemoryOnFault = MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT,
}
impl LockAllMemory
{
#[inline(always)]
pub fn lock(self)
{
let flags = self as i32;
let result = unsafe { mlockall(flags) };
if likely!(result == 0)
{
}
else if likely!(result == -1)
{
match errno().0
{
ENOMEM => panic!("the caller had a nonzero RLIMIT_MEMLOCK soft resource limit, but tried to lock more memory than the limit permitted. This limit is not enforced if the process is privileged (CAP_IPC_LOCK)."),
EPERM => panic!("The caller is not privileged, but needs privilege (CAP_IPC_LOCK) to perform the requested operation."),
EINVAL => panic!("Unknown flags were specified or MCL_ONFAULT was specified without either MCL_FUTURE or MCL_CURRENT"),
unexpected @ _ => panic!("Unexpected error {} from mlockall()", unexpected)
}
}
else
{
unreachable_code(format_args!("Unexpected result {} from mlockall()", result))
}
}
pub fn unlock()
{
let result = unsafe { munlockall() };
if likely!(result == 0)
{
}
else if likely!(result == -1)
{
match errno().0
{
ENOMEM => panic!("the caller had a nonzero RLIMIT_MEMLOCK soft resource limit, but tried to lock more memory than the limit permitted. This limit is not enforced if the process is privileged (CAP_IPC_LOCK)."),
EPERM => panic!("The caller is not privileged, but needs privilege (CAP_IPC_LOCK) to perform the requested operation. Or, (Linux 2.6.8 and earlier) The caller was not privileged (CAP_IPC_LOCK)"),
EINVAL => panic!("Unknown flags were specified or MCL_ONFAULT was specified without either MCL_FUTURE or MCL_CURRENT"),
unexpected @ _ => panic!("Unexpected error {} from munlockall()", unexpected)
}
}
else
{
unreachable_code(format_args!("Unexpected result {} from munlockall()", result))
}
}
}