Skip to main content

RangeAllocator

Struct RangeAllocator 

Source
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>
where T: Clone + Copy + Add<Output = T> + AddAssign + Sub<Output = T> + Eq + PartialOrd + Debug,

Source

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);
Source

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.

Source

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).
Source

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);
Source

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);
Source

pub fn allocated_ranges(&self) -> impl Iterator<Item = Range<T>> + '_

Returns an iterator over allocated non-empty ranges

Source

pub fn reset(&mut self)

Resets the allocator to its initial state.

This marks the entire initial range as free, effectively deallocating all previously allocated ranges.

Source

pub fn is_empty(&self) -> bool

Returns true if no ranges have been allocated.

This checks whether the allocator is in its initial state with all space free.

Source§

impl<T: Copy + Sub<Output = T> + Sum> RangeAllocator<T>

Source

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.

Trait Implementations§

Source§

impl<T: Debug> Debug for RangeAllocator<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RangeAllocator<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RangeAllocator<T>
where T: RefUnwindSafe,

§

impl<T> Send for RangeAllocator<T>
where T: Send,

§

impl<T> Sync for RangeAllocator<T>
where T: Sync,

§

impl<T> Unpin for RangeAllocator<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for RangeAllocator<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for RangeAllocator<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.