c1c534
From cfbc47467f2174875f63493458a2f350ec21b65d Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <cfbc47467f2174875f63493458a2f350ec21b65d@dist-git>
c1c534
From: Martin Kletzander <mkletzan@redhat.com>
c1c534
Date: Wed, 31 Jan 2018 16:32:15 +0100
c1c534
Subject: [PATCH] util: Reintroduce virBitmapSubtract
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
c1c534
c1c534
Already introduced in the past with 9479642fd3c5, but then renamed to
c1c534
virBitmapIntersect by a908e9e45eb2.  This time we'll really use it.
c1c534
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
Reviewed-by: John Ferlan <jferlan@redhat.com>
c1c534
(cherry picked from commit 449442c34d20753ca57852d9d7957dd7ba868fae)
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
---
c1c534
 src/libvirt_private.syms |  1 +
c1c534
 src/util/virbitmap.c     | 22 ++++++++++++++++++++++
c1c534
 src/util/virbitmap.h     |  3 +++
c1c534
 tests/virbitmaptest.c    | 42 ++++++++++++++++++++++++++++++++++++++++++
c1c534
 4 files changed, 68 insertions(+)
c1c534
c1c534
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c1c534
index 1d895a3bf6..8cc8cbcc90 100644
c1c534
--- a/src/libvirt_private.syms
c1c534
+++ b/src/libvirt_private.syms
c1c534
@@ -1383,6 +1383,7 @@ virBitmapSetAll;
c1c534
 virBitmapSetBit;
c1c534
 virBitmapSetBitExpand;
c1c534
 virBitmapSize;
c1c534
+virBitmapSubtract;
c1c534
 virBitmapToData;
c1c534
 virBitmapToDataBuf;
c1c534
 virBitmapToString;
c1c534
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
c1c534
index 47d16ee222..7338f0255a 100644
c1c534
--- a/src/util/virbitmap.c
c1c534
+++ b/src/util/virbitmap.c
c1c534
@@ -1174,3 +1174,25 @@ virBitmapIntersect(virBitmapPtr a,
c1c534
     for (i = 0; i < max; i++)
c1c534
         a->map[i] &= b->map[i];
c1c534
 }
c1c534
+
c1c534
+
c1c534
+/**
c1c534
+ * virBitmapSubtract:
c1c534
+ * @a: minuend/result
c1c534
+ * @b: subtrahend
c1c534
+ *
c1c534
+ * Performs subtraction of two bitmaps: a = a - b
c1c534
+ */
c1c534
+void
c1c534
+virBitmapSubtract(virBitmapPtr a,
c1c534
+                  virBitmapPtr b)
c1c534
+{
c1c534
+    size_t i;
c1c534
+    size_t max = a->map_len;
c1c534
+
c1c534
+    if (max > b->map_len)
c1c534
+        max = b->map_len;
c1c534
+
c1c534
+    for (i = 0; i < max; i++)
c1c534
+        a->map[i] &= ~b->map[i];
c1c534
+}
c1c534
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
c1c534
index e964a3edc9..7b2bea8b53 100644
c1c534
--- a/src/util/virbitmap.h
c1c534
+++ b/src/util/virbitmap.h
c1c534
@@ -150,4 +150,7 @@ bool virBitmapOverlaps(virBitmapPtr b1,
c1c534
 void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
c1c534
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
c1c534
 
c1c534
+void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
c1c534
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
c1c534
+
c1c534
 #endif
c1c534
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
c1c534
index 9daa8316f3..5bf30b2b1c 100644
c1c534
--- a/tests/virbitmaptest.c
c1c534
+++ b/tests/virbitmaptest.c
c1c534
@@ -701,6 +701,39 @@ test13(const void *opaque ATTRIBUTE_UNUSED)
c1c534
 
c1c534
 #undef TEST_MAP
c1c534
 
c1c534
+static int
c1c534
+test14(const void *opaque)
c1c534
+{
c1c534
+    const struct testBinaryOpData *data = opaque;
c1c534
+    virBitmapPtr amap = NULL;
c1c534
+    virBitmapPtr bmap = NULL;
c1c534
+    virBitmapPtr resmap = NULL;
c1c534
+    int ret = -1;
c1c534
+
c1c534
+    if (virBitmapParse(data->a, &amap, 256) < 0 ||
c1c534
+        virBitmapParse(data->b, &bmap, 256) < 0 ||
c1c534
+        virBitmapParse(data->res, &resmap, 256) < 0)
c1c534
+        goto cleanup;
c1c534
+
c1c534
+    virBitmapSubtract(amap, bmap);
c1c534
+
c1c534
+    if (!virBitmapEqual(amap, resmap)) {
c1c534
+        fprintf(stderr,
c1c534
+                "\n bitmap subtraction failed: '%s' - '%s' != '%s'\n",
c1c534
+                data->a, data->b, data->res);
c1c534
+        goto cleanup;
c1c534
+    }
c1c534
+
c1c534
+    ret = 0;
c1c534
+
c1c534
+ cleanup:
c1c534
+    virBitmapFree(amap);
c1c534
+    virBitmapFree(bmap);
c1c534
+    virBitmapFree(resmap);
c1c534
+
c1c534
+    return ret;
c1c534
+}
c1c534
+
c1c534
 
c1c534
 #define TESTBINARYOP(A, B, RES, FUNC)                                         \
c1c534
     testBinaryOpData.a = A;                                                   \
c1c534
@@ -750,6 +783,15 @@ mymain(void)
c1c534
     if (virTestRun("test13", test13, NULL) < 0)
c1c534
         ret = -1;
c1c534
 
c1c534
+    virTestCounterReset("test14-");
c1c534
+    TESTBINARYOP("0", "0", "0,^0", test14);
c1c534
+    TESTBINARYOP("0-3", "0", "1-3", test14);
c1c534
+    TESTBINARYOP("0-3", "0,3", "1-2", test14);
c1c534
+    TESTBINARYOP("0,^0", "0", "0,^0", test14);
c1c534
+    TESTBINARYOP("0-3", "0-3", "0,^0", test14);
c1c534
+    TESTBINARYOP("0-3", "0,^0", "0-3", test14);
c1c534
+    TESTBINARYOP("0,2", "1,3", "0,2", test14);
c1c534
+
c1c534
     return ret;
c1c534
 }
c1c534
 
c1c534
-- 
c1c534
2.16.1
c1c534