xref: /kvmtool/include/kvm/util.h (revision 610612576a8a68477e7e8289fca6df50caf43cd6)
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 #include <linux/types.h>
24 
25 #ifdef __GNUC__
26 #define NORETURN __attribute__((__noreturn__))
27 #else
28 #define NORETURN
29 #ifndef __attribute__
30 #define __attribute__(x)
31 #endif
32 #endif
33 
34 extern bool do_debug_print;
35 
36 #define PROT_RW (PROT_READ|PROT_WRITE)
37 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
38 
39 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
40 extern void die_perror(const char *s) NORETURN;
41 extern int pr_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
42 extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
43 extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
44 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
45 
46 #define pr_debug(fmt, ...)						\
47 	do {								\
48 		if (do_debug_print)					\
49 			pr_info("(%s) %s:%d: " fmt, __FILE__,		\
50 				__func__, __LINE__, ##__VA_ARGS__);	\
51 	} while (0)
52 
53 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
54 
55 #define DIE_IF(cnd)						\
56 do {								\
57 	if (cnd)						\
58 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
59 		__stringify(cnd) "\n");				\
60 } while (0)
61 
62 #define MSECS_TO_USECS(s) ((s) * 1000)
63 
64 /* Millisecond sleep */
65 static inline void msleep(unsigned int msecs)
66 {
67 	usleep(MSECS_TO_USECS(msecs));
68 }
69 
70 void *mmap_hugetlbfs(const char *htlbfs_path, u64 size);
71 
72 #endif /* KVM__UTIL_H */
73