404507
From e743acc0093fdb790509f64014ad8858ecfe0839 Mon Sep 17 00:00:00 2001
404507
Message-Id: <e743acc0093fdb790509f64014ad8858ecfe0839@dist-git>
404507
From: Martin Kletzander <mkletzan@redhat.com>
404507
Date: Wed, 31 Jan 2018 16:32:16 +0100
404507
Subject: [PATCH] util: Introduce virBitmapShrink
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
404507
404507
Sometimes the size of the bitmap matters and it might not be guessed correctly
404507
when parsing from some type of input.  For example virBitmapNewData() has Byte
404507
granularity, virBitmapNewString() has nibble granularity and so on.
404507
virBitmapParseUnlimited() can be tricked into creating huge bitmap that's not
404507
needed (e.g.: "0-2,^99999999").  This function provides a way to shrink the
404507
bitmap.  It is not supposed to free any memory.
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
Reviewed-by: John Ferlan <jferlan@redhat.com>
404507
(cherry picked from commit baca005367cf60743f67df44440fc316e6d20c19)
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
---
404507
 src/libvirt_private.syms |  1 +
404507
 src/util/virbitmap.c     | 23 +++++++++++++++++++++++
404507
 src/util/virbitmap.h     |  2 ++
404507
 3 files changed, 26 insertions(+)
404507
404507
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
404507
index 8cc8cbcc90..7dd4621b70 100644
404507
--- a/src/libvirt_private.syms
404507
+++ b/src/libvirt_private.syms
404507
@@ -1382,6 +1382,7 @@ virBitmapParseUnlimited;
404507
 virBitmapSetAll;
404507
 virBitmapSetBit;
404507
 virBitmapSetBitExpand;
404507
+virBitmapShrink;
404507
 virBitmapSize;
404507
 virBitmapSubtract;
404507
 virBitmapToData;
404507
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
404507
index 7338f0255a..b2c5c7a6a5 100644
404507
--- a/src/util/virbitmap.c
404507
+++ b/src/util/virbitmap.c
404507
@@ -1196,3 +1196,26 @@ virBitmapSubtract(virBitmapPtr a,
404507
     for (i = 0; i < max; i++)
404507
         a->map[i] &= ~b->map[i];
404507
 }
404507
+
404507
+
404507
+/**
404507
+ * virBitmapShrink:
404507
+ * @map: Pointer to bitmap
404507
+ * @b: last bit position to be excluded from bitmap
404507
+ *
404507
+ * Resizes the bitmap so that no more than @b bits will fit into it.  Nothing
404507
+ * will change if the size is already smaller than @b.
404507
+ *
404507
+ * NB: Does not adjust the map->map_len so that a subsequent virBitmapExpand
404507
+ * doesn't necessarily need to reallocate.
404507
+ */
404507
+void
404507
+virBitmapShrink(virBitmapPtr map,
404507
+                size_t b)
404507
+{
404507
+    if (!map)
404507
+        return;
404507
+
404507
+    if (map->max_bit >= b)
404507
+        map->max_bit = b;
404507
+}
404507
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
404507
index 7b2bea8b53..2464814055 100644
404507
--- a/src/util/virbitmap.h
404507
+++ b/src/util/virbitmap.h
404507
@@ -153,4 +153,6 @@ void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
404507
 void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
404507
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
404507
 
404507
+void virBitmapShrink(virBitmapPtr map, size_t b);
404507
+
404507
 #endif
404507
-- 
404507
2.16.1
404507