os/src/heap.rs

40 lines
1.1 KiB
Rust

use core::alloc::Layout;
use linked_list_allocator::LockedHeap;
use x86_64::{
structures::paging::{Page, PageTableFlags},
VirtAddr,
};
use crate::{mem::map_next, assert_canonical};
// address range is 0x4ea900000000 to 0x4ea9000fffff
static HEAP_START: u64 = 0x_4ea9_0000_0000; // '4ea9' is supposed to be 'heap', so its easy to spot
static HEAP_SIZE: u64 = 1024 * 1024; // 1 MebiByte or 0xfffff
#[global_allocator]
static ALLOC: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
fn alloc_error_handler(layout: Layout) -> ! {
panic!("Alloc error: {:#?}", layout)
}
pub fn init() {
assert_canonical(HEAP_START);
let page_start = Page::containing_address(VirtAddr::new(HEAP_START));
let page_end = Page::containing_address(VirtAddr::new(HEAP_START + HEAP_SIZE - 1));
let page_range = Page::range_inclusive(page_start, page_end);
for page in page_range {
map_next(page, PageTableFlags::PRESENT | PageTableFlags::WRITABLE)
.unwrap()
.flush();
}
unsafe {
ALLOC.lock().init(HEAP_START as usize, HEAP_SIZE as usize);
}
}