]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/birdlib.h
Flowspec: Fix values for true/false operators
[thirdparty/bird.git] / lib / birdlib.h
1 /*
2 * BIRD Library
3 *
4 * (c) 1998--2004 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_BIRDLIB_H_
10 #define _BIRD_BIRDLIB_H_
11
12 #include "lib/alloca.h"
13
14 /* Ugly structure offset handling macros */
15
16 struct align_probe { char x; long int y; };
17
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))
22
23 /* Utility macros */
24
25 #define MIN_(a,b) (((a)<(b))?(a):(b))
26 #define MAX_(a,b) (((a)>(b))?(a):(b))
27
28 #ifndef PARSER
29 #undef MIN
30 #undef MAX
31 #define MIN(a,b) MIN_(a,b)
32 #define MAX(a,b) MAX_(a,b)
33 #endif
34
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); })
42
43 static inline int uint_cmp(uint i1, uint i2)
44 { return (int)(i1 > i2) - (int)(i1 < i2); }
45
46 static inline int u64_cmp(u64 i1, u64 i2)
47 { return (int)(i1 > i2) - (int)(i1 < i2); }
48
49
50 /* Bitfield macros */
51
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)
58
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)
65
66 #ifndef NULL
67 #define NULL ((void *) 0)
68 #endif
69
70 /* Macros for gcc attributes */
71
72 #define NORET __attribute__((noreturn))
73 #define UNUSED __attribute__((unused))
74 #define PACKED __attribute__((packed))
75 #define NONNULL(...) __attribute__((nonnull((__VA_ARGS__))))
76
77 #ifndef HAVE_THREAD_LOCAL
78 #define _Thread_local
79 #endif
80
81 /* Microsecond time */
82
83 typedef s64 btime;
84 //typedef s64 bird_clock_t;
85
86 #define S_ * (btime) 1000000
87 #define MS_ * (btime) 1000
88 #define US_ * (btime) 1
89 #define TO_S /1000000
90 #define TO_MS /1000
91 #define TO_US /1
92
93 #ifndef PARSER
94 #define S S_
95 #define MS MS_
96 #define US US_
97 #define NS /1000
98 #endif
99
100 #define TIME_INFINITY ((s64) 0x7fffffffffffffff)
101
102
103 /* Rate limiting */
104
105 struct tbf {
106 btime timestamp; /* Last update */
107 u64 count; /* Available micro-tokens */
108 u16 burst; /* Max number of tokens */
109 u16 rate; /* Rate of replenishment (tokens / sec) */
110 u32 drop; /* Number of failed request since last successful */
111 };
112
113 /* Default TBF values for rate limiting log messages */
114 #define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
115
116 int tbf_limit(struct tbf *f);
117
118
119 /* Logging and dying */
120
121 typedef struct buffer {
122 byte *start;
123 byte *pos;
124 byte *end;
125 } buffer;
126
127 #define STACK_BUFFER_INIT(buf,size) \
128 do { \
129 buf.start = alloca(size); \
130 buf.pos = buf.start; \
131 buf.end = buf.start + size; \
132 } while(0)
133
134 #define LOG_BUFFER_INIT(buf) \
135 STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
136
137 #define LOG_BUFFER_SIZE 1024
138
139 #define log log_msg
140 void log_commit(int class, buffer *buf);
141 void log_msg(const char *msg, ...);
142 void log_rl(struct tbf *rl, const char *msg, ...);
143 void die(const char *msg, ...) NORET;
144 void bug(const char *msg, ...) NORET;
145
146 #define L_DEBUG "\001" /* Debugging messages */
147 #define L_TRACE "\002" /* Protocol tracing */
148 #define L_INFO "\003" /* Informational messages */
149 #define L_REMOTE "\004" /* Remote protocol errors */
150 #define L_WARN "\005" /* Local warnings */
151 #define L_ERR "\006" /* Local errors */
152 #define L_AUTH "\007" /* Authorization failed etc. */
153 #define L_FATAL "\010" /* Fatal errors */
154 #define L_BUG "\011" /* BIRD bugs */
155
156 void debug(const char *msg, ...); /* Printf to debug output */
157
158 /* Debugging */
159
160 #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
161 #define DBG(x, y...) debug(x, ##y)
162 #else
163 #define DBG(x, y...) do { } while(0)
164 #endif
165
166 #define ASSERT_DIE(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
167
168 #define EXPENSIVE_CHECK(x) /* intentionally left blank */
169
170 #ifdef DEBUGGING
171 #define ASSERT(x) ASSERT_DIE(x)
172 #define ASSUME(x) ASSERT_DIE(x)
173 #ifdef ENABLE_EXPENSIVE_CHECKS
174 #undef EXPENSIVE_CHECK
175 #define EXPENSIVE_CHECK(x) ASSERT_DIE(x)
176 #endif
177 #else
178 #define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
179 #define ASSUME(x) /* intentionally left blank */
180 #endif
181
182
183 #ifdef DEBUGGING
184 asm(
185 ".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
186 ".byte 1\n" /* Python */
187 ".asciz \"bird-gdb.py\"\n"
188 ".popsection\n"
189 );
190 #endif
191
192 /* Pseudorandom numbers */
193
194 u32 random_u32(void);
195
196 #endif