X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fbitmap%2Fbitmap.h;h=543828010188aa39490005ebe53dcaba3b52c1a5;hp=2be18f31e773e2d21482db199403a770cbd43bf3;hb=HEAD;hpb=2a8b4826ddf8face89bd37a5fc0982474f96e2e3 diff --git a/ccan/bitmap/bitmap.h b/ccan/bitmap/bitmap.h index 2be18f31..54382801 100644 --- a/ccan/bitmap/bitmap.h +++ b/ccan/bitmap/bitmap.h @@ -15,26 +15,24 @@ 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. */ -typedef struct { +typedef struct bitmap { bitmap_word w; } bitmap; #define BITMAP_DECLARE(_name, _nbits) \ bitmap (_name)[BITMAP_NWORDS(_nbits)] -static inline size_t bitmap_sizeof(int nbits) +static inline size_t bitmap_sizeof(unsigned long nbits) { return BITMAP_NWORDS(nbits) * sizeof(bitmap_word); } -static inline bitmap *bitmap_alloc(int nbits) -{ - return malloc(bitmap_sizeof(nbits)); -} - static inline bitmap_word bitmap_bswap(bitmap_word w) { if (BITMAP_WORD_BITS == 32) @@ -60,47 +58,50 @@ static inline bitmap_word bitmap_bswap(bitmap_word w) #define BITMAP_TAIL(_bm, _nbits) \ (BITMAP_TAILWORD(_bm, _nbits) & BITMAP_TAILBITS(_nbits)) -static inline void bitmap_set_bit(bitmap *bitmap, int n) +static inline void bitmap_set_bit(bitmap *bitmap, unsigned long n) { BITMAP_WORD(bitmap, n) |= BITMAP_WORDBIT(n); } -static inline void bitmap_clear_bit(bitmap *bitmap, int n) +static inline void bitmap_clear_bit(bitmap *bitmap, unsigned long n) { BITMAP_WORD(bitmap, n) &= ~BITMAP_WORDBIT(n); } -static inline void bitmap_change_bit(bitmap *bitmap, int n) +static inline void bitmap_change_bit(bitmap *bitmap, unsigned long n) { BITMAP_WORD(bitmap, n) ^= BITMAP_WORDBIT(n); } -static inline bool bitmap_test_bit(bitmap *bitmap, int n) +static inline bool bitmap_test_bit(const bitmap *bitmap, unsigned long n) { return !!(BITMAP_WORD(bitmap, n) & BITMAP_WORDBIT(n)); } +void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m); +void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m); -static inline void bitmap_zero(bitmap *bitmap, int nbits) +static inline void bitmap_zero(bitmap *bitmap, unsigned long nbits) { memset(bitmap, 0, bitmap_sizeof(nbits)); } -static inline void bitmap_fill(bitmap *bitmap, int nbits) +static inline void bitmap_fill(bitmap *bitmap, unsigned long nbits) { memset(bitmap, 0xff, bitmap_sizeof(nbits)); } -static inline void bitmap_copy(bitmap *dst, bitmap *src, int nbits) +static inline void bitmap_copy(bitmap *dst, const bitmap *src, + unsigned long nbits) { memcpy(dst, src, bitmap_sizeof(nbits)); } #define BITMAP_DEF_BINOP(_name, _op) \ static inline void bitmap_##_name(bitmap *dst, bitmap *src1, bitmap *src2, \ - int nbits) \ + unsigned long nbits) \ { \ - unsigned int i = 0; \ + unsigned long i = 0; \ for (i = 0; i < BITMAP_NWORDS(nbits); i++) { \ dst[i].w = src1[i].w _op src2[i].w; \ } \ @@ -113,24 +114,27 @@ BITMAP_DEF_BINOP(andnot, & ~) #undef BITMAP_DEF_BINOP -static inline void bitmap_complement(bitmap *dst, bitmap *src, int nbits) +static inline void bitmap_complement(bitmap *dst, const bitmap *src, + unsigned long nbits) { - unsigned int i; + unsigned long i; for (i = 0; i < BITMAP_NWORDS(nbits); i++) dst[i].w = ~src[i].w; } -static inline bool bitmap_equal(bitmap *src1, bitmap *src2, int nbits) +static inline bool bitmap_equal(const bitmap *src1, const bitmap *src2, + unsigned long nbits) { return (memcmp(src1, src2, BITMAP_HEADBYTES(nbits)) == 0) && (!BITMAP_HASTAIL(nbits) || (BITMAP_TAIL(src1, nbits) == BITMAP_TAIL(src2, nbits))); } -static inline bool bitmap_intersects(bitmap *src1, bitmap *src2, int nbits) +static inline bool bitmap_intersects(const bitmap *src1, const bitmap *src2, + unsigned long nbits) { - unsigned int i; + unsigned long i; for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) { if (src1[i].w & src2[i].w) @@ -142,9 +146,10 @@ static inline bool bitmap_intersects(bitmap *src1, bitmap *src2, int nbits) return false; } -static inline bool bitmap_subset(bitmap *src1, bitmap *src2, int nbits) +static inline bool bitmap_subset(const bitmap *src1, const bitmap *src2, + unsigned long nbits) { - unsigned int i; + unsigned long i; for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) { if (src1[i].w & ~src2[i].w) @@ -156,9 +161,9 @@ static inline bool bitmap_subset(bitmap *src1, bitmap *src2, int nbits) return true; } -static inline bool bitmap_full(bitmap *bitmap, int nbits) +static inline bool bitmap_full(const bitmap *bitmap, unsigned long nbits) { - unsigned int i; + unsigned long i; for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) { if (bitmap[i].w != -1UL) @@ -171,9 +176,9 @@ static inline bool bitmap_full(bitmap *bitmap, int nbits) return true; } -static inline bool bitmap_empty(bitmap *bitmap, int nbits) +static inline bool bitmap_empty(const bitmap *bitmap, unsigned long nbits) { - unsigned int i; + unsigned long i; for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) { if (bitmap[i].w != 0) @@ -185,4 +190,57 @@ static inline bool bitmap_empty(bitmap *bitmap, int 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_ */