From c56bfa27e471636e71c8a6b7988bfaf8aae6a345 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 10 Jan 2020 11:46:10 +0100 Subject: [PATCH] Implement find_region --- src/allocator/linked_list.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index e0be7007..f610515b 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -54,4 +54,29 @@ impl LinkedListAllocator { node_ptr.write(node); self.head.next = Some(&mut *node_ptr) } + + /// Looks for a free region with the given size and alignment and removes + /// it from the list. + /// + /// Returns a tuple of the list node and the start address of the allocation. + fn find_region(&mut self, size: usize, align: usize) -> Option<(&'static mut ListNode, usize)> { + // reference to current list node, updated for each iteration + let mut current = &mut self.head; + // look for a large enough memory region in linked list + while let Some(ref mut region) = current.next { + if let Ok(alloc_start) = Self::alloc_from_region(®ion, size, align) { + // region suitable for allocation -> remove node from list + let next = region.next.take(); + let ret = Some((current.next.take().unwrap(), alloc_start)); + current.next = next; + return ret; + } else { + // region not suitable -> continue with next region + current = current.next.as_mut().unwrap(); + } + } + + // no suitable region found + None + } }