00001 #ifndef MISC_H
00002 #define MISC_H
00003
00004
00005
00006
00007
00008
00009
00010 #define NELEM(x) ((int)(sizeof(x)/sizeof(*(x))))
00011 #define NORETURN(x) void x __attribute__((__noreturn__))
00012 #define DEFINE(x) typeof(x) x
00013 #define OFFSET_OF(type, elem) ((u8 *)&((type *)0)->elem - (u8 *)0)
00014 #define BASE_OF(type, elem, p) ((type *)((u8 *)(p) - OFFSET_OF(type, elem)))
00015
00016 #ifndef __cplusplus
00017 #define not !
00018 #endif
00019 #define elif else if
00020 #define Case break; case
00021 #define Default break; default
00022 #define streq(a, b) (strcmp((a), (b)) == 0)
00023 #define min(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b ? _a : _b; })
00024 #define max(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b ? _a : _b; })
00025 #define bound(a,b,c) ({ typeof(a) _a = a; typeof(b) _b = b; typeof(c) _c = c; \
00026 _b < _a ? _a : _b > _c ? _c : _b; })
00027
00028 typedef unsigned char u8;
00029 typedef unsigned short u16;
00030 typedef unsigned int u32;
00031
00032 typedef signed char s8;
00033 typedef signed short s16;
00034 typedef signed int s32;
00035
00036
00037
00038
00039
00040 extern char *prgname;
00041 void setprgname(char *argv_0);
00042
00043 NORETURN(fatal(const char *str, ...));
00044 NORETURN(fatal_ioerror(const char *str));
00045 NORETURN(out_of_mem(int size));
00046 void error(const char *str, ...);
00047 void ioerror(const char *str);
00048
00049 #endif