Skip to content

Commit

Permalink
remove unsafe from hwloc trait
Browse files Browse the repository at this point in the history
Signed-off-by: bg-furiosa <[email protected]>
  • Loading branch information
bg-furiosa committed Apr 5, 2024
1 parent 061675a commit a6458c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
44 changes: 18 additions & 26 deletions device-api/src/topology/hwloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<hwloc_obj_t>;
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<hwloc_obj_t>;
fn destroy_topology(&mut self);
}

pub struct HwlocTopology {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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(())
Expand All @@ -73,12 +69,8 @@ impl Hwloc for HwlocTopology {
}
}

unsafe fn get_common_ancestor_obj(
&self,
dev1bdf: &str,
dev2bdf: &str,
) -> DeviceResult<hwloc_obj_t> {
{
fn get_common_ancestor_obj(&self, dev1bdf: &str, dev2bdf: &str) -> DeviceResult<hwloc_obj_t> {
unsafe {
let dev1_obj = helper::hwloc_get_pcidev_by_busidstring(self.topology, dev1bdf);
if dev1_obj.is_null() {
return Err(DeviceError::hwloc_error(format!(
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions device-api/src/topology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,27 @@ 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
.set_topology_from_xml(xml_path.to_str().unwrap())?;
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,
Expand All @@ -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()
}
}
Expand Down

0 comments on commit a6458c4

Please sign in to comment.