pub fn free_region(&mut self, region: Region) -> Result<()> {
        let mut was_merged = false;
        let mut i = 0;

        while i < self.free_regions.len() && !was_merged {
            if self.free_regions[i] == region
                || self.free_regions[i].is_overlapping(&region)
            {
                anyhow::bail!(
                    "Attempting to free a suballocation twice will lead to
                     data inconsistency!"
                );
            } else if let MergeResult::Contiguous(merged) =
                region.merge(&self.free_regions[i])
            {
                let mut to_insert = merged;

                // check if the new merged region can fuse with the next free
                // region too. If it can, then build the fully merged region
                // and remove one entry from the free region vector.
                if i + 1 < self.free_regions.len() {
                    if let MergeResult::Contiguous(merged) =
                        to_insert.merge(&self.free_regions[i + 1])
                    {
                        to_insert = merged;
                        self.free_regions.remove(i + 1);
                    }
                }

                self.free_regions[i] = to_insert;
                was_merged = true;
            } else {
                if region.end() < self.free_regions[i].start() {
                    break;
                }
                i += 1;
            }
        }

        // The region is not contiguous with any other region in the
        // free_region vector. Insert it wherever the merge loop stopped so
        // that free_regions stay consecutive.
        if !was_merged {
            self.free_regions.insert(i, region);
        }

        Ok(())
    }
/// Free a subregion back into the set of free regions.
/// Regions are automatically joined to minimize fragmentation.
Free a subregion back into the set of free regions.
Regions are automatically joined to minimize fragmentation.
Free a subregion back into the set of free regions . Regions are automatically joined to minimize fragmentation .
pub fn free_region(&mut self, region: Region) -> Result<()> {
        let mut was_merged = false;
        let mut i = 0;
        while i < self.free_regions.len() && !was_merged {
            if self.free_regions[i] == region
                || self.free_regions[i].is_overlapping(&region)
            {
                anyhow::bail!(
                    "Attempting to free a suballocation twice will lead to
                     data inconsistency!"
                );
            } else if let MergeResult::Contiguous(merged) =
                region.merge(&self.free_regions[i])
            {
                let mut to_insert = merged;
                // check if the new merged region can fuse with the next free
                // region too. If it can, then build the fully merged region
                // and remove one entry from the free region vector.
                if i + 1 < self.free_regions.len() {
                    if let MergeResult::Contiguous(merged) =
                        to_insert.merge(&self.free_regions[i + 1])
                    {
                        to_insert = merged;
                        self.free_regions.remove(i + 1);
                    }
                }
                self.free_regions[i] = to_insert;
                was_merged = true;
            } else {
                if region.end() < self.free_regions[i].start() {
                    break;
                }
                i += 1;
            }
        }
        // The region is not contiguous with any other region in the
        // free_region vector. Insert it wherever the merge loop stopped so
        // that free_regions stay consecutive.
        if !was_merged {
            self.free_regions.insert(i, region);
        }
        Ok(())
    }
pub fn free_region ( & mut self , region : Region ) -> Result < ( ) > { let mut was_merged = false ; let mut i = 0 ; while i < self . free_regions . len ( ) && ! was_merged { if self . free_regions [ i ] == region || self . free_regions [ i ] . is_overlapping ( & region ) { anyhow :: bail ! ( " " ) ; } else if let MergeResult :: Contiguous ( merged ) = region . merge ( & self . free_regions [ i ] ) { let mut to_insert = merged ; // check if the new merged region can fuse with the next free // region too. If it can, then build the fully merged region // and remove one entry from the free region vector. if i + 1 < self . free_regions . len ( ) { if let MergeResult :: Contiguous ( merged ) = to_insert . merge ( & self . free_regions [ i + 1 ] ) { to_insert = merged ; self . free_regions . remove ( i + 1 ) ; } } self . free_regions [ i ] = to_insert ; was_merged = true ; } else { if region . end ( ) < self . free_regions [ i ] . start ( ) { break ; } i += 1 ; } } // The region is not contiguous with any other region in the // free_region vector. Insert it wherever the merge loop stopped so // that free_regions stay consecutive. if ! was_merged { self . free_regions . insert ( i , region ) ; } Ok ( ( ) ) }
Free a subregion back into the set of free regions.
Free a subregion back into the set of free regions .
&mut self
region
Region