Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / birdlib.h @ 6b3f1a54

History | View | Annotate | Download (4.01 KB)

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
#ifndef NULL
60
#define NULL ((void *) 0)
61
#endif
62

    
63
/* Macros for gcc attributes */
64

    
65
#define NORET __attribute__((noreturn))
66
#define UNUSED __attribute__((unused))
67
#define PACKED __attribute__((packed))
68

    
69
/* Microsecond time */
70

    
71
typedef s64 btime;
72
//typedef s64 bird_clock_t;
73

    
74
#define S_        * (btime) 1000000
75
#define MS_        * (btime) 1000
76
#define US_        * (btime) 1
77
#define TO_S        /1000000
78
#define TO_MS        /1000
79
#define TO_US        /1
80

    
81
#ifndef PARSER
82
#define S        S_
83
#define MS        MS_
84
#define US        US_
85
#define NS        /1000
86
#endif
87

    
88
#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
89

    
90

    
91
/* Rate limiting */
92

    
93
struct tbf {
94
  btime timestamp;                        /* Last update */
95
  u64 count;                                /* Available micro-tokens */
96
  u16 burst;                                /* Max number of tokens */
97
  u16 rate;                                /* Rate of replenishment (tokens / sec) */
98
  u32 drop;                                /* Number of failed request since last successful */
99
};
100

    
101
/* Default TBF values for rate limiting log messages */
102
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
103

    
104
int tbf_limit(struct tbf *f);
105

    
106

    
107
/* Logging and dying */
108

    
109
typedef struct buffer {
110
  byte *start;
111
  byte *pos;
112
  byte *end;
113
} buffer;
114

    
115
#define STACK_BUFFER_INIT(buf,size)                \
116
  do {                                                \
117
    buf.start = alloca(size);                        \
118
    buf.pos = buf.start;                        \
119
    buf.end = buf.start + size;                        \
120
  } while(0)
121

    
122
#define LOG_BUFFER_INIT(buf)                        \
123
  STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
124

    
125
#define LOG_BUFFER_SIZE 1024
126

    
127
#define log log_msg
128
void log_commit(int class, buffer *buf);
129
void log_msg(const char *msg, ...);
130
void log_rl(struct tbf *rl, const char *msg, ...);
131
void die(const char *msg, ...) NORET;
132
void bug(const char *msg, ...) NORET;
133

    
134
#define L_DEBUG "\001"                        /* Debugging messages */
135
#define L_TRACE "\002"                        /* Protocol tracing */
136
#define L_INFO "\003"                        /* Informational messages */
137
#define L_REMOTE "\004"                        /* Remote protocol errors */
138
#define L_WARN "\005"                        /* Local warnings */
139
#define L_ERR "\006"                        /* Local errors */
140
#define L_AUTH "\007"                        /* Authorization failed etc. */
141
#define L_FATAL "\010"                        /* Fatal errors */
142
#define L_BUG "\011"                        /* BIRD bugs */
143

    
144
void debug(const char *msg, ...);        /* Printf to debug output */
145

    
146
/* Debugging */
147

    
148
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
149
#define DBG(x, y...) debug(x, ##y)
150
#else
151
#define DBG(x, y...) do { } while(0)
152
#endif
153

    
154
#ifdef DEBUGGING
155
#define ASSERT(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
156
#else
157
#define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
158
#endif
159

    
160
/* Pseudorandom numbers */
161

    
162
u32 random_u32(void);
163

    
164
#endif