GlobalAlloc trait — custom memory allocator.
Implementation:
1use std::alloc::{GlobalAlloc, Layout};23struct MyAllocator;45unsafe impl GlobalAlloc for MyAllocator {6 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {7 libc::malloc(layout.size())8 }910 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {11 libc::free(ptr)12 }13}1415#[global_allocator]16static ALLOCATOR: MyAllocator = MyAllocator;
Use cases:
Key: Implement GlobalAlloc trait.