bug fix
[c11tester.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 <unistd.h>
10 #include "config.h"
11 #include "printf.h"
12
13 extern int model_out;
14 extern int switch_alloc;
15
16 #define model_print(fmt, ...) do { \
17                 switch_alloc = 1;              \
18                 char mprintbuf[256];                                                \
19                 int printbuflen=snprintf_(mprintbuf, 256, fmt, ## __VA_ARGS__);     \
20                 int lenleft = printbuflen < 256 ? printbuflen : 256;                   \
21                 int totalwritten = 0; \
22                 while(lenleft) {                                                    \
23                         int byteswritten=write(model_out, &mprintbuf[totalwritten], lenleft); \
24                         lenleft-=byteswritten;                                            \
25                         totalwritten+=byteswritten;                                       \
26                 }                                                                   \
27                 switch_alloc = 0;                                                   \
28 } while (0)
29
30 #ifdef CONFIG_DEBUG
31 #define DEBUG(fmt, ...) do { model_print("*** %15s:%-4d %25s() *** " fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__); } while (0)
32 #define DBG() DEBUG("\n")
33 #define DBG_ENABLED() (1)
34 #else
35 #define DEBUG(fmt, ...)
36 #define DBG()
37 #define DBG_ENABLED() (0)
38 #endif
39
40 void assert_hook(void);
41
42 #ifdef CONFIG_ASSERT
43 #define ASSERT(expr) \
44         do { \
45                 if (!(expr)) { \
46                         fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \
47                         /* print_trace(); // Trace printing may cause dynamic memory allocation */ \
48                         assert_hook();                           \
49                         _Exit(EXIT_FAILURE); \
50                 } \
51         } while (0)
52 #else
53 #define ASSERT(expr) \
54         do { } while (0)
55 #endif  /* CONFIG_ASSERT */
56
57 #define error_msg(...) fprintf(stderr, "Error: " __VA_ARGS__)
58
59 void print_trace(void);
60 #endif  /* __COMMON_H__ */