]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: Add a helper to check if all bits of a bitmap are clear
authorOsier Yang <jyang@redhat.com>
Fri, 5 Apr 2013 18:06:16 +0000 (02:06 +0800)
committerOsier Yang <jyang@redhat.com>
Sat, 6 Apr 2013 02:14:21 +0000 (10:14 +0800)
src/libvirt_private.syms
src/util/virbitmap.c
src/util/virbitmap.h
tests/virbitmaptest.c

index b7b963112f7662dc8bf0a7bbcadc9107311612a6..6b831b320172e29e96a8fbf55ac6720897490a5d 100644 (file)
@@ -1046,6 +1046,7 @@ virBitmapEqual;
 virBitmapFormat;
 virBitmapFree;
 virBitmapGetBit;
+virBitmapIsAllClear;
 virBitmapIsAllSet;
 virBitmapNew;
 virBitmapNewCopy;
index 21509ac3966338e6ac94269a77d3af42043a759a..998c302567ce9216a0e765e305babd578918af10 100644 (file)
@@ -590,6 +590,23 @@ bool virBitmapIsAllSet(virBitmapPtr bitmap)
     return true;
 }
 
+/**
+ * virBitmapIsAllClear:
+ * @bitmap: the bitmap to check
+ *
+ * check if all bits in @bitmap are clear
+ */
+bool virBitmapIsAllClear(virBitmapPtr bitmap)
+{
+    int i;
+
+    for (i = 0; i < bitmap->map_len; i++)
+        if (bitmap->map[i] != 0)
+            return false;
+
+    return true;
+}
+
 /**
  * virBitmapNextSetBit:
  * @bitmap: the bitmap
index 044c7a65fcf70a328279fefcaabcee4f58314b04..b682523fbe512a38ea3a9f44af28fb0dececfa18 100644 (file)
@@ -100,6 +100,9 @@ void virBitmapClearAll(virBitmapPtr bitmap)
 bool virBitmapIsAllSet(virBitmapPtr bitmap)
     ATTRIBUTE_NONNULL(1);
 
+bool virBitmapIsAllClear(virBitmapPtr bitmap)
+    ATTRIBUTE_NONNULL(1);
+
 ssize_t virBitmapNextSetBit(virBitmapPtr bitmap, ssize_t pos)
     ATTRIBUTE_NONNULL(1);
 
index 95d010a11348466b2aeb589ac6cd83899bebaf3c..90be0e281dfd873582bc6259860770b87bda3d2e 100644 (file)
@@ -125,6 +125,8 @@ static int test2(const void *data ATTRIBUTE_UNUSED)
         goto error;
 
     virBitmapClearAll(bitmap);
+    if (!virBitmapIsAllClear(bitmap))
+        goto error;
     if (testBit(bitmap, 0, size - 1, false) < 0)
         goto error;
     if (virBitmapCountBits(bitmap) != 0)
@@ -154,6 +156,9 @@ static int test3(const void *data ATTRIBUTE_UNUSED)
     if (!virBitmapIsAllSet(bitmap))
         goto error;
 
+    virBitmapClearAll(bitmap);
+    if (!virBitmapIsAllClear(bitmap))
+        goto error;
     ret = 0;
 
 error:
@@ -196,6 +201,9 @@ static int test4(const void *data ATTRIBUTE_UNUSED)
     if (virBitmapNextClearBit(bitmap, i) != -1)
         goto error;
 
+    if (!virBitmapIsAllClear(bitmap))
+        goto error;
+
     virBitmapFree(bitmap);
     bitmap = NULL;
 
@@ -406,6 +414,10 @@ static int test7(const void *v ATTRIBUTE_UNUSED)
         if (!virBitmapIsAllSet(bitmap))
             goto error;
 
+        virBitmapClearAll(bitmap);
+        if (!virBitmapIsAllClear(bitmap))
+            goto error;
+
         virBitmapFree(bitmap);
     }
 
@@ -416,6 +428,30 @@ error:
     return -1;
 }
 
+static int test8(const void *v ATTRIBUTE_UNUSED)
+{
+    virBitmapPtr bitmap = NULL;
+    char data[108] = {0x00,};
+
+    bitmap = virBitmapNewData(data, sizeof(data));
+    if (!bitmap)
+        goto error;
+
+    if (!virBitmapIsAllClear(bitmap))
+        goto error;
+
+    if (virBitmapSetBit(bitmap, 11) < 0)
+        goto error;
+
+    if (virBitmapIsAllClear(bitmap))
+        goto error;
+
+    return 0;
+error:
+    virBitmapFree(bitmap);
+    return -1;
+}
+
 static int
 mymain(void)
 {
@@ -435,7 +471,8 @@ mymain(void)
         ret = -1;
     if (virtTestRun("test7", 1, test7, NULL) < 0)
         ret = -1;
-
+    if (virtTestRun("test8", 1, test8, NULL) < 0)
+        ret = -1;
 
     return ret;
 }