xref: /kvmtool/include/linux/kernel.h (revision 0febaae00bb6f8c5e694f87d6354fbcbe81e6653)
1 
2 #ifndef KVM__LINUX_KERNEL_H_
3 #define KVM__LINUX_KERNEL_H_
4 
5 #include "asm/kernel.h"
6 
7 #define __round_mask(x, y)	((__typeof__(x))((y)-1))
8 #define round_down(x, y)	((x) & ~__round_mask(x, y))
9 
10 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
11 
12 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
13 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
14 #define IS_ALIGNED(x, a)	(((x) & ((typeof(x))(a) - 1)) == 0)
15 
16 #ifndef offsetof
17 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
18 #endif
19 
20 #ifndef container_of
21 /**
22  * container_of - cast a member of a structure out to the containing structure
23  * @ptr:	the pointer to the member.
24  * @type:	the type of the container struct this is embedded in.
25  * @member:	the name of the member within the struct.
26  *
27  */
28 #define container_of(ptr, type, member) ({			\
29 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
30 	(type *)((char *)__mptr - offsetof(type, member)); })
31 #endif
32 
33 #define min(x, y) ({				\
34 	typeof(x) _min1 = (x);			\
35 	typeof(y) _min2 = (y);			\
36 	(void) (&_min1 == &_min2);		\
37 	_min1 < _min2 ? _min1 : _min2; })
38 
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 
45 #define min_t(type, x, y) ({                    \
46 	type __min1 = (x);                      \
47 	type __min2 = (y);                      \
48 	__min1 < __min2 ? __min1: __min2; })
49 
50 #define max_t(type, x, y) ({                    \
51 	type __max1 = (x);                      \
52 	type __max2 = (y);                      \
53 	__max1 > __max2 ? __max1: __max2; })
54 
55 #define true 1
56 
57 #endif
58