xref: /kvmtool/include/kvm/util.h (revision 2a601aafee4175081688a2347764d0e2bfa1230e)
1 #ifndef UTIL_H_
2 #define UTIL_H_
3 
4 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
5 
6 /*
7  * Some bits are stolen from perf tool :)
8  */
9 
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <stddef.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <sys/param.h>
19 #include <sys/types.h>
20 
21 #ifdef __GNUC__
22 #define NORETURN __attribute__((__noreturn__))
23 #else
24 #define NORETURN
25 #ifndef __attribute__
26 #define __attribute__(x)
27 #endif
28 #endif
29 
30 #define __stringify_1(x)	#x
31 #define __stringify(x)		__stringify_1(x)
32 
33 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
34 extern void die_perror(const char *s) NORETURN;
35 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
36 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
37 extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
38 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
39 
40 #define DIE_IF(cnd)						\
41 do {								\
42 	if (cnd)						\
43 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
44 		__stringify(cnd) "\n");				\
45 } while (0)
46 
47 #endif /* UTIL_H_ */
48