Moves ARRAY_SIZE into the kernel only (XCC)
[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 KiB     1024u
31 #define MiB     1048576u
32 #define GiB     1073741824u
33 #define TiB     1099511627776ull
34 #define PiB     1125899906842624ull
35 #define EiB     1152921504606846976ull
36
37 #define FOR_CIRC_BUFFER(next, size, var) \
38         for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
39
40 #define STRINGIFY(s) __STRINGIFY(s)
41 #define __STRINGIFY(s) #s
42
43 /* A macro for testing if another macro has been #defined or not.  Can be used
44  * wherever you need a boolean defined.  Returns 0 or 1. */
45 #define is_defined(macro) is_defined_(macro)
46 #define is_defined_test_1 ,
47 #define is_defined_(value) is_defined__(is_defined_test_##value, value)
48 #define is_defined__(comma, value) is_defined___(comma 1, 0)
49 #define is_defined___(_, v, ...) v
50
51 // Efficient min and max operations
52 #ifdef ROS_KERNEL /* Glibc or other user libs have their own */
53 #define MIN(_a, _b)                                             \
54 ({                                                              \
55         typeof(_a) __a = (_a);                                  \
56         typeof(_b) __b = (_b);                                  \
57         __a <= __b ? __a : __b;                                 \
58 })
59 #define MAX(_a, _b)                                             \
60 ({                                                              \
61         typeof(_a) __a = (_a);                                  \
62         typeof(_b) __b = (_b);                                  \
63         __a >= __b ? __a : __b;                                 \
64 })
65
66 /* Other kernel-only includes */
67
68 /* Test for alignment, e.g. 2^6 */
69 #define ALIGNED(p, a)   (!(((uintptr_t)(p)) & ((a)-1)))
70 /* Aligns x up to the mask, e.g. (2^6 - 1) (round up if any mask bits are set)*/
71 #define __ALIGN_MASK(x, mask) (((uintptr_t)(x) + (mask)) & ~(mask))
72 /* Aligns x up to the alignment, e.g. 2^6. */
73 #define ALIGN(x, a) ((typeof(x)) __ALIGN_MASK(x, (a) - 1))
74 /* Will return false for 0.  Debatable, based on what you want. */
75 #define IS_PWR2(x) ((x) && !((x) & (x - 1)))
76
77 #define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
78
79 #endif
80
81 /* Rounding operations (efficient when n is a power of 2)
82  * Round down to the nearest multiple of n.
83  * The compiler should compile out the branch.  This is needed for 32 bit, so
84  * that we can round down uint64_t, without chopping off the top 32 bits. */
85 #define ROUNDDOWN(a, n)                                                        \
86 ({                                                                             \
87         typeof(a) __b;                                                             \
88         if (sizeof(a) == 8) {                                                      \
89                 uint64_t __a = (uint64_t) (a);                                         \
90                 __b = (typeof(a)) (__a - __a % (n));                                   \
91         } else {                                                                   \
92                 uintptr_t __a = (uintptr_t) (a);                                       \
93                 __b = (typeof(a)) (__a - __a % (n));                                   \
94         }                                                                          \
95         __b;                                                                       \
96 })
97
98 /* Round up to the nearest multiple of n */
99 #define ROUNDUP(a, n)                                                          \
100 ({                                                                             \
101         typeof(a) __b;                                                             \
102         if (sizeof(a) == 8) {                                                      \
103                 uint64_t __n = (uint64_t) (n);                                         \
104                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
105         } else {                                                                   \
106                 uintptr_t __n = (uintptr_t) (n);                                       \
107                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
108         }                                                                          \
109         __b;                                                                       \
110 })
111
112 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
113
114 // Return the integer logarithm of the value provided rounded down
115 static inline uintptr_t LOG2_DOWN(uintptr_t value)
116 {
117         value |= 1;  // clz(0) is undefined, just or in a 1 bit and define it
118         // intrinsic __builtin_clz supported by both > gcc4.6 and LLVM > 1.5
119         return (sizeof(value) == 8) ? 63 - __builtin_clzll(value)
120                                     : 31 - __builtin_clz(value);
121 }
122
123 // Return the integer logarithm of the value provided rounded up
124 static inline uintptr_t LOG2_UP(uintptr_t value)
125 {
126         uintptr_t ret = LOG2_DOWN(value);
127         ret += 0 != (value ^ ((uintptr_t) 1 << ret));  // Add 1 if a lower bit set
128         return ret;
129 }
130
131 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
132 {
133         return 1 << LOG2_UP(value);
134 }
135
136 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
137 {
138         return 1 << LOG2_DOWN(value);
139 }
140
141 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
142 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
143 {
144         if (!a)
145                 return FALSE;
146         return (uint64_t)(-1) / a < b;
147 }
148
149 // Return the offset of 'member' relative to the beginning of a struct type
150 #ifndef offsetof
151 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
152 #endif
153
154 /* Return the container/struct holding the object 'ptr' points to */
155 #define container_of(ptr, type, member) ({                                     \
156         (type*)((char*)ptr - offsetof(type, member));                             \
157 })
158
159 /* Force the reading exactly once of x.  You may still need mbs().  See
160  * http://lwn.net/Articles/508991/ for more info. */
161 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
162
163 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
164  * other callers spin til the func is complete. */
165 #define run_once(func)                                                         \
166 {                                                                              \
167         static bool ran_once = FALSE;                                              \
168         static atomic_t is_running = FALSE;                                        \
169         if (!ran_once) {                                                           \
170                 if (!atomic_swap(&is_running, TRUE)) {                                 \
171                         /* we won the race and get to run the func */                      \
172                         func;                                                              \
173                         wmb();  /* don't let the ran_once write pass previous writes */    \
174                         ran_once = TRUE;                                                   \
175                 } else {                                                               \
176                         /* someone else won, wait til they are done to break out */        \
177                         while (!ran_once)                                                  \
178                                 cpu_relax();                                                   \
179                                                                                \
180                 }                                                                      \
181         }                                                                          \
182 }
183
184 /* Unprotected, single-threaded version, makes sure func is run exactly once */
185 #define run_once_racy(func)                                                    \
186 {                                                                              \
187         static bool ran_once = FALSE;                                              \
188         if (!ran_once) {                                                           \
189                 func;                                                                  \
190                 ran_once = TRUE;                                                       \
191         }                                                                          \
192 }
193
194 /* Aborts with 'retcmd' if this function has already been called.  Compared to
195  * run_once, this is put at the top of a function that can be called from
196  * multiple sources but should only execute once. */
197 #define init_once_racy(retcmd)                                                 \
198 {                                                                              \
199         static bool initialized = FALSE;                                           \
200         if (initialized) {                                                         \
201                 retcmd;                                                                \
202         }                                                                          \
203         initialized = TRUE;                                                        \
204 }
205
206 #endif /* __ASSEMBLER__ */
207
208 #endif /* ROS_COMMON_H */