use mimalloc_pprof::sys;
#[test]
fn rezalloc_zeroes_past_the_old_usable_size() {
unsafe {
let n_old = 64usize;
let p = sys::mi_malloc(n_old).cast::<u8>();
assert!(!p.is_null());
let usable_old = mimalloc_pprof::usable_size(p);
assert!(usable_old >= n_old);
std::ptr::write_bytes(p, 0xA5, usable_old);
let n_new = usable_old + 4096; let q = mimalloc_pprof::rezalloc(p, n_new);
assert!(!q.is_null());
for i in usable_old..n_new {
assert_eq!(
*q.add(i),
0,
"byte {i} past the old usable size was not zeroed"
);
}
sys::mi_free(q.cast());
}
}
#[test]
fn recalloc_matches_rezalloc_for_the_grown_tail() {
unsafe {
let p = sys::mi_malloc(32).cast::<u8>();
assert!(!p.is_null());
let usable_old = mimalloc_pprof::usable_size(p);
std::ptr::write_bytes(p, 0x5A, usable_old);
let count = 64usize;
let each = 64usize;
assert!(count * each > usable_old);
let q = mimalloc_pprof::recalloc(p, count, each);
assert!(!q.is_null());
for i in usable_old..(count * each) {
assert_eq!(*q.add(i), 0, "byte {i} was not zeroed by recalloc");
}
sys::mi_free(q.cast());
}
}
#[test]
fn expand_never_moves_and_leaves_p_valid_on_failure() {
unsafe {
let p = sys::mi_malloc(64).cast::<u8>();
assert!(!p.is_null());
*p = 0x42;
let q = mimalloc_pprof::expand(p, 1 << 40);
assert!(
q.is_null(),
"expand must return null rather than move the block"
);
assert_eq!(*p, 0x42);
let usable = mimalloc_pprof::usable_size(p);
let r = mimalloc_pprof::expand(p, usable);
if !r.is_null() {
assert_eq!(r, p, "expand must not move the block");
}
sys::mi_free(p.cast());
}
}