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