Skip to content

Commit 3b16f0c

Browse files
authored
Add MapFlags::map_hugetlb_with_size_log2 (#2125)
* Add `MapFlags::map_hugetlb_with_size_log2` * Add changelog * Add TODO note
1 parent 1ea59c7 commit 3b16f0c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

changelog/2125.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `MapFlags::map_hugetlb_with_size_log2` method for Linux targets

src/sys/mman.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,33 @@ libc_bitflags! {
188188
}
189189
}
190190

191+
impl MapFlags {
192+
/// Create `MAP_HUGETLB` with provided size of huge page.
193+
///
194+
/// Under the hood it computes `MAP_HUGETLB | (huge_page_size_log2 << libc::MAP_HUGE_SHIFT`).
195+
/// `huge_page_size_log2` denotes logarithm of huge page size to use and should be
196+
/// between 16 and 63 (inclusively).
197+
///
198+
/// ```
199+
/// # use nix::sys::mman::MapFlags;
200+
/// let f = MapFlags::map_hugetlb_with_size_log2(30).unwrap();
201+
/// assert_eq!(f, MapFlags::MAP_HUGETLB | MapFlags::MAP_HUGE_1GB);
202+
/// ```
203+
// TODO: support Andorid and Fuchsia when https://github.com/rust-lang/libc/pull/3444
204+
// will be released
205+
#[cfg(target_os = "linux")]
206+
#[cfg_attr(docsrs, doc(cfg(all())))]
207+
pub fn map_hugetlb_with_size_log2(huge_page_size_log2: u32) -> Option<Self> {
208+
if (16..=63).contains(&huge_page_size_log2) {
209+
let flag = libc::MAP_HUGETLB
210+
| (huge_page_size_log2 << libc::MAP_HUGE_SHIFT) as i32;
211+
Some(Self(flag.into()))
212+
} else {
213+
None
214+
}
215+
}
216+
}
217+
191218
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
192219
libc_bitflags! {
193220
/// Options for [`mremap`].

0 commit comments

Comments
 (0)