core: make fake cur_slists stackable
[smatch.git] / bitmap.h
blob4d81ffc07538326b12b42adc7cc2b8945eda239f
1 #ifndef BITMAP_H
2 #define BITMAP_H
4 #define BITS_IN_LONG (sizeof(unsigned long)*8)
5 #define LONGS(x) ((x + BITS_IN_LONG - 1) & -BITS_IN_LONG)
7 /* Every bitmap gets its own type */
8 #define DECLARE_BITMAP(name, x) unsigned long name[LONGS(x)]
10 static inline int test_bit(unsigned int nr, unsigned long *bitmap)
12 unsigned long offset = nr / BITS_IN_LONG;
13 unsigned long bit = nr & (BITS_IN_LONG-1);
14 return (bitmap[offset] >> bit) & 1;
17 static inline void set_bit(unsigned int nr, unsigned long *bitmap)
19 unsigned long offset = nr / BITS_IN_LONG;
20 unsigned long bit = nr & (BITS_IN_LONG-1);
21 bitmap[offset] |= 1UL << bit;
24 static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
26 unsigned long offset = nr / BITS_IN_LONG;
27 unsigned long bit = nr & (BITS_IN_LONG-1);
28 bitmap[offset] &= ~(1UL << bit);
31 static inline int test_and_set_bit(unsigned int nr, unsigned long *bitmap)
33 unsigned long offset = nr / BITS_IN_LONG;
34 unsigned long bit = nr & (BITS_IN_LONG-1);
35 unsigned long old = bitmap[offset];
36 unsigned long mask = 1UL << bit;
37 bitmap[offset] = old | mask;
38 return (old & mask) != 0;
41 static inline int test_and_clear_bit(unsigned int nr, unsigned long *bitmap)
43 unsigned long offset = nr / BITS_IN_LONG;
44 unsigned long bit = nr & (BITS_IN_LONG-1);
45 unsigned long old = bitmap[offset];
46 unsigned long mask = 1UL << bit;
47 bitmap[offset] = old & ~mask;
48 return (old & mask) != 0;
51 #endif /* BITMAP_H */