]> git.ozlabs.org Git - ccan/blobdiff - ccan/bitmap/bitmap.h
bitmap: Fix some bugs on 32-bit platforms
[ccan] / ccan / bitmap / bitmap.h
index 34faf500ca23a71e97d52a1d7cfd820986cbc17d..beeb1e953cab473aaa0bcd70cb6755842629a988 100644 (file)
@@ -15,6 +15,9 @@ typedef unsigned long bitmap_word;
 #define BITMAP_NWORDS(_n)      \
        (((_n) + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
 
+#define BITMAP_WORD_0          (0)
+#define BITMAP_WORD_1          ((bitmap_word)-1UL)
+
 /*
  * We wrap each word in a structure for type checking.
  */
@@ -30,11 +33,6 @@ static inline size_t bitmap_sizeof(unsigned long nbits)
        return BITMAP_NWORDS(nbits) * sizeof(bitmap_word);
 }
 
-static inline bitmap *bitmap_alloc(unsigned long nbits)
-{
-       return malloc(bitmap_sizeof(nbits));
-}
-
 static inline bitmap_word bitmap_bswap(bitmap_word w)
 {
        if (BITMAP_WORD_BITS == 32)
@@ -192,4 +190,57 @@ static inline bool bitmap_empty(const bitmap *bitmap, unsigned long nbits)
        return true;
 }
 
+unsigned long bitmap_ffs(const bitmap *bitmap,
+                        unsigned long n, unsigned long m);
+
+/*
+ * Allocation functions
+ */
+static inline bitmap *bitmap_alloc(unsigned long nbits)
+{
+       return malloc(bitmap_sizeof(nbits));
+}
+
+static inline bitmap *bitmap_alloc0(unsigned long nbits)
+{
+       bitmap *bitmap;
+
+       bitmap = bitmap_alloc(nbits);
+       if (bitmap)
+               bitmap_zero(bitmap, nbits);
+       return bitmap;
+}
+
+static inline bitmap *bitmap_alloc1(unsigned long nbits)
+{
+       bitmap *bitmap;
+
+       bitmap = bitmap_alloc(nbits);
+       if (bitmap)
+               bitmap_fill(bitmap, nbits);
+       return bitmap;
+}
+
+static inline bitmap *bitmap_realloc0(bitmap *bitmap,
+                                     unsigned long obits, unsigned long nbits)
+{
+       bitmap = realloc(bitmap, bitmap_sizeof(nbits));
+
+       if ((nbits > obits) && bitmap)
+               bitmap_zero_range(bitmap, obits, nbits);
+
+       return bitmap;
+}
+
+static inline bitmap *bitmap_realloc1(bitmap *bitmap,
+                                     unsigned long obits, unsigned long nbits)
+{
+       bitmap = realloc(bitmap, bitmap_sizeof(nbits));
+
+       if ((nbits > obits) && bitmap)
+               bitmap_fill_range(bitmap, obits, nbits);
+
+       return bitmap;
+}
+
 #endif /* CCAN_BITMAP_H_ */