New upstream version 17.11.5
[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 #include <rte_config.h>
55
56 #ifndef typeof
57 #define typeof __typeof__
58 #endif
59
60 #ifndef asm
61 #define asm __asm__
62 #endif
63
64 /** C extension macro for environments lacking C11 features. */
65 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
66 #define RTE_STD_C11 __extension__
67 #else
68 #define RTE_STD_C11
69 #endif
70
71 /** Define GCC_VERSION **/
72 #ifdef RTE_TOOLCHAIN_GCC
73 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
74                 __GNUC_PATCHLEVEL__)
75 #endif
76
77 #ifdef RTE_ARCH_STRICT_ALIGN
78 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
79 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
80 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
81 #else
82 typedef uint64_t unaligned_uint64_t;
83 typedef uint32_t unaligned_uint32_t;
84 typedef uint16_t unaligned_uint16_t;
85 #endif
86
87 /**
88  * Force alignment
89  */
90 #define __rte_aligned(a) __attribute__((__aligned__(a)))
91
92 /**
93  * Force a structure to be packed
94  */
95 #define __rte_packed __attribute__((__packed__))
96
97 /******* Macro to mark functions and fields scheduled for removal *****/
98 #define __rte_deprecated        __attribute__((__deprecated__))
99
100 /*********** Macros to eliminate unused variable warnings ********/
101
102 /**
103  * short definition to mark a function parameter unused
104  */
105 #define __rte_unused __attribute__((__unused__))
106
107 /**
108  * definition to mark a variable or function parameter as used so
109  * as to avoid a compiler warning
110  */
111 #define RTE_SET_USED(x) (void)(x)
112
113 /**
114  * Run function before main() with low priority.
115  *
116  * The constructor will be run after prioritized constructors.
117  *
118  * @param func
119  *   Constructor function.
120  */
121 #define RTE_INIT(func) \
122 static void __attribute__((constructor, used)) func(void)
123
124 /**
125  * Run function before main() with high priority.
126  *
127  * @param func
128  *   Constructor function.
129  * @param prio
130  *   Priority number must be above 100.
131  *   Lowest number is the first to run.
132  */
133 #define RTE_INIT_PRIO(func, prio) \
134 static void __attribute__((constructor(prio), used)) func(void)
135
136 /**
137  * Force a function to be inlined
138  */
139 #define __rte_always_inline inline __attribute__((always_inline))
140
141 /**
142  * Force a function to be noinlined
143  */
144 #define __rte_noinline  __attribute__((noinline))
145
146 /*********** Macros for pointer arithmetic ********/
147
148 /**
149  * add a byte-value offset from a pointer
150  */
151 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
152
153 /**
154  * subtract a byte-value offset from a pointer
155  */
156 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
157
158 /**
159  * get the difference between two pointer values, i.e. how far apart
160  * in bytes are the locations they point two. It is assumed that
161  * ptr1 is greater than ptr2.
162  */
163 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
164
165 /*********** Macros/static functions for doing alignment ********/
166
167
168 /**
169  * Macro to align a pointer to a given power-of-two. The resultant
170  * pointer will be a pointer of the same type as the first parameter, and
171  * point to an address no higher than the first parameter. Second parameter
172  * must be a power-of-two value.
173  */
174 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
175         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
176
177 /**
178  * Macro to align a value to a given power-of-two. The resultant value
179  * will be of the same type as the first parameter, and will be no
180  * bigger than the first parameter. Second parameter must be a
181  * power-of-two value.
182  */
183 #define RTE_ALIGN_FLOOR(val, align) \
184         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
185
186 /**
187  * Macro to align a pointer to a given power-of-two. The resultant
188  * pointer will be a pointer of the same type as the first parameter, and
189  * point to an address no lower than the first parameter. Second parameter
190  * must be a power-of-two value.
191  */
192 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
193         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
194
195 /**
196  * Macro to align a value to a given power-of-two. The resultant value
197  * will be of the same type as the first parameter, and will be no lower
198  * than the first parameter. Second parameter must be a power-of-two
199  * value.
200  */
201 #define RTE_ALIGN_CEIL(val, align) \
202         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
203
204 /**
205  * Macro to align a pointer to a given power-of-two. The resultant
206  * pointer will be a pointer of the same type as the first parameter, and
207  * point to an address no lower than the first parameter. Second parameter
208  * must be a power-of-two value.
209  * This function is the same as RTE_PTR_ALIGN_CEIL
210  */
211 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
212
213 /**
214  * Macro to align a value to a given power-of-two. The resultant
215  * value will be of the same type as the first parameter, and
216  * will be no lower than the first parameter. Second parameter
217  * must be a power-of-two value.
218  * This function is the same as RTE_ALIGN_CEIL
219  */
220 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
221
222 /**
223  * Checks if a pointer is aligned to a given power-of-two value
224  *
225  * @param ptr
226  *   The pointer whose alignment is to be checked
227  * @param align
228  *   The power-of-two value to which the ptr should be aligned
229  *
230  * @return
231  *   True(1) where the pointer is correctly aligned, false(0) otherwise
232  */
233 static inline int
234 rte_is_aligned(void *ptr, unsigned align)
235 {
236         return RTE_PTR_ALIGN(ptr, align) == ptr;
237 }
238
239 /*********** Macros for compile type checks ********/
240
241 /**
242  * Triggers an error at compilation time if the condition is true.
243  */
244 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
245
246 /*********** Macros to work with powers of 2 ********/
247
248 /**
249  * Returns true if n is a power of 2
250  * @param n
251  *     Number to check
252  * @return 1 if true, 0 otherwise
253  */
254 static inline int
255 rte_is_power_of_2(uint32_t n)
256 {
257         return n && !(n & (n - 1));
258 }
259
260 /**
261  * Aligns input parameter to the next power of 2
262  *
263  * @param x
264  *   The integer value to algin
265  *
266  * @return
267  *   Input parameter aligned to the next power of 2
268  */
269 static inline uint32_t
270 rte_align32pow2(uint32_t x)
271 {
272         x--;
273         x |= x >> 1;
274         x |= x >> 2;
275         x |= x >> 4;
276         x |= x >> 8;
277         x |= x >> 16;
278
279         return x + 1;
280 }
281
282 /**
283  * Aligns 64b input parameter to the next power of 2
284  *
285  * @param v
286  *   The 64b value to align
287  *
288  * @return
289  *   Input parameter aligned to the next power of 2
290  */
291 static inline uint64_t
292 rte_align64pow2(uint64_t v)
293 {
294         v--;
295         v |= v >> 1;
296         v |= v >> 2;
297         v |= v >> 4;
298         v |= v >> 8;
299         v |= v >> 16;
300         v |= v >> 32;
301
302         return v + 1;
303 }
304
305 /*********** Macros for calculating min and max **********/
306
307 /**
308  * Macro to return the minimum of two numbers
309  */
310 #define RTE_MIN(a, b) \
311         __extension__ ({ \
312                 typeof (a) _a = (a); \
313                 typeof (b) _b = (b); \
314                 _a < _b ? _a : _b; \
315         })
316
317 /**
318  * Macro to return the maximum of two numbers
319  */
320 #define RTE_MAX(a, b) \
321         __extension__ ({ \
322                 typeof (a) _a = (a); \
323                 typeof (b) _b = (b); \
324                 _a > _b ? _a : _b; \
325         })
326
327 /*********** Other general functions / macros ********/
328
329 /**
330  * Searches the input parameter for the least significant set bit
331  * (starting from zero).
332  * If a least significant 1 bit is found, its bit index is returned.
333  * If the content of the input parameter is zero, then the content of the return
334  * value is undefined.
335  * @param v
336  *     input parameter, should not be zero.
337  * @return
338  *     least significant set bit in the input parameter.
339  */
340 static inline uint32_t
341 rte_bsf32(uint32_t v)
342 {
343         return (uint32_t)__builtin_ctz(v);
344 }
345
346 /**
347  * Return the rounded-up log2 of a integer.
348  *
349  * @param v
350  *     The input parameter.
351  * @return
352  *     The rounded-up log2 of the input, or 0 if the input is 0.
353  */
354 static inline uint32_t
355 rte_log2_u32(uint32_t v)
356 {
357         if (v == 0)
358                 return 0;
359         v = rte_align32pow2(v);
360         return rte_bsf32(v);
361 }
362
363
364 /**
365  * Return the last (most-significant) bit set.
366  *
367  * @note The last (most significant) bit is at position 32.
368  * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
369  *
370  * @param x
371  *     The input parameter.
372  * @return
373  *     The last (most-significant) bit set, or 0 if the input is 0.
374  */
375 static inline int
376 rte_fls_u32(uint32_t x)
377 {
378         return (x == 0) ? 0 : 32 - __builtin_clz(x);
379 }
380
381
382 #ifndef offsetof
383 /** Return the offset of a field in a structure. */
384 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
385 #endif
386
387 /**
388  * Return pointer to the wrapping struct instance.
389  *
390  * Example:
391  *
392  *  struct wrapper {
393  *      ...
394  *      struct child c;
395  *      ...
396  *  };
397  *
398  *  struct child *x = obtain(...);
399  *  struct wrapper *w = container_of(x, struct wrapper, c);
400  */
401 #ifndef container_of
402 #define container_of(ptr, type, member) __extension__ ({                \
403                         const typeof(((type *)0)->member) *_ptr = (ptr); \
404                         __attribute__((unused)) type *_target_ptr =     \
405                                 (type *)(ptr);                          \
406                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
407                 })
408 #endif
409
410 #define _RTE_STR(x) #x
411 /** Take a macro value and get a string version of it */
412 #define RTE_STR(x) _RTE_STR(x)
413
414 /**
415  * ISO C helpers to modify format strings using variadic macros.
416  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
417  * An empty %s argument is appended to avoid a dangling comma.
418  */
419 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
420 #define RTE_FMT_HEAD(fmt, ...) fmt
421 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
422
423 /** Mask value of type "tp" for the first "ln" bit set. */
424 #define RTE_LEN2MASK(ln, tp)    \
425         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
426
427 /** Number of elements in the array. */
428 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
429
430 /**
431  * Converts a numeric string to the equivalent uint64_t value.
432  * As well as straight number conversion, also recognises the suffixes
433  * k, m and g for kilobytes, megabytes and gigabytes respectively.
434  *
435  * If a negative number is passed in  i.e. a string with the first non-black
436  * character being "-", zero is returned. Zero is also returned in the case of
437  * an error with the strtoull call in the function.
438  *
439  * @param str
440  *     String containing number to convert.
441  * @return
442  *     Number.
443  */
444 static inline uint64_t
445 rte_str_to_size(const char *str)
446 {
447         char *endptr;
448         unsigned long long size;
449
450         while (isspace((int)*str))
451                 str++;
452         if (*str == '-')
453                 return 0;
454
455         errno = 0;
456         size = strtoull(str, &endptr, 0);
457         if (errno)
458                 return 0;
459
460         if (*endptr == ' ')
461                 endptr++; /* allow 1 space gap */
462
463         switch (*endptr){
464         case 'G': case 'g': size *= 1024; /* fall-through */
465         case 'M': case 'm': size *= 1024; /* fall-through */
466         case 'K': case 'k': size *= 1024; /* fall-through */
467         default:
468                 break;
469         }
470         return size;
471 }
472
473 /**
474  * Function to terminate the application immediately, printing an error
475  * message and returning the exit_code back to the shell.
476  *
477  * This function never returns
478  *
479  * @param exit_code
480  *     The exit code to be returned by the application
481  * @param format
482  *     The format string to be used for printing the message. This can include
483  *     printf format characters which will be expanded using any further parameters
484  *     to the function.
485  */
486 void
487 rte_exit(int exit_code, const char *format, ...)
488         __attribute__((noreturn))
489         __attribute__((format(printf, 2, 3)));
490
491 #ifdef __cplusplus
492 }
493 #endif
494
495 #endif