xref: /kvmtool/include/kvm/util.h (revision ac600533164e83ed17c132dc1c30668aef02e7f3)
1*ac600533SPrasad Joshi #include <linux/stringify.h>
2*ac600533SPrasad 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>
18ad054a21SCyrill Gorcunov #include <errno.h>
19ad054a21SCyrill Gorcunov #include <limits.h>
20ad054a21SCyrill Gorcunov #include <sys/param.h>
21ad054a21SCyrill Gorcunov #include <sys/types.h>
22ad054a21SCyrill Gorcunov 
23ad054a21SCyrill Gorcunov #ifdef __GNUC__
24ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__))
25ad054a21SCyrill Gorcunov #else
26ad054a21SCyrill Gorcunov #define NORETURN
27ad054a21SCyrill Gorcunov #ifndef __attribute__
28ad054a21SCyrill Gorcunov #define __attribute__(x)
29ad054a21SCyrill Gorcunov #endif
30ad054a21SCyrill Gorcunov #endif
31ad054a21SCyrill Gorcunov 
32ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
33ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
34ad054a21SCyrill Gorcunov extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
35ad054a21SCyrill Gorcunov extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
3607f9d0dbSCyrill Gorcunov extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
37ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
38ad054a21SCyrill Gorcunov 
39b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
40b3594ec7SCyrill Gorcunov 
41ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
42ad054a21SCyrill Gorcunov do {								\
43ad054a21SCyrill Gorcunov 	if (cnd)						\
44ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
45ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
46ad054a21SCyrill Gorcunov } while (0)
47ad054a21SCyrill Gorcunov 
480b322d96SCyrill Gorcunov extern size_t strlcat(char *dest, const char *src, size_t count);
490b322d96SCyrill Gorcunov 
5098ee79f4SPrasad Joshi /* some inline functions */
5198ee79f4SPrasad Joshi 
5298ee79f4SPrasad Joshi static inline const char *skip_prefix(const char *str, const char *prefix)
5398ee79f4SPrasad Joshi {
5498ee79f4SPrasad Joshi 	size_t len = strlen(prefix);
5598ee79f4SPrasad Joshi 	return strncmp(str, prefix, len) ? NULL : str + len;
5698ee79f4SPrasad Joshi }
5798ee79f4SPrasad Joshi 
58f3150089SPekka Enberg #endif /* KVM__UTIL_H */
59