let slice: &mut [u8] = unsafe { #[cfg(target_os = "linux")] let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ONE_K, 10 * ONE_K,
MRemapFlags::MREMAP_MAYMOVE,
None,
)
.unwrap(); #[cfg(target_os = "netbsd")] let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ONE_K, 10 * ONE_K,
MRemapFlags::MAP_REMAPDUP,
None,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.cast().as_ptr(), 10 * ONE_K)
};
// The first KB should still have the old data in it.
assert_eq!(slice[ONE_K - 1], 0xFF);
// The additional range should be zero-init'd and accessible.
assert_eq!(slice[10 * ONE_K - 1], 0x00);
slice[10 * ONE_K - 1] = 0xFF;
assert_eq!(slice[10 * ONE_K - 1], 0xFF);
}
#[test] #[cfg(any(target_os = "linux", target_os = "netbsd"))] // Segfaults for unknown reasons under QEMU for 32-bit targets #[cfg_attr(all(target_pointer_width = "32", qemu), ignore)] fn test_mremap_shrink() { use nix::libc::size_t; use nix::sys::mman::{mremap, MRemapFlags}; use std::num::NonZeroUsize; use std::ptr::NonNull;
let slice: &mut [u8] = unsafe { let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ten_one_k.into(),
ONE_K,
MRemapFlags::empty(),
None,
)
.unwrap(); // Since we didn't supply MREMAP_MAYMOVE, the address should be the // same.
assert_eq!(mem.as_ptr(), NonNull::from(&mut slice[..]).cast().as_ptr());
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
// The first KB should still be accessible and have the old data in it.
assert_eq!(slice[ONE_K - 1], 0xFF);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.