Loading RustViz Engine...
Implement GlobalAlloc counting live allocated heap bytes.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
pub struct TracingAlloc {
allocated: AtomicUsize,
}
unsafe impl GlobalAlloc for TracingAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.allocated.fetch_add(layout.size(), Ordering::SeqCst);
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.allocated.fetch_sub(layout.size(), Ordering::SeqCst);
System.dealloc(ptr, layout);
}
}