From a51e8903b83b0f80b8bfa68eae2c66a252fc6836 Mon Sep 17 00:00:00 2001 From: Ferdia McKeogh Date: Fri, 26 Jan 2024 16:56:58 +0000 Subject: [PATCH 1/2] Fix incorrect attempted merging at maximum order --- src/lib.rs | 3 ++- src/test.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 334d843..f9dea8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,7 +157,8 @@ impl Heap { // Merge free buddy lists let mut current_ptr = ptr.as_ptr() as usize; let mut current_class = class; - while current_class < self.free_list.len() { + + while current_class < self.free_list.len() - 1 { let buddy = current_ptr ^ (1 << current_class); let mut flag = false; for block in self.free_list[current_class].iter_mut() { diff --git a/src/test.rs b/src/test.rs index b19599f..7f2e0f1 100644 --- a/src/test.rs +++ b/src/test.rs @@ -192,3 +192,34 @@ fn test_frame_allocator_aligned() { Some(16) ); } + +#[test] +fn test_heap_merge_final_order() { + const NUM_ORDERS: usize = 5; + + let backing_size = 1 << NUM_ORDERS; + let backing_layout = Layout::from_size_align(backing_size, backing_size).unwrap(); + + // create a new heap with 5 orders + let mut heap = Heap::::new(); + + // allocate host memory for use by heap + let backing_allocation = unsafe { std::alloc::alloc(backing_layout) }; + + let start = backing_allocation as usize; + let middle = unsafe { backing_allocation.add(backing_size / 2) } as usize; + let end = unsafe { backing_allocation.add(backing_size) } as usize; + + // add two contiguous ranges of memory + unsafe { heap.add_to_heap(start, middle) }; + unsafe { heap.add_to_heap(middle, end) }; + + // NUM_ORDERS - 1 is the maximum order of the heap + let layout = Layout::from_size_align(1 << (NUM_ORDERS - 1), 1).unwrap(); + + // allocation should succeed, using one of the added ranges + let alloc = heap.alloc(layout).unwrap(); + + // deallocation should not attempt to merge the two contiguous ranges as the next order does not exist + heap.dealloc(alloc, layout); +} From b437bd367a5026ce19623f36e854273a70b599ae Mon Sep 17 00:00:00 2001 From: Ferdia McKeogh Date: Fri, 26 Jan 2024 17:25:12 +0000 Subject: [PATCH 2/2] Bump dependencies, clippy suggested changes --- Cargo.toml | 4 ++-- benches/memory_allocator_benchmark.rs | 5 +---- src/lib.rs | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2520b26..09759d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,8 @@ version = "0.9.8" optional = true [dev-dependencies] -criterion = "0.3" -ctor = "0.1.23" +criterion = "0.5.1" +ctor = "0.2.6" rand = "0.8.5" rand_chacha = "0.3.1" diff --git a/benches/memory_allocator_benchmark.rs b/benches/memory_allocator_benchmark.rs index a2cbe79..0055c10 100644 --- a/benches/memory_allocator_benchmark.rs +++ b/benches/memory_allocator_benchmark.rs @@ -12,6 +12,7 @@ use alloc::alloc::GlobalAlloc; use alloc::alloc::Layout; use buddy_system_allocator::LockedHeap; use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use rand::{Rng, SeedableRng}; const SMALL_SIZE: usize = 8; const LARGE_SIZE: usize = 1024 * 1024; // 1M @@ -42,10 +43,6 @@ pub fn large_alloc(heap: &LockedHeap) { pub fn mutil_thread_random_size(heap: &'static LockedHeap) { const THREAD_SIZE: usize = 10; - use rand::prelude::*; - use rand::{Rng, SeedableRng}; - use rand_chacha::ChaCha8Rng; - let mut threads = Vec::with_capacity(THREAD_SIZE); let alloc = Arc::new(heap); for i in 0..THREAD_SIZE { diff --git a/src/lib.rs b/src/lib.rs index f9dea8f..2fb10ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -262,7 +262,7 @@ unsafe impl GlobalAlloc for LockedHeap { .lock() .alloc(layout) .ok() - .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) + .map_or(core::ptr::null_mut(), |allocation| allocation.as_ptr()) } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { @@ -328,7 +328,7 @@ unsafe impl GlobalAlloc for LockedHeapWithRescue { inner .alloc(layout) .ok() - .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) + .map_or(core::ptr::null_mut(), |allocation| allocation.as_ptr()) } } }