/// This takes a vector of page frame numbers, and compacts them
/// into ranges of consecutive pages. The result is a vector
/// of (start_page_frame_number, range_length) pairs.
This takes a vector of page frame numbers, and compacts them
into ranges of consecutive pages. The result is a vector
of (start_page_frame_number, range_length) pairs.
This takes a vector of page frame numbers and compacts them into ranges of consecutive pages . The result is a vector of ( start_page_frame_number range_length ) pairs .
pub(crate) fn compact_page_frame_numbers(v: &mut [u32]) -> Vec<(u32, u32)> {
    if v.is_empty() {
        return vec![];
    }
    // Since the total number of pages that can be
    // received at once is `MAX_PAGE_COMPACT_BUFFER`,
    // this sort does not change the complexity of handling
    // an inflation.
    v.sort_unstable();
    // Since there are at most `MAX_PAGE_COMPACT_BUFFER` pages, setting the
    // capacity of `result` to this makes sense.
    let mut result = Vec::with_capacity(MAX_PAGE_COMPACT_BUFFER);
    // The most recent range of pages is [previous..previous + length).
    let mut previous = 0;
    let mut length = 1;
    for pfn_index in 1..v.len() {
        let page_frame_number = v[pfn_index];
        // Skip duplicate pages. This will ensure we only consider
        // distinct PFNs.
        if page_frame_number == v[pfn_index - 1] {
            error!("Skipping duplicate PFN {}.", page_frame_number);
            continue;
        }
        // Check if the current page frame number is adjacent to the most recent page range.
        // This operation will never overflow because for whatever value `v[previous]`
        // has in the u32 range, we know there are at least `length` consecutive numbers
        // greater than it in the array (the greatest so far being `page_frame_number`),
        // since `v[previous]` is before all of them in the sorted array and `length`
        // was incremented for each consecutive one. This is true only because we skip
        // duplicates.
        if page_frame_number == v[previous] + length {
            // If so, extend that range.
            length += 1;
        } else {
            // Otherwise, push (previous, length) to the result vector.
            result.push((v[previous], length));
            // And update the most recent range of pages.
            previous = pfn_index;
            length = 1;
        }
    }
    // Don't forget to push the last range to the result.
    result.push((v[previous], length));
    result
}
pub ( crate ) fn compact_page_frame_numbers ( v : & mut [ u32 ] ) -> Vec < ( u32 , u32 ) > { if v . is_empty ( ) { return vec ! [ ] ; } // Since the total number of pages that can be // received at once is `MAX_PAGE_COMPACT_BUFFER`, // this sort does not change the complexity of handling // an inflation. v . sort_unstable ( ) ; // Since there are at most `MAX_PAGE_COMPACT_BUFFER` pages, setting the // capacity of `result` to this makes sense. let mut result = Vec :: with_capacity ( MAX_PAGE_COMPACT_BUFFER ) ; // The most recent range of pages is [previous..previous + length). let mut previous = 0 ; let mut length = 1 ; for pfn_index in 1 .. v . len ( ) { let page_frame_number = v [ pfn_index ] ; // Skip duplicate pages. This will ensure we only consider // distinct PFNs. if page_frame_number == v [ pfn_index - 1 ] { error ! ( " " page_frame_number ) ; continue ; } // Check if the current page frame number is adjacent to the most recent page range. // This operation will never overflow because for whatever value `v[previous]` // has in the u32 range, we know there are at least `length` consecutive numbers // greater than it in the array (the greatest so far being `page_frame_number`), // since `v[previous]` is before all of them in the sorted array and `length` // was incremented for each consecutive one. This is true only because we skip // duplicates. if page_frame_number == v [ previous ] + length { // If so, extend that range. length += 1 ; } else { // Otherwise, push (previous, length) to the result vector. result . push ( ( v [ previous ] , length ) ) ; // And update the most recent range of pages. previous = pfn_index ; length = 1 ; } } // Don't forget to push the last range to the result. result . push ( ( v [ previous ] , length ) ) ; result }
This takes a vector of page frame numbers, and compacts them
into ranges of consecutive pages.
This takes a vector of page frame numbers and compacts them into ranges of consecutive pages .
v
&mut [u32]