1 #![feature(allocator_api, slice_ptr_get)]
3 use std::alloc::{Allocator as _, Global, GlobalAlloc, Layout, System};
6 static ALLOCATOR: Allocator = Allocator;
10 unsafe impl GlobalAlloc for Allocator {
11 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
12 // use specific size to avoid getting triggered by rt
13 if layout.size() == 123 {
14 println!("Allocated!")
20 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
21 if layout.size() == 123 {
22 println!("Dellocated!")
25 System.dealloc(ptr, layout)
30 // Only okay because we explicitly set a global allocator that uses the system allocator!
31 let l = Layout::from_size_align(123, 1).unwrap();
32 let ptr = Global.allocate(l).unwrap().as_non_null_ptr(); // allocating with Global...
34 System.deallocate(ptr, l);
35 } // ... and deallocating with System.
37 let ptr = System.allocate(l).unwrap().as_non_null_ptr(); // allocating with System...
39 Global.deallocate(ptr, l);
40 } // ... and deallocating with Global.