X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fbitmap%2Fbitmap.h;h=beeb1e953cab473aaa0bcd70cb6755842629a988;hp=34faf500ca23a71e97d52a1d7cfd820986cbc17d;hb=cdd0b8b5942126c9b8a779d2244b4aaffcb037e7;hpb=4e7f97a92ef78a956425f8d0d03e57230e131618 diff --git a/ccan/bitmap/bitmap.h b/ccan/bitmap/bitmap.h index 34faf500..beeb1e95 100644 --- a/ccan/bitmap/bitmap.h +++ b/ccan/bitmap/bitmap.h @@ -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_ */