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