Skip to content

Commit

Permalink
Fix incorrect attempted merging at maximum order
Browse files Browse the repository at this point in the history
<co-authored by Tom Spink tspink@gmail.com>
  • Loading branch information
fmckeogh committed Jan 26, 2024
1 parent f225090 commit a51e890
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ impl<const ORDER: usize> Heap<ORDER> {
// 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() {
Expand Down
31 changes: 31 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NUM_ORDERS>::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);
}

0 comments on commit a51e890

Please sign in to comment.