1
2#ifndef __TOOLS_LINUX_KERNEL_H
3#define __TOOLS_LINUX_KERNEL_H
4
5#include <stdarg.h>
6#include <stddef.h>
7#include <assert.h>
8#include <linux/build_bug.h>
9#include <linux/compiler.h>
10#include <endian.h>
11#include <byteswap.h>
12
13#ifndef UINT_MAX
14#define UINT_MAX (~0U)
15#endif
16
17#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
18
19#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
20#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
21
22#ifndef offsetof
23#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
24#endif
25
26#ifndef container_of
27
28
29
30
31
32
33
34#define container_of(ptr, type, member) ({ \
35 const typeof(((type *)0)->member) * __mptr = (ptr); \
36 (type *)((char *)__mptr - offsetof(type, member)); })
37#endif
38
39#ifndef max
40#define max(x, y) ({ \
41 typeof(x) _max1 = (x); \
42 typeof(y) _max2 = (y); \
43 (void) (&_max1 == &_max2); \
44 _max1 > _max2 ? _max1 : _max2; })
45#endif
46
47#ifndef min
48#define min(x, y) ({ \
49 typeof(x) _min1 = (x); \
50 typeof(y) _min2 = (y); \
51 (void) (&_min1 == &_min2); \
52 _min1 < _min2 ? _min1 : _min2; })
53#endif
54
55#ifndef roundup
56#define roundup(x, y) ( \
57{ \
58 const typeof(y) __y = y; \
59 (((x) + (__y - 1)) / __y) * __y; \
60} \
61)
62#endif
63
64#ifndef BUG_ON
65#ifdef NDEBUG
66#define BUG_ON(cond) do { if (cond) {} } while (0)
67#else
68#define BUG_ON(cond) assert(!(cond))
69#endif
70#endif
71#define BUG() BUG_ON(1)
72
73#if __BYTE_ORDER == __BIG_ENDIAN
74#define cpu_to_le16 bswap_16
75#define cpu_to_le32 bswap_32
76#define cpu_to_le64 bswap_64
77#define le16_to_cpu bswap_16
78#define le32_to_cpu bswap_32
79#define le64_to_cpu bswap_64
80#define cpu_to_be16
81#define cpu_to_be32
82#define cpu_to_be64
83#define be16_to_cpu
84#define be32_to_cpu
85#define be64_to_cpu
86#else
87#define cpu_to_le16
88#define cpu_to_le32
89#define cpu_to_le64
90#define le16_to_cpu
91#define le32_to_cpu
92#define le64_to_cpu
93#define cpu_to_be16 bswap_16
94#define cpu_to_be32 bswap_32
95#define cpu_to_be64 bswap_64
96#define be16_to_cpu bswap_16
97#define be32_to_cpu bswap_32
98#define be64_to_cpu bswap_64
99#endif
100
101int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
102int scnprintf(char * buf, size_t size, const char * fmt, ...);
103int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
104
105#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
106
107
108
109
110
111
112
113#define __round_mask(x, y) ((__typeof__(x))((y)-1))
114#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
115#define round_down(x, y) ((x) & ~__round_mask(x, y))
116
117#define current_gfp_context(k) 0
118#define synchronize_rcu()
119
120#endif
121