From b30cc7715c8296e35c402c65ff0a1cdf1bec9b62 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 16 Aug 2021 22:55:02 +0100 Subject: [PATCH] Add really simple block reuse --- agb/src/agb_alloc/block_allocator.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/agb/src/agb_alloc/block_allocator.rs b/agb/src/agb_alloc/block_allocator.rs index 4be4143c..03bd4359 100644 --- a/agb/src/agb_alloc/block_allocator.rs +++ b/agb/src/agb_alloc/block_allocator.rs @@ -72,6 +72,30 @@ impl BlockAllocator { unsafe impl GlobalAlloc for BlockAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + // find a block that this current request fits in + let block_layout = Layout::new::(); + let (full_layout, offset) = block_layout.extend(layout).unwrap(); + + { + let state = self.state.lock(); + let mut current_block = state.first_block; + while let Some(mut curr) = current_block { + let mut curr_block = curr.as_mut(); + + if !curr_block.used { + if let Some(next) = curr_block.next { + let size = next.cast::().as_ptr().offset_from(curr.as_ptr().cast()); + if size >= full_layout.size() as isize { + curr_block.used = true; + return curr.as_ptr().cast::().add(offset); + } + } + } + + current_block = curr_block.next; + } + } + self.new_block(layout) }