Something went wrong. Try again.
forked from cz-nic/bird
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295/* * BIRD Library * * (c) 1998--2004 Martin Mares <mj@ucw.cz> * * Can be freely distributed and used under the terms of the GNU GPL. */
#ifndef _BIRD_BIRDLIB_H_#define _BIRD_BIRDLIB_H_
#include "lib/alloca.h"#include <stddef.h>#include <stdarg.h>#include <stdalign.h>
/* Type checking and casting macros */
#define TYPE_EQUAL(type1, type2) \ __builtin_types_compatible_p(type1, type2)
#define TYPE_CHECK(type, val) \ STATIC_ASSERT(TYPE_EQUAL(type, typeof(val)))
#define TYPE_CAST(from, to, val) \ ({ TYPE_CHECK(from, val); (to) (val); })
#define LOOSE_CAST(from, to, val) \ ({ typeof(from) _val = val; (to) (_val); })
#define CHECKED_CAST(from, to, cond, val) \ ({ TYPE_CHECK(from, val); ASSERT(cond); (to) (val); })
#define MEMBER_TYPE(type, member) \ typeof(((type *)0)->member)
#define MEMBER_PTR_TYPE(type, member) \ typeof(&((type *)0)->member)
/* Ugly structure offset handling macros */
#define SKIP_BACK_(s, i, p) \ ((s *)((char *)(p) - offsetof(s, i)))
#define SKIP_BACK(s, i, p) \ ({ TYPE_CHECK(MEMBER_PTR_TYPE(s, i), p); SKIP_BACK_(s, i, p); })
#define OFFSETOF(s, i) offsetof(s, i)#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))#define CPU_STRUCT_ALIGN (alignof(max_align_t))
/* Utility macros */
#define MIN_(a,b) (((a)<(b))?(a):(b))#define MAX_(a,b) (((a)>(b))?(a):(b))#define CLAMP(a,l,h) (((a)<(l)) ? (l) : (((a)>(h)) ? (h) : (a)))
#ifndef PARSER#undef MIN#undef MAX#define MIN(a,b) MIN_(a,b)#define MAX(a,b) MAX_(a,b)#endif
#define ROUND_DOWN_POW2(a,b) ((a) & ~((b)-1))#define ROUND_UP_POW2(a,b) (((a)+((b)-1)) & ~((b)-1))
#define U64(c) UINT64_C(c)#define ABS(a) ((a)>=0 ? (a) : -(a))#define DELTA(a,b) (((a)>=(b))?(a)-(b):(b)-(a))#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))#define BYTES(n) ((((uint) (n)) + 7) / 8)#define CALL(fn, args...) ({ if (fn) fn(args); })#define ADVANCE(w, r, l) ({ r -= (l); w += (l); })
#define WITH(type, var, expr) \ for (typeof(type) var = (expr), *_dummy, **_once = &_dummy; _once; _once = NULL)
static inline int uint_cmp(uint i1, uint i2){ return (int)(i1 > i2) - (int)(i1 < i2); }
static inline int u64_cmp(u64 i1, u64 i2){ return (int)(i1 > i2) - (int)(i1 < i2); }
#define U32_UNDEF ((u32) -1)#define U64_UNDEF ((u64) -1)
/* Bitfield macros */
/* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */#define BIT32_VAL(p) (((u32) 1) << ((p) % 32))#define BIT32_TEST(b,p) ((b)[(p)/32] & BIT32_VAL(p))#define BIT32_SET(b,p) ((b)[(p)/32] |= BIT32_VAL(p))#define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))#define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
/* The same, but counting bits from MSB */#define BIT32R_VAL(p) ((((u32) 1) << 31) >> ((p) % 32))#define BIT32R_TEST(b,p) ((b)[(p)/32] & BIT32R_VAL(p))#define BIT32R_SET(b,p) ((b)[(p)/32] |= BIT32R_VAL(p))#define BIT32R_CLR(b,p) ((b)[(p)/32] &= ~BIT32R_VAL(p))#define BIT32R_ZERO(b,l) memset((b), 0, (l)/8)
/* Macros for gcc attributes */
#define NORET __attribute__((noreturn))#define UNUSED __attribute__((unused))#define PACKED __attribute__((packed))#define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))#define ALLOC_SIZE(...) __attribute__((alloc_size(__VA_ARGS__)))
#if __GNUC__ >= 10#define ACCESS_READ(...) __attribute__((access(read_only, __VA_ARGS__)))#define ACCESS_WRITE(...) __attribute__((access(write_only, __VA_ARGS__)))#define ACCESS_RW(...) __attribute__((access(read_write, __VA_ARGS__)))#else#define ACCESS_READ(...)#define ACCESS_WRITE(...)#define ACCESS_RW(...)#endif
#define STATIC_ASSERT(EXP) _Static_assert(EXP, #EXP)#define STATIC_ASSERT_MSG(EXP,MSG) _Static_assert(EXP, MSG)
#ifndef HAVE_THREAD_LOCAL#define _Thread_local#endif
/* Microsecond time */
typedef s64 btime;//typedef s64 bird_clock_t;
#define S_ * (btime) 1000000#define MS_ * (btime) 1000#define US_ * (btime) 1#define TO_S /1000000#define TO_MS /1000#define TO_US /1
#ifndef PARSER#define S S_#define MS MS_#define US US_#define NS /1000#endif
#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
/* Rate limiting */
struct tbf { btime timestamp; /* Last update */ u64 count; /* Available micro-tokens */ u16 burst; /* Max number of tokens */ u16 rate; /* Rate of replenishment (tokens / sec) */ u32 drop; /* Number of failed request since last successful */};
/* Default TBF values for rate limiting log messages */#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
int tbf_limit(struct tbf *f);
/* Logging and dying */
typedef struct buffer { byte *start; byte *pos; byte *end;} buffer;
#define STACK_BUFFER_INIT(buf,size) \ do { \ buf.start = alloca(size); \ buf.pos = buf.start; \ buf.end = buf.start + size; \ } while(0)
#define LOG_BUFFER_INIT(buf) \ STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
#define LOG_BUFFER_SIZE 1024
#define log log_msgvoid log_commit(int class, buffer *buf);void log_msg(const char *msg, ...);void log_rl(struct tbf *rl, const char *msg, ...);void die(const char *msg, ...) NORET;void bug(const char *msg, ...) NORET;void vlog(int class, const char *msg, va_list args);
#define L_DEBUG "\001" /* Debugging messages */#define L_TRACE "\002" /* Protocol tracing */#define L_INFO "\003" /* Informational messages */#define L_REMOTE "\004" /* Remote protocol errors */#define L_WARN "\005" /* Local warnings */#define L_ERR "\006" /* Local errors */#define L_AUTH "\007" /* Authorization failed etc. */#define L_FATAL "\010" /* Fatal errors */#define L_BUG "\011" /* BIRD bugs */#define L_MAX 10
void debug(const char *msg, ...); /* Printf to debug output */void debug_safe(const char *msg); /* Printf to debug output, async-safe */
/* Debugging */
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)#define DBG(x, y...) debug(x, ##y)#else#define DBG(x, y...) do { } while(0)#endif
#define ASSERT_DIE(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
#define EXPENSIVE_CHECK(x) /* intentionally left blank */
#ifdef DEBUGGING#define ASSERT(x) ASSERT_DIE(x)#define ASSUME(x) ASSERT_DIE(x)#ifdef ENABLE_EXPENSIVE_CHECKS#undef EXPENSIVE_CHECK#define EXPENSIVE_CHECK(x) ASSERT_DIE(x)#endif#else#define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)#define ASSUME(x) /* intentionally left blank */#endif
#ifdef DEBUGGINGasm( ".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n" ".byte 1\n" /* Python */ ".asciz \"bird-gdb.py\"\n" ".popsection\n" );#endif
/* Pseudorandom numbers */
u32 brandom(void);#define random_u32 brandomvoid random_init(void);void random_init_thread(void);void random_bytes(void *buf, size_t size);
/* Hashing */
/* Constant parameter for non-parametrized hashes */#define HASH_PARAM 2902958171u
/* Precomputed powers of HASH_PARAM */#define HASH_PARAM1 ((u64) HASH_PARAM)#define HASH_PARAM2 (HASH_PARAM1 * HASH_PARAM)#define HASH_PARAM3 (HASH_PARAM2 * HASH_PARAM)#define HASH_PARAM4 (HASH_PARAM3 * HASH_PARAM)
/* Reduce intermediate 64-bit value to final 32-bit value */static inline u32 hash_value(u64 a){ return ((u32) a) ^ ((u32) (a >> 32)); }
static inline u64 u32_hash0(u32 v, u32 p, u64 acc){ return (acc + v) * p; }
static inline u64 u64_hash0(u64 v, u32 p, u64 acc){ return u32_hash0(v >> 32, p, u32_hash0(v, p, acc)); }
static inline u32 u64_hash(u64 v){ return hash_value(u64_hash0(v, HASH_PARAM, 0)); }
/* Dumping */struct dump_request { u64 size; btime begin; uint indent, offset; void (*write)(struct dump_request *, const char *fmt, ...); void (*report)(struct dump_request *, int state, const char *fmt, ...);};
#define RDUMP(...) dreq->write(dreq, __VA_ARGS__)
#endif