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