xref: /kvmtool/include/kvm/util.h (revision ac600533164e83ed17c132dc1c30668aef02e7f3)
1 #include <linux/stringify.h>
2 
3 #ifndef KVM__UTIL_H
4 #define KVM__UTIL_H
5 
6 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
7 
8 /*
9  * Some bits are stolen from perf tool :)
10  */
11 
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <sys/param.h>
21 #include <sys/types.h>
22 
23 #ifdef __GNUC__
24 #define NORETURN __attribute__((__noreturn__))
25 #else
26 #define NORETURN
27 #ifndef __attribute__
28 #define __attribute__(x)
29 #endif
30 #endif
31 
32 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
33 extern void die_perror(const char *s) NORETURN;
34 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
35 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
36 extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
37 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
38 
39 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
40 
41 #define DIE_IF(cnd)						\
42 do {								\
43 	if (cnd)						\
44 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
45 		__stringify(cnd) "\n");				\
46 } while (0)
47 
48 extern size_t strlcat(char *dest, const char *src, size_t count);
49 
50 /* some inline functions */
51 
52 static inline const char *skip_prefix(const char *str, const char *prefix)
53 {
54 	size_t len = strlen(prefix);
55 	return strncmp(str, prefix, len) ? NULL : str + len;
56 }
57 
58 #endif /* KVM__UTIL_H */
59