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