1 #ifndef __TOOLS_LINUX_KERNEL_H
2 #define __TOOLS_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
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)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
scnprintf(char * buf,size_t size,const char * fmt,...)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 /*
98 * This looks more complex than it should be. But we need to
99 * get the type for the ~ right in round_down (it needs to be
100 * as wide as the result!), and we want to evaluate the macro
101 * arguments just once each.
102 */
103 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
104 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
105 #define round_down(x, y) ((x) & ~__round_mask(x, y))
106
107 #endif
108