[PATCH v1 4/7] virbitmap: Introduce virBitmapDoesIntersect

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This internal API just checks if two bitmaps intersect or not.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virbitmap.c     | 20 ++++++++++++++++++++
 src/util/virbitmap.h     |  3 +++
 tests/virbitmaptest.c    | 26 ++++++++++++++++++++++++++
 4 files changed, 50 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 44403fd..256edd5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1016,6 +1016,7 @@ virBitmapClearBit;
 virBitmapCopy;
 virBitmapCountBits;
 virBitmapDataToString;
+virBitmapDoesIntersect;
 virBitmapEqual;
 virBitmapFormat;
 virBitmapFree;
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index 1029635..9f82eb9 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -732,3 +732,23 @@ virBitmapDataToString(void *data,
     virBitmapFree(map);
     return ret;
 }
+
+bool
+virBitmapDoesIntersect(virBitmapPtr b1,
+                       virBitmapPtr b2)
+{
+    size_t i;
+
+    if (b1->max_bit > b2->max_bit) {
+        virBitmapPtr tmp = b1;
+        b1 = b2;
+        b2 = tmp;
+    }
+
+    for (i = 0; i < b1->map_len; i++) {
+        if (b1->map[i] & b2->map[i])
+            return true;
+    }
+
+    return false;
+}
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
index 142a218..063516a 100644
--- a/src/util/virbitmap.h
+++ b/src/util/virbitmap.h
@@ -114,5 +114,8 @@ size_t virBitmapCountBits(virBitmapPtr bitmap)
 char *virBitmapDataToString(void *data,
                             int len)
     ATTRIBUTE_NONNULL(1);
+bool virBitmapDoesIntersect(virBitmapPtr b1,
+                            virBitmapPtr b2)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
 #endif
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
index 048946f..09b40d7 100644
--- a/tests/virbitmaptest.c
+++ b/tests/virbitmaptest.c
@@ -510,6 +510,30 @@ test9(const void *opaque ATTRIBUTE_UNUSED)
 }
 
 static int
+test10(const void *opaque ATTRIBUTE_UNUSED)
+{
+    int ret = -1;
+    virBitmapPtr b1 = NULL, b2 = NULL, b3 = NULL;
+
+    if (virBitmapParse("0-3,5-8,11-15", 0, &b1, 20) < 0 ||
+        virBitmapParse("4,9,10,16-19", 0, &b2, 20) < 0 ||
+        virBitmapParse("15", 0, &b3, 20) < 0)
+        goto cleanup;
+
+    if (virBitmapDoesIntersect(b1, b2) ||
+        virBitmapDoesIntersect(b2, b3) ||
+        !virBitmapDoesIntersect(b1, b3))
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    virBitmapFree(b1);
+    virBitmapFree(b2);
+    virBitmapFree(b3);
+    return ret;
+}
+
+static int
 mymain(void)
 {
     int ret = 0;
@@ -532,6 +556,8 @@ mymain(void)
         ret = -1;
     if (virtTestRun("test9", test9, NULL) < 0)
         ret = -1;
+    if (virtTestRun("test10", test10, NULL) < 0)
+        ret = -1;
 
     return ret;
 }
-- 
1.8.5.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]