xref: /kvmtool/include/kvm/util.h (revision 37c34ca8e124b0aa7ef69e58ae28ffac8d56e557)
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 <stdbool.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <sys/param.h>
22 #include <sys/types.h>
23 
24 #ifdef __GNUC__
25 #define NORETURN __attribute__((__noreturn__))
26 #else
27 #define NORETURN
28 #ifndef __attribute__
29 #define __attribute__(x)
30 #endif
31 #endif
32 
33 extern bool do_debug_print;
34 
35 #define PROT_RW (PROT_READ|PROT_WRITE)
36 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
37 
38 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
39 extern void die_perror(const char *s) NORETURN;
40 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
41 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
42 extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
43 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
44 
45 #define debug(fmt, ...)							\
46 	do {								\
47 		if (do_debug_print)					\
48 			info("(%s) %s:%d: " fmt, __FILE__,		\
49 				__func__, __LINE__, ##__VA_ARGS__);	\
50 	} while (0)
51 
52 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
53 
54 #define DIE_IF(cnd)						\
55 do {								\
56 	if (cnd)						\
57 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
58 		__stringify(cnd) "\n");				\
59 } while (0)
60 
61 extern size_t strlcat(char *dest, const char *src, size_t count);
62 
63 /* some inline functions */
64 
65 static inline const char *skip_prefix(const char *str, const char *prefix)
66 {
67 	size_t len = strlen(prefix);
68 	return strncmp(str, prefix, len) ? NULL : str + len;
69 }
70 
71 #endif /* KVM__UTIL_H */
72