Skip to content

Commit

Permalink
chore: lookup next page first
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Dec 21, 2023
1 parent 7500b2f commit 37723f3
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,35 +421,6 @@ pub fn find_max_fit<S: NorFlash>(

let current_page = find_youngest_page(flash, flash_range.clone())?;

// Find the max space available on each page until we wrap around.
let mut max_free: Option<usize> = None;
let page_data_start_address =
calculate_page_address::<S>(flash_range.clone(), current_page) + S::WORD_SIZE as u32;
let page_data_end_address =
calculate_page_end_address::<S>(flash_range.clone(), current_page) - S::WORD_SIZE as u32;

let mut current_address = page_data_start_address;
let end_address = page_data_end_address;

while current_address < end_address {
let result = ItemHeader::read_new(flash, current_address, end_address)?;
match result {
Some(header) => current_address = header.next_item_address::<S>(current_address),
None => {
let data_address =
round_up_to_alignment_usize::<S>(current_address as usize + ItemHeader::LENGTH);
if data_address <= end_address as usize {
let free = round_down_to_alignment_usize::<S>(
end_address as usize - data_address as usize,
);
max_free = max_free.map(|current| current.max(free)).or(Some(free));
}

break;
}
}
}

// Check if we have space on the next page
let next_page = next_page::<S>(flash_range.clone(), current_page);
match get_page_state(flash, flash_range.clone(), next_page)? {
Expand All @@ -471,18 +442,47 @@ pub fn find_max_fit<S: NorFlash>(
)?
.is_none();
if next_page_empty {
max_free.replace(S::ERASE_SIZE - (2 * S::WORD_SIZE));
return Ok(Some(S::ERASE_SIZE - (2 * S::WORD_SIZE)));
}
}
PageState::Open => {
max_free.replace(S::ERASE_SIZE - (2 * S::WORD_SIZE));
return Ok(Some(S::ERASE_SIZE - (2 * S::WORD_SIZE)));
}
PageState::PartialOpen => {
// This should never happen
return Err(Error::Corrupted);
}
};

// See how much space we can ind in the current page.
let mut max_free: Option<usize> = None;
let page_data_start_address =
calculate_page_address::<S>(flash_range.clone(), current_page) + S::WORD_SIZE as u32;
let page_data_end_address =
calculate_page_end_address::<S>(flash_range.clone(), current_page) - S::WORD_SIZE as u32;

let mut current_address = page_data_start_address;
let end_address = page_data_end_address;

while current_address < end_address {
let result = ItemHeader::read_new(flash, current_address, end_address)?;
match result {
Some(header) => current_address = header.next_item_address::<S>(current_address),
None => {
let data_address =
round_up_to_alignment_usize::<S>(current_address as usize + ItemHeader::LENGTH);
if data_address <= end_address as usize {
let free = round_down_to_alignment_usize::<S>(
end_address as usize - data_address as usize,
);
max_free = max_free.map(|current| current.max(free)).or(Some(free));
}

break;
}
}
}

Ok(max_free)
}

Expand Down

0 comments on commit 37723f3

Please sign in to comment.