From 7ee95a3809aab3152837ceb5ffcd001ee9e83fde Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Sat, 23 Sep 2017 12:27:23 +0200 Subject: lib: Clean up bitmap implementation There was a return -1 in the allocate call which is unsafe since -1 may be a valid id in the bitmap. Since it's a data structure for internal use, I replaced the NULL checks with assertions. --- include/ouroboros/bitmap.h | 10 +++--- src/lib/bitmap.c | 85 +++++++++++++++++++++------------------------- 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/include/ouroboros/bitmap.h b/include/ouroboros/bitmap.h index a5921fc7..d40bb509 100644 --- a/include/ouroboros/bitmap.h +++ b/include/ouroboros/bitmap.h @@ -32,17 +32,17 @@ struct bmp; struct bmp * bmp_create(size_t bits, ssize_t offset); -void bmp_destroy(struct bmp * b); +void bmp_destroy(struct bmp * bmp); -ssize_t bmp_allocate(struct bmp * instance); +ssize_t bmp_allocate(struct bmp * bmp); -int bmp_release(struct bmp * instance, +int bmp_release(struct bmp * bmp, ssize_t id); -bool bmp_is_id_valid(struct bmp * b, +bool bmp_is_id_valid(struct bmp * bmp, ssize_t id); -bool bmp_is_id_used(struct bmp * b, +bool bmp_is_id_used(struct bmp * bmp, ssize_t id); #endif /* OUROBOROS_BITMAP_H */ diff --git a/src/lib/bitmap.c b/src/lib/bitmap.c index 6ddaaca4..75a201bc 100644 --- a/src/lib/bitmap.c +++ b/src/lib/bitmap.c @@ -100,69 +100,65 @@ struct bmp { struct bmp * bmp_create(size_t bits, ssize_t offset) { - struct bmp * tmp; + struct bmp * bmp; - if (bits == 0) - return NULL; + assert(bits); - tmp = malloc(sizeof(*tmp)); - if (tmp == NULL) + bmp = malloc(sizeof(*bmp)); + if (bmp == NULL) return NULL; - tmp->bitmap = malloc(BITS_TO_LONGS(bits) * sizeof(size_t)); - if (tmp->bitmap == NULL) { - free(tmp); + bmp->bitmap = malloc(BITS_TO_LONGS(bits) * sizeof(size_t)); + if (bmp->bitmap == NULL) { + free(bmp); return NULL; } - tmp->size = bits; - tmp->offset = offset; - bitmap_zero(tmp->bitmap, bits); + bmp->size = bits; + bmp->offset = offset; + bitmap_zero(bmp->bitmap, bits); - return tmp; + return bmp; } -void bmp_destroy(struct bmp * b) +void bmp_destroy(struct bmp * bmp) { - if (b == NULL) - return; + assert(bmp); - if (b->bitmap != NULL) - free(b->bitmap); + if (bmp->bitmap != NULL) + free(bmp->bitmap); - free(b); + free(bmp); } -static ssize_t bad_id(struct bmp * b) +static ssize_t bad_id(struct bmp * bmp) { - if (b == NULL) - return -1; + assert(bmp); - return b->offset - 1; + return bmp->offset - 1; } -ssize_t bmp_allocate(struct bmp * b) +ssize_t bmp_allocate(struct bmp * bmp) { size_t id; - if (b == NULL) - return -1; + assert(bmp); - id = find_next_zero_bit(b->bitmap, b->size); - if (id >= b->size) - return bad_id(b); + id = find_next_zero_bit(bmp->bitmap, bmp->size); + if (id >= bmp->size) + return bad_id(bmp); - bitmap_set(b->bitmap, id); + bitmap_set(bmp->bitmap, id); - return id + b->offset; + return id + bmp->offset; } -static bool is_id_valid(struct bmp * b, +static bool is_id_valid(struct bmp * bmp, ssize_t id) { - assert(b); + assert(bmp); - if ((id < b->offset) || (id > (ssize_t) (b->offset + b->size))) + if ((id < bmp->offset) || (id > (ssize_t) (bmp->offset + bmp->size))) return false; return true; @@ -177,34 +173,31 @@ static bool is_id_used(size_t * map, return (*p & mask) != 0; } -bool bmp_is_id_valid(struct bmp * b, +bool bmp_is_id_valid(struct bmp * bmp, ssize_t id) { - if (b == NULL) - return false; + assert(bmp); - return is_id_valid(b, id); + return is_id_valid(bmp, id); } -int bmp_release(struct bmp * b, +int bmp_release(struct bmp * bmp, ssize_t id) { - if (b == NULL) - return -1; + assert(bmp); - if (!is_id_valid(b, id)) + if (!is_id_valid(bmp, id)) return -1; - bitmap_clear(b->bitmap, id - b->offset); + bitmap_clear(bmp->bitmap, id - bmp->offset); return 0; } -bool bmp_is_id_used(struct bmp * b, +bool bmp_is_id_used(struct bmp * bmp, ssize_t id) { - if (b == NULL) - return false; + assert(bmp); - return is_id_used(b->bitmap, id - b->offset); + return is_id_used(bmp->bitmap, id - bmp->offset); } -- cgit v1.2.3