From 4085af2a3ee00a9cf85d4f2a8e85a39cee99102a Mon Sep 17 00:00:00 2001 From: "Samuel K. Gutierrez" Date: Mon, 18 Mar 2024 11:57:48 -0600 Subject: [PATCH] Add qvi_hwloc_bitmap_dup() and use it. Signed-off-by: Samuel K. Gutierrez --- src/qvi-hwloc.cc | 26 ++++++++++++++++++++------ src/qvi-hwloc.h | 9 +++++++++ src/qvi-hwpool.cc | 16 +++++----------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/qvi-hwloc.cc b/src/qvi-hwloc.cc index b0984c33..070be3db 100644 --- a/src/qvi-hwloc.cc +++ b/src/qvi-hwloc.cc @@ -705,10 +705,7 @@ qvi_hwloc_bitmap_copy( hwloc_const_cpuset_t src, hwloc_cpuset_t dest ) { - if (!src || !dest) { - assert(false); - return QV_ERR_INTERNAL; - } + assert(src && dest); if (hwloc_bitmap_copy(dest, src) != 0) { return QV_ERR_HWLOC; @@ -716,6 +713,24 @@ qvi_hwloc_bitmap_copy( return QV_SUCCESS; } +int +qvi_hwloc_bitmap_dup( + hwloc_const_cpuset_t src, + hwloc_cpuset_t *dest +) { + hwloc_cpuset_t idest = nullptr; + int rc = qvi_hwloc_bitmap_calloc(&idest); + if (rc != QV_SUCCESS) goto out; + + rc = qvi_hwloc_bitmap_copy(src, idest); +out: + if (rc != QV_SUCCESS) { + qvi_hwloc_bitmap_free(&idest); + } + *dest = idest; + return rc; +} + int qvi_hwloc_bitmap_nbits( hwloc_const_cpuset_t cpuset, @@ -1545,8 +1560,7 @@ qvi_hwloc_get_device_affinity( // lists tend to be small, so just perform a linear search for the given ID. for (const auto &dev : *devlist) { if (dev->visdev_id != device_id) continue; - qvi_hwloc_bitmap_calloc(&icpuset); - rc = qvi_hwloc_bitmap_copy(dev->cpuset, icpuset); + rc = qvi_hwloc_bitmap_dup(dev->cpuset, &icpuset); if (rc != QV_SUCCESS) goto out; } if (!icpuset) rc = QV_ERR_NOT_FOUND; diff --git a/src/qvi-hwloc.h b/src/qvi-hwloc.h index b6af97df..7caa7563 100644 --- a/src/qvi-hwloc.h +++ b/src/qvi-hwloc.h @@ -150,6 +150,15 @@ qvi_hwloc_bitmap_copy( hwloc_cpuset_t dest ); +/** + * + */ +int +qvi_hwloc_bitmap_dup( + hwloc_const_cpuset_t src, + hwloc_cpuset_t *dest +); + /** * Returns the number of bits required to represent a given cpuset. */ diff --git a/src/qvi-hwpool.cc b/src/qvi-hwpool.cc index a787c438..e139548c 100644 --- a/src/qvi-hwpool.cc +++ b/src/qvi-hwpool.cc @@ -175,10 +175,8 @@ qvi_hwpool_new_line_from_hwpool( qvi_line_hwpool_t *iline = nullptr; rc = qvi_line_hwpool_new(&iline); if (rc != QV_SUCCESS) goto out; - // Initialize and copy the cpuset. - rc = qvi_hwloc_bitmap_calloc(&iline->cpuset); - if (rc != QV_SUCCESS) goto out; - rc = qvi_hwloc_bitmap_copy(rpool->cpus.cpuset, iline->cpuset); + // Duplicate the cpuset. + rc = qvi_hwloc_bitmap_dup(rpool->cpus.cpuset, &iline->cpuset); if (rc != QV_SUCCESS) goto out; // Initialize and fill in the device information. iline->ndevinfos = ndevinfos; @@ -192,14 +190,10 @@ qvi_hwpool_new_line_from_hwpool( for (const auto &dinfo : rpool->devinfos) { iline->devinfos[idx].type = dinfo.second->type; iline->devinfos[idx].id = dinfo.second->id; - // Initialize and copy cpuset - rc = qvi_hwloc_bitmap_calloc( - &iline->devinfos[idx].affinity - ); - if (rc != QV_SUCCESS) break; - rc = qvi_hwloc_bitmap_copy( + // Duplicate the cpuset + rc = qvi_hwloc_bitmap_dup( dinfo.second->affinity, - iline->devinfos[idx].affinity + &iline->devinfos[idx].affinity ); if (rc != QV_SUCCESS) break; nw = asprintf(