20fec9d8affa35b7f06ca328c4a415c91b180a9c
[akaros.git] / kern / include / ros / common.h
1 #ifndef ROS_COMMON_H
2 #define ROS_COMMON_H
3
4 #ifndef __ASSEMBLER__
5 #ifndef __IVY__
6 #include <ros/noivy.h>
7 #endif
8
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 #include <stdbool.h>
13
14 typedef uintptr_t physaddr_t;
15 typedef long intreg_t;
16 typedef unsigned long uintreg_t;
17
18 #ifndef NULL
19 #define NULL ((void*) 0)
20 #endif
21
22 #ifndef TRUE
23 #define TRUE    1
24 #endif
25
26 #ifndef FALSE
27 #define FALSE   0
28 #endif
29
30 #define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))
31
32 #define FOR_CIRC_BUFFER(next, size, var) \
33         for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
34
35 // Efficient min and max operations
36 #ifdef ROS_KERNEL /* Glibc has their own */
37 #define MIN(_a, _b)                                             \
38 ({                                                              \
39         typeof(_a) __a = (_a);                                  \
40         typeof(_b) __b = (_b);                                  \
41         __a <= __b ? __a : __b;                                 \
42 })
43 #define MAX(_a, _b)                                             \
44 ({                                                              \
45         typeof(_a) __a = (_a);                                  \
46         typeof(_b) __b = (_b);                                  \
47         __a >= __b ? __a : __b;                                 \
48 })
49 #endif
50
51 /* Rounding operations (efficient when n is a power of 2)
52  * Round down to the nearest multiple of n.
53  * The compiler should compile out the branch.  This is needed for 32 bit, so
54  * that we can round down uint64_t, without chopping off the top 32 bits. */
55 #define ROUNDDOWN(a, n)                                                        \
56 ({                                                                             \
57         typeof(a) __b;                                                             \
58         if (sizeof(a) == 8) {                                                      \
59                 uint64_t __a = (uint64_t) (a);                                         \
60                 __b = (typeof(a)) (__a - __a % (n));                                   \
61         } else {                                                                   \
62                 uintptr_t __a = (uintptr_t) (a);                                       \
63                 __b = (typeof(a)) (__a - __a % (n));                                   \
64         }                                                                          \
65         __b;                                                                       \
66 })
67
68 /* Round up to the nearest multiple of n */
69 #define ROUNDUP(a, n)                                                          \
70 ({                                                                             \
71         typeof(a) __b;                                                             \
72         if (sizeof(a) == 8) {                                                      \
73                 uint64_t __n = (uint64_t) (n);                                         \
74                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
75         } else {                                                                   \
76                 uintptr_t __n = (uintptr_t) (n);                                       \
77                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
78         }                                                                          \
79         __b;                                                                       \
80 })
81
82 // Return the integer logarithm of the value provided rounded down
83 static inline uintptr_t LOG2_DOWN(uintptr_t value)
84 {
85         uintptr_t l = 0;
86         while( (value >> l) > 1 ) ++l;
87         return l;
88 }
89
90 // Return the integer logarithm of the value provided rounded up
91 static inline uintptr_t LOG2_UP(uintptr_t value)
92 {
93         uintptr_t _v = LOG2_DOWN(value);
94         if (value ^ (1 << _v))
95                 return _v + 1;
96         else
97                 return _v;
98 }
99
100 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
101 {
102         return 1 << LOG2_UP(value);
103 }
104
105 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
106 {
107         return 1 << LOG2_DOWN(value);
108 }
109
110 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
111 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
112 {
113         if (!a)
114                 return FALSE;
115         return (uint64_t)(-1) / a < b;
116 }
117
118 // Return the offset of 'member' relative to the beginning of a struct type
119 #ifndef offsetof
120 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
121 #endif
122
123 /* Return the container/struct holding the object 'ptr' points to */
124 #define container_of(ptr, type, member) ({                                     \
125         (type*)((char*)ptr - offsetof(type, member));                             \
126 })
127
128 /* Force the reading exactly once of x.  You may still need mbs().  See
129  * http://lwn.net/Articles/508991/ for more info. */
130 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
131
132 // Ivy currently can only handle 63 bits (OCaml thing), so use this to make
133 // a uint64_t programatically
134 #define UINT64(upper, lower) ( (((uint64_t)(upper)) << 32) | (lower) )
135
136 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
137  * other callers spin til the func is complete. */
138 #define run_once(func)                                                         \
139 {                                                                              \
140         static bool ran_once = FALSE;                                              \
141         static atomic_t is_running = FALSE;                                        \
142         if (!ran_once) {                                                           \
143                 if (!atomic_swap(&is_running, TRUE)) {                                 \
144                         /* we won the race and get to run the func */                      \
145                         func;                                                              \
146                         wmb();  /* don't let the ran_once write pass previous writes */    \
147                         ran_once = TRUE;                                                   \
148                 } else {                                                               \
149                         /* someone else won, wait til they are done to break out */        \
150                         while (!ran_once)                                                  \
151                                 cpu_relax();                                                   \
152                                                                                \
153                 }                                                                      \
154         }                                                                          \
155 }
156
157 /* Unprotected, single-threaded version, makes sure func is run exactly once */
158 #define run_once_racy(func)                                                    \
159 {                                                                              \
160         static bool ran_once = FALSE;                                              \
161         if (!ran_once) {                                                           \
162                 func;                                                                  \
163                 ran_once = TRUE;                                                       \
164         }                                                                          \
165 }
166
167 /* Aborts with 'retcmd' if this function has already been called.  Compared to
168  * run_once, this is put at the top of a function that can be called from
169  * multiple sources but should only execute once. */
170 #define init_once_racy(retcmd)                                                 \
171 {                                                                              \
172         static bool initialized = FALSE;                                           \
173         if (initialized) {                                                         \
174                 retcmd;                                                                \
175         }                                                                          \
176         initialized = TRUE;                                                        \
177 }
178
179 #endif /* __ASSEMBLER__ */
180
181 #endif /* ROS_COMMON_H */