Skip to content

Commit a477541

Browse files
committed
mman module netbsd additions.
1 parent bf4f273 commit a477541

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/sys/mman.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,22 @@ libc_bitflags!{
139139
}
140140
}
141141

142-
#[cfg(target_os = "linux")]
142+
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
143143
libc_bitflags!{
144144
/// Options for `mremap()`.
145145
pub struct MRemapFlags: c_int {
146146
/// Permit the kernel to relocate the mapping to a new virtual address, if necessary.
147+
#[cfg(target_os = "linux")]
147148
MREMAP_MAYMOVE;
148149
/// Place the mapping at exactly the address specified in `new_address`.
150+
#[cfg(target_os = "linux")]
149151
MREMAP_FIXED;
152+
/// Permits to use the old and new address as hints to relocate the mapping.
153+
#[cfg(target_os = "netbsd")]
154+
MAP_FIXED;
155+
/// Allows to duplicate the mapping to be able to apply different flags on the copy.
156+
#[cfg(target_os = "netbsd")]
157+
MAP_REMAPDUP;
150158
}
151159
}
152160

@@ -334,15 +342,24 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma
334342
///
335343
/// See the `mremap(2)` [man page](https://man7.org/linux/man-pages/man2/mremap.2.html) for
336344
/// detailed requirements.
337-
#[cfg(target_os = "linux")]
345+
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
338346
pub unsafe fn mremap(
339347
addr: *mut c_void,
340348
old_size: size_t,
341349
new_size: size_t,
342350
flags: MRemapFlags,
343351
new_address: Option<* mut c_void>,
344352
) -> Result<*mut c_void> {
353+
#[cfg(target_os = "linux")]
345354
let ret = libc::mremap(addr, old_size, new_size, flags.bits(), new_address.unwrap_or(std::ptr::null_mut()));
355+
#[cfg(target_os = "netbsd")]
356+
let ret = libc::mremap(
357+
addr,
358+
old_size,
359+
new_address.unwrap_or(std::ptr::null_mut()),
360+
new_size,
361+
flags.bits(),
362+
);
346363

347364
if ret == libc::MAP_FAILED {
348365
Err(Error::from(Errno::last()))

test/sys/test_mman.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn test_mmap_anonymous() {
2020
}
2121

2222
#[test]
23-
#[cfg(target_os = "linux")]
23+
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
2424
fn test_mremap_grow() {
2525
const ONE_K : size_t = 1024;
2626
let slice : &mut[u8] = unsafe {
@@ -35,9 +35,14 @@ fn test_mremap_grow() {
3535
assert_eq !(slice[ONE_K - 1], 0xFF);
3636

3737
let slice : &mut[u8] = unsafe {
38+
#[cfg(target_os = "linux")]
3839
let mem = mremap(slice.as_mut_ptr() as * mut c_void, ONE_K, 10 * ONE_K,
3940
MRemapFlags::MREMAP_MAYMOVE, None)
4041
.unwrap();
42+
#[cfg(target_os = "netbsd")]
43+
let mem = mremap(slice.as_mut_ptr() as * mut c_void, ONE_K, 10 * ONE_K,
44+
MRemapFlags::MAP_REMAPDUP, None)
45+
.unwrap();
4146
std::slice::from_raw_parts_mut(mem as * mut u8, 10 * ONE_K)
4247
};
4348

@@ -51,7 +56,7 @@ fn test_mremap_grow() {
5156
}
5257

5358
#[test]
54-
#[cfg(target_os = "linux")]
59+
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
5560
fn test_mremap_shrink() {
5661
const ONE_K : size_t = 1024;
5762
let slice : &mut[u8] = unsafe {
@@ -66,11 +71,16 @@ fn test_mremap_shrink() {
6671
assert_eq !(slice[ONE_K - 1], 0xFF);
6772

6873
let slice : &mut[u8] = unsafe {
74+
#[cfg(target_os = "linux")]
6975
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
7076
MRemapFlags::empty(), None)
7177
.unwrap();
7278
// Since we didn't supply MREMAP_MAYMOVE, the address should be the
7379
// same.
80+
#[cfg(target_os = "linux")]
81+
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
82+
MRemapFlags::MAP_FIXED), None)
83+
.unwrap();
7484
assert_eq !(mem, slice.as_mut_ptr() as * mut c_void);
7585
std::slice::from_raw_parts_mut(mem as * mut u8, ONE_K)
7686
};

0 commit comments

Comments
 (0)