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 ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
12 #define __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 BUG_ON
50 #define BUG_ON(cond) assert(!(cond))
51 #endif
52 
53 /*
54  * Both need more care to handle endianness
55  * (Don't use bitmap_copy_le() for now)
56  */
57 #define cpu_to_le64(x)	(x)
58 #define cpu_to_le32(x)	(x)
59 
60 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)61 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
62 {
63 	int i;
64 	ssize_t ssize = size;
65 
66 	i = vsnprintf(buf, size, fmt, args);
67 
68 	return (i >= ssize) ? (ssize - 1) : i;
69 }
70 
scnprintf(char * buf,size_t size,const char * fmt,...)71 static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
72 {
73 	va_list args;
74 	ssize_t ssize = size;
75 	int i;
76 
77 	va_start(args, fmt);
78 	i = vsnprintf(buf, size, fmt, args);
79 	va_end(args);
80 
81 	return (i >= ssize) ? (ssize - 1) : i;
82 }
83 
84 static inline unsigned long
simple_strtoul(const char * nptr,char ** endptr,int base)85 simple_strtoul(const char *nptr, char **endptr, int base)
86 {
87 	return strtoul(nptr, endptr, base);
88 }
89 
90 int eprintf(int level,
91 	    const char *fmt, ...) __attribute__((format(printf, 2, 3)));
92 
93 #ifndef pr_fmt
94 #define pr_fmt(fmt) fmt
95 #endif
96 
97 #define pr_err(fmt, ...) \
98 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
99 #define pr_warning(fmt, ...) \
100 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
101 #define pr_info(fmt, ...) \
102 	eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
103 #define pr_debug(fmt, ...) \
104 	eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
105 #define pr_debugN(n, fmt, ...) \
106 	eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
107 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
108 #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
109 #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
110 
111 #endif
112