zakhar_azg

Untitled

Dec 12th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.36 KB | None | 0 0
  1.     pub(super) fn set_size(&mut self, size: usize, block_bitmap: &mut Bitmap) -> Result<()> {
  2.         // ANCHOR_END: set_size
  3.         let current_size = self.size;
  4.         // debug!(msg = "SET_SIZE", old_size = self.size, new_size = size);
  5.         if size == self.size {
  6.             return Ok(());
  7.         }
  8.         if size > self.size {
  9.             let block_index = self.block_entry(self.size / BLOCK_SIZE, Some(block_bitmap))?;
  10.             Self::try_allocate_entry(block_index, Some(block_bitmap))?;
  11.  
  12.             // debug!(index = *block_index);
  13.             unsafe { BlockCache::block(*block_index)?.try_into_mut_slice::<u8>() }?
  14.                 .split_at_mut(current_size % BLOCK_SIZE)
  15.                 .1
  16.                 .fill(0);
  17.             BlockCache::flush_block(*block_index)?;
  18.  
  19.             for block in self.size.div_ceil(BLOCK_SIZE)..=size.div_ceil(BLOCK_SIZE) {
  20.                 // debug!(block);
  21.                 let block_index = self.block_entry(block, Some(block_bitmap))?;
  22.                 Self::try_allocate_entry(block_index, Some(block_bitmap))?;
  23.  
  24.                 // debug!(index = *block_index);
  25.                 unsafe { BlockCache::block(*block_index)?.try_into_mut_slice::<u8>() }?.fill(0);
  26.                 BlockCache::flush_block(*block_index)?;
  27.             }
  28.         } else if size == 0 {
  29.             for height in 0..MAX_HEIGHT {
  30.                 remove_tree(&mut self.root_blocks[height], height, block_bitmap)?;
  31.             }
  32.         } else {
  33.             let mut cumulative_size: usize = 0;
  34.             let mut height = 0;
  35.             while height != MAX_HEIGHT {
  36.                 cumulative_size += leaf_count_in_tree(height);
  37.                 if cumulative_size >= size.div_ceil(BLOCK_SIZE) {
  38.                     break;
  39.                 }
  40.                 height += 1;
  41.             }
  42.             let mut last_height = height;
  43.             if height != MAX_HEIGHT {
  44.                 height += 1;
  45.             }
  46.             while height != MAX_HEIGHT && self.root_blocks[height] != NO_BLOCK {
  47.                 remove_tree(&mut self.root_blocks[height], height, block_bitmap)?;
  48.                 height += 1;
  49.             }
  50.             let mut current_size = self.size.div_ceil(BLOCK_SIZE);
  51.             for prev in 0..last_height {
  52.                 current_size -= leaf_count_in_tree(prev);
  53.             }
  54.  
  55.             let mut current_block = self.root_blocks[last_height];
  56.             let mut option_block_bitmap = Some(block_bitmap);
  57.             while last_height != 0 {
  58.                 let current_child_size = leaf_count_in_tree(last_height - 1);
  59.                 let split_index = current_size.div_ceil(current_child_size);
  60.                 let next_layer = next_level(&mut current_block, &mut option_block_bitmap)?;
  61.                 for block in next_layer.split_at_mut(split_index).1 {
  62.                     remove_tree(
  63.                         block,
  64.                         last_height - 1,
  65.                         option_block_bitmap.as_mut().unwrap(),
  66.                     )?;
  67.                 }
  68.                 if split_index == 0 || current_size % current_child_size == 0 {
  69.                     break;
  70.                 }
  71.                 current_block = next_layer[split_index - 1];
  72.                 last_height -= 1;
  73.                 current_size -= current_child_size * split_index;
  74.             }
  75.         }
  76.         self.size = size;
  77.         Ok(())
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment