为内核添加动态内存分配器
parent
8066fafe5d
commit
0e1724297a
@ -1,8 +1,9 @@
|
|||||||
pub const APP_BASE_ADDRESS: usize = 0x80400000; // 载入的app的起始的地址
|
pub const APP_BASE_ADDRESS: usize = 0x80400000; // 载入的app的起始的地址
|
||||||
pub const APP_SIZE_LIMIT: usize = 0x20000; // app的最大的二进制文件能够使用的大小
|
pub const APP_SIZE_LIMIT: usize = 0x20000; // app的最大的二进制文件能够使用的大小
|
||||||
pub const MAX_APP_NUM: usize = 10; // 支持最大的用户应用数量
|
pub const MAX_APP_NUM: usize = 10; // 支持最大的用户应用数量
|
||||||
|
pub const KERNEL_HEAP_SIZE: usize = 0x30_0000; // 内核的堆大小 3M
|
||||||
|
|
||||||
pub const USER_STACK_SIZE: usize = 4096 * 2; // 栈大小为8kb
|
pub const USER_STACK_SIZE: usize = 4096 * 2; // 每个应用用户态的栈大小为8kb
|
||||||
pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
|
pub const KERNEL_STACK_SIZE: usize = 4096 * 2; // 每个应用的内核栈
|
||||||
|
|
||||||
pub use crate::board::*;
|
pub use crate::board::*;
|
@ -0,0 +1,59 @@
|
|||||||
|
use alloc::boxed::Box;
|
||||||
|
use alloc::vec;
|
||||||
|
use buddy_system_allocator::LockedHeap;
|
||||||
|
use crate::config::KERNEL_HEAP_SIZE;
|
||||||
|
use crate::println;
|
||||||
|
|
||||||
|
|
||||||
|
// 动态内存分配器
|
||||||
|
#[global_allocator]
|
||||||
|
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||||
|
|
||||||
|
// 堆的空间
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
// heap_test()
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
println!("bss_range {:?}", bss_range);
|
||||||
|
|
||||||
|
let a = Box::new(5);
|
||||||
|
println!("a: {:?} ptr: {:p}", a, a.as_ref());
|
||||||
|
let b = Box::new(6);
|
||||||
|
println!("b: {:?} ptr: {:p}", b, b.as_ref());
|
||||||
|
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
println!("HEAP_SPACE: {:?}", HEAP_SPACE.as_ptr())
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut v = vec![];
|
||||||
|
for i in 0..500{
|
||||||
|
v.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..500{
|
||||||
|
v.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?}", v);
|
||||||
|
|
||||||
|
// 通过输出打印, 发现 a 和b 均被分配在了 HEAP_SPACE 中, 且 HEAP_SPACE 被分配在了 bss段中, 这个段由我们 linker.ld 进行手动布局
|
||||||
|
// 目前整个bss段的大小都是3M, 说明当前bss段中只有这一个 数据
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
pub mod heap_allocator;
|
Loading…
Reference in New Issue