]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Reintroduce virBitmapSubtract
authorMartin Kletzander <mkletzan@redhat.com>
Thu, 5 Oct 2017 13:09:30 +0000 (15:09 +0200)
committerMartin Kletzander <mkletzan@redhat.com>
Sat, 18 Nov 2017 09:45:10 +0000 (10:45 +0100)
Already introduced in the past with 9479642fd3c5, but then renamed to
virBitmapIntersect by a908e9e45eb2.  This time we'll really use it.

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
tests/virbitmaptest.c

index 9d845c7f63f3ce8c1c46c70cf37ff039d9acc6a6..5d630d5d1b8a4cb9bb54dd7a9635a6ef15db64aa 100644 (file)
@@ -1376,6 +1376,7 @@ virBitmapSetAll;
 virBitmapSetBit;
 virBitmapSetBitExpand;
 virBitmapSize;
+virBitmapSubtract;
 virBitmapToData;
 virBitmapToDataBuf;
 virBitmapToString;
index 47d16ee2228f6b74194dd37853994d6a6834b638..7338f0255ab7bf03220f8dd078f38036a4eeae88 100644 (file)
@@ -1174,3 +1174,25 @@ virBitmapIntersect(virBitmapPtr a,
     for (i = 0; i < max; i++)
         a->map[i] &= b->map[i];
 }
+
+
+/**
+ * virBitmapSubtract:
+ * @a: minuend/result
+ * @b: subtrahend
+ *
+ * Performs subtraction of two bitmaps: a = a - b
+ */
+void
+virBitmapSubtract(virBitmapPtr a,
+                  virBitmapPtr b)
+{
+    size_t i;
+    size_t max = a->map_len;
+
+    if (max > b->map_len)
+        max = b->map_len;
+
+    for (i = 0; i < max; i++)
+        a->map[i] &= ~b->map[i];
+}
index e964a3edc9cbb4896921660ed9fc385c07f0f705..7b2bea8b534c654079cb9c57de87a28d8b6937b9 100644 (file)
@@ -150,4 +150,7 @@ bool virBitmapOverlaps(virBitmapPtr b1,
 void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 #endif
index 656ce456295f90b964d34ff4f526518de70606fd..9c0ffe70cb49c5749d999ef108cd0635e199ceaf 100644 (file)
@@ -701,6 +701,39 @@ test13(const void *opaque ATTRIBUTE_UNUSED)
 
 #undef TEST_MAP
 
+static int
+test14(const void *opaque)
+{
+    const struct testBinaryOpData *data = opaque;
+    virBitmapPtr amap = NULL;
+    virBitmapPtr bmap = NULL;
+    virBitmapPtr resmap = NULL;
+    int ret = -1;
+
+    if (virBitmapParse(data->a, &amap, 256) < 0 ||
+        virBitmapParse(data->b, &bmap, 256) < 0 ||
+        virBitmapParse(data->res, &resmap, 256) < 0)
+        goto cleanup;
+
+    virBitmapSubtract(amap, bmap);
+
+    if (!virBitmapEqual(amap, resmap)) {
+        fprintf(stderr,
+                "\n bitmap subtraction failed: '%s' - '%s' != '%s'\n",
+                data->a, data->b, data->res);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    virBitmapFree(amap);
+    virBitmapFree(bmap);
+    virBitmapFree(resmap);
+
+    return ret;
+}
+
 
 #define TESTBINARYOP(A, B, RES, FUNC) \
     testBinaryOpData.a = A; \
@@ -750,6 +783,15 @@ mymain(void)
     if (virTestRun("test13", test13, NULL) < 0)
         ret = -1;
 
+    virTestCounterReset("test14-");
+    TESTBINARYOP("0", "0", "0,^0", test14);
+    TESTBINARYOP("0-3", "0", "1-3", test14);
+    TESTBINARYOP("0-3", "0,3", "1-2", test14);
+    TESTBINARYOP("0,^0", "0", "0,^0", test14);
+    TESTBINARYOP("0-3", "0-3", "0,^0", test14);
+    TESTBINARYOP("0-3", "0,^0", "0-3", test14);
+    TESTBINARYOP("0,2", "1,3", "0,2", test14);
+
     return ret;
 }