summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@ugent.be>2017-05-24 13:15:38 +0000
committerSander Vrijders <sander.vrijders@ugent.be>2017-05-24 13:15:38 +0000
commitf986d188898f8b7cd6db3442d29728ca78dad76e (patch)
tree03ffb3b0b75affb94eba78408c161dba0494f94a /src
parentbe49633c7069c2a2273add8c1dd0425a8f25dd1e (diff)
parenta3ede18c5d3e652e9d0bade4b28193e666198574 (diff)
downloadouroboros-f986d188898f8b7cd6db3442d29728ca78dad76e.tar.gz
ouroboros-f986d188898f8b7cd6db3442d29728ca78dad76e.zip
Merged in dstaesse/ouroboros/be-lib-bitmap (pull request #511)
lib: Fix bug in bitmap
Diffstat (limited to 'src')
-rw-r--r--src/lib/bitmap.c11
-rw-r--r--src/lib/tests/bitmap_test.c15
2 files changed, 18 insertions, 8 deletions
diff --git a/src/lib/bitmap.c b/src/lib/bitmap.c
index bf9bb99d..ec663ec7 100644
--- a/src/lib/bitmap.c
+++ b/src/lib/bitmap.c
@@ -50,7 +50,7 @@ static size_t find_next_zero_bit(const size_t * addr,
tmp = ~addr[start];
while (!tmp) {
start++;
- if (start >= (nbits / BITS_PER_LONG))
+ if (start >= DIV_ROUND_UP(nbits, BITS_PER_LONG))
return nbits;
tmp = ~addr[start];
@@ -69,8 +69,7 @@ static size_t find_next_zero_bit(const size_t * addr,
static void bitmap_zero(size_t * dst,
size_t nbits)
{
- size_t len = BITS_TO_LONGS(nbits) * sizeof(size_t);
- memset(dst, 0, len);
+ memset(dst, 0, BITS_TO_LONGS(nbits) * sizeof(size_t));
}
static void bitmap_clear(size_t * map,
@@ -92,8 +91,8 @@ static void bitmap_set(size_t * map,
}
struct bmp {
- ssize_t offset;
- size_t size;
+ ssize_t offset;
+ size_t size;
size_t * bitmap;
};
@@ -116,7 +115,7 @@ struct bmp * bmp_create(size_t bits,
return NULL;
}
- tmp->size = bits;
+ tmp->size = bits;
tmp->offset = offset;
bitmap_zero(tmp->bitmap, bits);
diff --git a/src/lib/tests/bitmap_test.c b/src/lib/tests/bitmap_test.c
index e438f217..4f58de3b 100644
--- a/src/lib/tests/bitmap_test.c
+++ b/src/lib/tests/bitmap_test.c
@@ -30,7 +30,7 @@
int bitmap_test(int argc, char ** argv)
{
struct bmp * bmp;
- size_t bits = BITMAP_SIZE;
+ ssize_t bits = BITMAP_SIZE;
ssize_t id;
int i;
ssize_t r;
@@ -57,8 +57,19 @@ int bitmap_test(int argc, char ** argv)
for (i = offset; i < BITMAP_SIZE + 5 + offset; i++) {
id = bmp_allocate(bmp);
- if (!bmp_is_id_valid(bmp, id))
+ if (!bmp_is_id_valid(bmp, id)) {
+ if (i < BITMAP_SIZE + offset) {
+ printf("Failed valid ID %d (%zd).\n", i, id);
+ bmp_destroy(bmp);
+ return -1;
+ }
+ if (id >= offset && id < bits + offset) {
+ printf("Valid ID %zd returned invalid.\n", id);
+ bmp_destroy(bmp);
+ return -1;
+ }
continue;
+ }
if (!bmp_is_id_used(bmp, id)) {
printf("ID not marked in use.\n");