Make Region generic instead of using usize
Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I4c66bbc50d5f75cfe8bee1652aaac2e1ed308691
diff --git a/src/region_pool.rs b/src/region_pool.rs
index df5a026..4c81160 100644
--- a/src/region_pool.rs
+++ b/src/region_pool.rs
@@ -14,18 +14,20 @@
/// This trait provides the necessary information about a region to the `RegionPool`.
pub trait Region: Sized {
type Resource;
+ type Base: Ord + Copy;
+ type Length: Ord + Copy;
/// Get base address
- fn base(&self) -> usize;
+ fn base(&self) -> Self::Base;
// Get length
- fn length(&self) -> usize;
+ fn length(&self) -> Self::Length;
/// Check if the region is used
fn used(&self) -> bool;
/// Check if an area defined by its base address and length is inside the region
- fn contains(&self, base: usize, length: usize) -> bool;
+ fn contains(&self, base: Self::Base, length: Self::Length) -> bool;
/// Try append the parameter region. Return true on success.
fn try_append(&mut self, other: &Self) -> bool;
@@ -40,8 +42,8 @@
/// * The area is in the middle of the region -> return three areas
fn create_split(
&self,
- base: usize,
- length: usize,
+ base: Self::Base,
+ length: Self::Length,
resource: Option<Self::Resource>,
) -> (Self, Vec<Self>);
}
@@ -86,7 +88,11 @@
/// Allocate a region from the pool of a given length. It will select an area in which the
/// region fits and has the minimal size. Then it splits this area to get the allocated region
/// with the exact size.
- pub fn allocate(&mut self, length: usize, resource: T::Resource) -> Result<T, RegionPoolError> {
+ pub fn allocate(
+ &mut self,
+ length: T::Length,
+ resource: T::Resource,
+ ) -> Result<T, RegionPoolError> {
let region_to_allocate_from = self
.regions
.iter()
@@ -107,8 +113,8 @@
/// Acquire a region with the given base address and length.
pub fn acquire(
&mut self,
- base: usize,
- length: usize,
+ base: T::Base,
+ length: T::Length,
resource: T::Resource,
) -> Result<T, RegionPoolError> {
let region_to_acquire_from = self
@@ -152,8 +158,8 @@
/// Find the region which contains the given area
pub fn find_containing_region(
&self,
- base: usize,
- length: usize,
+ base: T::Base,
+ length: T::Length,
) -> Result<&T, RegionPoolError> {
let region_index = match self.regions.binary_search_by(|r| r.base().cmp(&base)) {
Ok(exact_index) => Ok(exact_index),
@@ -203,6 +209,8 @@
impl Region for RegionExample {
type Resource = ();
+ type Base = usize;
+ type Length = usize;
fn base(&self) -> usize {
self.base