| #![feature(allocator_api, slice_ptr_get)] |
| use std::alloc::{Allocator as _, Global, GlobalAlloc, Layout, System}; |
| static ALLOCATOR: Allocator = Allocator; |
| unsafe impl GlobalAlloc for Allocator { |
| unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| // use specific size to avoid getting triggered by rt |
| if layout.size() == 123 { |
| unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| if layout.size() == 123 { |
| System.dealloc(ptr, layout) |
| // Only okay because we explicitly set a global allocator that uses the system allocator! |
| let l = Layout::from_size_align(123, 1).unwrap(); |
| let ptr = Global.allocate(l).unwrap().as_non_null_ptr(); // allocating with Global... |
| System.deallocate(ptr, l); |
| } // ... and deallocating with System. |
| let ptr = System.allocate(l).unwrap().as_non_null_ptr(); // allocating with System... |
| Global.deallocate(ptr, l); |
| } // ... and deallocating with Global. |