XROAD
xroad_aux.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "xroad_common_fwd.h"
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <sched.h>
12 #include <assert.h>
13 #include <string.h>
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 enum
21 {
22  xroad_reg_page_sz = 4096, // TODO: Find a better place
23  xroad_huge_page_sz = 2 * 1024 * 1024 // TODO: Find a better place
24 };
25 
26 #define xroad_likely(x) __builtin_expect(!!(x), 1)
27 #define xroad_unlikely(x) __builtin_expect(!!(x), 0)
28 
29 #define xroad_cpu_pause() __asm__ volatile("pause\n": : :"memory")
30 #define xroad_fence() __asm__ volatile("mfence": : :"memory")
31 
32 #define __dtor__(fun) __attribute__((__cleanup__(fun)))
33 
34 #define __free_dtor__ __dtor__(free_destructor)
35 void free_destructor(void*);
36 
40 #define xroad_min(a, b) \
41 ({ __typeof__ (a) _a = (a); \
42  __typeof__ (b) _b = (b); \
43  _a < _b ? _a : _b; \
44 })
45 
49 #define xroad_max(a, b) \
50 ({ __typeof__ (a) _a = (a); \
51  __typeof__ (b) _b = (b); \
52  _a > _b ? _a : _b; \
53 })
54 
58 #define xroad_exchange(var, new_val) \
59  ({ \
60  __typeof__(var)* _p_var = &(var); \
61  __typeof__(var) _old_val = *_p_var; \
62  *_p_var = (new_val); \
63  _old_val; \
64  })
65 
70 #define xroad_dbl_cmp(lhs, rhs) \
71 ({ \
72  __typeof__(lhs) lhs1977_ = (lhs); \
73  __typeof__(rhs) rhs1977_ = (rhs); \
74  (fabs(lhs1977_ - rhs1977_) < XROAD_EPSILON) ? 0 : \
75  (lhs1977_ < rhs1977_ ? -1 : 1); \
76 })
77 
82 #define xroad_dbl_eq(lhs, rhs) (!xroad_dbl_cmp(lhs, rhs))
83 
89 #define xroad_div_roundup(lhs, rhs) \
90 ({ \
91  __typeof__(rhs) _r = (rhs); \
92  (lhs + _r - 1) / _r; \
93 })
94 
98 #define xroad_roundup(lhs, rhs) \
99 ({ \
100  __typeof__(rhs) _r = (rhs); \
101  ((lhs + _r - 1) / _r) * _r; \
102 })
103 
107 #define xroad_roundup_pow2(lhs, rhs) \
108 ({ \
109  __typeof__(rhs) _r = (rhs); \
110  assert(0 == (_r & (_r - 1))); \
111  ((lhs + _r - 1) & ~(_r - 1)); \
112 })
113 
117 #define xroad_ceil_pow2(v) \
118  ({ \
119  __typeof__(v) _v = (v); \
120  (_v <= 1) \
121  ? (__typeof__(v))1 \
122  : (__typeof__(v))1 << (sizeof(unsigned long long) * 8 - __builtin_clzll((unsigned long long)(_v - 1))); \
123  })
124 
128 #define xroad_is_pow2(v) (v && !(v & (v - 1)))
129 
131 #define xroad_uint_cyclic_less(l, r) \
132  ((__typeof__(l))(l - (__typeof__(l))(r)) > ((__typeof__(l))~0) / 2u)
133 
135 #define xroad_macro_get_first(first_arg, ...) first_arg
136 
143 
150 bool xroad_set_cpu_set(xroad_str_t cpus, cpu_set_t* cpu_set);
151 
157 void xroad_prefault_mem(const void* p, size_t s);
158 
160 extern pid_t g_self_pid;
161 
163 void xroad_reinit_pid(void);
164 
167 
168 /*
169  * checks if double is nan
170  */
171 static inline bool xroad_isnan(double d)
172 {
173  uint64_t bits;
174  memcpy(&bits, &d, sizeof(bits));
175 
176  // NaN: exponent = 0x7FF, mantissa != 0
177  return ((bits & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
178  ((bits & 0x000FFFFFFFFFFFFFULL) != 0);
179 }
180 
181 /*
182  * checks if double has finite value
183  */
184 static inline bool xroad_isfinite(double d)
185 {
186  uint64_t bits;
187  memcpy(&bits, &d, sizeof(bits));
188  return (bits & 0x7FF0000000000000ULL) != 0x7FF0000000000000ULL;
189 }
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #ifndef __cplusplus
196 # define XROAD_STATIC_ASSERT(...) _Static_assert(__VA_ARGS__)
197 # define xroad_alignof(x) _Alignof(x)
198 #else
199 # define XROAD_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
200 # define xroad_alignof(x) alignof(x)
201 #endif
202 
203 #define xroad_attr_unused __attribute__((unused))
204 
206 #define _XROAD_CONCAT2(s1, s2) s1##s2
208 #define _XROAD_CONCAT(s1, s2) _XROAD_CONCAT2(s1, s2)
209 
211 #define _xroad_arg_passthru(x) (x)
213 #define _xroad_arg_addrof(x) &(x)
214 
215 // Pair of macros to work around gcc 10 false-positive -Wstringop-overflow
216 #if __GNUC__ == 10
217 # define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_PUSH() \
218  _Pragma("GCC diagnostic push") \
219  _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"") \
220  do{}while(0)
221 # define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_POP() \
222  _Pragma("GCC diagnostic pop") \
223  do{}while(0)
224 #else
225 # define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_PUSH() do{}while(0)
226 # define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_POP() do{}while(0)
227 #endif
228 
229 /*
230  * generalized byte order reversal for integral types
231  */
232 #define xroad_bswap(x) \
233  _Generic((x), \
234  uint8_t: x, \
235  int8_t: x, \
236  uint16_t: (uint16_t)__builtin_bswap16((uint16_t)(x)), \
237  int16_t: (int16_t) __builtin_bswap16((uint16_t)(x)), \
238  uint32_t: (uint32_t)__builtin_bswap32((uint32_t)(x)), \
239  int32_t: (int32_t) __builtin_bswap32((uint32_t)(x)), \
240  uint64_t: (uint64_t)__builtin_bswap64((uint64_t)(x)), \
241  int64_t: (int64_t) __builtin_bswap64((uint64_t)(x)) \
242  )
243 
245 #define xroad_array_size(arr) (sizeof(arr) / sizeof(arr[0]))
246 
248 #define xroad_array_end(arr) (&(arr)[xroad_array_size(arr)])
Definition: xroad_string.h:29
bool xroad_set_cpu_set(xroad_str_t cpus, cpu_set_t *cpu_set)
void xroad_reinit_pid(void)
PID is cached. After fork without exec it must be reset.
xroad_str_t xroad_strerror(int32_t err)
pid_t xroad_get_tid()
Get current tid.
pid_t g_self_pid
Cached PID.
void xroad_prefault_mem(const void *p, size_t s)