]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bitmap.c
TODO: drop implemented entry
[thirdparty/systemd.git] / src / shared / bitmap.c
index a956a42cab0a5c3dafff4e011fa32420cc1db768..9aa766139bb05c232f13233c19e42f423353cfea 100644 (file)
@@ -1,4 +1,4 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <errno.h>
 #include <stddef.h>
 #include "macro.h"
 #include "memory-util.h"
 
-struct Bitmap {
-        uint64_t *bitmaps;
-        size_t n_bitmaps;
-        size_t bitmaps_allocated;
-};
-
 /* Bitmaps are only meant to store relatively small numbers
  * (corresponding to, say, an enum), so it is ok to limit
  * the max entry. 64k should be plenty. */
 #define BITMAPS_MAX_ENTRY 0xffff
 
 /* This indicates that we reached the end of the bitmap */
-#define BITMAP_END ((unsigned) -1)
+#define BITMAP_END (UINT_MAX)
 
 #define BITMAP_NUM_TO_OFFSET(n)           ((n) / (sizeof(uint64_t) * 8))
 #define BITMAP_NUM_TO_REM(n)              ((n) % (sizeof(uint64_t) * 8))
 #define BITMAP_OFFSET_TO_NUM(offset, rem) ((offset) * sizeof(uint64_t) * 8 + (rem))
 
-Bitmap *bitmap_new(void) {
+Bitmapbitmap_new(void) {
         return new0(Bitmap, 1);
 }
 
-Bitmap *bitmap_copy(Bitmap *b) {
+Bitmapbitmap_copy(Bitmap *b) {
         Bitmap *ret;
 
         ret = bitmap_new();
@@ -45,16 +39,16 @@ Bitmap *bitmap_copy(Bitmap *b) {
         if (!ret->bitmaps)
                 return mfree(ret);
 
-        ret->n_bitmaps = ret->bitmaps_allocated = b->n_bitmaps;
+        ret->n_bitmaps = b->n_bitmaps;
         return ret;
 }
 
-void bitmap_free(Bitmap *b) {
+Bitmap* bitmap_free(Bitmap *b) {
         if (!b)
-                return;
+                return NULL;
 
         free(b->bitmaps);
-        free(b);
+        return mfree(b);
 }
 
 int bitmap_ensure_allocated(Bitmap **b) {
@@ -87,7 +81,7 @@ int bitmap_set(Bitmap *b, unsigned n) {
         offset = BITMAP_NUM_TO_OFFSET(n);
 
         if (offset >= b->n_bitmaps) {
-                if (!GREEDY_REALLOC0(b->bitmaps, b->bitmaps_allocated, offset + 1))
+                if (!GREEDY_REALLOC0(b->bitmaps, offset + 1))
                         return -ENOMEM;
 
                 b->n_bitmaps = offset + 1;
@@ -117,7 +111,7 @@ void bitmap_unset(Bitmap *b, unsigned n) {
         b->bitmaps[offset] &= ~bitmask;
 }
 
-bool bitmap_isset(Bitmap *b, unsigned n) {
+bool bitmap_isset(const Bitmap *b, unsigned n) {
         uint64_t bitmask;
         unsigned offset;
 
@@ -134,7 +128,7 @@ bool bitmap_isset(Bitmap *b, unsigned n) {
         return !!(b->bitmaps[offset] & bitmask);
 }
 
-bool bitmap_isclear(Bitmap *b) {
+bool bitmap_isclear(const Bitmap *b) {
         unsigned i;
 
         if (!b)
@@ -148,16 +142,14 @@ bool bitmap_isclear(Bitmap *b) {
 }
 
 void bitmap_clear(Bitmap *b) {
-
         if (!b)
                 return;
 
         b->bitmaps = mfree(b->bitmaps);
         b->n_bitmaps = 0;
-        b->bitmaps_allocated = 0;
 }
 
-bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
+bool bitmap_iterate(const Bitmap *b, Iterator *i, unsigned *n) {
         uint64_t bitmask;
         unsigned offset, rem;
 
@@ -171,9 +163,9 @@ bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
         rem = BITMAP_NUM_TO_REM(i->idx);
         bitmask = UINT64_C(1) << rem;
 
-        for (; offset < b->n_bitmaps; offset ++) {
+        for (; offset < b->n_bitmaps; offset++) {
                 if (b->bitmaps[offset]) {
-                        for (; bitmask; bitmask <<= 1, rem ++) {
+                        for (; bitmask; bitmask <<= 1, rem++) {
                                 if (b->bitmaps[offset] & bitmask) {
                                         *n = BITMAP_OFFSET_TO_NUM(offset, rem);
                                         i->idx = *n + 1;
@@ -192,9 +184,9 @@ bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
         return false;
 }
 
-bool bitmap_equal(Bitmap *a, Bitmap *b) {
+bool bitmap_equal(const Bitmap *a, const Bitmap *b) {
         size_t common_n_bitmaps;
-        Bitmap *c;
+        const Bitmap *c;
         unsigned i;
 
         if (a == b)