Attribute Macro global_heap_allocator
#[global_heap_allocator]
Expand description
A macro attribute to register the global heap allocator.
The attributed static variable will be used to provide heap allocation for the kernel.
This attribute is not to be confused with Rust’s built-in
[global_allocator
] attribute, which applies to a static variable
implementing the unsafe GlobalAlloc
trait. In contrast, the
global_heap_allocator
attribute does not require the heap allocator to
implement an unsafe trait. global_heap_allocator
eventually relies on
[global_allocator
] to customize Rust’s heap allocator.
§Example
ⓘ
use core::alloc::{AllocError, Layout};
use ostd::{mm::heap::{GlobalHeapAllocator, HeapSlot}, global_heap_allocator};
// Of course it won't work and all allocations will fail.
// It's just an example.
#[global_heap_allocator]
static ALLOCATOR: MyHeapAllocator = MyHeapAllocator;
struct MyHeapAllocator;
impl GlobalHeapAllocator for MyHeapAllocator {
fn alloc(&self, _layout: Layout) -> Result<HeapSlot, AllocError> { None }
fn dealloc(&self, _slot: HeapSlot) -> Result<(), AllocError> {}
}