use tracking_allocator::{
AllocationGroupId, AllocationGroupToken, AllocationRegistry, AllocationTracker, Allocator,
};
use std::alloc::System;
#[global_allocator]
static GLOBAL: Allocator<System> = Allocator::system();
struct StdoutTracker;
impl AllocationTracker for StdoutTracker {
fn allocated(
&self,
addr: usize,
object_size: usize,
wrapped_size: usize,
group_id: AllocationGroupId,
) {
println!(
"allocation -> addr=0x{:0x} object_size={} wrapped_size={} group_id={:?}",
addr, object_size, wrapped_size, group_id
);
}
fn deallocated(
&self,
addr: usize,
object_size: usize,
wrapped_size: usize,
source_group_id: AllocationGroupId,
current_group_id: AllocationGroupId,
) {
println!(
"deallocation -> addr=0x{:0x} object_size={} wrapped_size={} source_group_id={:?} current_group_id={:?}",
addr, object_size, wrapped_size, source_group_id, current_group_id
);
}
}
fn main() {
let _ = AllocationRegistry::set_global_tracker(StdoutTracker)
.expect("no other global tracker should be set yet");
AllocationRegistry::enable_tracking();
let mut local_token =
AllocationGroupToken::register().expect("failed to register allocation group");
let local_guard = local_token.enter();
let s = String::from("Hello world!");
let mut v = Vec::new();
v.push(s);
drop(local_guard);
drop(v);
AllocationRegistry::disable_tracking();
}