0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-15 07:38:43 +00:00
bird/lib/birdlib.h

220 lines
5.8 KiB
C
Raw Normal View History

/*
* BIRD Library
*
2004-05-31 21:02:09 +00:00
* (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 "sysdep/config.h"
#include "lib/alloca.h"
/* Ugly structure offset handling macros */
2009-07-23 20:21:17 +00:00
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
2021-10-13 09:33:48 +00:00
#define SKIP_BACK(s, i, p) ({ s *_ptr = ((s *)((char *)p - OFFSETOF(s, i))); ASSERT_DIE(&_ptr->i == p); _ptr; })
2004-06-01 10:28:25 +00:00
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
#define CPU_STRUCT_ALIGN (MAX_(_Alignof(void*), _Alignof(u64)))
2022-04-10 12:11:46 +00:00
#define BIRD_CPU_ALIGN(s) BIRD_ALIGN((s), CPU_STRUCT_ALIGN)
/* Structure item alignment macros */
#define PADDING_NAME(id) _padding_##id
#define PADDING_(id, sz) u8 PADDING_NAME(id)[sz]
#if CPU_POINTER_ALIGNMENT == 4
#define PADDING(id, n32, n64) PADDING_(id, n32)
#elif CPU_POINTER_ALIGNMENT == 8
#define PADDING(id, n32, n64) PADDING_(id, n64)
#else
#error "Strange CPU pointer alignment: " CPU_POINTER_ALIGNMENT
#endif
/* Utility macros */
#define MIN_(a,b) (((a)<(b))?(a):(b))
#define MAX_(a,b) (((a)>(b))?(a):(b))
#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))
2015-11-24 12:47:28 +00:00
#define U64(c) UINT64_C(c)
2002-11-13 08:47:19 +00:00
#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); })
1998-07-09 19:36:05 +00:00
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); }
2015-02-24 15:19:14 +00:00
/* Bitfield macros */
/* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
2014-06-26 09:58:57 +00:00
#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)
#ifndef NULL
#define NULL ((void *) 0)
#endif
2004-06-05 09:01:12 +00:00
/* Macros for gcc attributes */
#define NORET __attribute__((noreturn))
#define USE_RESULT __atribute__((warn_unused_result))
2004-05-31 21:02:09 +00:00
#define UNUSED __attribute__((unused))
#define PACKED __attribute__((packed))
2020-04-27 20:33:10 +00:00
#define NONNULL(...) __attribute__((nonnull((__VA_ARGS__))))
2021-06-06 15:23:45 +00:00
#define STATIC_ASSERT(EXP) _Static_assert(EXP, #EXP)
#define STATIC_ASSERT_MSG(EXP,MSG) _Static_assert(EXP, MSG)
/* Microsecond time */
typedef s64 btime;
2017-11-28 16:06:10 +00:00
//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
2017-11-28 16:06:10 +00:00
#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
/* Rate limiting */
struct tbf {
2017-11-28 16:06:10 +00:00
btime timestamp; /* Last update */
u64 count; /* Available micro-tokens */
u16 burst; /* Max number of tokens */
2017-11-28 16:06:10 +00:00
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 }
2017-11-28 16:06:10 +00:00
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_msg
void 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;
#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 */
void debug(const char *msg, ...); /* Printf to debug output */
/* Debugging */
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
#define DBG(x, y...) debug(x, ##y)
#define DBGL(x, y...) debug(x "\n", ##y)
#elif defined(DEBUG_TO_LOG)
#define DBG(...) do { } while (0)
#define DBGL(...) log(L_DEBUG __VA_ARGS__)
#else
#define DBG(...) do { } while(0)
#define DBGL(...) do { } while (0)
#endif
2019-08-01 12:25:01 +00:00
#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
2019-08-01 12:25:01 +00:00
#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)
2019-08-01 12:25:01 +00:00
#define ASSUME(x) /* intentionally left blank */
#endif
2019-01-23 16:08:27 +00:00
#ifdef DEBUGGING
asm(
".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
".byte 1\n" /* Python */
".asciz \"bird-gdb.py\"\n"
".popsection\n"
);
#endif
/* Pseudorandom numbers */
u32 random_u32(void);
void random_init(void);
void random_bytes(void *buf, size_t size);
#endif