Allocates a range of addresses from the managed region. Returns `Some(allocated_address)` when successful, or `None` if an area of `size` can't be allocated or if alignment isn't a power of two.
(
&mut self,
address: Option<GuestAddress>,
size: GuestUsize,
align_size: Option<GuestUsize>,
)
| 164 | /// when successful, or `None` if an area of `size` can't be allocated or if alignment isn't |
| 165 | /// a power of two. |
| 166 | pub fn allocate( |
| 167 | &mut self, |
| 168 | address: Option<GuestAddress>, |
| 169 | size: GuestUsize, |
| 170 | align_size: Option<GuestUsize>, |
| 171 | ) -> Option<GuestAddress> { |
| 172 | if size == 0 { |
| 173 | return None; |
| 174 | } |
| 175 | |
| 176 | let alignment = align_size.unwrap_or(4); |
| 177 | if !alignment.is_power_of_two() { |
| 178 | return None; |
| 179 | } |
| 180 | |
| 181 | let new_addr = match address { |
| 182 | Some(req_address) => match self.available_range(req_address, size, alignment) { |
| 183 | Ok(addr) => addr, |
| 184 | Err(_) => { |
| 185 | return None; |
| 186 | } |
| 187 | }, |
| 188 | None => self.first_available_range(size, alignment)?, |
| 189 | }; |
| 190 | |
| 191 | self.ranges.insert(new_addr, size); |
| 192 | |
| 193 | Some(new_addr) |
| 194 | } |
| 195 | |
| 196 | /// Free an already allocated address range. |
| 197 | /// We can only free a range if it matches exactly an already allocated range. |
no test coverage detected