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 24 #ifdef __GNUC__ 25 #define NORETURN __attribute__((__noreturn__)) 26 #else 27 #define NORETURN 28 #ifndef __attribute__ 29 #define __attribute__(x) 30 #endif 31 #endif 32 33 extern bool do_debug_print; 34 35 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 36 extern void die_perror(const char *s) NORETURN; 37 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); 38 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 39 extern void info(const char *err, ...) __attribute__((format (printf, 1, 2))); 40 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 41 42 #define debug(fmt, ...) \ 43 do { \ 44 if (do_debug_print) \ 45 info("(%s) %s:%d: " fmt, __FILE__, \ 46 __func__, __LINE__, ##__VA_ARGS__); \ 47 } while (0) 48 49 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 50 51 #define DIE_IF(cnd) \ 52 do { \ 53 if (cnd) \ 54 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ 55 __stringify(cnd) "\n"); \ 56 } while (0) 57 58 extern size_t strlcat(char *dest, const char *src, size_t count); 59 60 /* some inline functions */ 61 62 static inline const char *skip_prefix(const char *str, const char *prefix) 63 { 64 size_t len = strlen(prefix); 65 return strncmp(str, prefix, len) ? NULL : str + len; 66 } 67 68 #endif /* KVM__UTIL_H */ 69