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