1use crate::config::KERNEL_HEAP_SIZE;
3use buddy_system_allocator::LockedHeap;
4use core::ptr::addr_of_mut;
5
6#[global_allocator]
7static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
9
10#[alloc_error_handler]
11pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
13 panic!("Heap allocation error, layout = {:?}", layout);
14}
15static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
17pub fn init_heap() {
19 unsafe {
20 HEAP_ALLOCATOR
21 .lock()
22 .init(addr_of_mut!(HEAP_SPACE) as usize, KERNEL_HEAP_SIZE);
23 }
24}
25
26#[allow(unused)]
27pub fn heap_test() {
28 use alloc::boxed::Box;
29 use alloc::vec::Vec;
30 unsafe extern "C" {
31 safe fn sbss();
32 safe fn ebss();
33 }
34 let bss_range = sbss as usize..ebss as usize;
35 let a = Box::new(5);
36 assert_eq!(*a, 5);
37 assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
38 drop(a);
39 let mut v: Vec<usize> = Vec::new();
40 for i in 0..500 {
41 v.push(i);
42 }
43 for (i, val) in v.iter().take(500).enumerate() {
44 assert_eq!(*val, i);
45 }
46 assert!(bss_range.contains(&(v.as_ptr() as usize)));
47 drop(v);
48 println!("heap_test passed!");
49}