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 /*
46 * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
47 * while a host application (like pmdinfogen) may have another compiler.
48 * RTE_CC_IS_GNU is true if the file is compiled with GCC,
49 * no matter it is a target or host application.
50 */
51 #define RTE_CC_IS_GNU 0
52 #if defined __clang__
53 #define RTE_CC_CLANG
54 #elif defined __INTEL_COMPILER
55 #define RTE_CC_ICC
56 #elif defined __GNUC__
57 #define RTE_CC_GCC
58 #undef RTE_CC_IS_GNU
59 #define RTE_CC_IS_GNU 1
60 #endif
61 #if RTE_CC_IS_GNU
62 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
63 __GNUC_PATCHLEVEL__)
64 #endif
65
66 /**
67 * Force alignment
68 */
69 #define __rte_aligned(a) __attribute__((__aligned__(a)))
70
71 #ifdef RTE_ARCH_STRICT_ALIGN
72 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
73 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
74 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
75 #else
76 typedef uint64_t unaligned_uint64_t;
77 typedef uint32_t unaligned_uint32_t;
78 typedef uint16_t unaligned_uint16_t;
79 #endif
80
81 /**
82 * Force a structure to be packed
83 */
84 #define __rte_packed __attribute__((__packed__))
85
86 /******* Macro to mark functions and fields scheduled for removal *****/
87 #define __rte_deprecated __attribute__((__deprecated__))
88 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
89
90 /**
91 * Macro to mark macros and defines scheduled for removal
92 */
93 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
94 #define RTE_PRAGMA(x) _Pragma(#x)
95 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
96 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
97 #else
98 #define RTE_DEPRECATED(x)
99 #endif
100
101 /**
102 * Mark a function or variable to a weak reference.
103 */
104 #define __rte_weak __attribute__((__weak__))
105
106 /**
107 * Force symbol to be generated even if it appears to be unused.
108 */
109 #define __rte_used __attribute__((used))
110
111 /*********** Macros to eliminate unused variable warnings ********/
112
113 /**
114 * short definition to mark a function parameter unused
115 */
116 #define __rte_unused __attribute__((__unused__))
117
118 /**
119 * Mark pointer as restricted with regard to pointer aliasing.
120 */
121 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
122 #define __rte_restrict __restrict
123 #else
124 #define __rte_restrict restrict
125 #endif
126
127 /**
128 * definition to mark a variable or function parameter as used so
129 * as to avoid a compiler warning
130 */
131 #define RTE_SET_USED(x) (void)(x)
132
133 /**
134 * Check format string and its arguments at compile-time.
135 *
136 * GCC on Windows assumes MS-specific format string by default,
137 * even if the underlying stdio implementation is ANSI-compliant,
138 * so this must be overridden.
139 */
140 #if RTE_CC_IS_GNU
141 #define __rte_format_printf(format_index, first_arg) \
142 __attribute__((format(gnu_printf, format_index, first_arg)))
143 #else
144 #define __rte_format_printf(format_index, first_arg) \
145 __attribute__((format(printf, format_index, first_arg)))
146 #endif
147
148 /**
149 * Tells compiler that the function returns a value that points to
150 * memory, where the size is given by the one or two arguments.
151 * Used by compiler to validate object size.
152 */
153 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
154 #define __rte_alloc_size(...) \
155 __attribute__((alloc_size(__VA_ARGS__)))
156 #else
157 #define __rte_alloc_size(...)
158 #endif
159
160 #define RTE_PRIORITY_LOG 101
161 #define RTE_PRIORITY_BUS 110
162 #define RTE_PRIORITY_CLASS 120
163 #define RTE_PRIORITY_LAST 65535
164
165 #define RTE_PRIO(prio) \
166 RTE_PRIORITY_ ## prio
167
168 /**
169 * Run function before main() with high priority.
170 *
171 * @param func
172 * Constructor function.
173 * @param prio
174 * Priority number must be above 100.
175 * Lowest number is the first to run.
176 */
177 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
178 #define RTE_INIT_PRIO(func, prio) \
179 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
180 #endif
181
182 /**
183 * Run function before main() with low priority.
184 *
185 * The constructor will be run after prioritized constructors.
186 *
187 * @param func
188 * Constructor function.
189 */
190 #define RTE_INIT(func) \
191 RTE_INIT_PRIO(func, LAST)
192
193 /**
194 * Run after main() with low priority.
195 *
196 * @param func
197 * Destructor function name.
198 * @param prio
199 * Priority number must be above 100.
200 * Lowest number is the last to run.
201 */
202 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
203 #define RTE_FINI_PRIO(func, prio) \
204 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
205 #endif
206
207 /**
208 * Run after main() with high priority.
209 *
210 * The destructor will be run *before* prioritized destructors.
211 *
212 * @param func
213 * Destructor function name.
214 */
215 #define RTE_FINI(func) \
216 RTE_FINI_PRIO(func, LAST)
217
218 /**
219 * Hint never returning function
220 */
221 #define __rte_noreturn __attribute__((noreturn))
222
223 /**
224 * Force a function to be inlined
225 */
226 #define __rte_always_inline inline __attribute__((always_inline))
227
228 /**
229 * Force a function to be noinlined
230 */
231 #define __rte_noinline __attribute__((noinline))
232
233 /**
234 * Hint function in the hot path
235 */
236 #define __rte_hot __attribute__((hot))
237
238 /**
239 * Hint function in the cold path
240 */
241 #define __rte_cold __attribute__((cold))
242
243 /*********** Macros for pointer arithmetic ********/
244
245 /**
246 * add a byte-value offset to a pointer
247 */
248 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
249
250 /**
251 * subtract a byte-value offset from a pointer
252 */
253 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
254
255 /**
256 * get the difference between two pointer values, i.e. how far apart
257 * in bytes are the locations they point two. It is assumed that
258 * ptr1 is greater than ptr2.
259 */
260 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
261
262 /**
263 * Workaround to cast a const field of a structure to non-const type.
264 */
265 #define RTE_CAST_FIELD(var, field, type) \
266 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
267
268 /*********** Macros/static functions for doing alignment ********/
269
270
271 /**
272 * Macro to align a pointer to a given power-of-two. The resultant
273 * pointer will be a pointer of the same type as the first parameter, and
274 * point to an address no higher than the first parameter. Second parameter
275 * must be a power-of-two value.
276 */
277 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
278 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
279
280 /**
281 * Macro to align a value to a given power-of-two. The resultant value
282 * will be of the same type as the first parameter, and will be no
283 * bigger than the first parameter. Second parameter must be a
284 * power-of-two value.
285 */
286 #define RTE_ALIGN_FLOOR(val, align) \
287 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
288
289 /**
290 * Macro to align a pointer to a given power-of-two. The resultant
291 * pointer will be a pointer of the same type as the first parameter, and
292 * point to an address no lower than the first parameter. Second parameter
293 * must be a power-of-two value.
294 */
295 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
296 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
297
298 /**
299 * Macro to align a value to a given power-of-two. The resultant value
300 * will be of the same type as the first parameter, and will be no lower
301 * than the first parameter. Second parameter must be a power-of-two
302 * value.
303 */
304 #define RTE_ALIGN_CEIL(val, align) \
305 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
306
307 /**
308 * Macro to align a pointer to a given power-of-two. The resultant
309 * pointer will be a pointer of the same type as the first parameter, and
310 * point to an address no lower than the first parameter. Second parameter
311 * must be a power-of-two value.
312 * This function is the same as RTE_PTR_ALIGN_CEIL
313 */
314 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
315
316 /**
317 * Macro to align a value to a given power-of-two. The resultant
318 * value will be of the same type as the first parameter, and
319 * will be no lower than the first parameter. Second parameter
320 * must be a power-of-two value.
321 * This function is the same as RTE_ALIGN_CEIL
322 */
323 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
324
325 /**
326 * Macro to align a value to the multiple of given value. The resultant
327 * value will be of the same type as the first parameter and will be no lower
328 * than the first parameter.
329 */
330 #define RTE_ALIGN_MUL_CEIL(v, mul) \
331 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
332
333 /**
334 * Macro to align a value to the multiple of given value. The resultant
335 * value will be of the same type as the first parameter and will be no higher
336 * than the first parameter.
337 */
338 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
339 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
340
341 /**
342 * Macro to align value to the nearest multiple of the given value.
343 * The resultant value might be greater than or less than the first parameter
344 * whichever difference is the lowest.
345 */
346 #define RTE_ALIGN_MUL_NEAR(v, mul) \
347 ({ \
348 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
349 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
350 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
351 })
352
353 /**
354 * Checks if a pointer is aligned to a given power-of-two value
355 *
356 * @param ptr
357 * The pointer whose alignment is to be checked
358 * @param align
359 * The power-of-two value to which the ptr should be aligned
360 *
361 * @return
362 * True(1) where the pointer is correctly aligned, false(0) otherwise
363 */
364 static inline int
rte_is_aligned(void * ptr,unsigned align)365 rte_is_aligned(void *ptr, unsigned align)
366 {
367 return RTE_PTR_ALIGN(ptr, align) == ptr;
368 }
369
370 /*********** Macros for compile type checks ********/
371
372 /**
373 * Triggers an error at compilation time if the condition is true.
374 */
375 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
376
377 /*********** Cache line related macros ********/
378
379 /** Cache line mask. */
380 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
381
382 /** Return the first cache-aligned value greater or equal to size. */
383 #define RTE_CACHE_LINE_ROUNDUP(size) \
384 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
385 RTE_CACHE_LINE_SIZE))
386
387 /** Cache line size in terms of log2 */
388 #if RTE_CACHE_LINE_SIZE == 64
389 #define RTE_CACHE_LINE_SIZE_LOG2 6
390 #elif RTE_CACHE_LINE_SIZE == 128
391 #define RTE_CACHE_LINE_SIZE_LOG2 7
392 #else
393 #error "Unsupported cache line size"
394 #endif
395
396 /** Minimum Cache line size. */
397 #define RTE_CACHE_LINE_MIN_SIZE 64
398
399 /** Force alignment to cache line. */
400 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
401
402 /** Force minimum cache line alignment. */
403 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
404
405 /*********** PA/IOVA type definitions ********/
406
407 /** Physical address */
408 typedef uint64_t phys_addr_t;
409 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
410
411 /**
412 * IO virtual address type.
413 * When the physical addressing mode (IOVA as PA) is in use,
414 * the translation from an IO virtual address (IOVA) to a physical address
415 * is a direct mapping, i.e. the same value.
416 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
417 */
418 typedef uint64_t rte_iova_t;
419 #define RTE_BAD_IOVA ((rte_iova_t)-1)
420
421 /*********** Structure alignment markers ********/
422
423 /** Generic marker for any place in a structure. */
424 __extension__ typedef void *RTE_MARKER[0];
425 /** Marker for 1B alignment in a structure. */
426 __extension__ typedef uint8_t RTE_MARKER8[0];
427 /** Marker for 2B alignment in a structure. */
428 __extension__ typedef uint16_t RTE_MARKER16[0];
429 /** Marker for 4B alignment in a structure. */
430 __extension__ typedef uint32_t RTE_MARKER32[0];
431 /** Marker for 8B alignment in a structure. */
432 __extension__ typedef uint64_t RTE_MARKER64[0];
433
434 /**
435 * Combines 32b inputs most significant set bits into the least
436 * significant bits to construct a value with the same MSBs as x
437 * but all 1's under it.
438 *
439 * @param x
440 * The integer whose MSBs need to be combined with its LSBs
441 * @return
442 * The combined value.
443 */
444 static inline uint32_t
rte_combine32ms1b(uint32_t x)445 rte_combine32ms1b(uint32_t x)
446 {
447 x |= x >> 1;
448 x |= x >> 2;
449 x |= x >> 4;
450 x |= x >> 8;
451 x |= x >> 16;
452
453 return x;
454 }
455
456 /**
457 * Combines 64b inputs most significant set bits into the least
458 * significant bits to construct a value with the same MSBs as x
459 * but all 1's under it.
460 *
461 * @param v
462 * The integer whose MSBs need to be combined with its LSBs
463 * @return
464 * The combined value.
465 */
466 static inline uint64_t
rte_combine64ms1b(uint64_t v)467 rte_combine64ms1b(uint64_t v)
468 {
469 v |= v >> 1;
470 v |= v >> 2;
471 v |= v >> 4;
472 v |= v >> 8;
473 v |= v >> 16;
474 v |= v >> 32;
475
476 return v;
477 }
478
479 /*********** Macros to work with powers of 2 ********/
480
481 /**
482 * Macro to return 1 if n is a power of 2, 0 otherwise
483 */
484 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
485
486 /**
487 * Returns true if n is a power of 2
488 * @param n
489 * Number to check
490 * @return 1 if true, 0 otherwise
491 */
492 static inline int
rte_is_power_of_2(uint32_t n)493 rte_is_power_of_2(uint32_t n)
494 {
495 return n && !(n & (n - 1));
496 }
497
498 /**
499 * Aligns input parameter to the next power of 2
500 *
501 * @param x
502 * The integer value to align
503 *
504 * @return
505 * Input parameter aligned to the next power of 2
506 */
507 static inline uint32_t
rte_align32pow2(uint32_t x)508 rte_align32pow2(uint32_t x)
509 {
510 x--;
511 x = rte_combine32ms1b(x);
512
513 return x + 1;
514 }
515
516 /**
517 * Aligns input parameter to the previous power of 2
518 *
519 * @param x
520 * The integer value to align
521 *
522 * @return
523 * Input parameter aligned to the previous power of 2
524 */
525 static inline uint32_t
rte_align32prevpow2(uint32_t x)526 rte_align32prevpow2(uint32_t x)
527 {
528 x = rte_combine32ms1b(x);
529
530 return x - (x >> 1);
531 }
532
533 /**
534 * Aligns 64b input parameter to the next power of 2
535 *
536 * @param v
537 * The 64b value to align
538 *
539 * @return
540 * Input parameter aligned to the next power of 2
541 */
542 static inline uint64_t
rte_align64pow2(uint64_t v)543 rte_align64pow2(uint64_t v)
544 {
545 v--;
546 v = rte_combine64ms1b(v);
547
548 return v + 1;
549 }
550
551 /**
552 * Aligns 64b input parameter to the previous power of 2
553 *
554 * @param v
555 * The 64b value to align
556 *
557 * @return
558 * Input parameter aligned to the previous power of 2
559 */
560 static inline uint64_t
rte_align64prevpow2(uint64_t v)561 rte_align64prevpow2(uint64_t v)
562 {
563 v = rte_combine64ms1b(v);
564
565 return v - (v >> 1);
566 }
567
568 /*********** Macros for calculating min and max **********/
569
570 /**
571 * Macro to return the minimum of two numbers
572 */
573 #define RTE_MIN(a, b) \
574 __extension__ ({ \
575 typeof (a) _a = (a); \
576 typeof (b) _b = (b); \
577 _a < _b ? _a : _b; \
578 })
579
580 /**
581 * Macro to return the maximum of two numbers
582 */
583 #define RTE_MAX(a, b) \
584 __extension__ ({ \
585 typeof (a) _a = (a); \
586 typeof (b) _b = (b); \
587 _a > _b ? _a : _b; \
588 })
589
590 /*********** Other general functions / macros ********/
591
592 /**
593 * Searches the input parameter for the least significant set bit
594 * (starting from zero).
595 * If a least significant 1 bit is found, its bit index is returned.
596 * If the content of the input parameter is zero, then the content of the return
597 * value is undefined.
598 * @param v
599 * input parameter, should not be zero.
600 * @return
601 * least significant set bit in the input parameter.
602 */
603 static inline uint32_t
rte_bsf32(uint32_t v)604 rte_bsf32(uint32_t v)
605 {
606 return (uint32_t)__builtin_ctz(v);
607 }
608
609 /**
610 * Searches the input parameter for the least significant set bit
611 * (starting from zero). Safe version (checks for input parameter being zero).
612 *
613 * @warning ``pos`` must be a valid pointer. It is not checked!
614 *
615 * @param v
616 * The input parameter.
617 * @param pos
618 * If ``v`` was not 0, this value will contain position of least significant
619 * bit within the input parameter.
620 * @return
621 * Returns 0 if ``v`` was 0, otherwise returns 1.
622 */
623 static inline int
rte_bsf32_safe(uint64_t v,uint32_t * pos)624 rte_bsf32_safe(uint64_t v, uint32_t *pos)
625 {
626 if (v == 0)
627 return 0;
628
629 *pos = rte_bsf32(v);
630 return 1;
631 }
632
633 /**
634 * Return the rounded-up log2 of a integer.
635 *
636 * @note Contrary to the logarithm mathematical operation,
637 * rte_log2_u32(0) == 0 and not -inf.
638 *
639 * @param v
640 * The input parameter.
641 * @return
642 * The rounded-up log2 of the input, or 0 if the input is 0.
643 */
644 static inline uint32_t
rte_log2_u32(uint32_t v)645 rte_log2_u32(uint32_t v)
646 {
647 if (v == 0)
648 return 0;
649 v = rte_align32pow2(v);
650 return rte_bsf32(v);
651 }
652
653
654 /**
655 * Return the last (most-significant) bit set.
656 *
657 * @note The last (most significant) bit is at position 32.
658 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
659 *
660 * @param x
661 * The input parameter.
662 * @return
663 * The last (most-significant) bit set, or 0 if the input is 0.
664 */
665 static inline int
rte_fls_u32(uint32_t x)666 rte_fls_u32(uint32_t x)
667 {
668 return (x == 0) ? 0 : 32 - __builtin_clz(x);
669 }
670
671 /**
672 * Searches the input parameter for the least significant set bit
673 * (starting from zero).
674 * If a least significant 1 bit is found, its bit index is returned.
675 * If the content of the input parameter is zero, then the content of the return
676 * value is undefined.
677 * @param v
678 * input parameter, should not be zero.
679 * @return
680 * least significant set bit in the input parameter.
681 */
682 static inline int
rte_bsf64(uint64_t v)683 rte_bsf64(uint64_t v)
684 {
685 return (uint32_t)__builtin_ctzll(v);
686 }
687
688 /**
689 * Searches the input parameter for the least significant set bit
690 * (starting from zero). Safe version (checks for input parameter being zero).
691 *
692 * @warning ``pos`` must be a valid pointer. It is not checked!
693 *
694 * @param v
695 * The input parameter.
696 * @param pos
697 * If ``v`` was not 0, this value will contain position of least significant
698 * bit within the input parameter.
699 * @return
700 * Returns 0 if ``v`` was 0, otherwise returns 1.
701 */
702 static inline int
rte_bsf64_safe(uint64_t v,uint32_t * pos)703 rte_bsf64_safe(uint64_t v, uint32_t *pos)
704 {
705 if (v == 0)
706 return 0;
707
708 *pos = rte_bsf64(v);
709 return 1;
710 }
711
712 /**
713 * Return the last (most-significant) bit set.
714 *
715 * @note The last (most significant) bit is at position 64.
716 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
717 * rte_fls_u64(0x8000000000000000) = 64
718 *
719 * @param x
720 * The input parameter.
721 * @return
722 * The last (most-significant) bit set, or 0 if the input is 0.
723 */
724 static inline int
rte_fls_u64(uint64_t x)725 rte_fls_u64(uint64_t x)
726 {
727 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
728 }
729
730 /**
731 * Return the rounded-up log2 of a 64-bit integer.
732 *
733 * @note Contrary to the logarithm mathematical operation,
734 * rte_log2_u64(0) == 0 and not -inf.
735 *
736 * @param v
737 * The input parameter.
738 * @return
739 * The rounded-up log2 of the input, or 0 if the input is 0.
740 */
741 static inline uint32_t
rte_log2_u64(uint64_t v)742 rte_log2_u64(uint64_t v)
743 {
744 if (v == 0)
745 return 0;
746 v = rte_align64pow2(v);
747 /* we checked for v being 0 already, so no undefined behavior */
748 return rte_bsf64(v);
749 }
750
751 #ifndef offsetof
752 /** Return the offset of a field in a structure. */
753 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
754 #endif
755
756 /**
757 * Return pointer to the wrapping struct instance.
758 *
759 * Example:
760 *
761 * struct wrapper {
762 * ...
763 * struct child c;
764 * ...
765 * };
766 *
767 * struct child *x = obtain(...);
768 * struct wrapper *w = container_of(x, struct wrapper, c);
769 */
770 #ifndef container_of
771 #define container_of(ptr, type, member) __extension__ ({ \
772 const typeof(((type *)0)->member) *_ptr = (ptr); \
773 __rte_unused type *_target_ptr = \
774 (type *)(ptr); \
775 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
776 })
777 #endif
778
779 /**
780 * Get the size of a field in a structure.
781 *
782 * @param type
783 * The type of the structure.
784 * @param field
785 * The field in the structure.
786 * @return
787 * The size of the field in the structure, in bytes.
788 */
789 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
790
791 #define _RTE_STR(x) #x
792 /** Take a macro value and get a string version of it */
793 #define RTE_STR(x) _RTE_STR(x)
794
795 /**
796 * ISO C helpers to modify format strings using variadic macros.
797 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
798 * An empty %s argument is appended to avoid a dangling comma.
799 */
800 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
801 #define RTE_FMT_HEAD(fmt, ...) fmt
802 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
803
804 /** Mask value of type "tp" for the first "ln" bit set. */
805 #define RTE_LEN2MASK(ln, tp) \
806 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
807
808 /** Number of elements in the array. */
809 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
810
811 /**
812 * Converts a numeric string to the equivalent uint64_t value.
813 * As well as straight number conversion, also recognises the suffixes
814 * k, m and g for kilobytes, megabytes and gigabytes respectively.
815 *
816 * If a negative number is passed in i.e. a string with the first non-black
817 * character being "-", zero is returned. Zero is also returned in the case of
818 * an error with the strtoull call in the function.
819 *
820 * @param str
821 * String containing number to convert.
822 * @return
823 * Number.
824 */
825 static inline uint64_t
rte_str_to_size(const char * str)826 rte_str_to_size(const char *str)
827 {
828 char *endptr;
829 unsigned long long size;
830
831 while (isspace((int)*str))
832 str++;
833 if (*str == '-')
834 return 0;
835
836 errno = 0;
837 size = strtoull(str, &endptr, 0);
838 if (errno)
839 return 0;
840
841 if (*endptr == ' ')
842 endptr++; /* allow 1 space gap */
843
844 switch (*endptr){
845 case 'G': case 'g': size *= 1024; /* fall-through */
846 case 'M': case 'm': size *= 1024; /* fall-through */
847 case 'K': case 'k': size *= 1024; /* fall-through */
848 default:
849 break;
850 }
851 return size;
852 }
853
854 /**
855 * Function to terminate the application immediately, printing an error
856 * message and returning the exit_code back to the shell.
857 *
858 * This function never returns
859 *
860 * @param exit_code
861 * The exit code to be returned by the application
862 * @param format
863 * The format string to be used for printing the message. This can include
864 * printf format characters which will be expanded using any further parameters
865 * to the function.
866 */
867 __rte_noreturn void
868 rte_exit(int exit_code, const char *format, ...)
869 __rte_format_printf(2, 3);
870
871 #ifdef __cplusplus
872 }
873 #endif
874
875 #endif
876