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