1ac600533SPrasad Joshi #include <linux/stringify.h> 2ac600533SPrasad Joshi 3f3150089SPekka Enberg #ifndef KVM__UTIL_H 4f3150089SPekka Enberg #define KVM__UTIL_H 5ad054a21SCyrill Gorcunov 62a601aafSPekka Enberg #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 72a601aafSPekka Enberg 8ad054a21SCyrill Gorcunov /* 9ad054a21SCyrill Gorcunov * Some bits are stolen from perf tool :) 10ad054a21SCyrill Gorcunov */ 11ad054a21SCyrill Gorcunov 12ad054a21SCyrill Gorcunov #include <unistd.h> 13ad054a21SCyrill Gorcunov #include <stdio.h> 14ad054a21SCyrill Gorcunov #include <stddef.h> 15ad054a21SCyrill Gorcunov #include <stdlib.h> 16ad054a21SCyrill Gorcunov #include <stdarg.h> 17ad054a21SCyrill Gorcunov #include <string.h> 18ed036f03SCyrill Gorcunov #include <stdbool.h> 19ad054a21SCyrill Gorcunov #include <errno.h> 20ad054a21SCyrill Gorcunov #include <limits.h> 21ad054a21SCyrill Gorcunov #include <sys/param.h> 22ad054a21SCyrill Gorcunov #include <sys/types.h> 23ad054a21SCyrill Gorcunov 24ad054a21SCyrill Gorcunov #ifdef __GNUC__ 25ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__)) 26ad054a21SCyrill Gorcunov #else 27ad054a21SCyrill Gorcunov #define NORETURN 28ad054a21SCyrill Gorcunov #ifndef __attribute__ 29ad054a21SCyrill Gorcunov #define __attribute__(x) 30ad054a21SCyrill Gorcunov #endif 31ad054a21SCyrill Gorcunov #endif 32ad054a21SCyrill Gorcunov 33ed036f03SCyrill Gorcunov extern bool do_debug_print; 34ed036f03SCyrill Gorcunov 3537c34ca8SSasha Levin #define PROT_RW (PROT_READ|PROT_WRITE) 3637c34ca8SSasha Levin #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) 3737c34ca8SSasha Levin 38ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 39ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN; 404542f276SCyrill Gorcunov extern int pr_error(const char *err, ...) __attribute__((format (printf, 1, 2))); 414542f276SCyrill Gorcunov extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); 424542f276SCyrill Gorcunov extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2))); 43ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 44ad054a21SCyrill Gorcunov 454542f276SCyrill Gorcunov #define pr_debug(fmt, ...) \ 46ed036f03SCyrill Gorcunov do { \ 47ed036f03SCyrill Gorcunov if (do_debug_print) \ 484542f276SCyrill Gorcunov pr_info("(%s) %s:%d: " fmt, __FILE__, \ 49ed036f03SCyrill Gorcunov __func__, __LINE__, ##__VA_ARGS__); \ 50ed036f03SCyrill Gorcunov } while (0) 51ed036f03SCyrill Gorcunov 52b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 53b3594ec7SCyrill Gorcunov 54ad054a21SCyrill Gorcunov #define DIE_IF(cnd) \ 55ad054a21SCyrill Gorcunov do { \ 56ad054a21SCyrill Gorcunov if (cnd) \ 57ad054a21SCyrill Gorcunov die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ 58ad054a21SCyrill Gorcunov __stringify(cnd) "\n"); \ 59ad054a21SCyrill Gorcunov } while (0) 60ad054a21SCyrill Gorcunov 610b322d96SCyrill Gorcunov extern size_t strlcat(char *dest, const char *src, size_t count); 620b322d96SCyrill Gorcunov 6398ee79f4SPrasad Joshi /* some inline functions */ 6498ee79f4SPrasad Joshi 6598ee79f4SPrasad Joshi static inline const char *skip_prefix(const char *str, const char *prefix) 6698ee79f4SPrasad Joshi { 6798ee79f4SPrasad Joshi size_t len = strlen(prefix); 6898ee79f4SPrasad Joshi return strncmp(str, prefix, len) ? NULL : str + len; 6998ee79f4SPrasad Joshi } 7098ee79f4SPrasad Joshi 71*aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000) 72*aa400b00SPrasad Joshi 73*aa400b00SPrasad Joshi /* Millisecond sleep */ 74*aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs) 75*aa400b00SPrasad Joshi { 76*aa400b00SPrasad Joshi usleep(MSECS_TO_USECS(msecs)); 77*aa400b00SPrasad Joshi } 78f3150089SPekka Enberg #endif /* KVM__UTIL_H */ 79