Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | //! Region pool |
| 5 | //! |
| 6 | //! The region pool component handles regions allocations from memory areas in a generic way. |
| 7 | |
| 8 | use core::ops::Range; |
| 9 | |
| 10 | use alloc::vec::Vec; |
| 11 | |
| 12 | /// Region interface |
| 13 | /// |
| 14 | /// This trait provides the necessary information about a region to the `RegionPool`. |
| 15 | pub trait Region: Sized { |
| 16 | type Resource; |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 17 | type Base: Ord + Copy; |
| 18 | type Length: Ord + Copy; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 19 | |
| 20 | /// Get base address |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 21 | fn base(&self) -> Self::Base; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 22 | |
| 23 | // Get length |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 24 | fn length(&self) -> Self::Length; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 25 | |
| 26 | /// Check if the region is used |
| 27 | fn used(&self) -> bool; |
| 28 | |
| 29 | /// Check if an area defined by its base address and length is inside the region |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 30 | fn contains(&self, base: Self::Base, length: Self::Length) -> bool; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 31 | |
| 32 | /// Try append the parameter region. Return true on success. |
| 33 | fn try_append(&mut self, other: &Self) -> bool; |
| 34 | |
| 35 | /// Split region into multiple regions by the area passed as a parameter. It returns the region |
| 36 | /// of the area as the first member of the return tuple and all the new sliced regions as the |
| 37 | /// second member of the tuple. |
| 38 | /// The function has to handle four cases: |
| 39 | /// * The area matches the region exactly -> return one region |
| 40 | /// * The area is at the beginning of the region -> return two areas |
| 41 | /// * The area is at the end of the region -> return two areas |
| 42 | /// * The area is in the middle of the region -> return three areas |
| 43 | fn create_split( |
| 44 | &self, |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 45 | base: Self::Base, |
| 46 | length: Self::Length, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 47 | resource: Option<Self::Resource>, |
| 48 | ) -> (Self, Vec<Self>); |
| 49 | } |
| 50 | |
| 51 | /// Region pool error type |
| 52 | #[derive(Debug, PartialEq)] |
| 53 | pub enum RegionPoolError { |
| 54 | NoSpace, |
| 55 | AlreadyUsed, |
| 56 | NotFound, |
| 57 | } |
| 58 | |
| 59 | /// Region pool |
| 60 | pub struct RegionPool<T: Region> { |
| 61 | regions: Vec<T>, |
| 62 | } |
| 63 | |
| 64 | impl<T: Region> RegionPool<T> { |
| 65 | /// Create empty region pool |
| 66 | pub fn new() -> Self { |
| 67 | Self { |
| 68 | regions: Vec::new(), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Add a region to the pool |
| 73 | pub fn add(&mut self, region: T) -> Result<(), RegionPoolError> { |
| 74 | if !self |
| 75 | .regions |
| 76 | .iter() |
| 77 | .any(|r| r.contains(region.base(), region.length())) |
| 78 | { |
| 79 | self.regions.push(region); |
| 80 | self.regions.sort_by_key(|a| a.base()); |
| 81 | |
| 82 | Ok(()) |
| 83 | } else { |
| 84 | Err(RegionPoolError::AlreadyUsed) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Allocate a region from the pool of a given length. It will select an area in which the |
| 89 | /// region fits and has the minimal size. Then it splits this area to get the allocated region |
| 90 | /// with the exact size. |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 91 | pub fn allocate( |
| 92 | &mut self, |
| 93 | length: T::Length, |
| 94 | resource: T::Resource, |
| 95 | ) -> Result<T, RegionPoolError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 96 | let region_to_allocate_from = self |
| 97 | .regions |
| 98 | .iter() |
| 99 | .enumerate() |
| 100 | .filter(|(_index, region)| region.length() >= length && !region.used()) |
| 101 | .min_by_key(|(_index_a, region)| region.length()); |
| 102 | |
| 103 | if let Some((index, region)) = region_to_allocate_from { |
| 104 | let (new_region, split_regions) = |
| 105 | region.create_split(region.base(), length, Some(resource)); |
| 106 | self.replace(index, split_regions); |
| 107 | Ok(new_region) |
| 108 | } else { |
| 109 | Err(RegionPoolError::NoSpace) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Acquire a region with the given base address and length. |
| 114 | pub fn acquire( |
| 115 | &mut self, |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 116 | base: T::Base, |
| 117 | length: T::Length, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 118 | resource: T::Resource, |
| 119 | ) -> Result<T, RegionPoolError> { |
| 120 | let region_to_acquire_from = self |
| 121 | .regions |
| 122 | .iter() |
| 123 | .enumerate() |
| 124 | .find(|(_, region)| region.contains(base, length) && !region.used()); |
| 125 | |
| 126 | if let Some((index, region)) = region_to_acquire_from { |
| 127 | let (new_region, split_regions) = region.create_split(base, length, Some(resource)); |
| 128 | self.replace(index, split_regions); |
| 129 | Ok(new_region) |
| 130 | } else { |
| 131 | Err(RegionPoolError::AlreadyUsed) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Release region |
| 136 | pub fn release(&mut self, region_to_release: T) -> Result<(), RegionPoolError> { |
| 137 | assert!(region_to_release.used()); |
| 138 | |
| 139 | let region_to_release_from = self.regions.iter().enumerate().find(|(_, r)| { |
| 140 | r.contains(region_to_release.base(), region_to_release.length()) && r.used() |
| 141 | }); |
| 142 | |
| 143 | if let Some((index, region)) = region_to_release_from { |
| 144 | self.replace( |
| 145 | index, |
| 146 | region |
| 147 | .create_split(region_to_release.base(), region_to_release.length(), None) |
| 148 | .1, |
| 149 | ); |
| 150 | self.regions.dedup_by(|a, b| b.try_append(a)); |
| 151 | |
| 152 | Ok(()) |
| 153 | } else { |
| 154 | Err(RegionPoolError::NotFound) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Find the region which contains the given area |
| 159 | pub fn find_containing_region( |
| 160 | &self, |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 161 | base: T::Base, |
| 162 | length: T::Length, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 163 | ) -> Result<&T, RegionPoolError> { |
| 164 | let region_index = match self.regions.binary_search_by(|r| r.base().cmp(&base)) { |
| 165 | Ok(exact_index) => Ok(exact_index), |
| 166 | Err(insert_index) => { |
| 167 | if insert_index > 0 { |
| 168 | Ok(insert_index - 1) |
| 169 | } else { |
| 170 | Err(RegionPoolError::NotFound) |
| 171 | } |
| 172 | } |
| 173 | }?; |
| 174 | |
| 175 | if self.regions[region_index].contains(base, length) { |
| 176 | Ok(&self.regions[region_index]) |
| 177 | } else { |
| 178 | Err(RegionPoolError::NotFound) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | fn replace(&mut self, index: usize, replacements: Vec<T>) { |
| 183 | self.regions.splice( |
| 184 | Range { |
| 185 | start: index, |
| 186 | end: index + 1, |
| 187 | }, |
| 188 | replacements, |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[cfg(test)] |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 194 | mod tests { |
| 195 | use super::*; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 196 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 197 | #[derive(Debug, PartialEq, Eq)] |
| 198 | struct RegionExample { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 199 | base: usize, |
| 200 | length: usize, |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 201 | used: bool, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 202 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 203 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 204 | impl RegionExample { |
| 205 | fn new(base: usize, length: usize, used: bool) -> Self { |
| 206 | Self { base, length, used } |
| 207 | } |
| 208 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 209 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 210 | impl Region for RegionExample { |
| 211 | type Resource = (); |
Imre Kis | 589aa05 | 2024-09-10 20:19:47 +0200 | [diff] [blame^] | 212 | type Base = usize; |
| 213 | type Length = usize; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 214 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 215 | fn base(&self) -> usize { |
| 216 | self.base |
| 217 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 218 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 219 | fn length(&self) -> usize { |
| 220 | self.length |
| 221 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 222 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 223 | fn used(&self) -> bool { |
| 224 | self.used |
| 225 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 226 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 227 | fn contains(&self, base: usize, length: usize) -> bool { |
| 228 | self.base <= base && base + length <= self.base + self.length |
| 229 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 230 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 231 | fn try_append(&mut self, other: &Self) -> bool { |
| 232 | if self.used == other.used && self.base + self.length == other.base { |
| 233 | self.length += other.length; |
| 234 | true |
| 235 | } else { |
| 236 | false |
| 237 | } |
| 238 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 239 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 240 | fn create_split( |
| 241 | &self, |
| 242 | base: usize, |
| 243 | length: usize, |
| 244 | resource: Option<Self::Resource>, |
| 245 | ) -> (Self, Vec<Self>) { |
| 246 | let mut res = Vec::new(); |
| 247 | if self.base != base { |
| 248 | res.push(RegionExample::new(self.base, base - self.base, self.used)); |
| 249 | } |
| 250 | res.push(RegionExample::new(base, length, resource.is_some())); |
| 251 | if self.base + self.length != base + length { |
| 252 | res.push(RegionExample::new( |
| 253 | base + length, |
| 254 | (self.base + self.length) - (base + length), |
| 255 | self.used, |
| 256 | )); |
| 257 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 258 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 259 | (RegionExample::new(base, length, resource.is_some()), res) |
| 260 | } |
| 261 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 262 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 263 | #[test] |
| 264 | fn test_region_contains() { |
| 265 | let region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 266 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 267 | assert!(region.contains(0x4000_0000, 0x1000_0000)); |
| 268 | assert!(!region.contains(0x4000_0000 - 1, 0x1000_0000 + 1)); |
| 269 | assert!(!region.contains(0x4000_0000, 0x1000_0000 + 1)); |
| 270 | assert!(region.contains(0x4000_0000 + 1, 0x1000_0000 - 1)); |
| 271 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 272 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 273 | #[test] |
| 274 | fn test_region_try_append() { |
| 275 | // Normal append |
| 276 | let mut region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 277 | let appending = RegionExample::new(0x5000_0000, 0x0000_1000, false); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 278 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 279 | assert!(region.try_append(&appending)); |
| 280 | assert_eq!(RegionExample::new(0x4000_0000, 0x1000_1000, false), region); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 281 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 282 | // Different used flags |
| 283 | let mut region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 284 | let appending = RegionExample::new(0x5000_0000, 0x0000_1000, true); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 285 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 286 | assert!(!region.try_append(&appending)); |
| 287 | assert_eq!(RegionExample::new(0x4000_0000, 0x1000_0000, false), region); |
| 288 | |
| 289 | // Not continious |
| 290 | let mut region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 291 | let appending = RegionExample::new(0x5000_1000, 0x0000_1000, false); |
| 292 | |
| 293 | assert!(!region.try_append(&appending)); |
| 294 | assert_eq!(RegionExample::new(0x4000_0000, 0x1000_0000, false), region); |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn test_region_create_split() { |
| 299 | // Cut first part |
| 300 | let region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 301 | |
| 302 | let res = region.create_split(0x4000_0000, 0x0000_1000, Some(())).1; |
| 303 | assert_eq!(RegionExample::new(0x4000_0000, 0x0000_1000, true), res[0]); |
| 304 | assert_eq!(RegionExample::new(0x4000_1000, 0x0fff_f000, false), res[1]); |
| 305 | |
| 306 | // Cut last part |
| 307 | let region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 308 | |
| 309 | let res = region.create_split(0x4fff_f000, 0x0000_1000, Some(())).1; |
| 310 | assert_eq!(RegionExample::new(0x4000_0000, 0x0fff_f000, false), res[0]); |
| 311 | assert_eq!(RegionExample::new(0x4fff_f000, 0x0000_1000, true), res[1]); |
| 312 | |
| 313 | // Cut middle part |
| 314 | let region = RegionExample::new(0x4000_0000, 0x1000_0000, false); |
| 315 | |
| 316 | let res = region.create_split(0x4020_0000, 0x0000_1000, Some(())).1; |
| 317 | assert_eq!(RegionExample::new(0x4000_0000, 0x0020_0000, false), res[0]); |
| 318 | assert_eq!(RegionExample::new(0x4020_0000, 0x0000_1000, true), res[1]); |
| 319 | assert_eq!(RegionExample::new(0x4020_1000, 0x0fdf_f000, false), res[2]); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn test_region_pool_add() { |
| 324 | let mut pool = RegionPool::new(); |
| 325 | assert_eq!( |
| 326 | Ok(()), |
| 327 | pool.add(RegionExample::new(0x4000_0000, 0x1000_0000, false)) |
| 328 | ); |
| 329 | assert_eq!( |
| 330 | Ok(()), |
| 331 | pool.add(RegionExample::new(0x5000_0000, 0x1000_0000, false)) |
| 332 | ); |
| 333 | assert_eq!( |
| 334 | Err(RegionPoolError::AlreadyUsed), |
| 335 | pool.add(RegionExample::new(0x4000_1000, 0x1000, false)) |
| 336 | ); |
| 337 | } |
| 338 | |
| 339 | #[test] |
| 340 | fn test_pool_allocate() { |
| 341 | let mut pool = RegionPool::new(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 342 | pool.add(RegionExample::new(0x4000_0000, 0x1000_0000, false)) |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 343 | .unwrap(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 344 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 345 | let res = pool.allocate(0x1000, ()); |
| 346 | assert_eq!(Ok(RegionExample::new(0x4000_0000, 0x1000, true)), res); |
| 347 | let res = pool.allocate(0x1_0000, ()); |
| 348 | assert_eq!(Ok(RegionExample::new(0x4000_1000, 0x1_0000, true)), res); |
| 349 | let res = pool.allocate(0x2000_0000, ()); |
| 350 | assert_eq!(Err(RegionPoolError::NoSpace), res); |
| 351 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 352 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 353 | #[test] |
| 354 | fn test_region_pool_acquire() { |
| 355 | let mut pool = RegionPool::new(); |
| 356 | pool.add(RegionExample::new(0x4000_0000, 0x1000_0000, false)) |
| 357 | .unwrap(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 358 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 359 | let res = pool.acquire(0x4000_1000, 0x1000, ()); |
| 360 | assert_eq!(Ok(RegionExample::new(0x4000_1000, 0x1000, true)), res); |
| 361 | let res = pool.allocate(0x1_0000, ()); |
| 362 | assert_eq!(Ok(RegionExample::new(0x4000_2000, 0x10000, true)), res); |
| 363 | let res = pool.acquire(0x4000_1000, 0x1000, ()); |
| 364 | assert_eq!(Err(RegionPoolError::AlreadyUsed), res); |
| 365 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 366 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 367 | #[test] |
| 368 | fn test_region_pool_release() { |
| 369 | let mut pool = RegionPool::new(); |
| 370 | pool.add(RegionExample::new(0x4000_0000, 0x1000_0000, false)) |
| 371 | .unwrap(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 372 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 373 | let res = pool.allocate(0x1000, ()); |
| 374 | assert_eq!(Ok(RegionExample::new(0x4000_0000, 0x1000, true)), res); |
| 375 | assert_eq!(Ok(()), pool.release(res.unwrap())); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 376 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 377 | let res = pool.allocate(0x1_0000, ()); |
| 378 | assert_eq!(Ok(RegionExample::new(0x4000_0000, 0x10000, true)), res); |
| 379 | assert_eq!(Ok(()), pool.release(res.unwrap())); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 380 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 381 | assert_eq!( |
| 382 | Err(RegionPoolError::NotFound), |
| 383 | pool.release(RegionExample::new(0x4000_0000, 0x1000, true)) |
| 384 | ); |
| 385 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 386 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 387 | #[test] |
| 388 | #[should_panic] |
| 389 | fn test_region_pool_release_not_used() { |
| 390 | let mut pool = RegionPool::new(); |
| 391 | pool.release(RegionExample::new(0x4000_0000, 0x1000, false)) |
| 392 | .unwrap(); |
| 393 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 394 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 395 | #[test] |
| 396 | fn test_region_pool_find_containing_region() { |
| 397 | let mut pool = RegionPool::<RegionExample>::new(); |
| 398 | pool.add(RegionExample::new(0x4000_0000, 0x1000_0000, false)) |
| 399 | .unwrap(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 400 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 401 | assert_eq!( |
| 402 | Err(RegionPoolError::NotFound), |
| 403 | pool.find_containing_region(0x0000_0000, 0x1000) |
| 404 | ); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 405 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 406 | assert_eq!( |
| 407 | Err(RegionPoolError::NotFound), |
| 408 | pool.find_containing_region(0x5000_0000, 0x1000) |
| 409 | ); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 410 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 411 | assert_eq!( |
| 412 | Err(RegionPoolError::NotFound), |
| 413 | pool.find_containing_region(0x4000_0000, 0x2000_0000) |
| 414 | ); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 415 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 416 | assert_eq!( |
| 417 | Ok(&RegionExample::new(0x4000_0000, 0x1000_0000, false)), |
| 418 | pool.find_containing_region(0x4000_0000, 0x1000_0000) |
| 419 | ); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 420 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 421 | assert_eq!( |
| 422 | Ok(&RegionExample::new(0x4000_0000, 0x1000_0000, false)), |
| 423 | pool.find_containing_region(0x4000_1000, 0x1000) |
| 424 | ); |
| 425 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 426 | } |