xref: /illumos-gate/usr/src/tools/smatch/src/bitmap.h (revision 1f5207b7)
1*1f5207b7SJohn Levon #ifndef BITMAP_H
2*1f5207b7SJohn Levon #define BITMAP_H
3*1f5207b7SJohn Levon 
4*1f5207b7SJohn Levon #define BITS_IN_LONG	(sizeof(unsigned long)*8)
5*1f5207b7SJohn Levon #define LONGS(x)	((x + BITS_IN_LONG - 1) & -BITS_IN_LONG)
6*1f5207b7SJohn Levon 
7*1f5207b7SJohn Levon /* Every bitmap gets its own type */
8*1f5207b7SJohn Levon #define DECLARE_BITMAP(name, x) unsigned long name[LONGS(x)]
9*1f5207b7SJohn Levon 
test_bit(unsigned int nr,unsigned long * bitmap)10*1f5207b7SJohn Levon static inline int test_bit(unsigned int nr, unsigned long *bitmap)
11*1f5207b7SJohn Levon {
12*1f5207b7SJohn Levon 	unsigned long offset = nr / BITS_IN_LONG;
13*1f5207b7SJohn Levon 	unsigned long bit = nr & (BITS_IN_LONG-1);
14*1f5207b7SJohn Levon 	return (bitmap[offset] >> bit) & 1;
15*1f5207b7SJohn Levon }
16*1f5207b7SJohn Levon 
set_bit(unsigned int nr,unsigned long * bitmap)17*1f5207b7SJohn Levon static inline void set_bit(unsigned int nr, unsigned long *bitmap)
18*1f5207b7SJohn Levon {
19*1f5207b7SJohn Levon 	unsigned long offset = nr / BITS_IN_LONG;
20*1f5207b7SJohn Levon 	unsigned long bit = nr & (BITS_IN_LONG-1);
21*1f5207b7SJohn Levon 	bitmap[offset] |= 1UL << bit;
22*1f5207b7SJohn Levon }
23*1f5207b7SJohn Levon 
clear_bit(unsigned int nr,unsigned long * bitmap)24*1f5207b7SJohn Levon static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
25*1f5207b7SJohn Levon {
26*1f5207b7SJohn Levon 	unsigned long offset = nr / BITS_IN_LONG;
27*1f5207b7SJohn Levon 	unsigned long bit = nr & (BITS_IN_LONG-1);
28*1f5207b7SJohn Levon 	bitmap[offset] &= ~(1UL << bit);
29*1f5207b7SJohn Levon }
30*1f5207b7SJohn Levon 
test_and_set_bit(unsigned int nr,unsigned long * bitmap)31*1f5207b7SJohn Levon static inline int test_and_set_bit(unsigned int nr, unsigned long *bitmap)
32*1f5207b7SJohn Levon {
33*1f5207b7SJohn Levon 	unsigned long offset = nr / BITS_IN_LONG;
34*1f5207b7SJohn Levon 	unsigned long bit = nr & (BITS_IN_LONG-1);
35*1f5207b7SJohn Levon 	unsigned long old = bitmap[offset];
36*1f5207b7SJohn Levon 	unsigned long mask = 1UL << bit;
37*1f5207b7SJohn Levon 	bitmap[offset] = old | mask;
38*1f5207b7SJohn Levon 	return (old & mask) != 0;
39*1f5207b7SJohn Levon }
40*1f5207b7SJohn Levon 
test_and_clear_bit(unsigned int nr,unsigned long * bitmap)41*1f5207b7SJohn Levon static inline int test_and_clear_bit(unsigned int nr, unsigned long *bitmap)
42*1f5207b7SJohn Levon {
43*1f5207b7SJohn Levon 	unsigned long offset = nr / BITS_IN_LONG;
44*1f5207b7SJohn Levon 	unsigned long bit = nr & (BITS_IN_LONG-1);
45*1f5207b7SJohn Levon 	unsigned long old = bitmap[offset];
46*1f5207b7SJohn Levon 	unsigned long mask = 1UL << bit;
47*1f5207b7SJohn Levon 	bitmap[offset] = old & ~mask;
48*1f5207b7SJohn Levon 	return (old & mask) != 0;
49*1f5207b7SJohn Levon }
50*1f5207b7SJohn Levon 
51*1f5207b7SJohn Levon #endif /* BITMAP_H */
52