Commit 6a9c567b authored by David Gibson's avatar David Gibson

bitmap: Add helper macro to statically declare bitmaps

For use as direct locals, or when the size is a constant, inside
structure definitions.
Signed-off-by: default avatarDavid Gibson <david@gibson.dropbear.id.au>
parent e0b9db61
...@@ -23,6 +23,7 @@ int main(int argc, char *argv[]) ...@@ -23,6 +23,7 @@ int main(int argc, char *argv[])
if (strcmp(argv[1], "testdepends") == 0) { if (strcmp(argv[1], "testdepends") == 0) {
printf("ccan/array_size\n"); printf("ccan/array_size\n");
printf("ccan/foreach\n");
return 0; return 0;
} }
......
...@@ -22,6 +22,9 @@ typedef struct { ...@@ -22,6 +22,9 @@ typedef struct {
bitmap_word w; bitmap_word w;
} bitmap; } bitmap;
#define BITMAP_DECLARE(_name, _nbits) \
bitmap (_name)[BITMAP_NWORDS(_nbits)]
static inline size_t bitmap_sizeof(int nbits) static inline size_t bitmap_sizeof(int nbits)
{ {
return BITMAP_NWORDS(nbits) * sizeof(bitmap_word); return BITMAP_NWORDS(nbits) * sizeof(bitmap_word);
......
#include <ccan/bitmap/bitmap.h> #include <ccan/bitmap/bitmap.h>
#include <ccan/tap/tap.h> #include <ccan/tap/tap.h>
#include <ccan/array_size/array_size.h> #include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>
int bitmap_sizes[] = { int bitmap_sizes[] = {
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
...@@ -11,13 +12,21 @@ int bitmap_sizes[] = { ...@@ -11,13 +12,21 @@ int bitmap_sizes[] = {
#define NSIZES ARRAY_SIZE(bitmap_sizes) #define NSIZES ARRAY_SIZE(bitmap_sizes)
#define NTESTS 9 #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; int i, j;
bool wrong; bool wrong;
if (dynalloc) {
bitmap = bitmap_alloc(nbits);
ok1(bitmap != NULL); ok1(bitmap != NULL);
} else {
bitmap = sbitmap;
marker = 0xdeadbeef;
}
bitmap_zero(bitmap, nbits); bitmap_zero(bitmap, nbits);
wrong = false; wrong = false;
...@@ -79,19 +88,27 @@ static void test_sizes(int nbits) ...@@ -79,19 +88,27 @@ static void test_sizes(int nbits)
} }
ok1(!wrong); ok1(!wrong);
if (dynalloc) {
free(bitmap); free(bitmap);
} else {
ok1(marker == 0xdeadbeef);
}
} }
int main(void) int main(void)
{ {
int i; int i;
bool dynalloc;
/* This is how many tests you plan to run */ /* This is how many tests you plan to run */
plan_tests(NSIZES * NTESTS); plan_tests(NSIZES * NTESTS * 2);
for (i = 0; i < NSIZES; i++) { for (i = 0; i < NSIZES; i++) {
diag("Testing %d-bit bitmap", bitmap_sizes[i]); foreach_int(dynalloc, false, true) {
test_sizes(bitmap_sizes[i]); 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 */ /* This exits depending on whether all tests passed */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment