2 * UCW Library -- Bit Array Operations
4 * (c) 2003--2006 Martin Mares <mj@ucw.cz>
5 * (c) 2012 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _UCW_BITARRAY_H
12 #define _UCW_BITARRAY_H
16 #ifdef CONFIG_UCW_CLEAN_ABI
17 #define bit_array_count_bits ucw_bit_array_count_bits
18 #define bit_array_xrealloc ucw_bit_array_xrealloc
21 typedef u32 *bitarray_t; // Must be initialized by bit_array_xmalloc(), bit_array_zero() or bit_array_set_all()
23 #define BIT_ARRAY_WORDS(n) (((n)+31)/32)
24 #define BIT_ARRAY_BYTES(n) (4*BIT_ARRAY_WORDS(n))
25 #define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
27 static inline bitarray_t bit_array_xmalloc(uint n)
29 return xmalloc(BIT_ARRAY_BYTES(n));
32 bitarray_t bit_array_xrealloc(bitarray_t a, uint old_n, uint new_n);
34 static inline bitarray_t bit_array_xmalloc_zero(uint n)
36 return xmalloc_zero(BIT_ARRAY_BYTES(n));
39 static inline void bit_array_zero(bitarray_t a, uint n)
41 bzero(a, BIT_ARRAY_BYTES(n));
44 static inline void bit_array_set_all(bitarray_t a, uint n)
47 memset(a, 255, w * 4);
53 static inline void bit_array_set(bitarray_t a, uint i)
55 a[i/32] |= (1 << (i%32));
58 static inline void bit_array_clear(bitarray_t a, uint i)
60 a[i/32] &= ~(1 << (i%32));
63 static inline void bit_array_assign(bitarray_t a, uint i, uint x)
68 bit_array_clear(a, i);
71 static inline uint bit_array_isset(bitarray_t a, uint i)
73 return a[i/32] & (1 << (i%32));
76 static inline uint bit_array_get(bitarray_t a, uint i)
78 return !! bit_array_isset(a, i);
81 static inline uint bit_array_test_and_set(bitarray_t a, uint i)
83 uint t = bit_array_isset(a, i);
88 static inline uint bit_array_test_and_clear(bitarray_t a, uint i)
90 uint t = bit_array_isset(a, i);
91 bit_array_clear(a, i);
95 uint bit_array_count_bits(bitarray_t a, uint n);
97 /* Iterate over all set bits */
98 #define BIT_ARRAY_FISH_BITS_BEGIN(var,ary,size) \
99 for (uint var##_hi=0; var##_hi < BIT_ARRAY_WORDS(size); var##_hi++) \
101 u32 var##_cur = ary[var##_hi]; \
102 for (uint var = 32 * var##_hi; var##_cur; var++, var##_cur >>= 1) \
106 #define BIT_ARRAY_FISH_BITS_END \