From fedfb0b82ef6e5da6a5378284b4a89822c4d867c Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Fri, 22 Oct 2021 12:00:24 +0530 Subject: [PATCH 1/3] [i2c] Use hashmap for device_map Use hashmap instead of an array of size 1 << 7, as only a few entries of the array are used here. Signed-off-by: Viresh Kumar --- src/i2c/src/i2c.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/i2c/src/i2c.rs b/src/i2c/src/i2c.rs index b026de6..a1f8a47 100644 --- a/src/i2c/src/i2c.rs +++ b/src/i2c/src/i2c.rs @@ -6,6 +6,7 @@ // SPDX-License-Identifier: Apache-2.0 use log::info; +use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::os::unix::io::AsRawFd; @@ -444,11 +445,10 @@ impl I2cAdapter { /// I2C map and helpers pub(crate) const MAX_I2C_VDEV: usize = 1 << 7; -const I2C_INVALID_ADAPTER: u32 = 0xFFFFFFFF; pub struct I2cMap { adapters: Vec>, - device_map: [u32; MAX_I2C_VDEV], + device_map: HashMap, } impl I2cMap { @@ -456,7 +456,7 @@ impl I2cMap { where Self: Sized, { - let mut device_map: [u32; MAX_I2C_VDEV] = [I2C_INVALID_ADAPTER; MAX_I2C_VDEV]; + let mut device_map = HashMap::new(); let mut adapters: Vec> = Vec::new(); for (i, device_cfg) in device_config.inner.iter().enumerate() { @@ -466,7 +466,7 @@ impl I2cMap { // Check that all addresses corresponding to the adapter are valid. for addr in &device_cfg.addr { adapter.set_device_addr(*addr as usize)?; - device_map[*addr as usize] = i as u32; + device_map.insert(*addr, i); } info!( @@ -484,21 +484,21 @@ impl I2cMap { } pub fn transfer(&self, reqs: &mut [I2cReq]) -> Result<()> { - let device = reqs[0].addr as usize; + let device = reqs[0].addr; // identify the device in the device_map - let index = self.device_map[device]; + let index = match self.device_map.get(&device) { + Some(&index) => index, - // This can happen a lot while scanning the bus, don't print any errors. - if index == I2C_INVALID_ADAPTER { - return Err(Error::ClientAddressInvalid); - } + // This can happen a lot while scanning the bus, don't print any errors. + None => return Err(Error::ClientAddressInvalid), + }; // get the corresponding adapter based on the device config. let adapter = &self.adapters[index as usize]; // Set device's address - adapter.set_device_addr(device)?; + adapter.set_device_addr(device as usize)?; adapter.transfer(reqs) } } @@ -562,11 +562,11 @@ pub mod tests { assert_eq!(i2c_map.adapters[1].adapter_no(), 2); assert_eq!(i2c_map.adapters[2].adapter_no(), 5); - assert_eq!(i2c_map.device_map[4], 0); - assert_eq!(i2c_map.device_map[32], 1); - assert_eq!(i2c_map.device_map[21], 1); - assert_eq!(i2c_map.device_map[10], 2); - assert_eq!(i2c_map.device_map[23], 2); + assert_eq!(i2c_map.device_map.get(&4), Some(&0)); + assert_eq!(i2c_map.device_map.get(&32), Some(&1)); + assert_eq!(i2c_map.device_map.get(&21), Some(&1)); + assert_eq!(i2c_map.device_map.get(&10), Some(&2)); + assert_eq!(i2c_map.device_map.get(&23), Some(&2)); } #[test] From 5402b6d5e55162158a9f20157146bc9ff0aefcc1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Nov 2021 04:08:17 +0000 Subject: [PATCH 2/3] build(deps): bump rust-vmm-ci from `b037be3` to `aee82cf` Bumps [rust-vmm-ci](https://github.com/rust-vmm/rust-vmm-ci) from `b037be3` to `aee82cf`. - [Release notes](https://github.com/rust-vmm/rust-vmm-ci/releases) - [Commits](https://github.com/rust-vmm/rust-vmm-ci/compare/b037be339677c2f24b7ba676fc9ff893ad474305...aee82cf0a405f2983ec493fcd55fda5a1ad03f38) --- updated-dependencies: - dependency-name: rust-vmm-ci dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- rust-vmm-ci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-vmm-ci b/rust-vmm-ci index b037be3..aee82cf 160000 --- a/rust-vmm-ci +++ b/rust-vmm-ci @@ -1 +1 @@ -Subproject commit b037be339677c2f24b7ba676fc9ff893ad474305 +Subproject commit aee82cf0a405f2983ec493fcd55fda5a1ad03f38 From b0e52d56e6a19aca1e9eb8a6e1550f3a6c310af8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Nov 2021 04:09:12 +0000 Subject: [PATCH 3/3] build(deps): bump libc from 0.2.105 to 0.2.107 Bumps [libc](https://github.com/rust-lang/libc) from 0.2.105 to 0.2.107. - [Release notes](https://github.com/rust-lang/libc/releases) - [Commits](https://github.com/rust-lang/libc/compare/0.2.105...0.2.107) --- updated-dependencies: - dependency-name: libc dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6a7a8e..7ef20bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,9 +122,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.105" +version = "0.2.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013" +checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" [[package]] name = "linked-hash-map"