Home | History | Annotate | Download | only in linux
      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 #define PERF_ALIGN(x, a)	__PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
     12 #define __PERF_ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
     13 
     14 #ifndef offsetof
     15 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
     16 #endif
     17 
     18 #ifndef container_of
     19 /**
     20  * container_of - cast a member of a structure out to the containing structure
     21  * @ptr:	the pointer to the member.
     22  * @type:	the type of the container struct this is embedded in.
     23  * @member:	the name of the member within the struct.
     24  *
     25  */
     26 #define container_of(ptr, type, member) ({			\
     27 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
     28 	(type *)((char *)__mptr - offsetof(type, member)); })
     29 #endif
     30 
     31 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
     32 
     33 #ifndef max
     34 #define max(x, y) ({				\
     35 	typeof(x) _max1 = (x);			\
     36 	typeof(y) _max2 = (y);			\
     37 	(void) (&_max1 == &_max2);		\
     38 	_max1 > _max2 ? _max1 : _max2; })
     39 #endif
     40 
     41 #ifndef min
     42 #define min(x, y) ({				\
     43 	typeof(x) _min1 = (x);			\
     44 	typeof(y) _min2 = (y);			\
     45 	(void) (&_min1 == &_min2);		\
     46 	_min1 < _min2 ? _min1 : _min2; })
     47 #endif
     48 
     49 #ifndef roundup
     50 #define roundup(x, y) (                                \
     51 {                                                      \
     52 	const typeof(y) __y = y;		       \
     53 	(((x) + (__y - 1)) / __y) * __y;	       \
     54 }                                                      \
     55 )
     56 #endif
     57 
     58 #ifndef BUG_ON
     59 #ifdef NDEBUG
     60 #define BUG_ON(cond) do { if (cond) {} } while (0)
     61 #else
     62 #define BUG_ON(cond) assert(!(cond))
     63 #endif
     64 #endif
     65 
     66 /*
     67  * Both need more care to handle endianness
     68  * (Don't use bitmap_copy_le() for now)
     69  */
     70 #define cpu_to_le64(x)	(x)
     71 #define cpu_to_le32(x)	(x)
     72 
     73 static inline int
     74 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
     75 {
     76 	int i;
     77 	ssize_t ssize = size;
     78 
     79 	i = vsnprintf(buf, size, fmt, args);
     80 
     81 	return (i >= ssize) ? (ssize - 1) : i;
     82 }
     83 
     84 static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
     85 {
     86 	va_list args;
     87 	ssize_t ssize = size;
     88 	int i;
     89 
     90 	va_start(args, fmt);
     91 	i = vsnprintf(buf, size, fmt, args);
     92 	va_end(args);
     93 
     94 	return (i >= ssize) ? (ssize - 1) : i;
     95 }
     96 
     97 static inline unsigned long
     98 simple_strtoul(const char *nptr, char **endptr, int base)
     99 {
    100 	return strtoul(nptr, endptr, base);
    101 }
    102 
    103 int eprintf(int level,
    104 	    const char *fmt, ...) __attribute__((format(printf, 2, 3)));
    105 
    106 #ifndef pr_fmt
    107 #define pr_fmt(fmt) fmt
    108 #endif
    109 
    110 #define pr_err(fmt, ...) \
    111 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
    112 #define pr_warning(fmt, ...) \
    113 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
    114 #define pr_info(fmt, ...) \
    115 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
    116 #define pr_debug(fmt, ...) \
    117 	eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
    118 #define pr_debugN(n, fmt, ...) \
    119 	eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
    120 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
    121 #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
    122 #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
    123 
    124 /*
    125  * This looks more complex than it should be. But we need to
    126  * get the type for the ~ right in round_down (it needs to be
    127  * as wide as the result!), and we want to evaluate the macro
    128  * arguments just once each.
    129  */
    130 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
    131 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
    132 #define round_down(x, y) ((x) & ~__round_mask(x, y))
    133 
    134 #endif
    135