Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ impl<const ORDER: usize> FrameAllocator<ORDER> {

let mut total = 0;
let mut current_start = start;
let max_block_size = 1 << (ORDER - 1);

while current_start < end {
let lowbit = if current_start > 0 {
current_start & (!current_start + 1)
} else {
32
max_block_size
};
let size = min(
min(lowbit, prev_power_of_two(end - current_start)),
1 << (ORDER - 1),
max_block_size,
);
total += size;

Expand Down
13 changes: 11 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ fn test_frame_allocator_add() {
assert!(frame.alloc(2).is_none());
}

#[test]
fn test_frame_allocator_add_from_zero_keeps_large_block() {
let mut frame = FrameAllocator::<7>::new();

frame.add_frame(0, 64);

assert_eq!(frame.alloc(64), Some(0));
}

#[test]
fn test_frame_allocator_allocate_large() {
let mut frame = FrameAllocator::<32>::new();
Expand All @@ -152,16 +161,16 @@ fn test_frame_allocator_add_large_size_split() {
frame.insert(0..10_000_000_000);

assert_eq!(frame.alloc(0x8000_0001), None);
assert_eq!(frame.alloc(0x8000_0000), Some(0));
assert_eq!(frame.alloc(0x8000_0000), Some(0x8000_0000));
assert_eq!(frame.alloc(0x8000_0000), Some(0x1_0000_0000));
}

#[test]
fn test_frame_allocator_add_large_size() {
let mut frame = FrameAllocator::<33>::new();

frame.insert(0..10_000_000_000);
assert_eq!(frame.alloc(0x8000_0001), Some(0x1_0000_0000));
assert_eq!(frame.alloc(0x8000_0001), Some(0));
}

#[test]
Expand Down
Loading