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