timeout-debug.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * D E B U G R O U T I N E S
  3. *
  4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  5. #if TIMEOUT_DEBUG - 0
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #undef TIMEOUT_DEBUG
  9. #define TIMEOUT_DEBUG 1
  10. #define DEBUG_LEVEL timeout_debug
  11. static int timeout_debug;
  12. #define SAYit_(lvl, fmt, ...) do { \
  13. if (DEBUG_LEVEL >= (lvl)) \
  14. fprintf(stderr, fmt "%s", __FILE__, __LINE__, __func__, __VA_ARGS__); \
  15. } while (0)
  16. #define SAYit(lvl, ...) SAYit_((lvl), "%s:%d:%s: " __VA_ARGS__, "\n")
  17. #define PANIC(...) do { \
  18. SAYit(0, __VA_ARGS__); \
  19. _Exit(EXIT_FAILURE); \
  20. } while (0)
  21. #else
  22. #undef TIMEOUT_DEBUG
  23. #define TIMEOUT_DEBUG 0
  24. #define DEBUG_LEVEL 0
  25. #define SAYit(...) (void)0
  26. #endif
  27. #define SAY(...) SAYit(1, __VA_ARGS__)
  28. #define HAI SAY("HAI")
  29. static inline char *fmt_(char *buf, uint64_t ts, int wheel_bit, int wheel_num) {
  30. char *p = buf;
  31. int wheel, n, i;
  32. for (wheel = wheel_num - 2; wheel >= 0; wheel--) {
  33. n = ((1 << wheel_bit) - 1) & (ts >> (wheel * WHEEL_BIT));
  34. for (i = wheel_bit - 1; i >= 0; i--) {
  35. *p++ = '0' + !!(n & (1 << i));
  36. }
  37. if (wheel != 0)
  38. *p++ = ':';
  39. }
  40. *p = 0;
  41. return buf;
  42. } /* fmt_() */
  43. #define fmt(ts) fmt_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + WHEEL_NUM + 1]){ 0 }), (ts), WHEEL_BIT, WHEEL_NUM)
  44. static inline char *bin64_(char *buf, uint64_t n, int wheel_bit) {
  45. char *p = buf;
  46. int i;
  47. for (i = 0; i < (1 << wheel_bit); i++) {
  48. *p++ = "01"[0x1 & (n >> (((1 << wheel_bit) - 1) - i))];
  49. }
  50. *p = 0;
  51. return buf;
  52. } /* bin64_() */
  53. #define bin64(ts) bin64_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + 1]){ 0 }), (ts), WHEEL_BIT)