Skip to content

Commit 8713927

Browse files
committed
MapFlags netbsd/freebsd constant additions.
Related to page alignment, adding map_aligned for custom alignment requirements.
1 parent 688cfe2 commit 8713927

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/sys/mman.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,13 @@ libc_bitflags! {
209209
#[cfg_attr(docsrs, doc(cfg(all())))]
210210
MAP_ALIGNMENT_64PB;
211211
/// Right operand value for the page alignment bitshift calculation
212-
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
212+
/// FIXME: not present in libc for FreeBSD
213+
#[cfg(target_os = "netbsd")]
213214
#[cfg_attr(docsrs, doc(cfg(all())))]
214215
MAP_ALIGNMENT_SHIFT;
215216
/// Mask to get the page alignment (as `(flags & align mask) >> align shift`)
216-
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
217+
#[cfg(target_os = "netbsd")]
218+
/// FIXME: not present in libc for FreeBSD
217219
#[cfg_attr(docsrs, doc(cfg(all())))]
218220
MAP_ALIGNMENT_MASK;
219221
}
@@ -643,7 +645,17 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
643645
/// For more information, see [`mmap(2)`].
644646
///
645647
/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2)
646-
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
648+
#[cfg(target_os = "netbsd")]
647649
pub const fn map_aligned(v: u32) -> u32 {
648650
v << MapFlags::MAP_ALIGNMENT_SHIFT.bits()
649651
}
652+
653+
/// Handy call to get the alignment set by `map_aligned`.
654+
///
655+
/// For more information, see [`mmap(2)`].
656+
///
657+
/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2)
658+
#[cfg(target_os = "netbsd")]
659+
pub const fn map_alignment(flags: u32) -> u32 {
660+
(flags & MapFlags::MAP_ALIGNMENT_MASK.bits() as u32) >> MapFlags::MAP_ALIGNMENT_SHIFT.bits()
661+
}

test/sys/test_mman.rs

+11
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,14 @@ fn test_mremap_shrink() {
120120
// The first KB should still be accessible and have the old data in it.
121121
assert_eq!(slice[ONE_K - 1], 0xFF);
122122
}
123+
124+
#[test]
125+
#[cfg(target_os = "netbsd")]
126+
pub fn test_map_aligned() {
127+
use nix::sys::mman::{map_aligned, map_alignment};
128+
129+
let aligned = map_aligned(16);
130+
let flags = libc::MAP_PRIVATE as u32 | libc::MAP_ANONYMOUS as u32 | aligned;
131+
assert_eq!(aligned, libc::MAP_ALIGNED(16) as u32);
132+
assert_eq!(map_alignment(flags), 16);
133+
}

0 commit comments

Comments
 (0)