1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TOOLS_LINUX_KERNEL_H
3 #define __TOOLS_LINUX_KERNEL_H
8 #include <linux/build_bug.h>
9 #include <linux/compiler.h>
10 #include <linux/math.h>
15 #define UINT_MAX (~0U)
18 #define _RET_IP_ ((unsigned long)__builtin_return_address(0))
20 #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
21 #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
24 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
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.
35 #define container_of(ptr, type, member) ({ \
36 const typeof(((type *)0)->member) * __mptr = (ptr); \
37 (type *)((char *)__mptr - offsetof(type, member)); })
41 #define max(x, y) ({ \
42 typeof(x) _max1 = (x); \
43 typeof(y) _max2 = (y); \
44 (void) (&_max1 == &_max2); \
45 _max1 > _max2 ? _max1 : _max2; })
49 #define min(x, y) ({ \
50 typeof(x) _min1 = (x); \
51 typeof(y) _min2 = (y); \
52 (void) (&_min1 == &_min2); \
53 _min1 < _min2 ? _min1 : _min2; })
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)
62 #define BUG_ON(cond) do { if (cond) {} } while (0)
64 #define BUG_ON(cond) assert(!(cond))
67 #define BUG() BUG_ON(1)
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
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
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, ...);
102 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
105 #define current_gfp_context(k) 0
106 #define synchronize_rcu()