xref: /aosp_15_r20/external/libbpf/include/linux/kernel.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 #ifndef __LINUX_KERNEL_H
4 #define __LINUX_KERNEL_H
5 
6 #include <linux/compiler.h>
7 
8 #ifndef offsetof
9 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
10 #endif
11 
12 #ifndef container_of
13 #define container_of(ptr, type, member) ({			\
14 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
15 	(type *)((char *)__mptr - offsetof(type, member)); })
16 #endif
17 
18 #ifndef max
19 #define max(x, y) ({				\
20 	typeof(x) _max1 = (x);			\
21 	typeof(y) _max2 = (y);			\
22 	(void) (&_max1 == &_max2);		\
23 	_max1 > _max2 ? _max1 : _max2; })
24 #endif
25 
26 #ifndef min
27 #define min(x, y) ({				\
28 	typeof(x) _min1 = (x);			\
29 	typeof(y) _min2 = (y);			\
30 	(void) (&_min1 == &_min2);		\
31 	_min1 < _min2 ? _min1 : _min2; })
32 #endif
33 
34 #ifndef roundup
35 #define roundup(x, y) (				\
36 {						\
37 	const typeof(y) __y = y;		\
38 	(((x) + (__y - 1)) / __y) * __y;	\
39 }						\
40 )
41 #endif
42 
43 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
45 
46 #endif
47