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 <assert.h> 13 #include <unistd.h> 14 #include <stdio.h> 15 #include <stddef.h> 16 #include <stdlib.h> 17 #include <stdarg.h> 18 #include <string.h> 19 #include <stdbool.h> 20 #include <errno.h> 21 #include <limits.h> 22 #include <sys/param.h> 23 #include <sys/types.h> 24 #include <linux/types.h> 25 26 #ifdef __GNUC__ 27 #define NORETURN __attribute__((__noreturn__)) 28 #else 29 #define NORETURN 30 #ifndef __attribute__ 31 #define __attribute__(x) 32 #endif 33 #endif 34 35 extern bool do_debug_print; 36 37 #define PROT_RW (PROT_READ|PROT_WRITE) 38 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) 39 40 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 41 extern void die_perror(const char *s) NORETURN; 42 extern int pr_error(const char *err, ...) __attribute__((format (printf, 1, 2))); 43 extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 44 extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2))); 45 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 46 47 #define pr_debug(fmt, ...) \ 48 do { \ 49 if (do_debug_print) \ 50 pr_info("(%s) %s:%d: " fmt, __FILE__, \ 51 __func__, __LINE__, ##__VA_ARGS__); \ 52 } while (0) 53 54 # 55 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 56 #define BUG_ON(condition) assert(!(condition)) 57 58 #define DIE_IF(cnd) \ 59 do { \ 60 if (cnd) \ 61 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ 62 __stringify(cnd) "\n"); \ 63 } while (0) 64 65 #define MSECS_TO_USECS(s) ((s) * 1000) 66 67 /* Millisecond sleep */ 68 static inline void msleep(unsigned int msecs) 69 { 70 usleep(MSECS_TO_USECS(msecs)); 71 } 72 73 void *mmap_hugetlbfs(const char *htlbfs_path, u64 size); 74 75 #endif /* KVM__UTIL_H */ 76