]>
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 "lib/alloca.h"
14 /* Ugly structure offset handling macros */
16 struct align_probe
{ char x
; long int y
; };
18 #define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
19 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
20 #define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
21 #define CPU_STRUCT_ALIGN (sizeof(struct align_probe))
25 #define MIN_(a,b) (((a)<(b))?(a):(b))
26 #define MAX_(a,b) (((a)>(b))?(a):(b))
31 #define MIN(a,b) MIN_(a,b)
32 #define MAX(a,b) MAX_(a,b)
35 #define U64(c) UINT64_C(c)
36 #define ABS(a) ((a)>=0 ? (a) : -(a))
37 #define DELTA(a,b) (((a)>=(b))?(a)-(b):(b)-(a))
38 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
39 #define BYTES(n) ((((uint) (n)) + 7) / 8)
40 #define CALL(fn, args...) ({ if (fn) fn(args); })
41 #define ADVANCE(w, r, l) ({ r -= (l); w += (l); })
43 static inline int uint_cmp(uint i1
, uint i2
)
44 { return (int)(i1
> i2
) - (int)(i1
< i2
); }
46 static inline int u64_cmp(u64 i1
, u64 i2
)
47 { return (int)(i1
> i2
) - (int)(i1
< i2
); }
52 /* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
53 #define BIT32_VAL(p) (((u32) 1) << ((p) % 32))
54 #define BIT32_TEST(b,p) ((b)[(p)/32] & BIT32_VAL(p))
55 #define BIT32_SET(b,p) ((b)[(p)/32] |= BIT32_VAL(p))
56 #define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))
57 #define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
59 /* The same, but counting bits from MSB */
60 #define BIT32R_VAL(p) ((((u32) 1) << 31) >> ((p) % 32))
61 #define BIT32R_TEST(b,p) ((b)[(p)/32] & BIT32R_VAL(p))
62 #define BIT32R_SET(b,p) ((b)[(p)/32] |= BIT32R_VAL(p))
63 #define BIT32R_CLR(b,p) ((b)[(p)/32] &= ~BIT32R_VAL(p))
64 #define BIT32R_ZERO(b,l) memset((b), 0, (l)/8)
67 #define NULL ((void *) 0)
70 /* Macros for gcc attributes */
72 #define NORET __attribute__((noreturn))
73 #define UNUSED __attribute__((unused))
74 #define PACKED __attribute__((packed))
76 #ifndef HAVE_THREAD_LOCAL
80 /* Microsecond time */
83 //typedef s64 bird_clock_t;
85 #define S_ * (btime) 1000000
86 #define MS_ * (btime) 1000
87 #define US_ * (btime) 1
99 #define TIME_INFINITY ((s64) 0x7fffffffffffffff)
105 btime timestamp
; /* Last update */
106 u64 count
; /* Available micro-tokens */
107 u16 burst
; /* Max number of tokens */
108 u16 rate
; /* Rate of replenishment (tokens / sec) */
109 u32 drop
; /* Number of failed request since last successful */
112 /* Default TBF values for rate limiting log messages */
113 #define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
115 int tbf_limit(struct tbf
*f
);
118 /* Logging and dying */
120 typedef struct buffer
{
126 #define STACK_BUFFER_INIT(buf,size) \
128 buf.start = alloca(size); \
129 buf.pos = buf.start; \
130 buf.end = buf.start + size; \
133 #define LOG_BUFFER_INIT(buf) \
134 STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
136 #define LOG_BUFFER_SIZE 1024
139 void log_commit(int class, buffer
*buf
);
140 void log_msg(const char *msg
, ...);
141 void log_rl(struct tbf
*rl
, const char *msg
, ...);
142 void die(const char *msg
, ...) NORET
;
143 void bug(const char *msg
, ...) NORET
;
145 #define L_DEBUG "\001" /* Debugging messages */
146 #define L_TRACE "\002" /* Protocol tracing */
147 #define L_INFO "\003" /* Informational messages */
148 #define L_REMOTE "\004" /* Remote protocol errors */
149 #define L_WARN "\005" /* Local warnings */
150 #define L_ERR "\006" /* Local errors */
151 #define L_AUTH "\007" /* Authorization failed etc. */
152 #define L_FATAL "\010" /* Fatal errors */
153 #define L_BUG "\011" /* BIRD bugs */
155 void debug(const char *msg
, ...); /* Printf to debug output */
159 #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
160 #define DBG(x, y...) debug(x, ##y)
162 #define DBG(x, y...) do { } while(0)
165 #define ASSERT_DIE(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
168 #define ASSERT(x) ASSERT_DIE(x)
169 #define ASSUME(x) ASSERT_DIE(x)
171 #define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
172 #define ASSUME(x) /* intentionally left blank */
177 ".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
178 ".byte 1\n" /* Python */
179 ".asciz \"bird-gdb.py\"\n"
184 /* Pseudorandom numbers */
186 u32
random_u32(void);