Add Apache License version 2.0.
[pbc.git] / include / pbc_utils.h
blobf3b75a3fa802fee54580e81ff10d8a4c590cf21b
1 #ifndef __PBC_UTILS_H__
2 #define __PBC_UTILS_H__
4 #ifdef PBC_DEBUG
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__))
12 /*@manual debug
13 Macro: if elements `a` and `b` are from different fields then exit.
15 #define PBC_ASSERT_MATCH2(a, b) \
16 (pbc_assert_match2(a, b, __func__))
18 /*@manual debug
19 Macro: if elements `a`, `b` and `c` are from different fields then exit.
21 #define PBC_ASSERT_MATCH3(a, b, c) \
22 (pbc_assert_match3(a, b, c, __func__))
24 #else
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))
30 #endif
32 // die, warn and info based on Git code.
34 /*@manual log
35 By default error messages are printed to standard error.
36 Call `pbc_set_msg_to_stderr(0)` to suppress messages.
38 int pbc_set_msg_to_stderr(int i);
40 /*@manual log
41 Reports error message and exits with code 128.
43 void pbc_die(const char *err, ...)
44 __attribute__((__noreturn__))
45 __attribute__((format (printf, 1, 2)));
47 /*@manual log
48 Reports informational message.
50 void pbc_info(const char *err, ...)
51 __attribute__((format (printf, 1, 2)));
53 /*@manual log
54 Reports warning message.
56 void pbc_warn(const char *err, ...)
57 __attribute__((format (printf, 1, 2)));
59 /*@manual log
60 Reports error message.
62 void pbc_error(const char *err, ...)
63 __attribute__((format (printf, 1, 2)));
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
74 // Not anymore: gcc no longer likes it -blynn
75 #define UNUSED_VAR(a) do { /* nothing */ } while (&a == 0)
76 #endif
77 #endif
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
82 static inline void *int_to_voidp(intptr_t i) {
83 return (void *) i;
86 // Compatibility with x64 MPIR in MSVC
87 #if defined(_MSC_VER) && defined(_WIN64)
88 typedef unsigned long long int pbc_mpui;
89 typedef signed long long int pbc_mpsi;
90 #else
91 typedef unsigned long int pbc_mpui;
92 typedef signed long int pbc_mpsi;
93 #endif
95 #endif //__PBC_UTILS_H__