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