Skip to content

Instantly share code, notes, and snippets.

@savelichalex
Created June 26, 2026 04:59
Show Gist options
  • Select an option

  • Save savelichalex/4a73c23c3b443c2b766e1887568f105b to your computer and use it in GitHub Desktop.

Select an option

Save savelichalex/4a73c23c3b443c2b766e1887568f105b to your computer and use it in GitHub Desktop.
ReportingAllocator from the book Rust in Action
#[global_allocator]
static ALLOCATOR: ReportingAllocator = ReportingAllocator;
struct ReportingAllocator;
unsafe impl GlobalAlloc for ReportingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let start = Instant::now();
let ptr = System.alloc(layout);
let end = Instant::now();
let time_taken = end - start;
let bytes_requested = layout.size();
eprintln!("{}\t{}", bytes_requested, time_taken.as_nanos());
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment