use bitflags::bitflags;
use core::{ffi::c_int, num::NonZeroUsize};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PageSize {
#[default]
Base,
Huge2MiB,
Huge1GiB,
Custom(NonZeroUsize),
}
impl PageSize {
pub const BASE_BYTES: usize = 4 * 1024;
pub const HUGE_2MIB_BYTES: usize = 2 * 1024 * 1024;
pub const HUGE_1GIB_BYTES: usize = 1024 * 1024 * 1024;
#[inline]
pub const fn new(bytes: usize) -> Option<Self> {
if !bytes.is_power_of_two() {
return None;
}
match bytes {
Self::BASE_BYTES => Some(Self::Base),
Self::HUGE_2MIB_BYTES => Some(Self::Huge2MiB),
Self::HUGE_1GIB_BYTES => Some(Self::Huge1GiB),
_ => match NonZeroUsize::new(bytes) {
Some(bytes) => Some(Self::Custom(bytes)),
None => None,
},
}
}
#[inline]
pub const fn bytes(self) -> usize {
match self {
Self::Base => Self::BASE_BYTES,
Self::Huge2MiB => Self::HUGE_2MIB_BYTES,
Self::Huge1GiB => Self::HUGE_1GIB_BYTES,
Self::Custom(bytes) => bytes.get(),
}
}
#[inline]
pub const fn is_huge(self) -> bool {
matches!(self, Self::Huge2MiB | Self::Huge1GiB)
}
}
bitflags! {
#[derive(Clone, Copy, Debug, Default)]
pub struct ProtFlags: c_int {
const PROT_NONE = 0;
const PROT_READ = 1;
const PROT_WRITE = 2;
const PROT_EXEC = 4;
}
}
bitflags! {
#[derive(Clone, Copy)]
pub struct MapFlags: c_int {
const MAP_PRIVATE = 2;
const MAP_FIXED = 16;
const MAP_ANONYMOUS = 32;
const MAP_HUGETLB = 0x40000;
const MAP_HUGE_2MB = 21 << 26;
const MAP_HUGE_1GB = 30 << 26;
}
}
impl MapFlags {
#[inline]
pub const fn huge_page_size(page_size: PageSize) -> Option<Self> {
match page_size {
PageSize::Base => None,
PageSize::Huge2MiB => Some(Self::MAP_HUGETLB.union(Self::MAP_HUGE_2MB)),
PageSize::Huge1GiB => Some(Self::MAP_HUGETLB.union(Self::MAP_HUGE_1GB)),
PageSize::Custom(_) => None,
}
}
}
#[repr(C)]
pub enum MadviseAdvice {
Normal = 0,
Random = 1,
Sequential = 2,
WillNeed = 3,
DontNeed = 4,
Free = 8,
Remove = 9,
DontFork = 10,
DoFork = 11,
HWPoison = 100,
SoftOffline = 101,
Mergeable = 12,
Unmergeable = 13,
HugePage = 14,
NoHugePage = 15,
DontDump = 16,
DoDump = 17,
WipeOnFork = 18,
KeepOnFork = 19,
Cold = 20,
PageOut = 21,
PopulateRead = 22,
PopulateWrite = 23,
DontNeedLocked = 24,
Collapse = 25,
GuardInstall = 102,
GuardRemove = 103,
}