Heap test passed on k210/qemu, heap size = 3M.

This commit is contained in:
Yifan Wu 2020-12-02 10:32:26 +08:00
parent 850559e5da
commit 528d99258a
7 changed files with 81 additions and 11 deletions

View file

@ -3,6 +3,7 @@ pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
pub const MAX_APP_NUM: usize = 4;
pub const APP_BASE_ADDRESS: usize = 0x80100000;
pub const APP_SIZE_LIMIT: usize = 0x20000;
pub const KERNEL_HEAP_SIZE: usize = 0x30_0000;
#[cfg(feature = "board_k210")]
pub const CPU_FREQ: usize = 10000000;

View file

@ -4,6 +4,9 @@
#![feature(llvm_asm)]
#![feature(panic_info_message)]
#![feature(const_in_array_repeat_expressions)]
#![feature(alloc_error_handler)]
extern crate alloc;
#[macro_use]
mod console;
@ -15,6 +18,7 @@ mod loader;
mod config;
mod task;
mod timer;
mod mm;
global_asm!(include_str!("entry.asm"));
global_asm!(include_str!("link_app.S"));
@ -33,6 +37,9 @@ fn clear_bss() {
pub fn rust_main() -> ! {
clear_bss();
println!("[kernel] Hello, world!");
mm::init_heap();
mm::heap_test();
loop {}
trap::init();
loader::load_apps();
trap::enable_interrupt();

View file

@ -0,0 +1,45 @@
use buddy_system_allocator::LockedHeap;
use crate::config::KERNEL_HEAP_SIZE;
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
panic!("Heap allocation error, layout = {:?}", layout);
}
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
pub fn init_heap() {
unsafe {
HEAP_ALLOCATOR
.lock()
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
}
}
#[allow(unused)]
pub fn heap_test() {
use alloc::boxed::Box;
use alloc::vec::Vec;
extern "C" {
fn sbss();
fn ebss();
}
let bss_range = sbss as usize..ebss as usize;
let a = Box::new(5);
assert_eq!(*a, 5);
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
drop(a);
let mut v: Vec<usize> = Vec::new();
for i in 0..500 {
v.push(i);
}
for i in 0..500 {
assert_eq!(v[i], i);
}
assert!(bss_range.contains(&(v.as_ptr() as usize)));
drop(v);
println!("heap_test passed!");
}

4
os/src/mm/mod.rs Normal file
View file

@ -0,0 +1,4 @@
mod heap_allocator;
pub use heap_allocator::init_heap;
pub use heap_allocator::heap_test;