xref: /kvmtool/include/kvm/util.h (revision 0b322d9635196147295807a190d07fde072cc70c)
1 #ifndef KVM__UTIL_H
2 #define KVM__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 extern size_t strlcat(char *dest, const char *src, size_t count);
48 
49 #endif /* KVM__UTIL_H */
50