diff --git a/ch4/os/src/main.rs b/ch4/os/src/main.rs index 0982f5f..eb00600 100644 --- a/ch4/os/src/main.rs +++ b/ch4/os/src/main.rs @@ -1,4 +1,5 @@ #![feature(panic_info_message)] +#![feature(alloc_error_handler)] #![no_std] #![no_main] diff --git a/ch4/os/src/mm/heap_allocator.rs b/ch4/os/src/mm/heap_allocator.rs index dc073b8..369aa0d 100644 --- a/ch4/os/src/mm/heap_allocator.rs +++ b/ch4/os/src/mm/heap_allocator.rs @@ -1,3 +1,4 @@ + use alloc::boxed::Box; use alloc::vec; use buddy_system_allocator::LockedHeap; @@ -9,6 +10,8 @@ use crate::println; #[global_allocator] static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty(); +// 内存分配失败时, 会调用此错误处理函数 + // 堆的空间 static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE]; @@ -56,4 +59,9 @@ pub fn heap_test() { // 通过输出打印, 发现 a 和b 均被分配在了 HEAP_SPACE 中, 且 HEAP_SPACE 被分配在了 bss段中, 这个段由我们 linker.ld 进行手动布局 // 目前整个bss段的大小都是3M, 说明当前bss段中只有这一个 数据 -} \ No newline at end of file +} + +#[alloc_error_handler] +pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! { + panic!("Heap allocation error, layout = {:?}", layout); +}