XROAD
xroad_aux.h File Reference
#include "xroad_common_fwd.h"
#include <stdint.h>
#include <stdbool.h>
#include <sched.h>
#include <assert.h>
#include <string.h>
Include dependency graph for xroad_aux.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define xroad_likely(x)   __builtin_expect(!!(x), 1)
 
#define xroad_unlikely(x)   __builtin_expect(!!(x), 0)
 
#define xroad_cpu_pause()   __asm__ volatile("pause\n": : :"memory")
 
#define xroad_fence()   __asm__ volatile("mfence": : :"memory")
 
#define __dtor__(fun)   __attribute__((__cleanup__(fun)))
 
#define __free_dtor__   __dtor__(free_destructor)
 
#define xroad_min(a, b)
 
#define xroad_max(a, b)
 
#define xroad_exchange(var, new_val)
 
#define xroad_dbl_cmp(lhs, rhs)
 
#define xroad_dbl_eq(lhs, rhs)   (!xroad_dbl_cmp(lhs, rhs))
 
#define xroad_div_roundup(lhs, rhs)
 
#define xroad_roundup(lhs, rhs)
 
#define xroad_roundup_pow2(lhs, rhs)
 
#define xroad_ceil_pow2(v)
 
#define xroad_is_pow2(v)   (v && !(v & (v - 1)))
 
#define xroad_uint_cyclic_less(l, r)    ((__typeof__(l))(l - (__typeof__(l))(r)) > ((__typeof__(l))~0) / 2u)
 Compare unsigned numbers in a cyclic manner (so UINT_MAX is less than 0).
 
#define xroad_macro_get_first(first_arg, ...)   first_arg
 Trivial helper to get first argument of macro.
 
#define XROAD_STATIC_ASSERT(...)   _Static_assert(__VA_ARGS__)
 
#define xroad_alignof(x)   _Alignof(x)
 
#define xroad_attr_unused   __attribute__((unused))
 
#define _XROAD_CONCAT2(s1, s2)   s1##s2
 Internal macro.
 
#define _XROAD_CONCAT(s1, s2)   _XROAD_CONCAT2(s1, s2)
 Internal macro.
 
#define _xroad_arg_passthru(x)   (x)
 Internal macro.
 
#define _xroad_arg_addrof(x)   &(x)
 Internal macro.
 
#define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_PUSH()   do{}while(0)
 
#define XROAD_GCC10_STROP_OVERFLOW_SUPPRESS_POP()   do{}while(0)
 
#define xroad_bswap(x)
 
#define xroad_array_size(arr)   (sizeof(arr) / sizeof(arr[0]))
 returns size of an array.
 
#define xroad_array_end(arr)   (&(arr)[xroad_array_size(arr)])
 returns array end
 

Enumerations

enum  { xroad_reg_page_sz = 4096 , xroad_huge_page_sz = 2 * 1024 * 1024 }
 

Functions

void free_destructor (void *)
 
xroad_str_t xroad_strerror (int32_t err)
 
bool xroad_set_cpu_set (xroad_str_t cpus, cpu_set_t *cpu_set)
 
void xroad_prefault_mem (const void *p, size_t s)
 
void xroad_reinit_pid (void)
 PID is cached. After fork without exec it must be reset.
 
pid_t xroad_get_tid ()
 Get current tid.
 

Variables

pid_t g_self_pid
 Cached PID.
 

Detailed Description

Macro Definition Documentation

◆ xroad_bswap

#define xroad_bswap (   x)
Value:
_Generic((x), \
uint8_t: x, \
int8_t: x, \
uint16_t: (uint16_t)__builtin_bswap16((uint16_t)(x)), \
int16_t: (int16_t) __builtin_bswap16((uint16_t)(x)), \
uint32_t: (uint32_t)__builtin_bswap32((uint32_t)(x)), \
int32_t: (int32_t) __builtin_bswap32((uint32_t)(x)), \
uint64_t: (uint64_t)__builtin_bswap64((uint64_t)(x)), \
int64_t: (int64_t) __builtin_bswap64((uint64_t)(x)) \
)

◆ xroad_ceil_pow2

#define xroad_ceil_pow2 (   v)
Value:
({ \
__typeof__(v) _v = (v); \
(_v <= 1) \
? (__typeof__(v))1 \
: (__typeof__(v))1 << (sizeof(unsigned long long) * 8 - __builtin_clzll((unsigned long long)(_v - 1))); \
})

Round up v to the nearest power of 2 (E.g. 5->8, 8->8)

◆ xroad_dbl_cmp

#define xroad_dbl_cmp (   lhs,
  rhs 
)
Value:
({ \
__typeof__(lhs) lhs1977_ = (lhs); \
__typeof__(rhs) rhs1977_ = (rhs); \
(fabs(lhs1977_ - rhs1977_) < XROAD_EPSILON) ? 0 : \
(lhs1977_ < rhs1977_ ? -1 : 1); \
})

compare two doubles

Returns
0 - equal, <0 - less, >0 - greater

◆ xroad_dbl_eq

#define xroad_dbl_eq (   lhs,
  rhs 
)    (!xroad_dbl_cmp(lhs, rhs))

compare two doubles for equality

Returns
1 - equal, 0 - not equal

◆ xroad_div_roundup

#define xroad_div_roundup (   lhs,
  rhs 
)
Value:
({ \
__typeof__(rhs) _r = (rhs); \
(lhs + _r - 1) / _r; \
})

Divide and round-up. Useful for positive integers.

Returns
lhs divided by rhs and rounded up (so that is the reminder of the division is non-zero the result is greater by 1)

◆ xroad_exchange

#define xroad_exchange (   var,
  new_val 
)
Value:
({ \
__typeof__(var)* _p_var = &(var); \
__typeof__(var) _old_val = *_p_var; \
*_p_var = (new_val); \
_old_val; \
})

Assign a new value and return the previous one (just like std::exchange in C++)

◆ xroad_is_pow2

#define xroad_is_pow2 (   v)    (v && !(v & (v - 1)))

Checks if integer is a power of 2

◆ xroad_max

#define xroad_max (   a,
  b 
)
Value:
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})

find max among a and b

◆ xroad_min

#define xroad_min (   a,
  b 
)
Value:
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})

find min among a and b

◆ xroad_roundup

#define xroad_roundup (   lhs,
  rhs 
)
Value:
({ \
__typeof__(rhs) _r = (rhs); \
((lhs + _r - 1) / _r) * _r; \
})

Round up lhs to the nearest multiple of rhs

◆ xroad_roundup_pow2

#define xroad_roundup_pow2 (   lhs,
  rhs 
)
Value:
({ \
__typeof__(rhs) _r = (rhs); \
assert(0 == (_r & (_r - 1))); \
((lhs + _r - 1) & ~(_r - 1)); \
})

Round up lhs to the nearest multiple of rhs where rhs is a power of 2

Function Documentation

◆ xroad_prefault_mem()

void xroad_prefault_mem ( const void *  p,
size_t  s 
)

Prefault (make sure pages are in RAM) a memory buffer.

Parameters
pPointer to the beginnig of memory buffer
sSize of memory buffer

◆ xroad_set_cpu_set()

bool xroad_set_cpu_set ( xroad_str_t  cpus,
cpu_set_t *  cpu_set 
)

fill cpu_set with CPUs

Parameters
[in]cpus- list of CPUs starting from 0 "0,1,2,3"
[out]cpu_set- cpu set to fill
Returns
true - filled, else cpus has wrong format

◆ xroad_strerror()

xroad_str_t xroad_strerror ( int32_t  err)

convert error code into string representation

Parameters
[in]err- error to convert
Returns
error string representation