summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mack <github@zonque.org>2015-07-31 19:21:51 +0200
committerDaniel Mack <github@zonque.org>2015-07-31 19:21:51 +0200
commit6ba32d1c2f04f3f01cea6ba1a082028412b5c474 (patch)
treef4bb92b0025bae9a1d8009b36dd7bfef23ae518c
parent3d5ff087ce729f7fe7fe5c1659f8546d57cb6282 (diff)
parent951c3eefacedcdbdb2cebf245f043aa3e81fb483 (diff)
Merge pull request #810 from mamikk/bitmap-fixes-v2
Bitmap fixes v2
-rw-r--r--src/basic/bitmap.c16
-rw-r--r--src/test/test-bitmap.c20
2 files changed, 33 insertions, 3 deletions
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c
index bf9d8d4d7c..7ea3357031 100644
--- a/src/basic/bitmap.c
+++ b/src/basic/bitmap.c
@@ -145,7 +145,10 @@ bool bitmap_isclear(Bitmap *b) {
void bitmap_clear(Bitmap *b) {
assert(b);
+ free(b->bitmaps);
+ b->bitmaps = NULL;
b->n_bitmaps = 0;
+ b->bitmaps_allocated = 0;
}
bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
@@ -184,6 +187,9 @@ bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
}
bool bitmap_equal(Bitmap *a, Bitmap *b) {
+ size_t common_n_bitmaps;
+ Bitmap *c;
+ unsigned i;
if (!a ^ !b)
return false;
@@ -191,8 +197,14 @@ bool bitmap_equal(Bitmap *a, Bitmap *b) {
if (!a)
return true;
- if (a->n_bitmaps != b->n_bitmaps)
+ common_n_bitmaps = MIN(a->n_bitmaps, b->n_bitmaps);
+ if (memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * common_n_bitmaps) != 0)
return false;
- return memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * a->n_bitmaps) == 0;
+ c = a->n_bitmaps > b->n_bitmaps ? a : b;
+ for (i = common_n_bitmaps; i < c->n_bitmaps; i++)
+ if (c->bitmaps[i] != 0)
+ return false;
+
+ return true;
}
diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c
index 96deeded7e..ff22117745 100644
--- a/src/test/test-bitmap.c
+++ b/src/test/test-bitmap.c
@@ -20,7 +20,7 @@
#include "bitmap.h"
int main(int argc, const char *argv[]) {
- _cleanup_bitmap_free_ Bitmap *b = NULL;
+ _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL;
Iterator it;
unsigned n = (unsigned) -1, i = 0;
@@ -101,5 +101,23 @@ int main(int argc, const char *argv[]) {
assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE);
+ bitmap_free(b);
+ b = NULL;
+ assert_se(bitmap_ensure_allocated(&b) == 0);
+ assert_se(bitmap_ensure_allocated(&b2) == 0);
+
+ assert_se(bitmap_equal(b, b2));
+ assert_se(bitmap_set(b, 0) == 0);
+ bitmap_unset(b, 0);
+ assert_se(bitmap_equal(b, b2));
+
+ assert_se(bitmap_set(b, 1) == 0);
+ bitmap_clear(b);
+ assert_se(bitmap_equal(b, b2));
+
+ assert_se(bitmap_set(b, 0) == 0);
+ assert_se(bitmap_set(b2, 0) == 0);
+ assert_se(bitmap_equal(b, b2));
+
return 0;
}