]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/birdlib.h
4 * (c) 1998--2004 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #ifndef _BIRD_BIRDLIB_H_
10 #define _BIRD_BIRDLIB_H_
12 #include "sysdep/unix/timer.h"
13 #include "lib/alloca.h"
15 /* Ugly structure offset handling macros */
17 #define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
18 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
19 #define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
23 #define MIN_(a,b) (((a)<(b))?(a):(b))
24 #define MAX_(a,b) (((a)>(b))?(a):(b))
29 #define MIN(a,b) MIN_(a,b)
30 #define MAX(a,b) MAX_(a,b)
33 #define U64(c) UINT64_C(c)
34 #define ABS(a) ((a)>=0 ? (a) : -(a))
35 #define DELTA(a,b) (((a)>=(b))?(a)-(b):(b)-(a))
36 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
37 #define CALL(fn, args...) ({ if (fn) fn(args); })
39 static inline int uint_cmp(uint i1
, uint i2
)
40 { return (int)(i1
> i2
) - (int)(i1
< i2
); }
42 static inline int u64_cmp(u64 i1
, u64 i2
)
43 { return (int)(i1
> i2
) - (int)(i1
< i2
); }
48 /* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
49 #define BIT32_VAL(p) (((u32) 1) << ((p) % 32))
50 #define BIT32_TEST(b,p) ((b)[(p)/32] & BIT32_VAL(p))
51 #define BIT32_SET(b,p) ((b)[(p)/32] |= BIT32_VAL(p))
52 #define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))
53 #define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
56 #define NULL ((void *) 0)
59 /* Macros for gcc attributes */
61 #define NORET __attribute__((noreturn))
62 #define UNUSED __attribute__((unused))
63 #define PACKED __attribute__((packed))
65 /* Microsecond time */
86 bird_clock_t timestamp
; /* Last update */
87 u16 count
; /* Available tokens */
88 u16 burst
; /* Max number of tokens */
89 u16 rate
; /* Rate of replenishment */
90 u16 mark
; /* Whether last op was limited */
93 /* Default TBF values for rate limiting log messages */
94 #define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
96 void tbf_update(struct tbf
*f
);
99 tbf_limit(struct tbf
*f
)
115 /* Logging and dying */
117 typedef struct buffer
{
123 #define STACK_BUFFER_INIT(buf,size) \
125 buf.start = alloca(size); \
126 buf.pos = buf.start; \
127 buf.end = buf.start + size; \
130 #define LOG_BUFFER_INIT(buf) \
131 STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
133 #define LOG_BUFFER_SIZE 1024
136 void log_commit(int class, buffer
*buf
);
137 void log_msg(const char *msg
, ...);
138 void log_rl(struct tbf
*rl
, const char *msg
, ...);
139 void die(const char *msg
, ...) NORET
;
140 void bug(const char *msg
, ...) NORET
;
142 #define L_DEBUG "\001" /* Debugging messages */
143 #define L_TRACE "\002" /* Protocol tracing */
144 #define L_INFO "\003" /* Informational messages */
145 #define L_REMOTE "\004" /* Remote protocol errors */
146 #define L_WARN "\005" /* Local warnings */
147 #define L_ERR "\006" /* Local errors */
148 #define L_AUTH "\007" /* Authorization failed etc. */
149 #define L_FATAL "\010" /* Fatal errors */
150 #define L_BUG "\011" /* BIRD bugs */
152 void debug(const char *msg
, ...); /* Printf to debug output */
156 #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
157 #define DBG(x, y...) debug(x, ##y)
159 #define DBG(x, y...) do { } while(0)
163 #define ASSERT(x) do { if (!(x)) bug("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
165 #define ASSERT(x) do { } while(0)
168 /* Pseudorandom numbers */
170 u32
random_u32(void);