Skip to content

Commit

Permalink
mman MapFlags update for FreeBSD/NetBSD regarding page alignment
Browse files Browse the repository at this point in the history
Related to page alignment, adding map_aligned for custom alignment
requirements.
  • Loading branch information
devnexen committed Nov 29, 2023
1 parent 39ad47b commit ffaa1a9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions src/sys/mman.rs
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -631,3 +661,23 @@ pub fn shm_unlink<P: ?Sized + NixPath>(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()
}
11 changes: 11 additions & 0 deletions test/sys/test_mman.rs
Expand Up @@ -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);
}

0 comments on commit ffaa1a9

Please sign in to comment.