xref: /kvmtool/include/kvm/util.h (revision ed036f03c501ad9f13edb85b078b147662d1f4fd)
1ac600533SPrasad Joshi #include <linux/stringify.h>
2ac600533SPrasad Joshi 
3f3150089SPekka Enberg #ifndef KVM__UTIL_H
4f3150089SPekka Enberg #define KVM__UTIL_H
5ad054a21SCyrill Gorcunov 
62a601aafSPekka Enberg #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
72a601aafSPekka Enberg 
8ad054a21SCyrill Gorcunov /*
9ad054a21SCyrill Gorcunov  * Some bits are stolen from perf tool :)
10ad054a21SCyrill Gorcunov  */
11ad054a21SCyrill Gorcunov 
12ad054a21SCyrill Gorcunov #include <unistd.h>
13ad054a21SCyrill Gorcunov #include <stdio.h>
14ad054a21SCyrill Gorcunov #include <stddef.h>
15ad054a21SCyrill Gorcunov #include <stdlib.h>
16ad054a21SCyrill Gorcunov #include <stdarg.h>
17ad054a21SCyrill Gorcunov #include <string.h>
18*ed036f03SCyrill Gorcunov #include <stdbool.h>
19ad054a21SCyrill Gorcunov #include <errno.h>
20ad054a21SCyrill Gorcunov #include <limits.h>
21ad054a21SCyrill Gorcunov #include <sys/param.h>
22ad054a21SCyrill Gorcunov #include <sys/types.h>
23ad054a21SCyrill Gorcunov 
24ad054a21SCyrill Gorcunov #ifdef __GNUC__
25ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__))
26ad054a21SCyrill Gorcunov #else
27ad054a21SCyrill Gorcunov #define NORETURN
28ad054a21SCyrill Gorcunov #ifndef __attribute__
29ad054a21SCyrill Gorcunov #define __attribute__(x)
30ad054a21SCyrill Gorcunov #endif
31ad054a21SCyrill Gorcunov #endif
32ad054a21SCyrill Gorcunov 
33*ed036f03SCyrill Gorcunov extern bool do_debug_print;
34*ed036f03SCyrill Gorcunov 
35ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
36ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
37ad054a21SCyrill Gorcunov extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
38ad054a21SCyrill Gorcunov extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
3907f9d0dbSCyrill Gorcunov extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
40ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
41ad054a21SCyrill Gorcunov 
42*ed036f03SCyrill Gorcunov #define debug(fmt, ...)							\
43*ed036f03SCyrill Gorcunov 	do {								\
44*ed036f03SCyrill Gorcunov 		if (do_debug_print)					\
45*ed036f03SCyrill Gorcunov 			info("(%s) %s:%d: " fmt, __FILE__,		\
46*ed036f03SCyrill Gorcunov 				__func__, __LINE__, ##__VA_ARGS__);	\
47*ed036f03SCyrill Gorcunov 	} while (0)
48*ed036f03SCyrill Gorcunov 
49b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
50b3594ec7SCyrill Gorcunov 
51ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
52ad054a21SCyrill Gorcunov do {								\
53ad054a21SCyrill Gorcunov 	if (cnd)						\
54ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
55ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
56ad054a21SCyrill Gorcunov } while (0)
57ad054a21SCyrill Gorcunov 
580b322d96SCyrill Gorcunov extern size_t strlcat(char *dest, const char *src, size_t count);
590b322d96SCyrill Gorcunov 
6098ee79f4SPrasad Joshi /* some inline functions */
6198ee79f4SPrasad Joshi 
6298ee79f4SPrasad Joshi static inline const char *skip_prefix(const char *str, const char *prefix)
6398ee79f4SPrasad Joshi {
6498ee79f4SPrasad Joshi 	size_t len = strlen(prefix);
6598ee79f4SPrasad Joshi 	return strncmp(str, prefix, len) ? NULL : str + len;
6698ee79f4SPrasad Joshi }
6798ee79f4SPrasad Joshi 
68f3150089SPekka Enberg #endif /* KVM__UTIL_H */
69