diff --git a/device-api/src/topology/hwloc.rs b/device-api/src/topology/hwloc.rs index 59ba7f4..57b7196 100644 --- a/device-api/src/topology/hwloc.rs +++ b/device-api/src/topology/hwloc.rs @@ -5,16 +5,12 @@ use crate::topology::helper; use crate::{DeviceError, DeviceResult}; pub trait Hwloc { - unsafe fn init_topology(&mut self) -> DeviceResult<()>; - unsafe fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()>; - unsafe fn load_topology(&mut self) -> DeviceResult<()>; - unsafe fn set_topology_from_xml(&mut self, xml_path: &str) -> DeviceResult<()>; - unsafe fn get_common_ancestor_obj( - &self, - dev1bdf: &str, - dev2bdf: &str, - ) -> DeviceResult; - unsafe fn destroy_topology(&mut self); + fn init_topology(&mut self) -> DeviceResult<()>; + fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()>; + fn load_topology(&mut self) -> DeviceResult<()>; + fn set_topology_from_xml(&mut self, xml_path: &str) -> DeviceResult<()>; + fn get_common_ancestor_obj(&self, dev1bdf: &str, dev2bdf: &str) -> DeviceResult; + fn destroy_topology(&mut self); } pub struct HwlocTopology { @@ -30,8 +26,8 @@ impl HwlocTopology { } impl Hwloc for HwlocTopology { - unsafe fn init_topology(&mut self) -> DeviceResult<()> { - { + fn init_topology(&mut self) -> DeviceResult<()> { + unsafe { if hwloc_topology_init(&mut self.topology) == 0 { Ok(()) } else { @@ -42,8 +38,8 @@ impl Hwloc for HwlocTopology { } } - unsafe fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()> { - { + fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()> { + unsafe { if hwloc_topology_set_io_types_filter(self.topology, filter) == 0 { Ok(()) } else { @@ -52,8 +48,8 @@ impl Hwloc for HwlocTopology { } } - unsafe fn load_topology(&mut self) -> DeviceResult<()> { - { + fn load_topology(&mut self) -> DeviceResult<()> { + unsafe { if hwloc_topology_load(self.topology) == 0 { Ok(()) } else { @@ -62,8 +58,8 @@ impl Hwloc for HwlocTopology { } } - unsafe fn set_topology_from_xml(&mut self, xmlpath: &str) -> DeviceResult<()> { - { + fn set_topology_from_xml(&mut self, xmlpath: &str) -> DeviceResult<()> { + unsafe { let xml_path_cstr = CString::new(xmlpath).unwrap(); if hwloc_topology_set_xml(self.topology, xml_path_cstr.as_ptr()) == 0 { Ok(()) @@ -73,12 +69,8 @@ impl Hwloc for HwlocTopology { } } - unsafe fn get_common_ancestor_obj( - &self, - dev1bdf: &str, - dev2bdf: &str, - ) -> DeviceResult { - { + fn get_common_ancestor_obj(&self, dev1bdf: &str, dev2bdf: &str) -> DeviceResult { + unsafe { let dev1_obj = helper::hwloc_get_pcidev_by_busidstring(self.topology, dev1bdf); if dev1_obj.is_null() { return Err(DeviceError::hwloc_error(format!( @@ -104,8 +96,8 @@ impl Hwloc for HwlocTopology { } } - unsafe fn destroy_topology(&mut self) { - { + fn destroy_topology(&mut self) { + unsafe { if !self.topology.is_null() { hwloc_topology_destroy(self.topology); self.topology = std::ptr::null_mut(); diff --git a/device-api/src/topology/mod.rs b/device-api/src/topology/mod.rs index 60f968e..783d86c 100644 --- a/device-api/src/topology/mod.rs +++ b/device-api/src/topology/mod.rs @@ -161,15 +161,15 @@ mod tests { } impl Hwloc for HwlocTopologyMock { - unsafe fn init_topology(&mut self) -> DeviceResult<()> { + fn init_topology(&mut self) -> DeviceResult<()> { self.hwloc_topology.init_topology() } - unsafe fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()> { + fn set_io_types_filter(&mut self, filter: hwloc_type_filter_e) -> DeviceResult<()> { self.hwloc_topology.set_io_types_filter(filter) } - unsafe fn load_topology(&mut self) -> DeviceResult<()> { + fn load_topology(&mut self) -> DeviceResult<()> { let current_dir = env::current_dir().unwrap(); let xml_path = current_dir.join("src/topology/test.xml"); self.hwloc_topology @@ -177,11 +177,11 @@ mod tests { self.hwloc_topology.load_topology() } - unsafe fn set_topology_from_xml(&mut self, xml_path: &str) -> DeviceResult<()> { + fn set_topology_from_xml(&mut self, xml_path: &str) -> DeviceResult<()> { self.hwloc_topology.set_topology_from_xml(xml_path) } - unsafe fn get_common_ancestor_obj( + fn get_common_ancestor_obj( &self, dev1bdf: &str, dev2bdf: &str, @@ -190,7 +190,7 @@ mod tests { .get_common_ancestor_obj(dev1bdf, dev2bdf) } - unsafe fn destroy_topology(&mut self) { + fn destroy_topology(&mut self) { self.hwloc_topology.destroy_topology() } }