Skip to content

Commit ffaa1a9

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 39ad47b commit ffaa1a9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
4848
([#2097](https://github.com/nix-rust/nix/pull/2097))
4949
- Add the ability to set `kevent_flags` on `SigEvent`.
5050
([#1731](https://github.com/nix-rust/nix/pull/1731))
51+
([#2088](https://github.com/nix-rust/nix/pull/2088))
52+
- Removed `flock` from `::nix::fcntl` on Solaris. ([#2082](https://github.com/nix-rust/nix/pull/2082))
5153

5254
### Changed
5355

src/sys/mman.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ libc_bitflags! {
154154
/// Pages will be discarded in the core dumps.
155155
#[cfg(target_os = "openbsd")]
156156
MAP_CONCEAL;
157+
/// Pages aligned on 64kb
158+
#[cfg(target_os = "netbsd")]
159+
#[cfg_attr(docsrs, doc(cfg(all())))]
160+
MAP_ALIGNMENT_64KB;
161+
/// Pages aligned on 16mb
162+
#[cfg(target_os = "netbsd")]
163+
#[cfg_attr(docsrs, doc(cfg(all())))]
164+
MAP_ALIGNMENT_16MB;
165+
/// Pages aligned on 4gb
166+
#[cfg(target_os = "netbsd")]
167+
#[cfg_attr(docsrs, doc(cfg(all())))]
168+
MAP_ALIGNMENT_4GB;
169+
/// Pages aligned on 1tb
170+
#[cfg(target_os = "netbsd")]
171+
#[cfg_attr(docsrs, doc(cfg(all())))]
172+
MAP_ALIGNMENT_1TB;
173+
/// Pages aligned on 256tb
174+
#[cfg(target_os = "netbsd")]
175+
#[cfg_attr(docsrs, doc(cfg(all())))]
176+
MAP_ALIGNMENT_256TB;
177+
/// Pages aligned on 64pb
178+
#[cfg(target_os = "netbsd")]
179+
#[cfg_attr(docsrs, doc(cfg(all())))]
180+
MAP_ALIGNMENT_64PB;
181+
/// Right operand value for the page alignment bitshift calculation
182+
#[cfg(target_os = "netbsd")]
183+
MAP_ALIGNMENT_SHIFT;
184+
/// Mask to get the page alignment (as `(flags & align mask) >> align shift`)
185+
#[cfg(target_os = "netbsd")]
186+
MAP_ALIGNMENT_MASK;
157187
}
158188
}
159189

@@ -631,3 +661,23 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
631661

632662
Errno::result(ret).map(drop)
633663
}
664+
665+
/// Matches BSD's `MAP_ALIGNED(x)` macro, x being ilog2(alignment).
666+
///
667+
/// For more information, see [`mmap(2)`].
668+
///
669+
/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2)
670+
#[cfg(target_os = "netbsd")]
671+
pub const fn map_aligned(v: u32) -> u32 {
672+
v << MapFlags::MAP_ALIGNMENT_SHIFT.bits()
673+
}
674+
675+
/// Handy call to get the alignment set by `map_aligned`.
676+
///
677+
/// For more information, see [`mmap(2)`].
678+
///
679+
/// [`mmap(2)`]: https://man.freebsd.org/cgi/man.cgi?mmap(2)
680+
#[cfg(target_os = "netbsd")]
681+
pub const fn map_alignment(flags: u32) -> u32 {
682+
(flags & MapFlags::MAP_ALIGNMENT_MASK.bits() as u32) >> MapFlags::MAP_ALIGNMENT_SHIFT.bits()
683+
}

test/sys/test_mman.rs

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

0 commit comments

Comments
 (0)