summaryrefslogtreecommitdiff
path: root/common/utils.h
blob: 219ff7515f5c193ce5bbe17660ce169d83fcdfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef UTILS_H
#define UTILS_H

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
#endif

/* Use this version of container_of() - it's safer than others */
#undef container_of
#define container_of(ptr, type, member) ({ \
	const typeof( ((type *)0)->member ) *__mptr = (ptr); \
	(type *)( (char *)__mptr - offsetof(type,member) );})

#define __min(tx,ty,x,y) ({		\
	tx _min1 = (x);			\
	ty _min2 = (y);			\
	(void)(&_min1 == &_min2);	\
	_min1 < _min2 ? _min1 : _min2; })

#define __max(tx,ty,x,y) ({		\
	tx _max1 = (x);			\
	ty _max2 = (y);			\
	(void)(&_max1 == &_max2);	\
	_max1 > _max2 ? _max1 : _max2; })

#define mint(x,y)	__min(typeof(x), typeof(y), x, y)
#define min_t(t,x,y)	__min(t, t, x, y)
#define maxt(x,y)	__max(typeof(x), typeof(y), x, y)
#define max_t(t,x,y)	__max(t, t, x, y)

#define ALIGN(v,a)	(((v) + ((a) - 1)) & ~((a) - 1))

#endif