Skip to content

Commit 922846c

Browse files
authored
Merge pull request #24 from qwandor/split
Split large range when adding rather than panicking.
2 parents 3fc35f0 + ee43b56 commit 922846c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/frame.rs

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

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

src/test.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,28 @@ 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]
133142
fn test_frame_allocator_add_large_size() {
134143
let mut frame = FrameAllocator::<33>::new();
135144

136145
frame.insert(0..10_000_000_000);
146+
assert_eq!(frame.alloc(0x8000_0001), Some(0x1_0000_0000));
137147
}
138148

139149
#[test]

0 commit comments

Comments
 (0)