From 2e1d132a9a8fecefddce77fabdaaff659c141167 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 10 Jan 2020 11:44:17 +0100 Subject: [PATCH] Implement add_free_region --- src/allocator/linked_list.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index 6e547e59..68c77ab3 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -1,3 +1,6 @@ +use super::align_up; +use core::mem; + struct ListNode { size: usize, next: Option<&'static mut ListNode>, @@ -43,6 +46,15 @@ impl LinkedListAllocator { /// Adds the given memory region to the front of the list. unsafe fn add_free_region(&mut self, addr: usize, size: usize) { - todo!(); + // ensure that the freed region is capable of holding ListNode + assert!(align_up(addr, mem::align_of::()) == addr); + assert!(size >= mem::size_of::()); + + // create a new list node and append it at the start of the list + let mut node = ListNode::new(size); + node.next = self.head.next.take(); + let node_ptr = addr as *mut ListNode; + node_ptr.write(node); + self.head.next = Some(&mut *node_ptr) } }