Skip to content

Commit 204de8d

Browse files
committed
mman MapFlags update for FreeBSD/NetBSD regarding page alignment
Related to page alignment, adding map_aligned for custom alignment requirements.
1 parent ee91423 commit 204de8d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/sys/mman.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,40 @@ libc_bitflags! {
184184
#[cfg(target_os = "openbsd")]
185185
#[cfg_attr(docsrs, doc(cfg(all())))]
186186
MAP_CONCEAL;
187+
/// Pages aligned on 64kb
188+
#[cfg(target_os = "netbsd")]
189+
#[cfg_attr(docsrs, doc(cfg(all())))]
190+
MAP_ALIGNMENT_64KB;
191+
/// Pages aligned on 16mb
192+
#[cfg(target_os = "netbsd")]
193+
#[cfg_attr(docsrs, doc(cfg(all())))]
194+
MAP_ALIGNMENT_16MB;
195+
/// Pages aligned on 4gb
196+
#[cfg(target_os = "netbsd")]
197+
#[cfg_attr(docsrs, doc(cfg(all())))]
198+
MAP_ALIGNMENT_4GB;
199+
/// Pages aligned on 1tb
200+
#[cfg(target_os = "netbsd")]
201+
#[cfg_attr(docsrs, doc(cfg(all())))]
202+
MAP_ALIGNMENT_1TB;
203+
/// Pages aligned on 256tb
204+
#[cfg(target_os = "netbsd")]
205+
#[cfg_attr(docsrs, doc(cfg(all())))]
206+
MAP_ALIGNMENT_256TB;
207+
/// Pages aligned on 64pb
208+
#[cfg(target_os = "netbsd")]
209+
#[cfg_attr(docsrs, doc(cfg(all())))]
210+
MAP_ALIGNMENT_64PB;
211+
/// Right operand value for the page alignment bitshift calculation
212+
/// FIXME: not present in libc for FreeBSD
213+
#[cfg(target_os = "netbsd")]
214+
#[cfg_attr(docsrs, doc(cfg(all())))]
215+
MAP_ALIGNMENT_SHIFT;
216+
/// Mask to get the page alignment (as `(flags & align mask) >> align shift`)
217+
#[cfg(target_os = "netbsd")]
218+
/// FIXME: not present in libc for FreeBSD
219+
#[cfg_attr(docsrs, doc(cfg(all())))]
220+
MAP_ALIGNMENT_MASK;
187221
}
188222
}
189223

@@ -605,3 +639,23 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
605639

606640
Errno::result(ret).map(drop)
607641
}
642+
643+
/// Matches BSD's `MAP_ALIGNED(x)` macro, x being ilog2(alignment).
644+
///
645+
/// For more information, see [`mmap(2)`].
646+
///
647+
/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2)
648+
#[cfg(target_os = "netbsd")]
649+
pub const fn map_aligned(v: u32) -> u32 {
650+
v << MapFlags::MAP_ALIGNMENT_SHIFT.bits()
651+
}
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

Lines changed: 11 additions & 0 deletions
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)