From 6fd0d668761cf961b0490267a4ead7fba9fe85dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 2 Oct 2023 12:42:21 +0300 Subject: [PATCH 1/3] Add `MapFlags::map_hugetlb_with_size_log2` --- src/sys/mman.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/sys/mman.rs b/src/sys/mman.rs index c2299e9648..c30f87bb38 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -190,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 { + 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`]. From d2785bf04272ef3e9b254595d241d1171e544e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 13 Nov 2023 23:58:26 +0300 Subject: [PATCH 2/3] Add changelog --- changelog/2125.added.md | 1 + 1 file changed, 1 insertion(+) 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 From 47f51787d2f68a0287056b63a83bca0758a3490e Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Tue, 14 Nov 2023 04:28:59 +0300 Subject: [PATCH 3/3] Add TODO note --- src/sys/mman.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sys/mman.rs b/src/sys/mman.rs index c30f87bb38..6708f1ab3f 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -202,6 +202,8 @@ impl 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 {