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