Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapFlags netbsd/freebsd constant additions. #2090

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/2090.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added `MAP_ALIGNMENT*` constants and `map_aligned` for NetBSD to `nix::sys::mman`.

50 changes: 50 additions & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
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())))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase and remove the #[cfg_attr(docsrs, doc(cfg(all())))] attributes from all the variants as we have removed them all from the codebase (#2192)

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
Original file line number Diff line number Diff line change
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);
}