• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
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 _RET_IP_		((unsigned long)__builtin_return_address(0))
18 
19 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
20 
21 #define PERF_ALIGN(x, a)	__PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
22 #define __PERF_ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
23 
24 #ifndef offsetof
25 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
26 #endif
27 
28 #ifndef container_of
29 /**
30  * container_of - cast a member of a structure out to the containing structure
31  * @ptr:	the pointer to the member.
32  * @type:	the type of the container struct this is embedded in.
33  * @member:	the name of the member within the struct.
34  *
35  */
36 #define container_of(ptr, type, member) ({			\
37 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
38 	(type *)((char *)__mptr - offsetof(type, member)); })
39 #endif
40 
41 #ifndef max
42 #define max(x, y) ({				\
43 	typeof(x) _max1 = (x);			\
44 	typeof(y) _max2 = (y);			\
45 	(void) (&_max1 == &_max2);		\
46 	_max1 > _max2 ? _max1 : _max2; })
47 #endif
48 
49 #ifndef min
50 #define min(x, y) ({				\
51 	typeof(x) _min1 = (x);			\
52 	typeof(y) _min2 = (y);			\
53 	(void) (&_min1 == &_min2);		\
54 	_min1 < _min2 ? _min1 : _min2; })
55 #endif
56 
57 #define max_t(type, x, y)	max((type)x, (type)y)
58 #define min_t(type, x, y)	min((type)x, (type)y)
59 #define clamp(val, lo, hi)	min((typeof(val))max(val, lo), hi)
60 
61 #ifndef roundup
62 #define roundup(x, y) (                                \
63 {                                                      \
64 	const typeof(y) __y = y;		       \
65 	(((x) + (__y - 1)) / __y) * __y;	       \
66 }                                                      \
67 )
68 #endif
69 
70 #ifndef BUG_ON
71 #ifdef NDEBUG
72 #define BUG_ON(cond) do { if (cond) {} } while (0)
73 #else
74 #define BUG_ON(cond) assert(!(cond))
75 #endif
76 #endif
77 #define BUG()	BUG_ON(1)
78 
79 #if __BYTE_ORDER == __BIG_ENDIAN
80 #define cpu_to_le16 bswap_16
81 #define cpu_to_le32 bswap_32
82 #define cpu_to_le64 bswap_64
83 #define le16_to_cpu bswap_16
84 #define le32_to_cpu bswap_32
85 #define le64_to_cpu bswap_64
86 #define cpu_to_be16
87 #define cpu_to_be32
88 #define cpu_to_be64
89 #define be16_to_cpu
90 #define be32_to_cpu
91 #define be64_to_cpu
92 #else
93 #define cpu_to_le16
94 #define cpu_to_le32
95 #define cpu_to_le64
96 #define le16_to_cpu
97 #define le32_to_cpu
98 #define le64_to_cpu
99 #define cpu_to_be16 bswap_16
100 #define cpu_to_be32 bswap_32
101 #define cpu_to_be64 bswap_64
102 #define be16_to_cpu bswap_16
103 #define be32_to_cpu bswap_32
104 #define be64_to_cpu bswap_64
105 #endif
106 
107 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
108 int scnprintf(char * buf, size_t size, const char * fmt, ...);
109 int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
110 
111 #ifndef ARRAY_SIZE
112 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
113 #endif
114 
115 /*
116  * This looks more complex than it should be. But we need to
117  * get the type for the ~ right in round_down (it needs to be
118  * as wide as the result!), and we want to evaluate the macro
119  * arguments just once each.
120  */
121 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
122 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
123 #define round_down(x, y) ((x) & ~__round_mask(x, y))
124 
125 #define current_gfp_context(k) 0
126 #define synchronize_rcu()
127 
128 #endif
129