alloc.rs
1 use std::alloc::{GlobalAlloc, Layout, System}; 2 use std::sync::atomic::{AtomicU64, Ordering}; 3 4 pub struct Allocator(System, AtomicU64); 5 6 unsafe impl GlobalAlloc for Allocator { 7 unsafe fn alloc(&self, l: Layout) -> *mut u8 { 8 self.1.fetch_add(l.size() as u64, Ordering::SeqCst); 9 self.0.alloc(l) 10 } 11 unsafe fn dealloc(&self, ptr: *mut u8, l: Layout) { 12 self.0.dealloc(ptr, l); 13 self.1.fetch_sub(l.size() as u64, Ordering::SeqCst); 14 } 15 } 16 17 impl Allocator { 18 pub const fn new(a: System) -> Self { 19 Allocator(a, AtomicU64::new(0)) 20 } 21 22 pub fn reset(&self) { 23 self.1.store(0, Ordering::SeqCst); 24 } 25 26 pub fn allocated(&self) -> u64 { 27 self.1.load(Ordering::SeqCst) 28 } 29 }