]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virBitmapUnion()
authorAndrea Bolognani <abologna@redhat.com>
Thu, 30 May 2019 17:08:28 +0000 (19:08 +0200)
committerAndrea Bolognani <abologna@redhat.com>
Tue, 4 Jun 2019 07:29:35 +0000 (09:29 +0200)
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virbitmap.c
src/util/virbitmap.h
tests/virbitmaptest.c

index f2b8dc445d0b32703ecc62f93a91eff733c3b249..c0e6eefda5e964ff6d454bd15ba251c0beb3569d 100644 (file)
@@ -1517,6 +1517,7 @@ virBitmapSubtract;
 virBitmapToData;
 virBitmapToDataBuf;
 virBitmapToString;
+virBitmapUnion;
 
 
 # util/virbuffer.h
index 91df4b8601d3421d274d7f338513384ed6094e00..a61f4b20956d8e59562a47cbb97993edabaf8ae5 100644 (file)
@@ -1260,6 +1260,33 @@ virBitmapIntersect(virBitmapPtr a,
 }
 
 
+/**
+ * virBitmapUnion:
+ * @a: bitmap, modified to contain result
+ * @b: other bitmap
+ *
+ * Performs union of two bitmaps: a = union(a, b)
+ *
+ * Returns 0 on success, <0 on failure.
+ */
+int
+virBitmapUnion(virBitmapPtr a,
+               const virBitmap *b)
+{
+    size_t i;
+
+    if (a->nbits < b->nbits &&
+        virBitmapExpand(a, b->nbits - 1) < 0) {
+        return -1;
+    }
+
+    for (i = 0; i < b->map_len; i++)
+        a->map[i] |= b->map[i];
+
+    return 0;
+}
+
+
 /**
  * virBitmapSubtract:
  * @a: minuend/result
index 38dc06412a8ef4c8516f823da2944588c77aebc7..8696214da815dc1c6a3e8c98e8bc068c26299d68 100644 (file)
@@ -149,6 +149,10 @@ bool virBitmapOverlaps(virBitmapPtr b1,
 void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+int virBitmapUnion(virBitmapPtr a,
+                   const virBitmap *b)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
index 2fbafc0a76321c447b7a3f3f79004b8b91dfb314..cafe865dde779f9c35909ae72a110a3de5a5c175 100644 (file)
@@ -740,6 +740,34 @@ test14(const void *opaque)
     return ret;
 }
 
+/* virBitmapUnion() */
+static int
+test15(const void *opaque)
+{
+    const struct testBinaryOpData *data = opaque;
+    VIR_AUTOPTR(virBitmap) amap = NULL;
+    VIR_AUTOPTR(virBitmap) bmap = NULL;
+    VIR_AUTOPTR(virBitmap) resmap = NULL;
+
+    if (!(amap = virBitmapParseUnlimited(data->a)) ||
+        !(bmap = virBitmapParseUnlimited(data->b)) ||
+        !(resmap = virBitmapParseUnlimited(data->res))) {
+        return -1;
+    }
+
+    if (virBitmapUnion(amap, bmap) < 0)
+        return -1;
+
+    if (!virBitmapEqual(amap, resmap)) {
+        fprintf(stderr,
+                "\n bitmap union failed: union('%s', '%s') != '%s'\n",
+                data->a, data->b, data->res);
+        return -1;
+    }
+
+    return 0;
+}
+
 
 #define TESTBINARYOP(A, B, RES, FUNC) \
     testBinaryOpData.a = A; \
@@ -798,6 +826,16 @@ mymain(void)
     TESTBINARYOP("0-3", "0,^0", "0-3", test14);
     TESTBINARYOP("0,2", "1,3", "0,2", test14);
 
+    /* virBitmapUnion() */
+    virTestCounterReset("test15-");
+    TESTBINARYOP("0-1", "0-1", "0-1", test15);
+    TESTBINARYOP("0", "1", "0-1", test15);
+    TESTBINARYOP("0-1", "2-3", "0-3", test15);
+    TESTBINARYOP("0-3", "1-2", "0-3", test15);
+    TESTBINARYOP("0,^0", "12345", "12345", test15);
+    TESTBINARYOP("12345", "0,^0", "12345", test15);
+    TESTBINARYOP("0,^0", "0,^0", "0,^0", test15);
+
     return ret;
 }