xref: /kvmtool/include/kvm/util.h (revision 0b322d9635196147295807a190d07fde072cc70c)
1f3150089SPekka Enberg #ifndef KVM__UTIL_H
2f3150089SPekka Enberg #define KVM__UTIL_H
3ad054a21SCyrill Gorcunov 
42a601aafSPekka Enberg #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
52a601aafSPekka Enberg 
6ad054a21SCyrill Gorcunov /*
7ad054a21SCyrill Gorcunov  * Some bits are stolen from perf tool :)
8ad054a21SCyrill Gorcunov  */
9ad054a21SCyrill Gorcunov 
10ad054a21SCyrill Gorcunov #include <unistd.h>
11ad054a21SCyrill Gorcunov #include <stdio.h>
12ad054a21SCyrill Gorcunov #include <stddef.h>
13ad054a21SCyrill Gorcunov #include <stdlib.h>
14ad054a21SCyrill Gorcunov #include <stdarg.h>
15ad054a21SCyrill Gorcunov #include <string.h>
16ad054a21SCyrill Gorcunov #include <errno.h>
17ad054a21SCyrill Gorcunov #include <limits.h>
18ad054a21SCyrill Gorcunov #include <sys/param.h>
19ad054a21SCyrill Gorcunov #include <sys/types.h>
20ad054a21SCyrill Gorcunov 
21ad054a21SCyrill Gorcunov #ifdef __GNUC__
22ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__))
23ad054a21SCyrill Gorcunov #else
24ad054a21SCyrill Gorcunov #define NORETURN
25ad054a21SCyrill Gorcunov #ifndef __attribute__
26ad054a21SCyrill Gorcunov #define __attribute__(x)
27ad054a21SCyrill Gorcunov #endif
28ad054a21SCyrill Gorcunov #endif
29ad054a21SCyrill Gorcunov 
30ad054a21SCyrill Gorcunov #define __stringify_1(x)	#x
31ad054a21SCyrill Gorcunov #define __stringify(x)		__stringify_1(x)
32ad054a21SCyrill Gorcunov 
33ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
34ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
35ad054a21SCyrill Gorcunov extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
36ad054a21SCyrill Gorcunov extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
3707f9d0dbSCyrill Gorcunov extern void info(const char *err, ...) __attribute__((format (printf, 1, 2)));
38ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
39ad054a21SCyrill Gorcunov 
40ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
41ad054a21SCyrill Gorcunov do {								\
42ad054a21SCyrill Gorcunov 	if (cnd)						\
43ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
44ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
45ad054a21SCyrill Gorcunov } while (0)
46ad054a21SCyrill Gorcunov 
47*0b322d96SCyrill Gorcunov extern size_t strlcat(char *dest, const char *src, size_t count);
48*0b322d96SCyrill Gorcunov 
49f3150089SPekka Enberg #endif /* KVM__UTIL_H */
50