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