1 #ifndef XC_BITOPS_H
2 #define XC_BITOPS_H 1
3 
4 /* bitmap operations for single threaded access */
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
9 /* Needed by several includees, but no longer used for bitops. */
10 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
11 #define ORDER_LONG (sizeof(unsigned long) == 4 ? 5 : 6)
12 
13 #define BITMAP_ENTRY(_nr,_bmap) ((_bmap))[(_nr) / 8]
14 #define BITMAP_SHIFT(_nr) ((_nr) % 8)
15 
16 /* calculate required space for number of bytes needed to hold nr_bits */
bitmap_size(int nr_bits)17 static inline int bitmap_size(int nr_bits)
18 {
19     return (nr_bits + 7) / 8;
20 }
21 
bitmap_alloc(int nr_bits)22 static inline void *bitmap_alloc(int nr_bits)
23 {
24     return calloc(1, bitmap_size(nr_bits));
25 }
26 
bitmap_set(void * addr,int nr_bits)27 static inline void bitmap_set(void *addr, int nr_bits)
28 {
29     memset(addr, 0xff, bitmap_size(nr_bits));
30 }
31 
bitmap_clear(void * addr,int nr_bits)32 static inline void bitmap_clear(void *addr, int nr_bits)
33 {
34     memset(addr, 0, bitmap_size(nr_bits));
35 }
36 
test_bit(int nr,const void * _addr)37 static inline int test_bit(int nr, const void *_addr)
38 {
39     const char *addr = _addr;
40     return (BITMAP_ENTRY(nr, addr) >> BITMAP_SHIFT(nr)) & 1;
41 }
42 
clear_bit(int nr,void * _addr)43 static inline void clear_bit(int nr, void *_addr)
44 {
45     char *addr = _addr;
46     BITMAP_ENTRY(nr, addr) &= ~(1UL << BITMAP_SHIFT(nr));
47 }
48 
set_bit(int nr,void * _addr)49 static inline void set_bit(int nr, void *_addr)
50 {
51     char *addr = _addr;
52     BITMAP_ENTRY(nr, addr) |= (1UL << BITMAP_SHIFT(nr));
53 }
54 
test_and_clear_bit(int nr,void * addr)55 static inline int test_and_clear_bit(int nr, void *addr)
56 {
57     int oldbit = test_bit(nr, addr);
58     clear_bit(nr, addr);
59     return oldbit;
60 }
61 
test_and_set_bit(int nr,void * addr)62 static inline int test_and_set_bit(int nr, void *addr)
63 {
64     int oldbit = test_bit(nr, addr);
65     set_bit(nr, addr);
66     return oldbit;
67 }
68 
bitmap_or(void * _dst,const void * _other,int nr_bits)69 static inline void bitmap_or(void *_dst, const void *_other,
70                              int nr_bits)
71 {
72     char *dst = _dst;
73     const char *other = _other;
74     int i;
75     for ( i = 0; i < bitmap_size(nr_bits); ++i )
76         dst[i] |= other[i];
77 }
78 
79 #endif  /* XC_BITOPS_H */
80