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) } }