]> git.ozlabs.org Git - ccan/commitdiff
bitmap: Add helper macro to statically declare bitmaps
authorDavid Gibson <david@gibson.dropbear.id.au>
Thu, 5 Sep 2013 14:37:14 +0000 (00:37 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Thu, 5 Sep 2013 14:37:14 +0000 (00:37 +1000)
For use as direct locals, or when the size is a constant, inside
structure definitions.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/bitmap/_info
ccan/bitmap/bitmap.h
ccan/bitmap/test/run.c

index 26214726f1dd6ac3ad8bb5e68106054ff978f811..2038058ff205846a4ce38ed2ca1f424ab60cac6c 100644 (file)
@@ -23,6 +23,7 @@ int main(int argc, char *argv[])
 
         if (strcmp(argv[1], "testdepends") == 0) {
                 printf("ccan/array_size\n");
+               printf("ccan/foreach\n");
                 return 0;
         }
 
index 259b35149a90b3286d1ead12ec2c00f0e5dc47dc..f43449d79f20f6b280a8d1a8881a3e4fe0ff3610 100644 (file)
@@ -22,6 +22,9 @@ typedef struct {
        bitmap_word w;
 } bitmap;
 
+#define BITMAP_DECLARE(_name, _nbits) \
+       bitmap (_name)[BITMAP_NWORDS(_nbits)]
+
 static inline size_t bitmap_sizeof(int nbits)
 {
        return BITMAP_NWORDS(nbits) * sizeof(bitmap_word);
index 5911a363429d204330225991af08733797aec429..b67172a582319955e5344f101f58ade38730424e 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/bitmap/bitmap.h>
 #include <ccan/tap/tap.h>
 #include <ccan/array_size/array_size.h>
+#include <ccan/foreach/foreach.h>
 
 int bitmap_sizes[] = {
        1, 2, 3, 4, 5, 6, 7, 8,
@@ -11,13 +12,21 @@ int bitmap_sizes[] = {
 #define NSIZES ARRAY_SIZE(bitmap_sizes)
 #define NTESTS 9
 
-static void test_sizes(int nbits)
+static void test_sizes(int nbits, bool dynalloc)
 {
-       bitmap *bitmap = bitmap_alloc(nbits);
+       BITMAP_DECLARE(sbitmap, nbits);
+       uint32_t marker;
+       bitmap *bitmap;
        int i, j;
        bool wrong;
 
-       ok1(bitmap != NULL);
+       if (dynalloc) {
+               bitmap = bitmap_alloc(nbits);
+               ok1(bitmap != NULL);
+       } else {
+               bitmap = sbitmap;
+               marker = 0xdeadbeef;
+       }
 
        bitmap_zero(bitmap, nbits);
        wrong = false;
@@ -78,20 +87,28 @@ static void test_sizes(int nbits)
                wrong = wrong || bitmap_full(bitmap, nbits);
        }
        ok1(!wrong);
-               
-       free(bitmap);
+       
+       if (dynalloc) {
+               free(bitmap);
+       } else {
+               ok1(marker == 0xdeadbeef);
+       }
 }
 
 int main(void)
 {
        int i;
+       bool dynalloc;
 
        /* This is how many tests you plan to run */
-       plan_tests(NSIZES * NTESTS);
+       plan_tests(NSIZES * NTESTS * 2);
 
        for (i = 0; i < NSIZES; i++) {
-               diag("Testing %d-bit bitmap", bitmap_sizes[i]);
-               test_sizes(bitmap_sizes[i]);
+               foreach_int(dynalloc, false, true) {
+                       diag("Testing %d-bit bitmap (%s allocation)",
+                            bitmap_sizes[i], dynalloc ? "dynamic" : "static");
+                       test_sizes(bitmap_sizes[i], dynalloc);
+               }
        }
 
        /* This exits depending on whether all tests passed */