1afc66e3f5fb1e897e9281adbb53ec1c8f3e253b
[deb_dpdk.git] / lib / librte_eal / common / include / rte_common.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
36
37 /**
38  * @file
39  *
40  * Generic, commonly-used macro and inline function definitions
41  * for DPDK.
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdint.h>
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <limits.h>
53
54 #ifndef typeof
55 #define typeof __typeof__
56 #endif
57
58 #ifndef asm
59 #define asm __asm__
60 #endif
61
62 /** C extension macro for environments lacking C11 features. */
63 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
64 #define RTE_STD_C11 __extension__
65 #else
66 #define RTE_STD_C11
67 #endif
68
69 /** Define GCC_VERSION **/
70 #ifdef RTE_TOOLCHAIN_GCC
71 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
72                 __GNUC_PATCHLEVEL__)
73 #endif
74
75 #ifdef RTE_ARCH_STRICT_ALIGN
76 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
77 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
78 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
79 #else
80 typedef uint64_t unaligned_uint64_t;
81 typedef uint32_t unaligned_uint32_t;
82 typedef uint16_t unaligned_uint16_t;
83 #endif
84
85 /**
86  * Force alignment
87  */
88 #define __rte_aligned(a) __attribute__((__aligned__(a)))
89
90 /**
91  * Force a structure to be packed
92  */
93 #define __rte_packed __attribute__((__packed__))
94
95 /******* Macro to mark functions and fields scheduled for removal *****/
96 #define __rte_deprecated        __attribute__((__deprecated__))
97
98 /*********** Macros to eliminate unused variable warnings ********/
99
100 /**
101  * short definition to mark a function parameter unused
102  */
103 #define __rte_unused __attribute__((__unused__))
104
105 /**
106  * definition to mark a variable or function parameter as used so
107  * as to avoid a compiler warning
108  */
109 #define RTE_SET_USED(x) (void)(x)
110
111 /**
112  * Force a function to be inlined
113  */
114 #define __rte_always_inline inline __attribute__((always_inline))
115
116 /**
117  * Force a function to be noinlined
118  */
119 #define __rte_noinline  __attribute__((noinline))
120
121 /*********** Macros for pointer arithmetic ********/
122
123 /**
124  * add a byte-value offset from a pointer
125  */
126 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
127
128 /**
129  * subtract a byte-value offset from a pointer
130  */
131 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
132
133 /**
134  * get the difference between two pointer values, i.e. how far apart
135  * in bytes are the locations they point two. It is assumed that
136  * ptr1 is greater than ptr2.
137  */
138 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
139
140 /*********** Macros/static functions for doing alignment ********/
141
142
143 /**
144  * Macro to align a pointer to a given power-of-two. The resultant
145  * pointer will be a pointer of the same type as the first parameter, and
146  * point to an address no higher than the first parameter. Second parameter
147  * must be a power-of-two value.
148  */
149 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
150         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
151
152 /**
153  * Macro to align a value to a given power-of-two. The resultant value
154  * will be of the same type as the first parameter, and will be no
155  * bigger than the first parameter. Second parameter must be a
156  * power-of-two value.
157  */
158 #define RTE_ALIGN_FLOOR(val, align) \
159         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
160
161 /**
162  * Macro to align a pointer to a given power-of-two. The resultant
163  * pointer will be a pointer of the same type as the first parameter, and
164  * point to an address no lower than the first parameter. Second parameter
165  * must be a power-of-two value.
166  */
167 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
168         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
169
170 /**
171  * Macro to align a value to a given power-of-two. The resultant value
172  * will be of the same type as the first parameter, and will be no lower
173  * than the first parameter. Second parameter must be a power-of-two
174  * value.
175  */
176 #define RTE_ALIGN_CEIL(val, align) \
177         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
178
179 /**
180  * Macro to align a pointer to a given power-of-two. The resultant
181  * pointer will be a pointer of the same type as the first parameter, and
182  * point to an address no lower than the first parameter. Second parameter
183  * must be a power-of-two value.
184  * This function is the same as RTE_PTR_ALIGN_CEIL
185  */
186 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
187
188 /**
189  * Macro to align a value to a given power-of-two. The resultant
190  * value will be of the same type as the first parameter, and
191  * will be no lower than the first parameter. Second parameter
192  * must be a power-of-two value.
193  * This function is the same as RTE_ALIGN_CEIL
194  */
195 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
196
197 /**
198  * Checks if a pointer is aligned to a given power-of-two value
199  *
200  * @param ptr
201  *   The pointer whose alignment is to be checked
202  * @param align
203  *   The power-of-two value to which the ptr should be aligned
204  *
205  * @return
206  *   True(1) where the pointer is correctly aligned, false(0) otherwise
207  */
208 static inline int
209 rte_is_aligned(void *ptr, unsigned align)
210 {
211         return RTE_PTR_ALIGN(ptr, align) == ptr;
212 }
213
214 /*********** Macros for compile type checks ********/
215
216 /**
217  * Triggers an error at compilation time if the condition is true.
218  */
219 #ifndef __OPTIMIZE__
220 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
221 #else
222 extern int RTE_BUILD_BUG_ON_detected_error;
223 #define RTE_BUILD_BUG_ON(condition) do {             \
224         ((void)sizeof(char[1 - 2*!!(condition)]));   \
225         if (condition)                               \
226                 RTE_BUILD_BUG_ON_detected_error = 1; \
227 } while(0)
228 #endif
229
230 /*********** Macros to work with powers of 2 ********/
231
232 /**
233  * Returns true if n is a power of 2
234  * @param n
235  *     Number to check
236  * @return 1 if true, 0 otherwise
237  */
238 static inline int
239 rte_is_power_of_2(uint32_t n)
240 {
241         return n && !(n & (n - 1));
242 }
243
244 /**
245  * Aligns input parameter to the next power of 2
246  *
247  * @param x
248  *   The integer value to algin
249  *
250  * @return
251  *   Input parameter aligned to the next power of 2
252  */
253 static inline uint32_t
254 rte_align32pow2(uint32_t x)
255 {
256         x--;
257         x |= x >> 1;
258         x |= x >> 2;
259         x |= x >> 4;
260         x |= x >> 8;
261         x |= x >> 16;
262
263         return x + 1;
264 }
265
266 /**
267  * Aligns 64b input parameter to the next power of 2
268  *
269  * @param v
270  *   The 64b value to align
271  *
272  * @return
273  *   Input parameter aligned to the next power of 2
274  */
275 static inline uint64_t
276 rte_align64pow2(uint64_t v)
277 {
278         v--;
279         v |= v >> 1;
280         v |= v >> 2;
281         v |= v >> 4;
282         v |= v >> 8;
283         v |= v >> 16;
284         v |= v >> 32;
285
286         return v + 1;
287 }
288
289 /*********** Macros for calculating min and max **********/
290
291 /**
292  * Macro to return the minimum of two numbers
293  */
294 #define RTE_MIN(a, b) \
295         __extension__ ({ \
296                 typeof (a) _a = (a); \
297                 typeof (b) _b = (b); \
298                 _a < _b ? _a : _b; \
299         })
300
301 /**
302  * Macro to return the maximum of two numbers
303  */
304 #define RTE_MAX(a, b) \
305         __extension__ ({ \
306                 typeof (a) _a = (a); \
307                 typeof (b) _b = (b); \
308                 _a > _b ? _a : _b; \
309         })
310
311 /*********** Other general functions / macros ********/
312
313 /**
314  * Searches the input parameter for the least significant set bit
315  * (starting from zero).
316  * If a least significant 1 bit is found, its bit index is returned.
317  * If the content of the input parameter is zero, then the content of the return
318  * value is undefined.
319  * @param v
320  *     input parameter, should not be zero.
321  * @return
322  *     least significant set bit in the input parameter.
323  */
324 static inline uint32_t
325 rte_bsf32(uint32_t v)
326 {
327         return __builtin_ctz(v);
328 }
329
330 /**
331  * Return the rounded-up log2 of a integer.
332  *
333  * @param v
334  *     The input parameter.
335  * @return
336  *     The rounded-up log2 of the input, or 0 if the input is 0.
337  */
338 static inline uint32_t
339 rte_log2_u32(uint32_t v)
340 {
341         if (v == 0)
342                 return 0;
343         v = rte_align32pow2(v);
344         return rte_bsf32(v);
345 }
346
347 #ifndef offsetof
348 /** Return the offset of a field in a structure. */
349 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
350 #endif
351
352 /**
353  * Return pointer to the wrapping struct instance.
354  *
355  * Example:
356  *
357  *  struct wrapper {
358  *      ...
359  *      struct child c;
360  *      ...
361  *  };
362  *
363  *  struct child *x = obtain(...);
364  *  struct wrapper *w = container_of(x, struct wrapper, c);
365  */
366 #ifndef container_of
367 #define container_of(ptr, type, member) __extension__ ({                \
368                         const typeof(((type *)0)->member) *_ptr = (ptr); \
369                         __attribute__((unused)) type *_target_ptr =     \
370                                 (type *)(ptr);                          \
371                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
372                 })
373 #endif
374
375 #define _RTE_STR(x) #x
376 /** Take a macro value and get a string version of it */
377 #define RTE_STR(x) _RTE_STR(x)
378
379 /**
380  * ISO C helpers to modify format strings using variadic macros.
381  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
382  * An empty %s argument is appended to avoid a dangling comma.
383  */
384 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
385 #define RTE_FMT_HEAD(fmt, ...) fmt
386 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
387
388 /** Mask value of type "tp" for the first "ln" bit set. */
389 #define RTE_LEN2MASK(ln, tp)    \
390         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
391
392 /** Number of elements in the array. */
393 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
394
395 /**
396  * Converts a numeric string to the equivalent uint64_t value.
397  * As well as straight number conversion, also recognises the suffixes
398  * k, m and g for kilobytes, megabytes and gigabytes respectively.
399  *
400  * If a negative number is passed in  i.e. a string with the first non-black
401  * character being "-", zero is returned. Zero is also returned in the case of
402  * an error with the strtoull call in the function.
403  *
404  * @param str
405  *     String containing number to convert.
406  * @return
407  *     Number.
408  */
409 static inline uint64_t
410 rte_str_to_size(const char *str)
411 {
412         char *endptr;
413         unsigned long long size;
414
415         while (isspace((int)*str))
416                 str++;
417         if (*str == '-')
418                 return 0;
419
420         errno = 0;
421         size = strtoull(str, &endptr, 0);
422         if (errno)
423                 return 0;
424
425         if (*endptr == ' ')
426                 endptr++; /* allow 1 space gap */
427
428         switch (*endptr){
429         case 'G': case 'g': size *= 1024; /* fall-through */
430         case 'M': case 'm': size *= 1024; /* fall-through */
431         case 'K': case 'k': size *= 1024; /* fall-through */
432         default:
433                 break;
434         }
435         return size;
436 }
437
438 /**
439  * Function to terminate the application immediately, printing an error
440  * message and returning the exit_code back to the shell.
441  *
442  * This function never returns
443  *
444  * @param exit_code
445  *     The exit code to be returned by the application
446  * @param format
447  *     The format string to be used for printing the message. This can include
448  *     printf format characters which will be expanded using any further parameters
449  *     to the function.
450  */
451 void
452 rte_exit(int exit_code, const char *format, ...)
453         __attribute__((noreturn))
454         __attribute__((format(printf, 2, 3)));
455
456 #ifdef __cplusplus
457 }
458 #endif
459
460 #endif