diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 8cfd6d6d54..0a51417bf0 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -184,6 +184,40 @@ libc_bitflags! { #[cfg(target_os = "openbsd")] #[cfg_attr(docsrs, doc(cfg(all())))] MAP_CONCEAL; + /// Pages aligned on 64kb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_64KB; + /// Pages aligned on 16mb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_16MB; + /// Pages aligned on 4gb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_4GB; + /// Pages aligned on 1tb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_1TB; + /// Pages aligned on 256tb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_256TB; + /// Pages aligned on 64pb + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_64PB; + /// Right operand value for the page alignment bitshift calculation + /// FIXME: not present in libc for FreeBSD + #[cfg(target_os = "netbsd")] + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_SHIFT; + /// Mask to get the page alignment (as `(flags & align mask) >> align shift`) + #[cfg(target_os = "netbsd")] + /// FIXME: not present in libc for FreeBSD + #[cfg_attr(docsrs, doc(cfg(all())))] + MAP_ALIGNMENT_MASK; } } @@ -605,3 +639,23 @@ pub fn shm_unlink(name: &P) -> Result<()> { Errno::result(ret).map(drop) } + +/// Matches BSD's `MAP_ALIGNED(x)` macro, x being ilog2(alignment). +/// +/// For more information, see [`mmap(2)`]. +/// +/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2) +#[cfg(target_os = "netbsd")] +pub const fn map_aligned(v: u32) -> u32 { + v << MapFlags::MAP_ALIGNMENT_SHIFT.bits() +} + +/// Handy call to get the alignment set by `map_aligned`. +/// +/// For more information, see [`mmap(2)`]. +/// +/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2) +#[cfg(target_os = "netbsd")] +pub const fn map_alignment(flags: u32) -> u32 { + (flags & MapFlags::MAP_ALIGNMENT_MASK.bits() as u32) >> MapFlags::MAP_ALIGNMENT_SHIFT.bits() +} diff --git a/test/sys/test_mman.rs b/test/sys/test_mman.rs index b4674e53fa..bdf6e3a288 100644 --- a/test/sys/test_mman.rs +++ b/test/sys/test_mman.rs @@ -120,3 +120,14 @@ fn test_mremap_shrink() { // The first KB should still be accessible and have the old data in it. assert_eq!(slice[ONE_K - 1], 0xFF); } + +#[test] +#[cfg(target_os = "netbsd")] +pub fn test_map_aligned() { + use nix::sys::mman::{map_aligned, map_alignment}; + + let aligned = map_aligned(16); + let flags = libc::MAP_PRIVATE as u32 | libc::MAP_ANONYMOUS as u32 | aligned; + assert_eq!(aligned, libc::MAP_ALIGNED(16) as u32); + assert_eq!(map_alignment(flags), 16); +}