From 70a52c291d97e5520c785ca00c4d2266c539bca3 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 10 Jan 2020 11:48:56 +0100 Subject: [PATCH] Implement alloc_from_region --- src/allocator/linked_list.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index f610515b..80c69131 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -79,4 +79,27 @@ impl LinkedListAllocator { // no suitable region found None } + + /// Try to use the given region for an allocation with given size and alignment. + /// + /// Returns the allocation start address on success. + fn alloc_from_region(region: &ListNode, size: usize, align: usize) -> Result { + let alloc_start = align_up(region.start_addr(), align); + let alloc_end = alloc_start + size; + + if alloc_end > region.end_addr() { + // region too small + return Err(()); + } + + let excess_size = region.end_addr() - alloc_end; + if excess_size > 0 && excess_size < mem::size_of::() { + // rest of region too small to hold a ListNode (required because the + // allocation splits the region in a used and a free part) + return Err(()); + } + + // region suitable for allocation + Ok(alloc_start) + } }