use std::{
alloc::{GlobalAlloc, Layout, System},
cell::Cell,
};
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Allocations {
pub calls: usize,
pub bytes: usize,
}
thread_local! {
static COUNTS: Cell<Option<Allocations>> = const { Cell::new(None) };
}
struct Allocator;
fn record(bytes: usize) {
let _ = COUNTS.try_with(|counts| {
if let Some(mut value) = counts.get() {
value.calls += 1;
value.bytes += bytes;
counts.set(Some(value));
}
});
}
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc(layout) };
if !ptr.is_null() {
record(layout.size());
}
ptr
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ptr = unsafe { System.alloc_zeroed(layout) };
if !ptr.is_null() {
record(layout.size());
}
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) };
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 {
let ptr = unsafe { System.realloc(ptr, layout, size) };
if !ptr.is_null() {
record(size);
}
ptr
}
}
#[global_allocator]
static ALLOCATOR: Allocator = Allocator;
pub(crate) fn measure<R>(work: impl FnOnce() -> R) -> (R, Allocations) {
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
COUNTS.with(|counts| counts.set(None));
}
}
COUNTS.with(|counts| {
assert!(counts.get().is_none());
counts.set(Some(Allocations::default()));
});
let guard = Guard;
let result = work();
let counts = COUNTS.with(|counts| counts.get().unwrap());
drop(guard);
(result, counts)
}