MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / allocate_device_id

Method allocate_device_id

pci/src/bus.rs:223–244  ·  view source on GitHub ↗

Allocates a PCI device ID on the bus. - `id`: ID to allocate on the bus. If [`None`], the next free device ID on the bus is allocated, else the ID given is allocated ## Errors Returns [`PciRootError::AlreadyInUsePciDeviceSlot`] in case the ID requested is already allocated. Returns [`PciRootError::InvalidPciDeviceSlot`] in case the requested ID exceeds the maximum number of devices allowed per

(&mut self, id: Option<u8>)

Source from the content-addressed store, hash-verified

221 /// [`PciRootError::NoPciDeviceSlotAvailable`] if no free device
222 /// slot is available on the bus.
223 pub fn allocate_device_id(&mut self, id: Option<u8>) -> Result<u8> {
224 if let Some(idx) = id.map(|i| i as usize) {
225 if idx < NUM_DEVICE_IDS as usize {
226 if self.device_ids[idx] == DeviceIdState::Allocated {
227 Err(PciRootError::AlreadyInUsePciDeviceSlot(idx))
228 } else {
229 self.device_ids[idx] = DeviceIdState::Allocated;
230 Ok(idx as u8)
231 }
232 } else {
233 Err(PciRootError::InvalidPciDeviceSlot(idx))
234 }
235 } else {
236 for (idx, device_id) in self.device_ids.iter_mut().enumerate() {
237 if *device_id == DeviceIdState::Free {
238 *device_id = DeviceIdState::Allocated;
239 return Ok(idx as u8);
240 }
241 }
242 Err(PciRootError::NoPciDeviceSlotAvailable)
243 }
244 }
245
246 /// Frees a PCI device ID on the bus.
247 ///

Calls 2

iter_mutMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected