0a7476
From a760b1ce1cf91d5aa83409fce9421c39633784fd Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <a760b1ce1cf91d5aa83409fce9421c39633784fd@dist-git>
0a7476
From: Andrea Bolognani <abologna@redhat.com>
0a7476
Date: Tue, 4 Jun 2019 16:22:04 +0200
0a7476
Subject: [PATCH] util: Introduce virBitmapUnion()
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
(cherry picked from commit 1b2ac8010cc1fe871f538b3f48c5e48213c5c074)
0a7476
0a7476
https://bugzilla.redhat.com/show_bug.cgi?id=1703661
0a7476
0a7476
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
0a7476
Message-Id: <20190604142207.2036-4-abologna@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
---
0a7476
 src/libvirt_private.syms |  1 +
0a7476
 src/util/virbitmap.c     | 27 +++++++++++++++++++++++++++
0a7476
 src/util/virbitmap.h     |  4 ++++
0a7476
 tests/virbitmaptest.c    | 38 ++++++++++++++++++++++++++++++++++++++
0a7476
 4 files changed, 70 insertions(+)
0a7476
0a7476
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
0a7476
index 3a99cb6774..e9c5b5ea33 100644
0a7476
--- a/src/libvirt_private.syms
0a7476
+++ b/src/libvirt_private.syms
0a7476
@@ -1475,6 +1475,7 @@ virBitmapSubtract;
0a7476
 virBitmapToData;
0a7476
 virBitmapToDataBuf;
0a7476
 virBitmapToString;
0a7476
+virBitmapUnion;
0a7476
 
0a7476
 
0a7476
 # util/virbuffer.h
0a7476
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
0a7476
index d6715025d4..7df0a2d4f3 100644
0a7476
--- a/src/util/virbitmap.c
0a7476
+++ b/src/util/virbitmap.c
0a7476
@@ -1269,6 +1269,33 @@ virBitmapIntersect(virBitmapPtr a,
0a7476
 }
0a7476
 
0a7476
 
0a7476
+/**
0a7476
+ * virBitmapUnion:
0a7476
+ * @a: bitmap, modified to contain result
0a7476
+ * @b: other bitmap
0a7476
+ *
0a7476
+ * Performs union of two bitmaps: a = union(a, b)
0a7476
+ *
0a7476
+ * Returns 0 on success, <0 on failure.
0a7476
+ */
0a7476
+int
0a7476
+virBitmapUnion(virBitmapPtr a,
0a7476
+               const virBitmap *b)
0a7476
+{
0a7476
+    size_t i;
0a7476
+
0a7476
+    if (a->nbits < b->nbits &&
0a7476
+        virBitmapExpand(a, b->nbits - 1) < 0) {
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    for (i = 0; i < b->map_len; i++)
0a7476
+        a->map[i] |= b->map[i];
0a7476
+
0a7476
+    return 0;
0a7476
+}
0a7476
+
0a7476
+
0a7476
 /**
0a7476
  * virBitmapSubtract:
0a7476
  * @a: minuend/result
0a7476
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
0a7476
index 312e7e2933..5934508d11 100644
0a7476
--- a/src/util/virbitmap.h
0a7476
+++ b/src/util/virbitmap.h
0a7476
@@ -151,6 +151,10 @@ bool virBitmapOverlaps(virBitmapPtr b1,
0a7476
 void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
0a7476
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
0a7476
 
0a7476
+int virBitmapUnion(virBitmapPtr a,
0a7476
+                   const virBitmap *b)
0a7476
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
0a7476
+
0a7476
 void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
0a7476
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
0a7476
 
0a7476
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
0a7476
index 2fbafc0a76..cafe865dde 100644
0a7476
--- a/tests/virbitmaptest.c
0a7476
+++ b/tests/virbitmaptest.c
0a7476
@@ -740,6 +740,34 @@ test14(const void *opaque)
0a7476
     return ret;
0a7476
 }
0a7476
 
0a7476
+/* virBitmapUnion() */
0a7476
+static int
0a7476
+test15(const void *opaque)
0a7476
+{
0a7476
+    const struct testBinaryOpData *data = opaque;
0a7476
+    VIR_AUTOPTR(virBitmap) amap = NULL;
0a7476
+    VIR_AUTOPTR(virBitmap) bmap = NULL;
0a7476
+    VIR_AUTOPTR(virBitmap) resmap = NULL;
0a7476
+
0a7476
+    if (!(amap = virBitmapParseUnlimited(data->a)) ||
0a7476
+        !(bmap = virBitmapParseUnlimited(data->b)) ||
0a7476
+        !(resmap = virBitmapParseUnlimited(data->res))) {
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    if (virBitmapUnion(amap, bmap) < 0)
0a7476
+        return -1;
0a7476
+
0a7476
+    if (!virBitmapEqual(amap, resmap)) {
0a7476
+        fprintf(stderr,
0a7476
+                "\n bitmap union failed: union('%s', '%s') != '%s'\n",
0a7476
+                data->a, data->b, data->res);
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    return 0;
0a7476
+}
0a7476
+
0a7476
 
0a7476
 #define TESTBINARYOP(A, B, RES, FUNC) \
0a7476
     testBinaryOpData.a = A; \
0a7476
@@ -798,6 +826,16 @@ mymain(void)
0a7476
     TESTBINARYOP("0-3", "0,^0", "0-3", test14);
0a7476
     TESTBINARYOP("0,2", "1,3", "0,2", test14);
0a7476
 
0a7476
+    /* virBitmapUnion() */
0a7476
+    virTestCounterReset("test15-");
0a7476
+    TESTBINARYOP("0-1", "0-1", "0-1", test15);
0a7476
+    TESTBINARYOP("0", "1", "0-1", test15);
0a7476
+    TESTBINARYOP("0-1", "2-3", "0-3", test15);
0a7476
+    TESTBINARYOP("0-3", "1-2", "0-3", test15);
0a7476
+    TESTBINARYOP("0,^0", "12345", "12345", test15);
0a7476
+    TESTBINARYOP("12345", "0,^0", "12345", test15);
0a7476
+    TESTBINARYOP("0,^0", "0,^0", "0,^0", test15);
0a7476
+
0a7476
     return ret;
0a7476
 }
0a7476
 
0a7476
-- 
0a7476
2.21.0
0a7476