pub struct RangeAllocator<T> { /* private fields */ }Expand description
A range allocator that manages allocation and deallocation of contiguous ranges.
The allocator starts with an initial range and maintains a list of free ranges. It uses a best-fit allocation strategy to minimize fragmentation when allocating new ranges.
§Type Parameters
T- The type used for range bounds. Must support arithmetic operations and ordering.
Implementations§
Source§impl<T> RangeAllocator<T>
impl<T> RangeAllocator<T>
Sourcepub fn new(range: Range<T>) -> Self
pub fn new(range: Range<T>) -> Self
Creates a new range allocator with the specified initial range.
The entire initial range is marked as free and available for allocation.
§Arguments
range- The initial range that this allocator will manage.
§Example
use range_alloc_arceos::RangeAllocator;
let allocator = RangeAllocator::new(0..1024);Sourcepub fn initial_range(&self) -> &Range<T>
pub fn initial_range(&self) -> &Range<T>
Returns a reference to the initial range managed by this allocator.
This is the range that was provided when the allocator was created,
or the expanded range if grow_to was called.
Sourcepub fn grow_to(&mut self, new_end: T)
pub fn grow_to(&mut self, new_end: T)
Grows the allocator’s range to a new end point.
This extends the upper bound of the initial range and makes the new space available for allocation. If the last free range ends at the current upper bound, it is extended; otherwise, a new free range is added.
§Arguments
new_end- The new end point for the range (must be greater than the current end).
Sourcepub fn allocate_range(
&mut self,
length: T,
) -> Result<Range<T>, RangeAllocationError<T>>
pub fn allocate_range( &mut self, length: T, ) -> Result<Range<T>, RangeAllocationError<T>>
Allocates a contiguous range of the specified length.
This method uses a best-fit allocation strategy to find the smallest free range that can satisfy the request, minimizing fragmentation. If no single contiguous range is large enough, it returns an error with information about the total fragmented free space.
§Arguments
length- The length of the range to allocate.
§Returns
Ok(Range<T>)- The allocated range if successful.Err(RangeAllocationError<T>)- If allocation fails, containing information about the total fragmented free space available.
§Example
use range_alloc_arceos::RangeAllocator;
let mut allocator = RangeAllocator::new(0..100);
let range = allocator.allocate_range(20).unwrap();
assert_eq!(range, 0..20);Sourcepub fn free_range(&mut self, range: Range<T>)
pub fn free_range(&mut self, range: Range<T>)
Frees a previously allocated range, making it available for future allocations.
This method attempts to merge the freed range with adjacent free ranges to reduce fragmentation. The freed range must be within the initial range and must not be empty.
§Arguments
range- The range to free. Must be within the allocator’s initial range.
§Panics
Panics if the range is outside the initial range or if the range is empty (start >= end).
§Example
use range_alloc_arceos::RangeAllocator;
let mut allocator = RangeAllocator::new(0..100);
let range = allocator.allocate_range(20).unwrap();
allocator.free_range(range);Sourcepub fn allocated_ranges(&self) -> impl Iterator<Item = Range<T>> + '_
pub fn allocated_ranges(&self) -> impl Iterator<Item = Range<T>> + '_
Returns an iterator over allocated non-empty ranges
Source§impl<T: Copy + Sub<Output = T> + Sum> RangeAllocator<T>
impl<T: Copy + Sub<Output = T> + Sum> RangeAllocator<T>
Sourcepub fn total_available(&self) -> T
pub fn total_available(&self) -> T
Returns the total amount of free space available across all free ranges.
This sums the lengths of all free ranges, giving the total amount of space that could be allocated if fragmentation is not an issue.