add custom alloc; still doesn't work

This commit is contained in:
mtgmonkey 2025-05-13 21:38:37 -04:00
parent b2475a7b27
commit 8901f41a01
4 changed files with 14 additions and 6 deletions

Binary file not shown.

View file

@ -1,4 +1,4 @@
use crate::memory::area_frame_allocator;
use crate::memory::area_frame_allocator::AreaFrameAllocator;
use linked_list::LinkedListAllocator;
use x86_64::VirtAddr;
use x86_64::structures::paging::mapper::MapToError;
@ -10,7 +10,7 @@ pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100KiB
#[global_allocator]
static ALLOCATOR: Locked<LinkedListAllocator> = Locked::new(LinkedListAllocator::new());
static ALLOCATOR: Locked<AreaFrameAllocator> = Locked::new(AreaFrameAllocator::new());
pub struct Locked<A> {
inner: spin::Mutex<A>,
@ -31,6 +31,7 @@ impl<A> Locked<A> {
pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
multiboot_information_address: usize,
) -> Result<(), MapToError<Size4KiB>> {
let page_range = {
let heap_start = VirtAddr::new(HEAP_START as u64);
@ -47,7 +48,7 @@ pub fn init_heap(
unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
}
unsafe {
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
ALLOCATOR.lock().init(multiboot_information_address);
}
Ok(())
}

View file

@ -13,6 +13,7 @@ mod vga_buffer;
use core::panic::PanicInfo;
use multiboot2::{BootInformation, BootInformationHeader};
use x86_64::VirtAddr;
#[unsafe(no_mangle)]
pub extern "C" fn rust_main(multiboot_information_address: usize) {
@ -68,6 +69,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
multiboot_end,
multiboot_end - multiboot_start
);
//init();
halt()
}

View file

@ -63,9 +63,14 @@ impl AreaFrameAllocator {
}
}
fn find_frame(&mut self, size: usize, align: usize) -> Option<(Frame, usize)> {
fn add_frame(&mut self) {
self.next_free_frame.number += 1;
}
fn find_frame(&mut self, size: usize, _align: usize) -> Option<(Frame, usize)> {
let next_addr = self.next_free_frame.number * PAGE_SIZE + HEAP_START;
if &next_addr + core::mem::align_of::<Frame>() <= PAGE_SIZE + HEAP_START {
if &next_addr + core::mem::align_of::<Frame>() <= PAGE_SIZE + HEAP_START
&& size < core::mem::align_of::<Frame>()
{
Some((Frame::containing_address(next_addr), next_addr))
} else {
None
@ -91,5 +96,5 @@ unsafe impl GlobalAlloc for Locked<AreaFrameAllocator> {
None => ptr::null_mut(),
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}