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