Skip to content

Commit 8697b69

Browse files
authored
Merge pull request #31 from fmckeogh/master
Fix incorrect attempted merging at maximum order
2 parents f225090 + b437bd3 commit 8697b69

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ version = "0.9.8"
2020
optional = true
2121

2222
[dev-dependencies]
23-
criterion = "0.3"
24-
ctor = "0.1.23"
23+
criterion = "0.5.1"
24+
ctor = "0.2.6"
2525
rand = "0.8.5"
2626
rand_chacha = "0.3.1"
2727

benches/memory_allocator_benchmark.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use alloc::alloc::GlobalAlloc;
1212
use alloc::alloc::Layout;
1313
use buddy_system_allocator::LockedHeap;
1414
use criterion::{black_box, criterion_group, criterion_main, Criterion};
15+
use rand::{Rng, SeedableRng};
1516

1617
const SMALL_SIZE: usize = 8;
1718
const LARGE_SIZE: usize = 1024 * 1024; // 1M
@@ -42,10 +43,6 @@ pub fn large_alloc<const ORDER: usize>(heap: &LockedHeap<ORDER>) {
4243
pub fn mutil_thread_random_size<const ORDER: usize>(heap: &'static LockedHeap<ORDER>) {
4344
const THREAD_SIZE: usize = 10;
4445

45-
use rand::prelude::*;
46-
use rand::{Rng, SeedableRng};
47-
use rand_chacha::ChaCha8Rng;
48-
4946
let mut threads = Vec::with_capacity(THREAD_SIZE);
5047
let alloc = Arc::new(heap);
5148
for i in 0..THREAD_SIZE {

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ impl<const ORDER: usize> Heap<ORDER> {
157157
// Merge free buddy lists
158158
let mut current_ptr = ptr.as_ptr() as usize;
159159
let mut current_class = class;
160-
while current_class < self.free_list.len() {
160+
161+
while current_class < self.free_list.len() - 1 {
161162
let buddy = current_ptr ^ (1 << current_class);
162163
let mut flag = false;
163164
for block in self.free_list[current_class].iter_mut() {
@@ -261,7 +262,7 @@ unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeap<ORDER> {
261262
.lock()
262263
.alloc(layout)
263264
.ok()
264-
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
265+
.map_or(core::ptr::null_mut(), |allocation| allocation.as_ptr())
265266
}
266267

267268
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
@@ -327,7 +328,7 @@ unsafe impl<const ORDER: usize> GlobalAlloc for LockedHeapWithRescue<ORDER> {
327328
inner
328329
.alloc(layout)
329330
.ok()
330-
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
331+
.map_or(core::ptr::null_mut(), |allocation| allocation.as_ptr())
331332
}
332333
}
333334
}

src/test.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,34 @@ fn test_frame_allocator_aligned() {
192192
Some(16)
193193
);
194194
}
195+
196+
#[test]
197+
fn test_heap_merge_final_order() {
198+
const NUM_ORDERS: usize = 5;
199+
200+
let backing_size = 1 << NUM_ORDERS;
201+
let backing_layout = Layout::from_size_align(backing_size, backing_size).unwrap();
202+
203+
// create a new heap with 5 orders
204+
let mut heap = Heap::<NUM_ORDERS>::new();
205+
206+
// allocate host memory for use by heap
207+
let backing_allocation = unsafe { std::alloc::alloc(backing_layout) };
208+
209+
let start = backing_allocation as usize;
210+
let middle = unsafe { backing_allocation.add(backing_size / 2) } as usize;
211+
let end = unsafe { backing_allocation.add(backing_size) } as usize;
212+
213+
// add two contiguous ranges of memory
214+
unsafe { heap.add_to_heap(start, middle) };
215+
unsafe { heap.add_to_heap(middle, end) };
216+
217+
// NUM_ORDERS - 1 is the maximum order of the heap
218+
let layout = Layout::from_size_align(1 << (NUM_ORDERS - 1), 1).unwrap();
219+
220+
// allocation should succeed, using one of the added ranges
221+
let alloc = heap.alloc(layout).unwrap();
222+
223+
// deallocation should not attempt to merge the two contiguous ranges as the next order does not exist
224+
heap.dealloc(alloc, layout);
225+
}

0 commit comments

Comments
 (0)