New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_eal / common / include / rte_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7
8 /**
9  * @file
10  *
11  * Generic, commonly-used macro and inline function definitions
12  * for DPDK.
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #include <rte_config.h>
26
27 #ifndef typeof
28 #define typeof __typeof__
29 #endif
30
31 #ifndef asm
32 #define asm __asm__
33 #endif
34
35 /** C extension macro for environments lacking C11 features. */
36 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
37 #define RTE_STD_C11 __extension__
38 #else
39 #define RTE_STD_C11
40 #endif
41
42 /** Define GCC_VERSION **/
43 #ifdef RTE_TOOLCHAIN_GCC
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
45                 __GNUC_PATCHLEVEL__)
46 #endif
47
48 #ifdef RTE_ARCH_STRICT_ALIGN
49 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
50 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
51 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
52 #else
53 typedef uint64_t unaligned_uint64_t;
54 typedef uint32_t unaligned_uint32_t;
55 typedef uint16_t unaligned_uint16_t;
56 #endif
57
58 /**
59  * Force alignment
60  */
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
62
63 /**
64  * Force a structure to be packed
65  */
66 #define __rte_packed __attribute__((__packed__))
67
68 /******* Macro to mark functions and fields scheduled for removal *****/
69 #define __rte_deprecated        __attribute__((__deprecated__))
70
71 /**
72  * Mark a function or variable to a weak reference.
73  */
74 #define __rte_weak __attribute__((__weak__))
75
76 /*********** Macros to eliminate unused variable warnings ********/
77
78 /**
79  * short definition to mark a function parameter unused
80  */
81 #define __rte_unused __attribute__((__unused__))
82
83 /**
84  * definition to mark a variable or function parameter as used so
85  * as to avoid a compiler warning
86  */
87 #define RTE_SET_USED(x) (void)(x)
88
89 #define RTE_PRIORITY_LOG 101
90 #define RTE_PRIORITY_BUS 110
91 #define RTE_PRIORITY_CLASS 120
92 #define RTE_PRIORITY_LAST 65535
93
94 #define RTE_PRIO(prio) \
95         RTE_PRIORITY_ ## prio
96
97 /**
98  * Run function before main() with high priority.
99  *
100  * @param func
101  *   Constructor function.
102  * @param prio
103  *   Priority number must be above 100.
104  *   Lowest number is the first to run.
105  */
106 #define RTE_INIT_PRIO(func, prio) \
107 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
108
109 /**
110  * Run function before main() with low priority.
111  *
112  * The constructor will be run after prioritized constructors.
113  *
114  * @param func
115  *   Constructor function.
116  */
117 #define RTE_INIT(func) \
118         RTE_INIT_PRIO(func, LAST)
119
120 /**
121  * Run after main() with low priority.
122  *
123  * @param func
124  *   Destructor function name.
125  * @param prio
126  *   Priority number must be above 100.
127  *   Lowest number is the last to run.
128  */
129 #define RTE_FINI_PRIO(func, prio) \
130 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
131
132 /**
133  * Run after main() with high priority.
134  *
135  * The destructor will be run *before* prioritized destructors.
136  *
137  * @param func
138  *   Destructor function name.
139  */
140 #define RTE_FINI(func) \
141         RTE_FINI_PRIO(func, LAST)
142
143 /**
144  * Force a function to be inlined
145  */
146 #define __rte_always_inline inline __attribute__((always_inline))
147
148 /**
149  * Force a function to be noinlined
150  */
151 #define __rte_noinline  __attribute__((noinline))
152
153 /*********** Macros for pointer arithmetic ********/
154
155 /**
156  * add a byte-value offset to a pointer
157  */
158 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
159
160 /**
161  * subtract a byte-value offset from a pointer
162  */
163 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
164
165 /**
166  * get the difference between two pointer values, i.e. how far apart
167  * in bytes are the locations they point two. It is assumed that
168  * ptr1 is greater than ptr2.
169  */
170 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
171
172 /**
173  * Workaround to cast a const field of a structure to non-const type.
174  */
175 #define RTE_CAST_FIELD(var, field, type) \
176         (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
177
178 /*********** Macros/static functions for doing alignment ********/
179
180
181 /**
182  * Macro to align a pointer to a given power-of-two. The resultant
183  * pointer will be a pointer of the same type as the first parameter, and
184  * point to an address no higher than the first parameter. Second parameter
185  * must be a power-of-two value.
186  */
187 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
188         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
189
190 /**
191  * Macro to align a value to a given power-of-two. The resultant value
192  * will be of the same type as the first parameter, and will be no
193  * bigger than the first parameter. Second parameter must be a
194  * power-of-two value.
195  */
196 #define RTE_ALIGN_FLOOR(val, align) \
197         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
198
199 /**
200  * Macro to align a pointer to a given power-of-two. The resultant
201  * pointer will be a pointer of the same type as the first parameter, and
202  * point to an address no lower than the first parameter. Second parameter
203  * must be a power-of-two value.
204  */
205 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
206         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
207
208 /**
209  * Macro to align a value to a given power-of-two. The resultant value
210  * will be of the same type as the first parameter, and will be no lower
211  * than the first parameter. Second parameter must be a power-of-two
212  * value.
213  */
214 #define RTE_ALIGN_CEIL(val, align) \
215         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
216
217 /**
218  * Macro to align a pointer to a given power-of-two. The resultant
219  * pointer will be a pointer of the same type as the first parameter, and
220  * point to an address no lower than the first parameter. Second parameter
221  * must be a power-of-two value.
222  * This function is the same as RTE_PTR_ALIGN_CEIL
223  */
224 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
225
226 /**
227  * Macro to align a value to a given power-of-two. The resultant
228  * value will be of the same type as the first parameter, and
229  * will be no lower than the first parameter. Second parameter
230  * must be a power-of-two value.
231  * This function is the same as RTE_ALIGN_CEIL
232  */
233 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
234
235 /**
236  * Macro to align a value to the multiple of given value. The resultant
237  * value will be of the same type as the first parameter and will be no lower
238  * than the first parameter.
239  */
240 #define RTE_ALIGN_MUL_CEIL(v, mul) \
241         (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
242
243 /**
244  * Macro to align a value to the multiple of given value. The resultant
245  * value will be of the same type as the first parameter and will be no higher
246  * than the first parameter.
247  */
248 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
249         ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
250
251 /**
252  * Checks if a pointer is aligned to a given power-of-two value
253  *
254  * @param ptr
255  *   The pointer whose alignment is to be checked
256  * @param align
257  *   The power-of-two value to which the ptr should be aligned
258  *
259  * @return
260  *   True(1) where the pointer is correctly aligned, false(0) otherwise
261  */
262 static inline int
263 rte_is_aligned(void *ptr, unsigned align)
264 {
265         return RTE_PTR_ALIGN(ptr, align) == ptr;
266 }
267
268 /*********** Macros for compile type checks ********/
269
270 /**
271  * Triggers an error at compilation time if the condition is true.
272  */
273 #ifndef __OPTIMIZE__
274 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
275 #else
276 extern int RTE_BUILD_BUG_ON_detected_error;
277 #define RTE_BUILD_BUG_ON(condition) do {             \
278         ((void)sizeof(char[1 - 2*!!(condition)]));   \
279         if (condition)                               \
280                 RTE_BUILD_BUG_ON_detected_error = 1; \
281 } while(0)
282 #endif
283
284 /**
285  * Combines 32b inputs most significant set bits into the least
286  * significant bits to construct a value with the same MSBs as x
287  * but all 1's under it.
288  *
289  * @param x
290  *    The integer whose MSBs need to be combined with its LSBs
291  * @return
292  *    The combined value.
293  */
294 static inline uint32_t
295 rte_combine32ms1b(register uint32_t x)
296 {
297         x |= x >> 1;
298         x |= x >> 2;
299         x |= x >> 4;
300         x |= x >> 8;
301         x |= x >> 16;
302
303         return x;
304 }
305
306 /**
307  * Combines 64b inputs most significant set bits into the least
308  * significant bits to construct a value with the same MSBs as x
309  * but all 1's under it.
310  *
311  * @param v
312  *    The integer whose MSBs need to be combined with its LSBs
313  * @return
314  *    The combined value.
315  */
316 static inline uint64_t
317 rte_combine64ms1b(register uint64_t v)
318 {
319         v |= v >> 1;
320         v |= v >> 2;
321         v |= v >> 4;
322         v |= v >> 8;
323         v |= v >> 16;
324         v |= v >> 32;
325
326         return v;
327 }
328
329 /*********** Macros to work with powers of 2 ********/
330
331 /**
332  * Macro to return 1 if n is a power of 2, 0 otherwise
333  */
334 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
335
336 /**
337  * Returns true if n is a power of 2
338  * @param n
339  *     Number to check
340  * @return 1 if true, 0 otherwise
341  */
342 static inline int
343 rte_is_power_of_2(uint32_t n)
344 {
345         return n && !(n & (n - 1));
346 }
347
348 /**
349  * Aligns input parameter to the next power of 2
350  *
351  * @param x
352  *   The integer value to algin
353  *
354  * @return
355  *   Input parameter aligned to the next power of 2
356  */
357 static inline uint32_t
358 rte_align32pow2(uint32_t x)
359 {
360         x--;
361         x = rte_combine32ms1b(x);
362
363         return x + 1;
364 }
365
366 /**
367  * Aligns input parameter to the previous power of 2
368  *
369  * @param x
370  *   The integer value to algin
371  *
372  * @return
373  *   Input parameter aligned to the previous power of 2
374  */
375 static inline uint32_t
376 rte_align32prevpow2(uint32_t x)
377 {
378         x = rte_combine32ms1b(x);
379
380         return x - (x >> 1);
381 }
382
383 /**
384  * Aligns 64b input parameter to the next power of 2
385  *
386  * @param v
387  *   The 64b value to align
388  *
389  * @return
390  *   Input parameter aligned to the next power of 2
391  */
392 static inline uint64_t
393 rte_align64pow2(uint64_t v)
394 {
395         v--;
396         v = rte_combine64ms1b(v);
397
398         return v + 1;
399 }
400
401 /**
402  * Aligns 64b input parameter to the previous power of 2
403  *
404  * @param v
405  *   The 64b value to align
406  *
407  * @return
408  *   Input parameter aligned to the previous power of 2
409  */
410 static inline uint64_t
411 rte_align64prevpow2(uint64_t v)
412 {
413         v = rte_combine64ms1b(v);
414
415         return v - (v >> 1);
416 }
417
418 /*********** Macros for calculating min and max **********/
419
420 /**
421  * Macro to return the minimum of two numbers
422  */
423 #define RTE_MIN(a, b) \
424         __extension__ ({ \
425                 typeof (a) _a = (a); \
426                 typeof (b) _b = (b); \
427                 _a < _b ? _a : _b; \
428         })
429
430 /**
431  * Macro to return the maximum of two numbers
432  */
433 #define RTE_MAX(a, b) \
434         __extension__ ({ \
435                 typeof (a) _a = (a); \
436                 typeof (b) _b = (b); \
437                 _a > _b ? _a : _b; \
438         })
439
440 /*********** Other general functions / macros ********/
441
442 /**
443  * Searches the input parameter for the least significant set bit
444  * (starting from zero).
445  * If a least significant 1 bit is found, its bit index is returned.
446  * If the content of the input parameter is zero, then the content of the return
447  * value is undefined.
448  * @param v
449  *     input parameter, should not be zero.
450  * @return
451  *     least significant set bit in the input parameter.
452  */
453 static inline uint32_t
454 rte_bsf32(uint32_t v)
455 {
456         return (uint32_t)__builtin_ctz(v);
457 }
458
459 /**
460  * Return the rounded-up log2 of a integer.
461  *
462  * @param v
463  *     The input parameter.
464  * @return
465  *     The rounded-up log2 of the input, or 0 if the input is 0.
466  */
467 static inline uint32_t
468 rte_log2_u32(uint32_t v)
469 {
470         if (v == 0)
471                 return 0;
472         v = rte_align32pow2(v);
473         return rte_bsf32(v);
474 }
475
476 #ifndef offsetof
477 /** Return the offset of a field in a structure. */
478 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
479 #endif
480
481 /**
482  * Return pointer to the wrapping struct instance.
483  *
484  * Example:
485  *
486  *  struct wrapper {
487  *      ...
488  *      struct child c;
489  *      ...
490  *  };
491  *
492  *  struct child *x = obtain(...);
493  *  struct wrapper *w = container_of(x, struct wrapper, c);
494  */
495 #ifndef container_of
496 #define container_of(ptr, type, member) __extension__ ({                \
497                         const typeof(((type *)0)->member) *_ptr = (ptr); \
498                         __attribute__((unused)) type *_target_ptr =     \
499                                 (type *)(ptr);                          \
500                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
501                 })
502 #endif
503
504 #define _RTE_STR(x) #x
505 /** Take a macro value and get a string version of it */
506 #define RTE_STR(x) _RTE_STR(x)
507
508 /**
509  * ISO C helpers to modify format strings using variadic macros.
510  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
511  * An empty %s argument is appended to avoid a dangling comma.
512  */
513 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
514 #define RTE_FMT_HEAD(fmt, ...) fmt
515 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
516
517 /** Mask value of type "tp" for the first "ln" bit set. */
518 #define RTE_LEN2MASK(ln, tp)    \
519         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
520
521 /** Number of elements in the array. */
522 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
523
524 /**
525  * Converts a numeric string to the equivalent uint64_t value.
526  * As well as straight number conversion, also recognises the suffixes
527  * k, m and g for kilobytes, megabytes and gigabytes respectively.
528  *
529  * If a negative number is passed in  i.e. a string with the first non-black
530  * character being "-", zero is returned. Zero is also returned in the case of
531  * an error with the strtoull call in the function.
532  *
533  * @param str
534  *     String containing number to convert.
535  * @return
536  *     Number.
537  */
538 static inline uint64_t
539 rte_str_to_size(const char *str)
540 {
541         char *endptr;
542         unsigned long long size;
543
544         while (isspace((int)*str))
545                 str++;
546         if (*str == '-')
547                 return 0;
548
549         errno = 0;
550         size = strtoull(str, &endptr, 0);
551         if (errno)
552                 return 0;
553
554         if (*endptr == ' ')
555                 endptr++; /* allow 1 space gap */
556
557         switch (*endptr){
558         case 'G': case 'g': size *= 1024; /* fall-through */
559         case 'M': case 'm': size *= 1024; /* fall-through */
560         case 'K': case 'k': size *= 1024; /* fall-through */
561         default:
562                 break;
563         }
564         return size;
565 }
566
567 /**
568  * Function to terminate the application immediately, printing an error
569  * message and returning the exit_code back to the shell.
570  *
571  * This function never returns
572  *
573  * @param exit_code
574  *     The exit code to be returned by the application
575  * @param format
576  *     The format string to be used for printing the message. This can include
577  *     printf format characters which will be expanded using any further parameters
578  *     to the function.
579  */
580 void
581 rte_exit(int exit_code, const char *format, ...)
582         __attribute__((noreturn))
583         __attribute__((format(printf, 2, 3)));
584
585 #ifdef __cplusplus
586 }
587 #endif
588
589 #endif