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