]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virBitmapShrink
authorMartin Kletzander <mkletzan@redhat.com>
Thu, 9 Nov 2017 15:12:33 +0000 (16:12 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Sat, 18 Nov 2017 09:45:10 +0000 (10:45 +0100)
Sometimes the size of the bitmap matters and it might not be guessed correctly
when parsing from some type of input.  For example virBitmapNewData() has Byte
granularity, virBitmapNewString() has nibble granularity and so on.
virBitmapParseUnlimited() can be tricked into creating huge bitmap that's not
needed (e.g.: "0-2,^99999999").  This function provides a way to shrink the
bitmap.  It is not supposed to free any memory.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/libvirt_private.syms
src/util/virbitmap.c
src/util/virbitmap.h

index 5d630d5d1b8a4cb9bb54dd7a9635a6ef15db64aa..d3ca6b2ec44bd460c08799d461f05917576a2953 100644 (file)
@@ -1375,6 +1375,7 @@ virBitmapParseUnlimited;
 virBitmapSetAll;
 virBitmapSetBit;
 virBitmapSetBitExpand;
+virBitmapShrink;
 virBitmapSize;
 virBitmapSubtract;
 virBitmapToData;
index 7338f0255ab7bf03220f8dd078f38036a4eeae88..b2c5c7a6a5ac7fe2c21d79a910524aa9170d799f 100644 (file)
@@ -1196,3 +1196,26 @@ virBitmapSubtract(virBitmapPtr a,
     for (i = 0; i < max; i++)
         a->map[i] &= ~b->map[i];
 }
+
+
+/**
+ * virBitmapShrink:
+ * @map: Pointer to bitmap
+ * @b: last bit position to be excluded from bitmap
+ *
+ * Resizes the bitmap so that no more than @b bits will fit into it.  Nothing
+ * will change if the size is already smaller than @b.
+ *
+ * NB: Does not adjust the map->map_len so that a subsequent virBitmapExpand
+ * doesn't necessarily need to reallocate.
+ */
+void
+virBitmapShrink(virBitmapPtr map,
+                size_t b)
+{
+    if (!map)
+        return;
+
+    if (map->max_bit >= b)
+        map->max_bit = b;
+}
index 7b2bea8b534c654079cb9c57de87a28d8b6937b9..2464814055de9d74edc7b834a7817d5ae5d3901a 100644 (file)
@@ -153,4 +153,6 @@ void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
 void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+void virBitmapShrink(virBitmapPtr map, size_t b);
+
 #endif