1 #ifndef __PBC_UTILS_H__
2 #define __PBC_UTILS_H__
3 
4 #ifdef PBC_DEBUG
5 
6 /*@manual debug
7 Macro: if `expr` evaluates to 0, print `msg` and exit.
8 */
9 #define PBC_ASSERT(expr, msg) \
10     (pbc_assert(expr, msg, __func__))
11 
12 /*@manual debug
13 Macro: if elements `a` and `b` are from different fields then exit.
14 */
15 #define PBC_ASSERT_MATCH2(a, b) \
16     (pbc_assert_match2(a, b, __func__))
17 
18 /*@manual debug
19 Macro: if elements `a`, `b` and `c` are from different fields then exit.
20 */
21 #define PBC_ASSERT_MATCH3(a, b, c) \
22     (pbc_assert_match3(a, b, c, __func__))
23 
24 #else
25 
26 #define PBC_ASSERT(expr, msg) ((void) (0))
27 #define PBC_ASSERT_MATCH2(a, b) ((void) (0))
28 #define PBC_ASSERT_MATCH3(a, b, c) ((void) (0))
29 
30 #endif
31 
32 // die, warn and info based on Git code.
33 
34 /*@manual log
35 By default error messages are printed to standard error.
36 Call `pbc_set_msg_to_stderr(0)` to suppress messages.
37 */
38 int pbc_set_msg_to_stderr(int i);
39 
40 /*@manual log
41 Reports error message and exits with code 128.
42 */
43 void pbc_die(const char *err, ...)
44     __attribute__((__noreturn__))
45     __attribute__((format (printf, 1, 2)));
46 
47 /*@manual log
48 Reports informational message.
49 */
50 void pbc_info(const char *err, ...)
51     __attribute__((format (printf, 1, 2)));
52 
53 /*@manual log
54 Reports warning message.
55 */
56 void pbc_warn(const char *err, ...)
57     __attribute__((format (printf, 1, 2)));
58 
59 /*@manual log
60 Reports error message.
61 */
62 void pbc_error(const char *err, ...)
63     __attribute__((format (printf, 1, 2)));
64 
65 #ifndef UNUSED_VAR
66 #if defined(__GNUC__)
67 // We could use __attribute__((unused)) instead.
68 #define UNUSED_VAR(a) (void) a
69 #else
70 // From the ACE project: http://www.cs.wustl.edu/~schmidt/ACE.html
71 // silences warnings, and generates no code for many compilers
72 // See ACE_wrappers/ace/ace/config-macros.h:391
73 //
74 // Not anymore: gcc no longer likes it -blynn
75 #define UNUSED_VAR(a) do { /* nothing */ } while (&a == 0)
76 #endif
77 #endif
78 
79 // For storing small integers in void *
80 // C99 standard introduced the intptr_t and uintptr_t types,
81 // guaranteed to be able to hold pointers
int_to_voidp(intptr_t i)82 static inline void *int_to_voidp(intptr_t i) {
83   return (void *) i;
84 }
85 
86 #endif //__PBC_UTILS_H__
87