mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-02-09 21:25:59 +08:00
27 lines
694 B
Rust
27 lines
694 B
Rust
//! The global allocator
|
|
|
|
use crate::config::KERNEL_HEAP_SIZE;
|
|
use buddy_system_allocator::LockedHeap;
|
|
|
|
#[global_allocator]
|
|
/// heap allocator instance
|
|
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
|
|
|
/// heap space ([u8; KERNEL_HEAP_SIZE])
|
|
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
|
|
|
|
/// initiate heap allocator
|
|
pub fn init_heap() {
|
|
unsafe {
|
|
HEAP_ALLOCATOR
|
|
.lock()
|
|
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
|
|
}
|
|
}
|
|
|
|
#[alloc_error_handler]
|
|
/// panic when heap allocation error occurs
|
|
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
|
|
panic!("Heap allocation error, layout = {:?}", layout);
|
|
}
|