Annotation of embedaddon/bird/lib/birdlib.h, revision 1.1.1.1
1.1 misho 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 "timer.h"
13: #include "alloca.h"
14:
15: /* Ugly structure offset handling macros */
16:
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))
20:
21: /* Utility macros */
22:
23: #define MIN_(a,b) (((a)<(b))?(a):(b))
24: #define MAX_(a,b) (((a)>(b))?(a):(b))
25:
26: #ifndef PARSER
27: #undef MIN
28: #undef MAX
29: #define MIN(a,b) MIN_(a,b)
30: #define MAX(a,b) MAX_(a,b)
31: #endif
32:
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:
38:
39: /* Bitfield macros */
40:
41: /* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
42: #define BIT32_VAL(p) (((u32) 1) << ((p) % 32))
43: #define BIT32_TEST(b,p) ((b)[(p)/32] & BIT32_VAL(p))
44: #define BIT32_SET(b,p) ((b)[(p)/32] |= BIT32_VAL(p))
45: #define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))
46: #define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
47:
48: #ifndef NULL
49: #define NULL ((void *) 0)
50: #endif
51:
52: #ifndef IPV6
53: #define IP_VERSION 4
54: #else
55: #define IP_VERSION 6
56: #endif
57:
58:
59: /* Macros for gcc attributes */
60:
61: #define NORET __attribute__((noreturn))
62: #define UNUSED __attribute__((unused))
63: #define PACKED __attribute__((packed))
64:
65: #ifdef IPV6
66: #define UNUSED4
67: #define UNUSED6 UNUSED
68: #else
69: #define UNUSED4 UNUSED
70: #define UNUSED6
71: #endif
72:
73: /* Microsecond time */
74:
75: typedef s64 btime;
76:
77: #define S_ *1000000
78: #define MS_ *1000
79: #define US_ *1
80: #define TO_S /1000000
81: #define TO_MS /1000
82: #define TO_US /1
83:
84: #ifndef PARSER
85: #define S S_
86: #define MS MS_
87: #define US US_
88: #endif
89:
90:
91: /* Rate limiting */
92:
93: struct tbf {
94: bird_clock_t timestamp; /* Last update */
95: u16 count; /* Available tokens */
96: u16 burst; /* Max number of tokens */
97: u16 rate; /* Rate of replenishment */
98: u16 mark; /* Whether last op was limited */
99: };
100:
101: /* Default TBF values for rate limiting log messages */
102: #define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
103:
104: void tbf_update(struct tbf *f);
105:
106: static inline int
107: tbf_limit(struct tbf *f)
108: {
109: tbf_update(f);
110:
111: if (!f->count)
112: {
113: f->mark = 1;
114: return 1;
115: }
116:
117: f->count--;
118: f->mark = 0;
119: return 0;
120: }
121:
122:
123: /* Logging and dying */
124:
125: typedef struct buffer {
126: byte *start;
127: byte *pos;
128: byte *end;
129: } buffer;
130:
131: #define STACK_BUFFER_INIT(buf,size) \
132: do { \
133: buf.start = alloca(size); \
134: buf.pos = buf.start; \
135: buf.end = buf.start + size; \
136: } while(0)
137:
138: #define LOG_BUFFER_INIT(buf) \
139: STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
140:
141: #define LOG_BUFFER_SIZE 1024
142:
143: #define log log_msg
144: void log_commit(int class, buffer *buf);
145: void log_msg(const char *msg, ...);
146: void log_rl(struct tbf *rl, const char *msg, ...);
147: void die(const char *msg, ...) NORET;
148: void bug(const char *msg, ...) NORET;
149:
150: #define L_DEBUG "\001" /* Debugging messages */
151: #define L_TRACE "\002" /* Protocol tracing */
152: #define L_INFO "\003" /* Informational messages */
153: #define L_REMOTE "\004" /* Remote protocol errors */
154: #define L_WARN "\005" /* Local warnings */
155: #define L_ERR "\006" /* Local errors */
156: #define L_AUTH "\007" /* Authorization failed etc. */
157: #define L_FATAL "\010" /* Fatal errors */
158: #define L_BUG "\011" /* BIRD bugs */
159:
160: void debug(const char *msg, ...); /* Printf to debug output */
161:
162: /* Debugging */
163:
164: #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
165: #define DBG(x, y...) debug(x, ##y)
166: #else
167: #define DBG(x, y...) do { } while(0)
168: #endif
169:
170: #ifdef DEBUGGING
171: #define ASSERT(x) do { if (!(x)) bug("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
172: #else
173: #define ASSERT(x) do { } while(0)
174: #endif
175:
176: /* Pseudorandom numbers */
177:
178: u32 random_u32(void);
179:
180: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>