File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bird2 / lib / birdlib.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 21 16:03:56 2019 UTC (4 years, 8 months ago) by misho
Branches: bird2, MAIN
CVS tags: v2_0_7p0, HEAD
bird2 ver 2.0.7

    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: 
   76: #ifndef HAVE_THREAD_LOCAL
   77: #define _Thread_local
   78: #endif
   79: 
   80: /* Microsecond time */
   81: 
   82: typedef s64 btime;
   83: //typedef s64 bird_clock_t;
   84: 
   85: #define S_	* (btime) 1000000
   86: #define MS_	* (btime) 1000
   87: #define US_	* (btime) 1
   88: #define TO_S	/1000000
   89: #define TO_MS	/1000
   90: #define TO_US	/1
   91: 
   92: #ifndef PARSER
   93: #define S	S_
   94: #define MS	MS_
   95: #define US	US_
   96: #define NS	/1000
   97: #endif
   98: 
   99: #define TIME_INFINITY ((s64) 0x7fffffffffffffff)
  100: 
  101: 
  102: /* Rate limiting */
  103: 
  104: struct tbf {
  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 */
  110: };
  111: 
  112: /* Default TBF values for rate limiting log messages */
  113: #define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
  114: 
  115: int tbf_limit(struct tbf *f);
  116: 
  117: 
  118: /* Logging and dying */
  119: 
  120: typedef struct buffer {
  121:   byte *start;
  122:   byte *pos;
  123:   byte *end;
  124: } buffer;
  125: 
  126: #define STACK_BUFFER_INIT(buf,size)		\
  127:   do {						\
  128:     buf.start = alloca(size);			\
  129:     buf.pos = buf.start;			\
  130:     buf.end = buf.start + size;			\
  131:   } while(0)
  132: 
  133: #define LOG_BUFFER_INIT(buf)			\
  134:   STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
  135: 
  136: #define LOG_BUFFER_SIZE 1024
  137: 
  138: #define log log_msg
  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;
  144: 
  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 */
  154: 
  155: void debug(const char *msg, ...);	/* Printf to debug output */
  156: 
  157: /* Debugging */
  158: 
  159: #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
  160: #define DBG(x, y...) debug(x, ##y)
  161: #else
  162: #define DBG(x, y...) do { } while(0)
  163: #endif
  164: 
  165: #ifdef DEBUGGING
  166: #define ASSERT(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
  167: #else
  168: #define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
  169: #endif
  170: 
  171: #ifdef DEBUGGING
  172: asm(
  173:     ".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
  174:     ".byte 1\n" /* Python */
  175:     ".asciz \"bird-gdb.py\"\n"
  176:     ".popsection\n"
  177:    );
  178: #endif
  179: 
  180: /* Pseudorandom numbers */
  181: 
  182: u32 random_u32(void);
  183: 
  184: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>