misc.h 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. #ifndef MISC_H
  2. #define MISC_H
  3. #define PTR (void *)
  4. #define NELEM(x) ((int)(sizeof(x)/sizeof(*(x))))
  5. #define NORETURN(x) void x __attribute__((__noreturn__))
  6. #define DEFINE(x) typeof(x) x
  7. #define OFFSET_OF(type, elem) ((u8 *)&((type *)0)->elem - (u8 *)0)
  8. #define BASE_OF(type, elem, p) ((type *)((u8 *)(p) - OFFSET_OF(type, elem)))
  9. #define not !
  10. #define streq(a, b) (strcmp((a), (b)) == 0)
  11. #define min(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b ? _a : _b; })
  12. #define max(a,b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b ? _a : _b; })
  13. #define bound(a,b,c) ({ typeof(a) _a = a; typeof(b) _b = b; typeof(c) _c = c; \
  14. _b < _a ? _a : _b > _c ? _c : _b; })
  15. typedef unsigned char u8;
  16. typedef unsigned short u16;
  17. typedef unsigned int u32;
  18. typedef signed char s8;
  19. typedef signed short s16;
  20. typedef signed int s32;
  21. extern char *prgname;
  22. void setprgname(char *argv_0);
  23. NORETURN(fatal(const char *str, ...));
  24. NORETURN(fatal_ioerror(const char *str));
  25. NORETURN(out_of_mem(int size));
  26. void error(const char *str, ...);
  27. void ioerror(const char *str);
  28. #endif