use rusty_alloc::alloc::{collect, free, malloc, stats};
use rusty_alloc::types::SEGMENT_SLICE_SIZE;
const TWO_MIB: usize = 2 * SEGMENT_SLICE_SIZE;
#[test]
fn same_size_reuses_the_span_other_sizes_do_not_and_collect_flushes() {
let a = malloc(TWO_MIB);
assert!(!a.is_null());
unsafe { core::ptr::write_bytes(a, 0x5A, TWO_MIB) };
let retired = stats().pages_retired;
unsafe { free(a) };
assert_eq!(
stats().pages_retired,
retired,
"the freed large span was retired, not cached"
);
let b = malloc(TWO_MIB);
assert_eq!(
b, a,
"a same-size large allocation did not reuse the cached span"
);
unsafe { core::ptr::write_bytes(b, 0x11, TWO_MIB) };
unsafe { free(b) };
let c = malloc(TWO_MIB + SEGMENT_SLICE_SIZE);
assert!(!c.is_null());
assert_ne!(c, a, "a larger allocation was handed the cached 2 MiB span");
let d = malloc(TWO_MIB);
assert_eq!(
d, a,
"the cached span did not survive an allocation of another size"
);
unsafe { free(c) }; let retired = stats().pages_retired;
unsafe { free(d) }; assert_eq!(
stats().pages_retired - retired,
2,
"a run of large frees left a span cached"
);
let e = malloc(TWO_MIB);
assert!(!e.is_null());
unsafe { free(e) }; let retired = stats().pages_retired;
collect(false);
assert!(
stats().pages_retired > retired,
"collect did not retire the cached large span"
);
}