[libvirt] [PATCH 5/6] util: bitmap: Use VIR_SHRINK_N in virBitmapShrink

Peter Krempa pkrempa at redhat.com
Mon Feb 5 13:17:46 UTC 2018


The function only reduces the size of the bitmap thus we can use the
appropriate shrinking function which also does not have any return
value.

Since virBitmapShrink now does not return any value callers need to be
fixed as well.
---
 src/conf/domain_conf.c |  3 +--
 src/util/virbitmap.c   | 20 ++++++++++----------
 src/util/virbitmap.h   |  2 +-
 src/util/virresctrl.c  |  3 +--
 tests/virbitmaptest.c  |  6 ++----
 5 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e827b2a810..34aae82f15 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -18453,8 +18453,7 @@ virDomainCachetuneDefParse(virDomainDefPtr def,

     /* We need to limit the bitmap to number of vCPUs.  If there's nothing left,
      * then we can just clean up and return 0 immediately */
-    if (virBitmapShrink(vcpus, def->maxvcpus) < 0)
-        goto cleanup;
+    virBitmapShrink(vcpus, def->maxvcpus);

     if (virBitmapIsAllClear(vcpus)) {
         ret = 0;
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index d1e5a9d1ea..82b1f76427 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -1292,15 +1292,16 @@ virBitmapSubtract(virBitmapPtr a,
  * Reduces the bitmap to size @b.  Nothing will change if the size is already
  * smaller than or equal to @b.
  */
-int
+void
 virBitmapShrink(virBitmapPtr map,
                 size_t b)
 {
+    size_t toremove;
     size_t nl = 0;
     size_t nb = 0;

     if (!map)
-        return 0;
+        return;

     if (map->nbits >= b)
         map->nbits = b;
@@ -1309,14 +1310,13 @@ virBitmapShrink(virBitmapPtr map,
     nb = map->nbits % VIR_BITMAP_BITS_PER_UNIT;
     map->map[nl] &= ((1UL << nb) - 1);

-    nl++;
-    if (nl == map->map_len)
-        return 0;
+    toremove = map->map_alloc - (nl + 1);

-    if (VIR_REALLOC_N(map->map, nl) < 0)
-        return -1;
+    if (toremove == 0)
+        return;

-    map->map_len = nl;
-    map->map_alloc = nl;
-    return 0;
+    VIR_SHRINK_N(map->map, map->map_alloc, toremove);
+
+    /* length needs to be fixed as well */
+    map->map_len = map->map_alloc;
 }
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
index 5a3362a19f..2464814055 100644
--- a/src/util/virbitmap.h
+++ b/src/util/virbitmap.h
@@ -153,6 +153,6 @@ void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
 void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

-int virBitmapShrink(virBitmapPtr map, size_t b);
+void virBitmapShrink(virBitmapPtr map, size_t b);

 #endif
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index ef388757a7..70426199ce 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -941,8 +941,7 @@ virResctrlAllocParseProcessCache(virResctrlInfoPtr resctrl,
     if (!mask)
         return -1;

-    if (virBitmapShrink(mask, resctrl->levels[level]->types[type]->bits) < 0)
-        goto cleanup;
+    virBitmapShrink(mask, resctrl->levels[level]->types[type]->bits);

     if (virResctrlAllocUpdateMask(alloc, level, type, cache_id, mask) < 0)
         goto cleanup;
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
index fffecdf1f6..2fbafc0a76 100644
--- a/tests/virbitmaptest.c
+++ b/tests/virbitmaptest.c
@@ -656,12 +656,10 @@ test12(const void *opaque ATTRIBUTE_UNUSED)

     TEST_MAP(1024, "34,1023");

-    if (virBitmapShrink(map, 35) < 0)
-        goto cleanup;
+    virBitmapShrink(map, 35);
     TEST_MAP(35, "34");

-    if (virBitmapShrink(map, 34) < 0)
-        goto cleanup;
+    virBitmapShrink(map, 34);
     TEST_MAP(34, "");

     ret = 0;
-- 
2.15.0




More information about the libvir-list mailing list