malloc: modify compiler warning workarounds
[cdsspec-compiler.git] / common.h
1 /** @file common.h
2  *  @brief General purpose macros.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #include <stdio.h>
9 #include "config.h"
10
11 extern int model_out;
12
13 #define model_print(fmt, ...) do { dprintf(model_out, fmt, ##__VA_ARGS__); } while (0)
14
15 #ifdef CONFIG_DEBUG
16 #define DEBUG(fmt, ...) do { model_print("*** %15s:%-4d %25s() *** " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); } while (0)
17 #define DBG() DEBUG("\n")
18 #define DBG_ENABLED() (1)
19 #else
20 #define DEBUG(fmt, ...)
21 #define DBG()
22 #define DBG_ENABLED() (0)
23 #endif
24
25 void assert_hook(void);
26
27 #ifdef CONFIG_ASSERT
28 #define ASSERT(expr) \
29 do { \
30         if (!(expr)) { \
31                 fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \
32                 /* print_trace(); // Trace printing may cause dynamic memory allocation */ \
33                 assert_hook();                           \
34                 exit(EXIT_FAILURE); \
35         } \
36 } while (0)
37 #else
38 #define ASSERT(expr) \
39         do { } while (0)
40 #endif /* CONFIG_ASSERT */
41
42 #define error_msg(...) fprintf(stderr, "Error: " __VA_ARGS__)
43
44 void print_trace(void);
45 #endif /* __COMMON_H__ */