Created
June 26, 2026 04:59
-
-
Save savelichalex/4a73c23c3b443c2b766e1887568f105b to your computer and use it in GitHub Desktop.
ReportingAllocator from the book Rust in Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[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