Skip to content

Commit

Permalink
Ref: Update freelist to use new block api
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexvZyl committed Oct 13, 2023
1 parent 1ee5a62 commit ab24fdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl Block {
self.n_elements
}

pub fn set_n_elements(&mut self, n_elements: i32) {
self.n_elements = n_elements
}

/// Returns the new number of elements.
/// Errors if the block overlaps with the following block.
pub fn grow(&mut self, increase: i32) -> Result<i32, i32> {
Expand All @@ -46,7 +50,7 @@ impl Block {
self.next_block_index != NONE_INT
}

/// The block should probably be removed from the freelist if this is true.
/// The block should be removed from the freelist if this is true.
pub fn is_empty(&self) -> bool {
self.n_elements == 0
}
Expand Down
44 changes: 26 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<T> Freelist<T>
/// * Performs a non-primitive cast when checknig adjacency.
unsafe fn blocks_are_adjacent(&self, first_block_index: i32, second_block_index: i32) -> bool
{
first_block_index + self.get_block(first_block_index).element_count == second_block_index
first_block_index + self.get_block(first_block_index).get_n_elements() == second_block_index
}

/// Find and commit a block that fits the size requirement.
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<T> Freelist<T>
let block = self.get_block(block_idx);

// Entire block is consumed.
if element_count == block.element_count
if element_count == block.get_n_elements()
{
let next_block = block.get_next_block_index();
match prev_block_index
Expand All @@ -141,23 +141,23 @@ impl<T> Freelist<T>
Some(..) =>
{
self.get_block_mut(prev_block_index.unwrap())
.connect(next_block);
.connect_at(next_block);
}
}
}
// Part of block is consumed.
else if element_count < block.element_count
else if element_count < block.get_n_elements()
{
// Create new block with reduced size.
let new_index = block_idx + element_count;
self.create_new_block(new_index,
block.element_count - element_count,
block.get_n_elements() - element_count,
block.get_next_block_index());
// Update the prev block.
if prev_block_index != None
{
self.get_block_mut(prev_block_index.unwrap())
.connect(Some(new_index));
.connect_at(Some(new_index));
}
}
// TODO: Throw. Too many elements for block.
Expand All @@ -179,8 +179,12 @@ impl<T> Freelist<T>
// Just extend the last block's count.
if self.is_last_block_at_end()
{
self.get_block_mut(self.find_last_block_index().unwrap())
.element_count += capacity_increase;
let result = self.get_block_mut(self.find_last_block_index().unwrap())
.grow(capacity_increase);
match result {
Ok(val) => {},
Err(val) => {}
}
}
// Create new block.
else
Expand All @@ -190,7 +194,7 @@ impl<T> Freelist<T>
if last_block_index != None
{
self.get_block_mut(last_block_index.unwrap())
.connect(Some(current_capacity));
.connect_at(Some(current_capacity));
}
}
}
Expand All @@ -217,7 +221,7 @@ impl<T> Freelist<T>
Some(..) =>
{
let last_block = self.get_block(last_block_index.unwrap());
return last_block_index.unwrap() + last_block.element_count
return last_block_index.unwrap() + last_block.get_n_elements()
== self.capacity_blocks();
}
}
Expand All @@ -234,7 +238,7 @@ impl<T> Freelist<T>
unsafe fn connect_blocks(&mut self, first_block_index: i32, second_block_index: i32)
{
self.get_block_mut(first_block_index)
.connect(Some(second_block_index));
.connect_at(Some(second_block_index));
}

/// Create a new block at the index with the given values and return a
Expand All @@ -251,9 +255,9 @@ impl<T> Freelist<T>
next_block_index: Option<i32>)
-> &mut Block
{
let mut new_block = self.get_block_mut(block_index);
new_block.set_next_block_index(next_block_index);
new_block.element_count = element_count;
let new_block = self.get_block_mut(block_index);
new_block.connect_at(next_block_index);
new_block.set_n_elements(element_count);
return new_block;
}

Expand Down Expand Up @@ -338,7 +342,7 @@ impl<T> Freelist<T>
loop
{
// Found large enough block.
if current_block.element_count >= element_count
if current_block.get_n_elements() >= element_count
{
return (prev_block_index, Some(current_block_index));
}
Expand Down Expand Up @@ -371,10 +375,14 @@ impl<T> Freelist<T>
if self.blocks_are_adjacent(first_block_index, second_block_index)
{
let next_block_index = self.get_block(second_block_index).get_next_block_index();
let second_block_count = self.get_block(second_block_index).element_count;
let second_block_count = self.get_block(second_block_index).get_n_elements();
let first_block = self.get_block_mut(first_block_index);
first_block.connect(next_block_index);
first_block.element_count += second_block_count;
first_block.connect_at(next_block_index);
let result = first_block.grow(second_block_count);
match result {
Err(val) => {},
Ok(val) => {},
}
return true;
}
}
Expand Down

0 comments on commit ab24fdc

Please sign in to comment.