Skip to content

Commit 1ab8175

Browse files
committed
Add MapFlags::map_hugetlb_with_size_log2
1 parent 2f4861f commit 1ab8175

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/sys/mman.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use crate::Result;
88
#[cfg(feature = "fs")]
99
use crate::{fcntl::OFlag, sys::stat::Mode};
1010
use libc::{self, c_int, c_void, off_t, size_t};
11-
use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}};
11+
use std::{
12+
num::NonZeroUsize,
13+
os::unix::io::{AsFd, AsRawFd},
14+
};
1215

1316
libc_bitflags! {
1417
/// Desired memory protection of a memory mapping.
@@ -187,6 +190,31 @@ libc_bitflags! {
187190
}
188191
}
189192

193+
impl MapFlags {
194+
/// Create `MAP_HUGETLB` with provided size of huge page.
195+
///
196+
/// Under the hood it computes `MAP_HUGETLB | (huge_page_size_log2 << libc::MAP_HUGE_SHIFT`).
197+
/// `huge_page_size_log2` denotes logarithm of huge page size to use and should be
198+
/// between 16 and 63 (inclusively).
199+
///
200+
/// ```
201+
/// # use nix::sys::mman::MapFlags;
202+
/// let f = MapFlags::map_hugetlb_with_size_log2(30).unwrap();
203+
/// assert_eq!(f, MapFlags::MAP_HUGETLB | MapFlags::MAP_HUGE_1GB);
204+
/// ```
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+
190218
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
191219
libc_bitflags! {
192220
/// Options for [`mremap`].

0 commit comments

Comments
 (0)