devices: Add new method to remove all occurrences of a BusDevice

The same BusDevice can be inserted multiple times on the same bus at
different ranges. This is the case for a PCI device with multiple memory
BARs for example, as each BAR will be inserted at a specific MMIO range.

This new method allows to find and remove every occurence of the same
device so that we can make sure it is fully removed from the bus.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-03-06 15:55:08 +01:00 committed by Rob Bradford
parent f8e2008e0e
commit 0e21c32c0b

View File

@ -171,6 +171,24 @@ impl Bus {
Ok(()) Ok(())
} }
/// Removes all entries referencing the given device.
pub fn remove_by_device(&self, device: &Arc<Mutex<dyn BusDevice>>) -> Result<()> {
let mut device_list = self.devices.write().unwrap();
let mut remove_key_list = Vec::new();
for (key, value) in device_list.iter() {
if Arc::ptr_eq(&value.upgrade().unwrap(), device) {
remove_key_list.push(*key);
}
}
for key in remove_key_list.iter() {
device_list.remove(key);
}
Ok(())
}
/// Updates the address range for an existing device. /// Updates the address range for an existing device.
pub fn update_range( pub fn update_range(
&self, &self,