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 #define PROT_RW (PROT_READ|PROT_WRITE) 36 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) 37 38 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 39 extern void die_perror(const char *s) NORETURN; 40 extern int pr_error(const char *err, ...) __attribute__((format (printf, 1, 2))); 41 extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 42 extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2))); 43 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 44 45 #define pr_debug(fmt, ...) \ 46 do { \ 47 if (do_debug_print) \ 48 pr_info("(%s) %s:%d: " fmt, __FILE__, \ 49 __func__, __LINE__, ##__VA_ARGS__); \ 50 } while (0) 51 52 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 53 54 #define DIE_IF(cnd) \ 55 do { \ 56 if (cnd) \ 57 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ 58 __stringify(cnd) "\n"); \ 59 } while (0) 60 61 extern size_t strlcat(char *dest, const char *src, size_t count); 62 63 /* some inline functions */ 64 65 static inline const char *skip_prefix(const char *str, const char *prefix) 66 { 67 size_t len = strlen(prefix); 68 return strncmp(str, prefix, len) ? NULL : str + len; 69 } 70 71 #define MSECS_TO_USECS(s) ((s) * 1000) 72 73 /* Millisecond sleep */ 74 static inline void msleep(unsigned int msecs) 75 { 76 usleep(MSECS_TO_USECS(msecs)); 77 } 78 #endif /* KVM__UTIL_H */ 79