Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions snmalloc-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,40 +233,4 @@ mod tests {
assert!(usz >= 8);
}
}

#[test]
fn test_memory_stats() {
let alloc = SnMalloc::new();
let before = SnMalloc::memory_stats();

let layout = Layout::from_size_align(1 << 20, 64).unwrap();
let ptr = unsafe { alloc.alloc(layout) };
assert!(!ptr.is_null());

let during = SnMalloc::memory_stats();
assert!(
during.current_memory_usage > 0,
"current usage should be non-zero after allocation"
);
assert!(
during.current_memory_usage >= before.current_memory_usage,
"current usage should not decrease after allocation"
);
assert!(
during.peak_memory_usage >= before.peak_memory_usage,
"peak usage should not decrease after allocation"
);

unsafe { alloc.dealloc(ptr, layout) };

let after = SnMalloc::memory_stats();
assert!(
after.current_memory_usage <= during.current_memory_usage,
"current usage should decrease after dealloc"
);
assert!(
after.peak_memory_usage >= during.peak_memory_usage,
"peak usage should never decrease"
);
}
}
47 changes: 47 additions & 0 deletions snmalloc-rs/tests/memory_stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! `memory_stats` reads a process-global atomic counter that every
//! allocation in the process feeds into. Running this assertion in the
//! same binary as other allocating tests (which cargo runs in parallel
//! threads by default) makes the `after <= during` check racy: another
//! test thread can pull a fresh chunk from the backend between the two
//! snapshots and push `current_memory_usage` up. Cargo runs each file
//! under `tests/` as its own binary in its own process, giving us the
//! isolation this test requires.

use snmalloc_rs::SnMalloc;
use std::alloc::{GlobalAlloc, Layout};

#[test]
fn test_memory_stats() {
let alloc = SnMalloc::new();
let before = SnMalloc::memory_stats();

let layout = Layout::from_size_align(1 << 20, 64).unwrap();
let ptr = unsafe { alloc.alloc(layout) };
assert!(!ptr.is_null());

let during = SnMalloc::memory_stats();
assert!(
during.current_memory_usage > 0,
"current usage should be non-zero after allocation"
);
assert!(
during.current_memory_usage >= before.current_memory_usage,
"current usage should not decrease after allocation"
);
assert!(
during.peak_memory_usage >= before.peak_memory_usage,
"peak usage should not decrease after allocation"
);

unsafe { alloc.dealloc(ptr, layout) };

let after = SnMalloc::memory_stats();
assert!(
after.current_memory_usage <= during.current_memory_usage,
"current usage should decrease after dealloc"
);
assert!(
after.peak_memory_usage >= during.peak_memory_usage,
"peak usage should never decrease"
);
}
Loading