Skip to content

Commit

Permalink
Add MapFlags::map_hugetlb_with_size_log2
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 2, 2023
1 parent 996db47 commit 46a8272
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::Result;
#[cfg(feature = "fs")]
use crate::{fcntl::OFlag, sys::stat::Mode};
use libc::{self, c_int, c_void, off_t, size_t};
use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}};
use std::{
num::NonZeroUsize,
os::unix::io::{AsFd, AsRawFd},
};

libc_bitflags! {
/// Desired memory protection of a memory mapping.
Expand Down Expand Up @@ -187,6 +190,31 @@ libc_bitflags! {
}
}

impl MapFlags {
/// Create `MAP_HUGETLB` with provided size of huge page.
///
/// Under the hood it computes `MAP_HUGETLB | (huge_page_size_log2 << libc::MAP_HUGE_SHIFT`).
/// `huge_page_size_log2` denotes logarithm of huge page size to use and should be
/// between 16 and 63 (inclusively).
///
/// ```
/// use nix::sys::mman::MapFlags;
/// let f = MapFlags::map_hugetlb_with_size_log2(30).unwrap();
/// assert_eq!(f, MapFlags::MAP_HUGETLB | MapFlags::MAP_HUGE_1GB);
/// ```
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn map_hugetlb_with_size_log2(huge_page_size_log2: u32) -> Option<Self> {
if (16..=63).contains(&huge_page_size_log2) {
let flag = libc::MAP_HUGETLB
| (huge_page_size_log2 << libc::MAP_HUGE_SHIFT) as i32;
Some(Self(flag.into()))
} else {
None
}
}
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
libc_bitflags! {
/// Options for [`mremap`].
Expand Down

0 comments on commit 46a8272

Please sign in to comment.