Adds bitmask functions to user/
authorBarret Rhoden <brho@cs.berkeley.edu>
Sat, 19 Feb 2011 00:17:02 +0000 (16:17 -0800)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:35:58 +0000 (17:35 -0700)
They are mostly similar to the kernel's.  If you find a bug in one of
them, it's probably in the other.

user/include/i686/bitmask.h [new file with mode: 0644]
user/include/sparc/bitmask.h [new file with mode: 0644]

diff --git a/user/include/i686/bitmask.h b/user/include/i686/bitmask.h
new file mode 100644 (file)
index 0000000..785b27f
--- /dev/null
@@ -0,0 +1,141 @@
+#ifndef PARLIB_ARCH_BITMASK_H
+#define PARLIB_ARCH_BITMASK_H
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <arch/atomic.h>
+#include <stdio.h>
+
+#define DECL_BITMASK(name, size) \
+       uint8_t (name)[BYTES_FOR_BITMASK((size))]
+
+#define BYTES_FOR_BITMASK(size) \
+       (((size) - 1) / 8 + 1)
+
+#define BYTES_FOR_BITMASK_WITH_CHECK(size) \
+       ((size) ? ((size) - (1)) / (8) + (1) : (0))
+
+static bool GET_BITMASK_BIT(uint8_t* name, size_t bit) 
+{
+       return (((name)[(bit)/8] & (1 << ((bit) % 8))) ? 1 : 0);
+}
+
+#define SET_BITMASK_BIT(name, bit) \
+       ((name)[(bit)/8] |= (1 << ((bit) % 8)));
+/*
+static void SET_BITMASK_BIT(uint8_t* name, size_t bit)
+{
+       ((name)[(bit)/8] |= (1 << ((bit) % 8)));
+}
+*/
+
+#define CLR_BITMASK_BIT(name, bit) \
+       ((name)[(bit)/8] &= ~(1 << ((bit) % 8)));
+/*
+static void CLR_BITMASK_BIT(uint8_t* name, size_t bit) 
+{
+       ((name)[(bit)/8] &= ~(1 << ((bit) % 8)));
+}
+*/
+
+static void SET_BITMASK_BIT_ATOMIC(uint8_t* name, size_t bit) 
+{
+       (atomic_orb(&(name)[(bit)/8], (1 << ((bit) % 8))));
+}
+
+#define CLR_BITMASK_BIT_ATOMIC(name, bit) \
+       (atomic_andb(&(name)[(bit)/8], ~(1 << ((bit) % 8))))
+
+#define CLR_BITMASK(name, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memset((void*)((uintptr_t)(name)), 0, BYTES_FOR_BITMASK((size))); \
+       } \
+})
+
+#define FILL_BITMASK(name, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memset((void*)((uintptr_t)(name)), 255, BYTES_FOR_BITMASK((size))); \
+       } \
+       (name)[BYTES_FOR_BITMASK((size))-1] >>= (((size) % 8) ? (8 - ((size) % 8)) : 0 ); \
+}) 
+
+#define COPY_BITMASK(newmask, oldmask, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memcpy((void*)((uintptr_t)(newmask)), \
+           (void*)((uintptr_t)(oldmask)), \
+           BYTES_FOR_BITMASK((size))); \
+       } \
+})
+
+// this checks the entire last byte, so keep it 0 in the other macros
+#define BITMASK_IS_CLEAR(name, size) ({ \
+       uint32_t __n = BYTES_FOR_BITMASK((size)); \
+       bool clear = 1; \
+       while (__n-- > 0) { \
+               if ((name)[__n]) { \
+                       clear = 0; \
+                       break;\
+               }\
+       } \
+       clear; })
+
+static inline bool BITMASK_IS_FULL(uint8_t* map, size_t size)
+{
+       int _size = size;
+       for (int i = 0; i < BYTES_FOR_BITMASK(size); i++) {
+               for (int j = 0; j < MIN(8,_size); j++)
+                       if(!((map[i] >> j) &1))
+                               return FALSE;
+                       _size--;
+       }
+       return TRUE;
+}
+
+#define PRINT_BITMASK(name, size) { \
+       int i;  \
+       int _size = size; \
+       for (i = 0; i < BYTES_FOR_BITMASK(size); i++) { \
+               int j;  \
+               for (j = 0; j < MIN(8,_size); j++) \
+                       printk("%x", ((name)[i] >> j) & 1);     \
+                       _size--; \
+       } \
+       printk("\n"); \
+}
+
+static inline bool BITMASK_IS_SET_IN_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               if(!GET_BITMASK_BIT(m, i))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static inline bool BITMASK_IS_CLR_IN_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               if(GET_BITMASK_BIT(m, i))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static inline void SET_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               SET_BITMASK_BIT(m, i);
+       }
+}
+
+static inline void CLR_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               CLR_BITMASK_BIT(m, i);
+       }
+}
+#endif /* PARLIB_ARCH_BITMASK_H */
diff --git a/user/include/sparc/bitmask.h b/user/include/sparc/bitmask.h
new file mode 100644 (file)
index 0000000..7899975
--- /dev/null
@@ -0,0 +1,107 @@
+#ifndef PARLIB_ARCH_BITMASK_H
+#define PARLIB_ARCH_BITMASK_H
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <arch/atomic.h>
+#include <stdio.h>
+
+#define BYTES_FOR_BITMASK(size) (size)
+#define BYTES_FOR_BITMASK_WITH_CHECK(size) (size)
+#define DECL_BITMASK(name, size) uint8_t (name)[BYTES_FOR_BITMASK((size))]
+
+#define GET_BITMASK_BIT(name, bit) ((name)[(bit)])
+#define SET_BITMASK_BIT(name, bit) ((name)[(bit)] = 1)
+#define CLR_BITMASK_BIT(name, bit) ((name)[(bit)] = 0)
+#define SET_BITMASK_BIT_ATOMIC SET_BITMASK_BIT
+#define CLR_BITMASK_BIT_ATOMIC CLR_BITMASK_BIT
+
+#define CLR_BITMASK(name, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memset((void*)((uintptr_t)(name)), 0, BYTES_FOR_BITMASK((size))); \
+       } \
+})
+
+#define FILL_BITMASK(name, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memset((void*)((uintptr_t)(name)), 1, BYTES_FOR_BITMASK((size))); \
+       } \
+}) 
+
+#define COPY_BITMASK(newmask, oldmask, size) \
+({ \
+       {TRUSTEDBLOCK \
+       memcpy((void*)((uintptr_t)(newmask)), \
+           (void*)((uintptr_t)(oldmask)), \
+           BYTES_FOR_BITMASK((size))); \
+       } \
+})
+
+// this checks the entire last byte, so keep it 0 in the other macros
+#define BITMASK_IS_CLEAR(name, size) ({ \
+       uint32_t __n = BYTES_FOR_BITMASK((size)); \
+       bool clear = 1; \
+       while (__n-- > 0) { \
+               if ((name)[__n]) { \
+                       clear = 0; \
+                       break;\
+               }\
+       } \
+       clear; })
+
+static inline bool BITMASK_IS_FULL(uint8_t* map, size_t size)
+{
+       int _size = size;
+       for (int i = 0; i < BYTES_FOR_BITMASK(size); i++) {
+               for (int j = 0; j < MIN(8,_size); j++)
+                       if(!GET_BITMASK_BIT(map, i))
+                               return FALSE;
+                       _size--;
+       }
+       return TRUE;
+}
+
+#define PRINT_BITMASK(name, size) { \
+       int i;  \
+       for (i = 0; i < BYTES_FOR_BITMASK(size); i++) { \
+               printk("%x", (name)[i] );       \
+       } \
+       printk("\n"); \
+}
+
+static inline bool BITMASK_IS_SET_IN_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               if(!GET_BITMASK_BIT(m, i))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static inline bool BITMASK_IS_CLR_IN_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               if(GET_BITMASK_BIT(m, i))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static inline void SET_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               SET_BITMASK_BIT(m, i);
+       }
+}
+
+static inline void CLR_BITMASK_RANGE(uint8_t* m, size_t beg, size_t end)
+{
+       for(size_t i=beg; i<end; i++) {
+               CLR_BITMASK_BIT(m, i);
+       }
+}
+
+#endif /* PARLIB_ARCH_BITMASK_H */