1 #ifndef PERF_LINUX_KERNEL_H_ 2 #define PERF_LINUX_KERNEL_H_ 3 4 #include <stdarg.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <assert.h> 8 9 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 10 11 /* ANDROID_CHANGE_BEGIN */ 12 #if defined(__BIONIC__) || defined(__APPLE__) 13 /* Bionic defines ALIGN in sys/param.h */ 14 #define KERNEL_ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 15 #else 16 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 17 #endif 18 /* ANDROID_CHANGE_END */ 19 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 20 21 #ifndef offsetof 22 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 23 #endif 24 25 #ifndef container_of 26 /** 27 * container_of - cast a member of a structure out to the containing structure 28 * @ptr: the pointer to the member. 29 * @type: the type of the container struct this is embedded in. 30 * @member: the name of the member within the struct. 31 * 32 */ 33 #define container_of(ptr, type, member) ({ \ 34 const typeof(((type *)0)->member) * __mptr = (ptr); \ 35 (type *)((char *)__mptr - offsetof(type, member)); }) 36 #endif 37 38 /* ANDROID_CHANGE_BEGIN */ 39 #ifndef __BIONIC__ 40 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 41 #endif 42 /* ANDROID_CHANGE_END */ 43 44 #ifndef max 45 #define max(x, y) ({ \ 46 typeof(x) _max1 = (x); \ 47 typeof(y) _max2 = (y); \ 48 (void) (&_max1 == &_max2); \ 49 _max1 > _max2 ? _max1 : _max2; }) 50 #endif 51 52 #ifndef min 53 #define min(x, y) ({ \ 54 typeof(x) _min1 = (x); \ 55 typeof(y) _min2 = (y); \ 56 (void) (&_min1 == &_min2); \ 57 _min1 < _min2 ? _min1 : _min2; }) 58 #endif 59 60 #ifndef BUG_ON 61 #define BUG_ON(cond) assert(!(cond)) 62 #endif 63 64 /* 65 * Both need more care to handle endianness 66 * (Don't use bitmap_copy_le() for now) 67 */ 68 #define cpu_to_le64(x) (x) 69 #define cpu_to_le32(x) (x) 70 71 static inline int 72 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 73 { 74 int i; 75 ssize_t ssize = size; 76 77 i = vsnprintf(buf, size, fmt, args); 78 79 return (i >= ssize) ? (ssize - 1) : i; 80 } 81 82 static inline int scnprintf(char * buf, size_t size, const char * fmt, ...) 83 { 84 va_list args; 85 ssize_t ssize = size; 86 int i; 87 88 va_start(args, fmt); 89 i = vsnprintf(buf, size, fmt, args); 90 va_end(args); 91 92 return (i >= ssize) ? (ssize - 1) : i; 93 } 94 95 static inline unsigned long 96 simple_strtoul(const char *nptr, char **endptr, int base) 97 { 98 return strtoul(nptr, endptr, base); 99 } 100 101 int eprintf(int level, 102 const char *fmt, ...) __attribute__((format(printf, 2, 3))); 103 104 #ifndef pr_fmt 105 #define pr_fmt(fmt) fmt 106 #endif 107 108 #define pr_err(fmt, ...) \ 109 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__) 110 #define pr_warning(fmt, ...) \ 111 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__) 112 #define pr_info(fmt, ...) \ 113 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__) 114 #define pr_debug(fmt, ...) \ 115 eprintf(1, pr_fmt(fmt), ##__VA_ARGS__) 116 #define pr_debugN(n, fmt, ...) \ 117 eprintf(n, pr_fmt(fmt), ##__VA_ARGS__) 118 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__) 119 #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__) 120 #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__) 121 122 #endif 123