Minor fix in user bitmask functions (XCC)
[akaros.git] / user / parlib / include / sparc / bitmask.h
1 #ifndef PARLIB_ARCH_BITMASK_H
2 #define PARLIB_ARCH_BITMASK_H
3
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <arch/atomic.h>
8 #include <stdio.h>
9
10 #define BYTES_FOR_BITMASK(size) (size)
11 #define BYTES_FOR_BITMASK_WITH_CHECK(size) (size)
12 #define DECL_BITMASK(name, size) uint8_t (name)[BYTES_FOR_BITMASK((size))]
13
14 #define GET_BITMASK_BIT(name, bit) ((name)[(bit)])
15 #define SET_BITMASK_BIT(name, bit) ((name)[(bit)] = 1)
16 #define CLR_BITMASK_BIT(name, bit) ((name)[(bit)] = 0)
17 #define SET_BITMASK_BIT_ATOMIC SET_BITMASK_BIT
18 #define CLR_BITMASK_BIT_ATOMIC CLR_BITMASK_BIT
19
20 #define CLR_BITMASK(name, size) \
21 ({ \
22         {TRUSTEDBLOCK \
23         memset((void*)((uintptr_t)(name)), 0, BYTES_FOR_BITMASK((size))); \
24         } \
25 })
26
27 #define FILL_BITMASK(name, size) \
28 ({ \
29         {TRUSTEDBLOCK \
30         memset((void*)((uintptr_t)(name)), 1, BYTES_FOR_BITMASK((size))); \
31         } \
32 }) 
33
34 #define COPY_BITMASK(newmask, oldmask, size) \
35 ({ \
36         {TRUSTEDBLOCK \
37         memcpy((void*)((uintptr_t)(newmask)), \
38            (void*)((uintptr_t)(oldmask)), \
39            BYTES_FOR_BITMASK((size))); \
40         } \
41 })
42
43 // this checks the entire last byte, so keep it 0 in the other macros
44 #define BITMASK_IS_CLEAR(name, size) ({ \
45         uint32_t __n = BYTES_FOR_BITMASK((size)); \
46         bool clear = 1; \
47         while (__n-- > 0) { \
48                 if ((name)[__n]) { \
49                         clear = 0; \
50                         break;\
51                 }\
52         } \
53         clear; })
54
55 static inline bool BITMASK_IS_FULL(uint8_t* map, size_t size)
56 {
57         int _size = size;
58         for (int i = 0; i < BYTES_FOR_BITMASK(size); i++) {
59                 for (int j = 0; j < MIN(8,_size); j++)
60                         if(!GET_BITMASK_BIT(map, i))
61                                 return FALSE;
62                         _size--;
63         }
64         return TRUE;
65 }
66
67 #define PRINT_BITMASK(name, size) { \
68         int i;  \
69         for (i = 0; i < BYTES_FOR_BITMASK(size); i++) { \
70                 printf("%x", (name)[i] );       \
71         } \
72         printf("\n"); \
73 }
74
75 #endif /* PARLIB_ARCH_BITMASK_H */