Skip to content

Commit a269562

Browse files
committed
Split large range when adding rather than panicking.
If the order of the allocator isn't large enough for the ideal class of the range being added, use the largest available class.
1 parent e5831c6 commit a269562

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/frame.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ impl<const ORDER: usize> FrameAllocator<ORDER> {
6161
} else {
6262
32
6363
};
64-
let size = min(lowbit, prev_power_of_two(end - current_start));
64+
let size = min(
65+
min(lowbit, prev_power_of_two(end - current_start)),
66+
1 << (ORDER - 1),
67+
);
6568
total += size;
6669

6770
self.free_list[size.trailing_zeros() as usize].insert(current_start);

src/test.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,20 @@ fn test_frame_allocator_add() {
122122
}
123123

124124
#[test]
125-
#[should_panic]
126-
fn test_frame_allocator_add_large_size_panics() {
125+
fn test_frame_allocator_allocate_large() {
126+
let mut frame = FrameAllocator::<32>::new();
127+
assert_eq!(frame.alloc(10_000_000_000), None);
128+
}
129+
130+
#[test]
131+
fn test_frame_allocator_add_large_size_split() {
127132
let mut frame = FrameAllocator::<32>::new();
128133

129134
frame.insert(0..10_000_000_000);
135+
136+
assert_eq!(frame.alloc(0x8000_0001), None);
137+
assert_eq!(frame.alloc(0x8000_0000), Some(0x8000_0000));
138+
assert_eq!(frame.alloc(0x8000_0000), Some(0x1_0000_0000));
130139
}
131140

132141
#[test]

0 commit comments

Comments
 (0)