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