From c583719ad04e037339fa1085b5fc37a0ac3bdab9 Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Wed, 31 May 2023 11:02:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=A8=E6=80=81=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=88=86=E9=85=8D=E5=A4=B1=E8=B4=A5=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch4/os/src/main.rs | 1 + ch4/os/src/mm/heap_allocator.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) 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); +}