From 0e21c32c0b46969cca5a1057bed6029018ca0d25 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 6 Mar 2020 15:55:08 +0100 Subject: [PATCH] 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 --- devices/src/bus.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/devices/src/bus.rs b/devices/src/bus.rs index 7ccd8c211..6acc47299 100644 --- a/devices/src/bus.rs +++ b/devices/src/bus.rs @@ -171,6 +171,24 @@ impl Bus { Ok(()) } + /// Removes all entries referencing the given device. + pub fn remove_by_device(&self, device: &Arc>) -> 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. pub fn update_range( &self,