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