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 Aug 7, 2023
1 parent ee91423 commit 204de8d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/sys/mman.rs
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -605,3 +639,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 @@ -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);
}

0 comments on commit 204de8d

Please sign in to comment.