Skip to content

Commit

Permalink
Fixed grow_to assuming that the last free range is always at the end …
Browse files Browse the repository at this point in the history
…(Issue: gfx-rs#1)
  • Loading branch information
dbartussek committed May 18, 2024
1 parent fdc3ebd commit 998c6ce
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ where
}

pub fn grow_to(&mut self, new_end: T) {
if let Some(last_range) = self.free_ranges.last_mut() {
let initial_range_end = self.initial_range.end;
if let Some(last_range) = self
.free_ranges
.last_mut()
.filter(|last_range| last_range.end == initial_range_end)
{
last_range.end = new_end;
} else {
self.free_ranges.push(self.initial_range.end..new_end);
Expand Down Expand Up @@ -222,6 +227,30 @@ mod tests {
alloc.free_range(0..14);
}

#[test]
fn test_grow_with_hole_at_start() {
let mut alloc = RangeAllocator::new(0..6);

assert_eq!(alloc.allocate_range(3), Ok(0..3));
assert_eq!(alloc.allocate_range(3), Ok(3..6));
alloc.free_range(0..3);

alloc.grow_to(9);
assert_eq!(alloc.allocated_ranges().collect::<Vec<_>>(), [3..6]);
}
#[test]
fn test_grow_with_hole_in_middle() {
let mut alloc = RangeAllocator::new(0..6);

assert_eq!(alloc.allocate_range(2), Ok(0..2));
assert_eq!(alloc.allocate_range(2), Ok(2..4));
assert_eq!(alloc.allocate_range(2), Ok(4..6));
alloc.free_range(2..4);

alloc.grow_to(9);
assert_eq!(alloc.allocated_ranges().collect::<Vec<_>>(), [0..2, 4..6]);
}

#[test]
fn test_dont_use_block_that_is_too_small() {
let mut alloc = RangeAllocator::new(0..10);
Expand Down

0 comments on commit 998c6ce

Please sign in to comment.