Whamcloud - gitweb
LU-13485 libcfs: FIELD_SIZEOF macro removed
[fs/lustre-release.git] / lustre / utils / lstddef.h
1 #ifndef _LSTDDEF_H
2 #define _LSTDDEF_H
3
4 #include <unistd.h>
5 #include <linux/types.h>
6 #include <sys/param.h>
7 #include <sys/syscall.h>
8 #include <sys/types.h>
9
10 #define __ALIGN_LSTDDEF_MASK(x, mask) (((x) + (mask)) & ~(mask))
11 #define __ALIGN_LSTDDEF(x, a) __ALIGN_LSTDDEF_MASK(x, (typeof(x))(a) - 1)
12 #define __LSTDDEF_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
13
14 #define ALIGN(x, a)             __ALIGN_LSTDDEF((x), (a))
15 #define ALIGN_DOWN(x, a)        __ALIGN_LSTDDEF((x) - ((a) - 1), (a))
16 #define __ALIGN_MASK(x, mask)   __ALIGN_LSTDDEF_MASK((x), (mask))
17 #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((unsigned long)(p), (a)))
18 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
19
20 #ifndef __must_be_array
21 # define __must_be_array(arr) 0
22 #endif
23
24 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
25
26 /*
27  * This looks more complex than it should be. But we need to
28  * get the type for the ~ right in round_down (it needs to be
29  * as wide as the result!), and we want to evaluate the macro
30  * arguments just once each.
31  */
32 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
33 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
34 #define round_down(x, y) ((x) & ~__round_mask(x, y))
35
36 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
37
38 #define DIV_ROUND_DOWN_ULL(ll, d) \
39         ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
40
41 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
42
43 #if BITS_PER_LONG == 32
44 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
45 #else
46 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
47 #endif
48
49 #define rounddown(x, y) ({                              \
50         typeof(x) __x = (x);                            \
51         __x - (__x % (y));                              \
52 })
53
54 /*
55  * Divide positive or negative dividend by positive divisor and round
56  * to closest integer. Result is undefined for negative divisors and
57  * for negative dividends if the divisor variable type is unsigned.
58  */
59 #define DIV_ROUND_CLOSEST(x, divisor) ({                \
60         typeof(x) __x = x;                              \
61         typeof(divisor) __d = divisor;                  \
62         (((typeof(x))-1) > 0 ||                         \
63          ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
64                 (((__x) + ((__d) / 2)) / (__d)) :       \
65                 (((__x) - ((__d) / 2)) / (__d));        \
66 })
67
68 /*
69  * Same as above but for u64 dividends. divisor must be a 32-bit
70  * number.
71  */
72 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({            \
73         typeof(divisor) __d = divisor;                  \
74         unsigned long long _tmp = (x) + (__d) / 2;      \
75         do_div(_tmp, __d);                              \
76         _tmp;                                           \
77 })
78
79 /*
80  * Multiplies an integer by a fraction, while avoiding unnecessary
81  * overflow or loss of precision.
82  */
83 #define mult_frac(x, numer, denom) ({                   \
84         typeof(x) quot = (x) / (denom);                 \
85         typeof(x) rem  = (x) % (denom);                 \
86         (quot * (numer)) + ((rem * (numer)) / (denom)); \
87 })
88
89 /**
90  * upper_32_bits - return bits 32-63 of a number
91  * @n: the number we're accessing
92  *
93  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
94  * the "right shift count >= width of type" warning when that quantity is
95  * 32-bits.
96  */
97 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
98
99 /**
100  * lower_32_bits - return bits 0-31 of a number
101  * @n: the number we're accessing
102  */
103 #define lower_32_bits(n) ((__u32)(n))
104
105 /**
106  * abs - return absolute value of an argument
107  * @x: the value.  If it is unsigned type, it is converted to signed type first
108  *   (s64, long or int depending on its size).
109  *
110  * Return: an absolute value of x.  If x is 64-bit, macro's return type is s64,
111  *   otherwise it is signed long.
112  */
113 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({     \
114                 __s64 __x = (x);                                        \
115                 (__x < 0) ? -__x : __x;                                 \
116         }), ({                                                          \
117                 long ret;                                               \
118                 if (sizeof(x) == sizeof(long)) {                        \
119                         long __x = (x);                                 \
120                         ret = (__x < 0) ? -__x : __x;                   \
121                 } else {                                                \
122                         int __x = (x);                                  \
123                         ret = (__x < 0) ? -__x : __x;                   \
124                 }                                                       \
125                 ret;                                                    \
126         }))
127
128 /**
129  * reciprocal_scale - "scale" a value into range [0, ep_ro)
130  * @val: value
131  * @ep_ro: right open interval endpoint
132  *
133  * Perform a "reciprocal multiplication" in order to "scale" a value into
134  * range [0, ep_ro), where the upper interval endpoint is right-open.
135  * This is useful, e.g. for accessing a index of an array containing
136  * ep_ro elements, for example. Think of it as sort of modulus, only that
137  * the result isn't that of modulo. ;) Note that if initial input is a
138  * small value, then result will return 0.
139  *
140  * Return: a result based on val in interval [0, ep_ro).
141  */
142 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
143 {
144         return (__u32)(((__u64) val * ep_ro) >> 32);
145 }
146
147 /*
148  * min()/max()/clamp() macros that also do
149  * strict type-checking.. See the
150  * "unnecessary" pointer comparison.
151  */
152 #define min(x, y) ({                            \
153         typeof(x) _min1 = (x);                  \
154         typeof(y) _min2 = (y);                  \
155         (void) (&_min1 == &_min2);              \
156         _min1 < _min2 ? _min1 : _min2;          \
157 })
158
159 #define max(x, y) ({                            \
160         typeof(x) _max1 = (x);                  \
161         typeof(y) _max2 = (y);                  \
162         (void) (&_max1 == &_max2);              \
163         _max1 > _max2 ? _max1 : _max2;          \
164 })
165
166 #define min3(x, y, z) ({                        \
167         typeof(x) _min1 = (x);                  \
168         typeof(y) _min2 = (y);                  \
169         typeof(z) _min3 = (z);                  \
170         (void) (&_min1 == &_min2);              \
171         (void) (&_min1 == &_min3);              \
172         _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
173                 (_min2 < _min3 ? _min2 : _min3); \
174 })
175
176 #define max3(x, y, z) ({                        \
177         typeof(x) _max1 = (x);                  \
178         typeof(y) _max2 = (y);                  \
179         typeof(z) _max3 = (z);                  \
180         (void) (&_max1 == &_max2);              \
181         (void) (&_max1 == &_max3);              \
182         _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
183                 (_max2 > _max3 ? _max2 : _max3); \
184 })
185
186 /**
187  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
188  * @x: value1
189  * @y: value2
190  */
191 #define min_not_zero(x, y) ({                   \
192         typeof(x) __x = (x);                    \
193         typeof(y) __y = (y);                    \
194         __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
195 })
196
197 /**
198  * clamp - return a value clamped to a given range with strict typechecking
199  * @val: current value
200  * @min: minimum allowable value
201  * @max: maximum allowable value
202  *
203  * This macro does strict typechecking of min/max to make sure they are of the
204  * same type as val.  See the unnecessary pointer comparisons.
205  */
206 #define clamp(val, min, max) ({                 \
207         typeof(val) __val = (val);              \
208         typeof(min) __min = (min);              \
209         typeof(max) __max = (max);              \
210         (void) (&__val == &__min);              \
211         (void) (&__val == &__max);              \
212         __val = __val < __min ? __min : __val;  \
213         __val > __max ? __max : __val;          \
214 })
215
216 /*
217  * ..and if you can't take the strict
218  * types, you can specify one yourself.
219  *
220  * Or not use min/max/clamp at all, of course.
221  */
222 #define min_t(type, x, y) ({                    \
223         type __min1 = (x);                      \
224         type __min2 = (y);                      \
225         __min1 < __min2 ? __min1 : __min2;      \
226 })
227
228 #define max_t(type, x, y) ({                    \
229         type __max1 = (x);                      \
230         type __max2 = (y);                      \
231         __max1 > __max2 ? __max1 : __max2;      \
232 })
233
234 /**
235  * clamp_t - return a value clamped to a given range using a given type
236  * @type: the type of variable to use
237  * @val: current value
238  * @min: minimum allowable value
239  * @max: maximum allowable value
240  *
241  * This macro does no typechecking and uses temporary variables of type
242  * 'type' to make all the comparisons.
243  */
244 #define clamp_t(type, val, min, max) ({         \
245         type __val = (val);                     \
246         type __min = (min);                     \
247         type __max = (max);                     \
248         __val = __val < __min ? __min : __val;  \
249         __val > __max ? __max : __val;          \
250 })
251
252 /**
253  * clamp_val - return a value clamped to a given range using val's type
254  * @val: current value
255  * @min: minimum allowable value
256  * @max: maximum allowable value
257  *
258  * This macro does no typechecking and uses temporary variables of whatever
259  * type the input argument 'val' is.  This is useful when val is an unsigned
260  * type and min and max are literals that will otherwise be assigned a signed
261  * integer type.
262  */
263 #define clamp_val(val, min, max) ({             \
264         typeof(val) __val = (val);              \
265         typeof(val) __min = (min);              \
266         typeof(val) __max = (max);              \
267         __val = __val < __min ? __min : __val;  \
268         __val > __max ? __max : __val;          \
269 })
270
271 /*
272  * swap - swap value of @a and @b
273  */
274 #define swap(a, b) do {                         \
275         typeof(a) __tmp = (a);                  \
276         (a) = (b);                              \
277         (b) = __tmp;                            \
278 } while (0)
279
280 /**
281  * container_of - cast a member of a structure out to the containing structure
282  * @ptr:        the pointer to the member.
283  * @type:       the type of the container struct this is embedded in.
284  * @member:     the name of the member within the struct.
285  *
286  */
287 #define container_of(ptr, type, member) ({                      \
288         const typeof(((type *)0)->member) *__mptr = (ptr);      \
289         (type *)((char *)__mptr - offsetof(type, member));      \
290 })
291
292 #ifndef HAVE_COPY_FILE_RANGE
293
294 #ifndef __NR_copy_file_range
295
296 #if defined(_ASM_X86_UNISTD_64_H)
297 #define __NR_copy_file_range 326
298 #elif defined(_ASM_X86_UNISTD_32_H)
299 #define __NR_copy_file_range 285
300 #else
301 #define __NR_copy_file_range 285
302 #endif
303
304 #endif
305
306 static inline loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out,
307                                      loff_t *off_out, size_t len,
308                                      unsigned int flags)
309 {
310         return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
311                        off_out, len, flags);
312 }
313 #endif /* !HAVE_COPY_FILE_RANGE */
314
315 #endif /* !_LSTDDEF_H */