From ffaa1a9d184b6c9e863f0fe609dcc6cb053b023f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 7 Aug 2023 11:18:02 +0100 Subject: [PATCH] mman MapFlags update for FreeBSD/NetBSD regarding page alignment Related to page alignment, adding map_aligned for custom alignment requirements. --- CHANGELOG.md | 2 ++ src/sys/mman.rs | 50 +++++++++++++++++++++++++++++++++++++++++++ test/sys/test_mman.rs | 11 ++++++++++ 3 files changed, 63 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 934d611e84..163e3ca2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ([#2097](https://github.com/nix-rust/nix/pull/2097)) - Add the ability to set `kevent_flags` on `SigEvent`. ([#1731](https://github.com/nix-rust/nix/pull/1731)) + ([#2088](https://github.com/nix-rust/nix/pull/2088)) +- Removed `flock` from `::nix::fcntl` on Solaris. ([#2082](https://github.com/nix-rust/nix/pull/2082)) ### Changed diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 6247ea4e17..1cc4d2ced6 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -154,6 +154,36 @@ libc_bitflags! { /// Pages will be discarded in the core dumps. #[cfg(target_os = "openbsd")] 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 + #[cfg(target_os = "netbsd")] + MAP_ALIGNMENT_SHIFT; + /// Mask to get the page alignment (as `(flags & align mask) >> align shift`) + #[cfg(target_os = "netbsd")] + MAP_ALIGNMENT_MASK; } } @@ -631,3 +661,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 3689f642be..3f1f9c75e8 100644 --- a/test/sys/test_mman.rs +++ b/test/sys/test_mman.rs @@ -119,3 +119,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); +}