1 #ifndef KVM__UTIL_H 2 #define KVM__UTIL_H 3 4 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 5 6 /* 7 * Some bits are stolen from perf tool :) 8 */ 9 10 #include <unistd.h> 11 #include <stdio.h> 12 #include <stddef.h> 13 #include <stdlib.h> 14 #include <stdarg.h> 15 #include <string.h> 16 #include <errno.h> 17 #include <limits.h> 18 #include <sys/param.h> 19 #include <sys/types.h> 20 21 #ifdef __GNUC__ 22 #define NORETURN __attribute__((__noreturn__)) 23 #else 24 #define NORETURN 25 #ifndef __attribute__ 26 #define __attribute__(x) 27 #endif 28 #endif 29 30 #define __stringify_1(x) #x 31 #define __stringify(x) __stringify_1(x) 32 33 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 34 extern void die_perror(const char *s) NORETURN; 35 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); 36 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 37 extern void info(const char *err, ...) __attribute__((format (printf, 1, 2))); 38 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 39 40 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 41 42 #define DIE_IF(cnd) \ 43 do { \ 44 if (cnd) \ 45 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ 46 __stringify(cnd) "\n"); \ 47 } while (0) 48 49 extern size_t strlcat(char *dest, const char *src, size_t count); 50 51 /* some inline functions */ 52 53 static inline const char *skip_prefix(const char *str, const char *prefix) 54 { 55 size_t len = strlen(prefix); 56 return strncmp(str, prefix, len) ? NULL : str + len; 57 } 58 59 #endif /* KVM__UTIL_H */ 60