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