-
Notifications
You must be signed in to change notification settings - Fork 14
/
bitmap.h
51 lines (43 loc) · 1.4 KB
/
bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef BITMAP_H
#define BITMAP_H
#define BITS_IN_LONG (sizeof(unsigned long)*8)
#define LONGS(x) ((x + BITS_IN_LONG - 1) & -BITS_IN_LONG)
/* Every bitmap gets its own type */
#define DECLARE_BITMAP(name, x) unsigned long name[LONGS(x)]
static inline int test_bit(unsigned int nr, unsigned long *bitmap)
{
unsigned long offset = nr / BITS_IN_LONG;
unsigned long bit = nr & (BITS_IN_LONG-1);
return (bitmap[offset] >> bit) & 1;
}
static inline void set_bit(unsigned int nr, unsigned long *bitmap)
{
unsigned long offset = nr / BITS_IN_LONG;
unsigned long bit = nr & (BITS_IN_LONG-1);
bitmap[offset] |= 1UL << bit;
}
static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
{
unsigned long offset = nr / BITS_IN_LONG;
unsigned long bit = nr & (BITS_IN_LONG-1);
bitmap[offset] &= ~(1UL << bit);
}
static inline int test_and_set_bit(unsigned int nr, unsigned long *bitmap)
{
unsigned long offset = nr / BITS_IN_LONG;
unsigned long bit = nr & (BITS_IN_LONG-1);
unsigned long old = bitmap[offset];
unsigned long mask = 1UL << bit;
bitmap[offset] = old | mask;
return (old & mask) != 0;
}
static inline int test_and_clear_bit(unsigned int nr, unsigned long *bitmap)
{
unsigned long offset = nr / BITS_IN_LONG;
unsigned long bit = nr & (BITS_IN_LONG-1);
unsigned long old = bitmap[offset];
unsigned long mask = 1UL << bit;
bitmap[offset] = old & ~mask;
return (old & mask) != 0;
}
#endif /* BITMAP_H */