Trait GlobalHeapAllocator

Source
pub trait GlobalHeapAllocator: Sync {
    // Required methods
    fn alloc(&self, layout: Layout) -> Result<HeapSlot, AllocError>;
    fn dealloc(&self, slot: HeapSlot) -> Result<(), AllocError>;
}
Expand description

The trait for the global heap allocator.

By providing the slab (Slab) and heap slot (HeapSlot) mechanisms, OSTD allows users to implement their own kernel heap in a safe manner, as an alternative to the unsafe core::alloc::GlobalAlloc.

To provide the global heap allocator, use crate::global_heap_allocator to mark a static variable that implements this trait. Use crate::global_heap_allocator_slot_map to specify the sizes of slots for different layouts. This latter restriction may be lifted in the future.

Required Methods§

Source

fn alloc(&self, layout: Layout) -> Result<HeapSlot, AllocError>

Allocates a HeapSlot according to the layout.

OSTD calls this method to allocate memory from the global heap.

The returned HeapSlot must be valid for the layout, i.e., the size must be at least the size of the layout and the alignment must be at least the alignment of the layout. Furthermore, the size of the returned HeapSlot must match the size returned by the function marked with crate::global_heap_allocator_slot_map.

Source

fn dealloc(&self, slot: HeapSlot) -> Result<(), AllocError>

Deallocates a HeapSlot.

OSTD calls this method to deallocate memory back to the global heap.

Each deallocation must correspond to exactly one previous allocation. The provided HeapSlot must match the one returned from the original allocation.

Implementors§