Create a basic LinkedListAllocator type

This commit is contained in:
Philipp Oppermann
2020-01-10 11:42:04 +01:00
parent 75d826bf69
commit 63e8577d77
3 changed files with 50 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ use x86_64::{
}; };
pub mod bump; pub mod bump;
pub mod linked_list;
pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB

View File

@@ -0,0 +1,48 @@
struct ListNode {
size: usize,
next: Option<&'static mut ListNode>,
}
impl ListNode {
const fn new(size: usize) -> Self {
ListNode {
size,
next: None,
}
}
fn start_addr(&self) -> usize {
self as *const Self as usize
}
fn end_addr(&self) -> usize {
self.start_addr() + self.size
}
}
pub struct LinkedListAllocator {
head: ListNode,
}
impl LinkedListAllocator {
/// Creates an empty LinkedListAllocator.
pub const fn new() -> Self {
Self {
head: ListNode::new(0),
}
}
/// Initialize the allocator with the given heap bounds.
///
/// This function is unsafe because the caller must guarantee that the given
/// heap bounds are valid and that the heap is unused. This method must be
/// called only once.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
self.add_free_region(heap_start, heap_size);
}
/// Adds the given memory region to the front of the list.
unsafe fn add_free_region(&mut self, addr: usize, size: usize) {
todo!();
}
}

View File

@@ -3,6 +3,7 @@
#![feature(custom_test_frameworks)] #![feature(custom_test_frameworks)]
#![feature(abi_x86_interrupt)] #![feature(abi_x86_interrupt)]
#![feature(alloc_error_handler)] #![feature(alloc_error_handler)]
#![feature(const_fn)]
#![test_runner(crate::test_runner)] #![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]