Cleans up userspace headers
[akaros.git] / user / parlib / include / bitmask.h
1 #ifndef PARLIB_BITMASK_H
2 #define PARLIB_BITMASK_H
3
4 #include <arch/bitmask.h>
5
6 __BEGIN_DECLS
7
8 static inline bool BITMASK_IS_SET_IN_RANGE(uint8_t* m, size_t beg, size_t end)
9 {
10         for(size_t i=beg; i<end; i++) {
11                 if(!GET_BITMASK_BIT(m, i))
12                         return FALSE;
13         }
14         return TRUE;
15 }
16
17 static inline bool BITMASK_IS_CLR_IN_RANGE(uint8_t* m, size_t beg, size_t end)
18 {
19         for(size_t i=beg; i<end; i++) {
20                 if(GET_BITMASK_BIT(m, i))
21                         return FALSE;
22         }
23         return TRUE;
24 }
25
26 static inline void SET_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
27 {
28         for(size_t i=beg; i<end; i++) {
29                 SET_BITMASK_BIT(m, i);
30         }
31 }
32
33 static inline void CLR_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
34 {
35         for(size_t i=beg; i<end; i++) {
36                 CLR_BITMASK_BIT(m, i);
37         }
38 }
39
40 /* Runs *work on every bit in the bitmask, passing *work the value of the bit
41  * that is set.  Optionally clears the bit from the bitmask. 
42  *
43  * We need this to be a macro, so that the calling code doesn't need the
44  * address for work_fn.  This matters for code that has nested functions that
45  * use local variables, since taking the address of work_fn will force the
46  * compiler to put the function on the stack and incur icache coherency
47  * overheads. */
48 #define BITMASK_FOREACH_SET(name, size, work_fn, clear)                        \
49 {                                                                              \
50         for (int i = 0; i < (size); i++) {                                         \
51                 bool present = GET_BITMASK_BIT((name), i);                             \
52                 if (present && (clear))                                                \
53                         CLR_BITMASK_BIT_ATOMIC((name), i);                                 \
54                 if (present)                                                           \
55                         (work_fn)(i);                                                      \
56         }                                                                          \
57 }                                                                              
58                                                                                
59 __END_DECLS
60
61 #endif /* PARLIB_BITMASK_H */