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