diff options
| author | Natasha Moongrave <natasha@256phi.eu> | 2026-03-30 14:43:34 +0200 |
|---|---|---|
| committer | Natasha Moongrave <natasha@256phi.eu> | 2026-03-30 14:43:34 +0200 |
| commit | edbf1bdd217cdc905211b90d231e7695c253d73c (patch) | |
| tree | c4646f4278f19cb148680b390d1fc84696ecec86 | |
| parent | 781fe28b327ed2610d3d761fbceccab953aeeb33 (diff) | |
create the allocator
| -rw-r--r-- | StrixKernel/src/allocator.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/StrixKernel/src/allocator.rs b/StrixKernel/src/allocator.rs new file mode 100644 index 0000000..7792666 --- /dev/null +++ b/StrixKernel/src/allocator.rs @@ -0,0 +1,54 @@ +use alloc::alloc::{GlobalAlloc, Layout}; +use core::ptr::null_mut; +use linked_list_allocator::LockedHeap; +use x86_64::{ + VirtAddr, + structures::paging::{ + FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError, + }, +}; + +pub const HEAP_START: usize = 0x_4444_4444_0000; +pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB + +#[global_allocator] +static ALLOCATOR: LockedHeap = LockedHeap::empty(); + +pub fn init_heap( + mapper: &mut impl Mapper<Size4KiB>, + frame_allocator: &mut impl FrameAllocator<Size4KiB>, +) -> Result<(), MapToError<Size4KiB>> { + let page_range = { + let heap_start = VirtAddr::new(HEAP_START as u64); + let heap_end = heap_start + HEAP_SIZE - 1u64; + let heap_start_page = Page::containing_address(heap_start); + let heap_end_page = Page::containing_address(heap_end); + Page::range_inclusive(heap_start_page, heap_end_page) + }; + + for page in page_range { + let frame = frame_allocator + .allocate_frame() + .ok_or(MapToError::FrameAllocationFailed)?; + let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE; + unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() }; + } + + unsafe { + ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE); + } + + Ok(()) +} + +pub struct Dummy; + +unsafe impl GlobalAlloc for Dummy { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { + panic!("dealloc should be never called") + } +}
\ No newline at end of file |
