From 3b16f0c8f71dee9010f71cb2a596caaf3723d3a0 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Tue, 14 Nov 2023 05:20:53 +0300 Subject: [PATCH] Add `MapFlags::map_hugetlb_with_size_log2` (#2125) * Add `MapFlags::map_hugetlb_with_size_log2` * Add changelog * Add TODO note --- changelog/2125.added.md | 1 + src/sys/mman.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 changelog/2125.added.md diff --git a/changelog/2125.added.md b/changelog/2125.added.md new file mode 100644 index 0000000000..1a7600be9f --- /dev/null +++ b/changelog/2125.added.md @@ -0,0 +1 @@ +Added `MapFlags::map_hugetlb_with_size_log2` method for Linux targets \ No newline at end of file diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 1e4536ae8c..a86916c075 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -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 { + 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`].