From 63e8577d771445715bd70bb536637a419fdbde1a Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 10 Jan 2020 11:42:04 +0100 Subject: [PATCH] Create a basic LinkedListAllocator type --- src/allocator.rs | 1 + src/allocator/linked_list.rs | 48 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 50 insertions(+) create mode 100644 src/allocator/linked_list.rs diff --git a/src/allocator.rs b/src/allocator.rs index e2b6396a..52ebbd68 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -9,6 +9,7 @@ use x86_64::{ }; pub mod bump; +pub mod linked_list; pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs new file mode 100644 index 00000000..6e547e59 --- /dev/null +++ b/src/allocator/linked_list.rs @@ -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!(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 250e1ba9..79956351 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![feature(custom_test_frameworks)] #![feature(abi_x86_interrupt)] #![feature(alloc_error_handler)] +#![feature(const_fn)] #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"]