[libvirt] [PATCH] bitmap: add way to find next clear bit

Stefan Berger stefanb at linux.vnet.ibm.com
Sat Feb 2 04:41:27 UTC 2013


On 02/01/2013 09:16 PM, Eric Blake wrote:
> We had an easy way to iterate set bits, but not for iterating
> cleared bits.
>
> * src/util/virbitmap.h (virBitmapNextClearBit): New prototype.
> * src/util/virbitmap.c (virBitmapNextClearBit): Implement it.
> * src/libvirt_private.syms (bitmap.h): Export it.
> * tests/virbitmaptest.c (test4): Test it.
> ---


I didn't test it yours so far... Here's at least the part of the test I 
had fabricated (your bitmap extension seems better):

Index: libvirt/tests/virbitmaptest.c
===================================================================
--- libvirt.orig/tests/virbitmaptest.c
+++ libvirt/tests/virbitmaptest.c
@@ -390,6 +390,74 @@ error:
      return -1;
  }

+/* test for virBitmapNextClearBit */
+static int test8(const void *data ATTRIBUTE_UNUSED)
+{
+    const char *bitsString = "0, 2-4, 6-10, 12, 14-18, 20, 22, 25, 
28-33, 35, "
+        "41-42, 45-46";
+    int size = 48;
+    int noBitsPos[] = {
+        1,  5,  11, 13, 19, 21, 23, 24,
+        26, 27, 34, 36, 37, 38, 39, 40,
+        43, 44, 47
+    };
+    int npos = 19;
+    virBitmapPtr bitmap = NULL;
+    int i, j;
+
+    /* 1. zero set */
+
+    bitmap = virBitmapNew(size);
+    if (!bitmap)
+        goto error;
+
+    if (virBitmapNextClearBit(bitmap, -1) != 0)
+        goto error;
+
+    virBitmapFree(bitmap);
+    bitmap = NULL;
+
+    /* 2. partial set */
+
+    if (virBitmapParse(bitsString, 0, &bitmap, size) < 0)
+        goto error;
+    if (!bitmap)
+        goto error;
+
+    j = 0;
+    i = -1;
+
+    while (j < npos) {
+        i = virBitmapNextClearBit(bitmap, i);
+        if (i != noBitsPos[j++])
+            goto error;
+    }
+
+    if (virBitmapNextSetBit(bitmap, i) > 0)
+        goto error;
+
+    /* 3. full set */
+
+    i = -1;
+    virBitmapSetAll(bitmap);
+
+    for (j = 0; j < size; j++) {
+        i = virBitmapNextSetBit(bitmap, i);
+        if (i != j)
+            goto error;
+    }
+
+    if (virBitmapNextClearBit(bitmap, i) > 0)
+        goto error;
+
+    virBitmapFree(bitmap);
+    return 0;
+
+error:
+    virBitmapFree(bitmap);
+    return -1;
+}
+
  static int
  mymain(void)
  {
@@ -409,6 +477,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;


Maybe we can merge the two for a joint effort :-)  -- leaving test 4 
untouched.

Patch 3 of the series has calls missing regarding clearing of the bitmap 
upon failure and VM shutdown. I added that after sending.

Regards,
    Stefan




More information about the libvir-list mailing list