xref: /linux/tools/include/linux/kernel.h (revision 2da68a77)
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 <linux/math.h>
11 #include <endian.h>
12 #include <byteswap.h>
13 
14 #ifndef UINT_MAX
15 #define UINT_MAX	(~0U)
16 #endif
17 
18 #define _RET_IP_		((unsigned long)__builtin_return_address(0))
19 
20 #define PERF_ALIGN(x, a)	__PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
21 #define __PERF_ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
22 
23 #ifndef offsetof
24 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
25 #endif
26 
27 #ifndef container_of
28 /**
29  * container_of - cast a member of a structure out to the containing structure
30  * @ptr:	the pointer to the member.
31  * @type:	the type of the container struct this is embedded in.
32  * @member:	the name of the member within the struct.
33  *
34  */
35 #define container_of(ptr, type, member) ({			\
36 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
37 	(type *)((char *)__mptr - offsetof(type, member)); })
38 #endif
39 
40 #ifndef max
41 #define max(x, y) ({				\
42 	typeof(x) _max1 = (x);			\
43 	typeof(y) _max2 = (y);			\
44 	(void) (&_max1 == &_max2);		\
45 	_max1 > _max2 ? _max1 : _max2; })
46 #endif
47 
48 #ifndef min
49 #define min(x, y) ({				\
50 	typeof(x) _min1 = (x);			\
51 	typeof(y) _min2 = (y);			\
52 	(void) (&_min1 == &_min2);		\
53 	_min1 < _min2 ? _min1 : _min2; })
54 #endif
55 
56 #define max_t(type, x, y)	max((type)x, (type)y)
57 #define min_t(type, x, y)	min((type)x, (type)y)
58 #define clamp(val, lo, hi)	min((typeof(val))max(val, lo), hi)
59 
60 #ifndef BUG_ON
61 #ifdef NDEBUG
62 #define BUG_ON(cond) do { if (cond) {} } while (0)
63 #else
64 #define BUG_ON(cond) assert(!(cond))
65 #endif
66 #endif
67 #define BUG()	BUG_ON(1)
68 
69 #if __BYTE_ORDER == __BIG_ENDIAN
70 #define cpu_to_le16 bswap_16
71 #define cpu_to_le32 bswap_32
72 #define cpu_to_le64 bswap_64
73 #define le16_to_cpu bswap_16
74 #define le32_to_cpu bswap_32
75 #define le64_to_cpu bswap_64
76 #define cpu_to_be16
77 #define cpu_to_be32
78 #define cpu_to_be64
79 #define be16_to_cpu
80 #define be32_to_cpu
81 #define be64_to_cpu
82 #else
83 #define cpu_to_le16
84 #define cpu_to_le32
85 #define cpu_to_le64
86 #define le16_to_cpu
87 #define le32_to_cpu
88 #define le64_to_cpu
89 #define cpu_to_be16 bswap_16
90 #define cpu_to_be32 bswap_32
91 #define cpu_to_be64 bswap_64
92 #define be16_to_cpu bswap_16
93 #define be32_to_cpu bswap_32
94 #define be64_to_cpu bswap_64
95 #endif
96 
97 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
98 int scnprintf(char * buf, size_t size, const char * fmt, ...);
99 int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
100 
101 #ifndef ARRAY_SIZE
102 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
103 #endif
104 
105 #define current_gfp_context(k) 0
106 #define synchronize_rcu()
107 
108 #endif
109