]>
Commit | Line | Data |
---|---|---|
cba1da49 MY |
1 | #ifndef _LINUX_KERNEL_H |
2 | #define _LINUX_KERNEL_H | |
3 | ||
4 | ||
5 | #include <linux/types.h> | |
6 | ||
48c7ea39 MY |
7 | #define USHRT_MAX ((u16)(~0U)) |
8 | #define SHRT_MAX ((s16)(USHRT_MAX>>1)) | |
9 | #define SHRT_MIN ((s16)(-SHRT_MAX - 1)) | |
cba1da49 MY |
10 | #define INT_MAX ((int)(~0U>>1)) |
11 | #define INT_MIN (-INT_MAX - 1) | |
48c7ea39 MY |
12 | #define UINT_MAX (~0U) |
13 | #define LONG_MAX ((long)(~0UL>>1)) | |
14 | #define LONG_MIN (-LONG_MAX - 1) | |
15 | #define ULONG_MAX (~0UL) | |
cba1da49 | 16 | #define LLONG_MAX ((long long)(~0ULL>>1)) |
48c7ea39 MY |
17 | #define LLONG_MIN (-LLONG_MAX - 1) |
18 | #define ULLONG_MAX (~0ULL) | |
19 | #define SIZE_MAX (~(size_t)0) | |
cba1da49 MY |
20 | |
21 | #define U8_MAX ((u8)~0U) | |
48c7ea39 MY |
22 | #define S8_MAX ((s8)(U8_MAX>>1)) |
23 | #define S8_MIN ((s8)(-S8_MAX - 1)) | |
24 | #define U16_MAX ((u16)~0U) | |
25 | #define S16_MAX ((s16)(U16_MAX>>1)) | |
26 | #define S16_MIN ((s16)(-S16_MAX - 1)) | |
cba1da49 | 27 | #define U32_MAX ((u32)~0U) |
48c7ea39 MY |
28 | #define S32_MAX ((s32)(U32_MAX>>1)) |
29 | #define S32_MIN ((s32)(-S32_MAX - 1)) | |
cba1da49 | 30 | #define U64_MAX ((u64)~0ULL) |
48c7ea39 MY |
31 | #define S64_MAX ((s64)(U64_MAX>>1)) |
32 | #define S64_MIN ((s64)(-S64_MAX - 1)) | |
33 | ||
34 | #define STACK_MAGIC 0xdeadbeef | |
35 | ||
36 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) | |
cba1da49 MY |
37 | |
38 | #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1) | |
39 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | |
48c7ea39 MY |
40 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
41 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) | |
cba1da49 MY |
42 | |
43 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
44 | ||
45 | /* | |
46 | * This looks more complex than it should be. But we need to | |
47 | * get the type for the ~ right in round_down (it needs to be | |
48 | * as wide as the result!), and we want to evaluate the macro | |
49 | * arguments just once each. | |
50 | */ | |
51 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) | |
52 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) | |
53 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) | |
54 | ||
48c7ea39 | 55 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) |
cba1da49 MY |
56 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
57 | ||
48c7ea39 MY |
58 | #if BITS_PER_LONG == 32 |
59 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) | |
60 | #else | |
61 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) | |
62 | #endif | |
63 | ||
111396cc MY |
64 | /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ |
65 | #define roundup(x, y) ( \ | |
66 | { \ | |
67 | const typeof(y) __y = y; \ | |
68 | (((x) + (__y - 1)) / __y) * __y; \ | |
69 | } \ | |
70 | ) | |
48c7ea39 MY |
71 | #define rounddown(x, y) ( \ |
72 | { \ | |
73 | typeof(x) __x = (x); \ | |
74 | __x - (__x % (y)); \ | |
75 | } \ | |
76 | ) | |
77 | ||
cba1da49 MY |
78 | /* |
79 | * Divide positive or negative dividend by positive divisor and round | |
80 | * to closest integer. Result is undefined for negative divisors and | |
81 | * for negative dividends if the divisor variable type is unsigned. | |
82 | */ | |
83 | #define DIV_ROUND_CLOSEST(x, divisor)( \ | |
84 | { \ | |
85 | typeof(x) __x = x; \ | |
86 | typeof(divisor) __d = divisor; \ | |
87 | (((typeof(x))-1) > 0 || \ | |
88 | ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ | |
89 | (((__x) + ((__d) / 2)) / (__d)) : \ | |
90 | (((__x) - ((__d) / 2)) / (__d)); \ | |
91 | } \ | |
92 | ) | |
93 | ||
94 | /* | |
95 | * Multiplies an integer by a fraction, while avoiding unnecessary | |
96 | * overflow or loss of precision. | |
97 | */ | |
98 | #define mult_frac(x, numer, denom)( \ | |
99 | { \ | |
100 | typeof(x) quot = (x) / (denom); \ | |
101 | typeof(x) rem = (x) % (denom); \ | |
102 | (quot * (numer)) + ((rem * (numer)) / (denom)); \ | |
103 | } \ | |
104 | ) | |
105 | ||
106 | /** | |
107 | * upper_32_bits - return bits 32-63 of a number | |
108 | * @n: the number we're accessing | |
109 | * | |
110 | * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress | |
111 | * the "right shift count >= width of type" warning when that quantity is | |
112 | * 32-bits. | |
113 | */ | |
114 | #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) | |
115 | ||
116 | /** | |
117 | * lower_32_bits - return bits 0-31 of a number | |
118 | * @n: the number we're accessing | |
119 | */ | |
120 | #define lower_32_bits(n) ((u32)(n)) | |
121 | ||
122 | /* | |
123 | * abs() handles unsigned and signed longs, ints, shorts and chars. For all | |
124 | * input types abs() returns a signed long. | |
125 | * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() | |
126 | * for those. | |
127 | */ | |
128 | #define abs(x) ({ \ | |
129 | long ret; \ | |
130 | if (sizeof(x) == sizeof(long)) { \ | |
131 | long __x = (x); \ | |
132 | ret = (__x < 0) ? -__x : __x; \ | |
133 | } else { \ | |
134 | int __x = (x); \ | |
135 | ret = (__x < 0) ? -__x : __x; \ | |
136 | } \ | |
137 | ret; \ | |
138 | }) | |
139 | ||
140 | #define abs64(x) ({ \ | |
141 | s64 __x = (x); \ | |
142 | (__x < 0) ? -__x : __x; \ | |
143 | }) | |
144 | ||
145 | /* | |
146 | * min()/max()/clamp() macros that also do | |
147 | * strict type-checking.. See the | |
148 | * "unnecessary" pointer comparison. | |
149 | */ | |
150 | #define min(x, y) ({ \ | |
151 | typeof(x) _min1 = (x); \ | |
152 | typeof(y) _min2 = (y); \ | |
b4141195 | 153 | (void) (&_min1 == &_min2); \ |
cba1da49 MY |
154 | _min1 < _min2 ? _min1 : _min2; }) |
155 | ||
156 | #define max(x, y) ({ \ | |
157 | typeof(x) _max1 = (x); \ | |
158 | typeof(y) _max2 = (y); \ | |
b4141195 | 159 | (void) (&_max1 == &_max2); \ |
cba1da49 MY |
160 | _max1 > _max2 ? _max1 : _max2; }) |
161 | ||
b4141195 MY |
162 | #define min3(x, y, z) min((typeof(x))min(x, y), z) |
163 | #define max3(x, y, z) max((typeof(x))max(x, y), z) | |
cba1da49 | 164 | |
48c7ea39 MY |
165 | /** |
166 | * min_not_zero - return the minimum that is _not_ zero, unless both are zero | |
167 | * @x: value1 | |
168 | * @y: value2 | |
169 | */ | |
170 | #define min_not_zero(x, y) ({ \ | |
171 | typeof(x) __x = (x); \ | |
172 | typeof(y) __y = (y); \ | |
173 | __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) | |
174 | ||
175 | /** | |
176 | * clamp - return a value clamped to a given range with strict typechecking | |
177 | * @val: current value | |
178 | * @lo: lowest allowable value | |
179 | * @hi: highest allowable value | |
180 | * | |
181 | * This macro does strict typechecking of lo/hi to make sure they are of the | |
182 | * same type as val. See the unnecessary pointer comparisons. | |
183 | */ | |
184 | #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) | |
185 | ||
cba1da49 MY |
186 | /* |
187 | * ..and if you can't take the strict | |
188 | * types, you can specify one yourself. | |
189 | * | |
190 | * Or not use min/max/clamp at all, of course. | |
191 | */ | |
192 | #define min_t(type, x, y) ({ \ | |
193 | type __min1 = (x); \ | |
194 | type __min2 = (y); \ | |
195 | __min1 < __min2 ? __min1: __min2; }) | |
196 | ||
197 | #define max_t(type, x, y) ({ \ | |
198 | type __max1 = (x); \ | |
199 | type __max2 = (y); \ | |
200 | __max1 > __max2 ? __max1: __max2; }) | |
201 | ||
48c7ea39 MY |
202 | /** |
203 | * clamp_t - return a value clamped to a given range using a given type | |
204 | * @type: the type of variable to use | |
205 | * @val: current value | |
206 | * @lo: minimum allowable value | |
207 | * @hi: maximum allowable value | |
208 | * | |
209 | * This macro does no typechecking and uses temporary variables of type | |
210 | * 'type' to make all the comparisons. | |
211 | */ | |
212 | #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) | |
213 | ||
214 | /** | |
215 | * clamp_val - return a value clamped to a given range using val's type | |
216 | * @val: current value | |
217 | * @lo: minimum allowable value | |
218 | * @hi: maximum allowable value | |
219 | * | |
220 | * This macro does no typechecking and uses temporary variables of whatever | |
221 | * type the input argument 'val' is. This is useful when val is an unsigned | |
222 | * type and min and max are literals that will otherwise be assigned a signed | |
223 | * integer type. | |
224 | */ | |
225 | #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) | |
226 | ||
227 | ||
228 | /* | |
229 | * swap - swap value of @a and @b | |
230 | */ | |
231 | #define swap(a, b) \ | |
232 | do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) | |
233 | ||
cba1da49 MY |
234 | /** |
235 | * container_of - cast a member of a structure out to the containing structure | |
236 | * @ptr: the pointer to the member. | |
237 | * @type: the type of the container struct this is embedded in. | |
238 | * @member: the name of the member within the struct. | |
239 | * | |
240 | */ | |
241 | #define container_of(ptr, type, member) ({ \ | |
242 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
243 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
244 | ||
245 | #endif |