Skip to content

Commit

Permalink
Add MapFlags::map_hugetlb_with_size_log2 (#2125)
Browse files Browse the repository at this point in the history
* Add `MapFlags::map_hugetlb_with_size_log2`

* Add changelog

* Add TODO note
  • Loading branch information
newpavlov authored Nov 14, 2023
1 parent 1ea59c7 commit 3b16f0c
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2125.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `MapFlags::map_hugetlb_with_size_log2` method for Linux targets
27 changes: 27 additions & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
@@ -188,6 +188,33 @@ 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);
/// ```
// TODO: support Andorid and Fuchsia when https://github.com/rust-lang/libc/pull/3444
// will be released
#[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`].

0 comments on commit 3b16f0c

Please sign in to comment.