Branch data Line data Source code
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 <limits.h>
21 : :
22 : : #include <rte_config.h>
23 : :
24 : : /* OS specific include */
25 : : #include <rte_os.h>
26 : :
27 : : #ifndef RTE_TOOLCHAIN_MSVC
28 : : #ifndef typeof
29 : : #define typeof __typeof__
30 : : #endif
31 : : #endif
32 : :
33 : : #ifndef __cplusplus
34 : : #ifndef asm
35 : : #define asm __asm__
36 : : #endif
37 : : #endif
38 : :
39 : : #ifdef RTE_TOOLCHAIN_MSVC
40 : : #define __extension__
41 : : #endif
42 : :
43 : : /*
44 : : * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
45 : : * while a host application (like pmdinfogen) may have another compiler.
46 : : * RTE_CC_IS_GNU is true if the file is compiled with GCC,
47 : : * no matter it is a target or host application.
48 : : */
49 : : #define RTE_CC_IS_GNU 0
50 : : #if defined __clang__
51 : : #define RTE_CC_CLANG
52 : : #elif defined __INTEL_COMPILER
53 : : #define RTE_CC_ICC
54 : : #elif defined __GNUC__
55 : : #define RTE_CC_GCC
56 : : #undef RTE_CC_IS_GNU
57 : : #define RTE_CC_IS_GNU 1
58 : : #endif
59 : : #if RTE_CC_IS_GNU
60 : : #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
61 : : __GNUC_PATCHLEVEL__)
62 : : #endif
63 : :
64 : : /**
65 : : * Force alignment
66 : : */
67 : : #ifdef RTE_TOOLCHAIN_MSVC
68 : : #define __rte_aligned(a)
69 : : #else
70 : : #define __rte_aligned(a) __attribute__((__aligned__(a)))
71 : : #endif
72 : :
73 : : #ifdef RTE_ARCH_STRICT_ALIGN
74 : : typedef uint64_t unaligned_uint64_t __rte_aligned(1);
75 : : typedef uint32_t unaligned_uint32_t __rte_aligned(1);
76 : : typedef uint16_t unaligned_uint16_t __rte_aligned(1);
77 : : #else
78 : : typedef uint64_t unaligned_uint64_t;
79 : : typedef uint32_t unaligned_uint32_t;
80 : : typedef uint16_t unaligned_uint16_t;
81 : : #endif
82 : :
83 : : /**
84 : : * Force a structure to be packed
85 : : */
86 : : #ifdef RTE_TOOLCHAIN_MSVC
87 : : #define __rte_packed
88 : : #else
89 : : #define __rte_packed __attribute__((__packed__))
90 : : #endif
91 : :
92 : : /**
93 : : * Macro to mark a type that is not subject to type-based aliasing rules
94 : : */
95 : : #ifdef RTE_TOOLCHAIN_MSVC
96 : : #define __rte_may_alias
97 : : #else
98 : : #define __rte_may_alias __attribute__((__may_alias__))
99 : : #endif
100 : :
101 : : /******* Macro to mark functions and fields scheduled for removal *****/
102 : : #ifdef RTE_TOOLCHAIN_MSVC
103 : : #define __rte_deprecated
104 : : #define __rte_deprecated_msg(msg)
105 : : #else
106 : : #define __rte_deprecated __attribute__((__deprecated__))
107 : : #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
108 : : #endif
109 : :
110 : : /**
111 : : * Macro to mark macros and defines scheduled for removal
112 : : */
113 : : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
114 : : #define RTE_PRAGMA(x) _Pragma(#x)
115 : : #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
116 : : #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
117 : : #else
118 : : #define RTE_DEPRECATED(x)
119 : : #endif
120 : :
121 : : /**
122 : : * Mark a function or variable to a weak reference.
123 : : */
124 : : #define __rte_weak __attribute__((__weak__))
125 : :
126 : : /**
127 : : * Force symbol to be generated even if it appears to be unused.
128 : : */
129 : : #ifdef RTE_TOOLCHAIN_MSVC
130 : : #define __rte_used
131 : : #else
132 : : #define __rte_used __attribute__((used))
133 : : #endif
134 : :
135 : : /*********** Macros to eliminate unused variable warnings ********/
136 : :
137 : : /**
138 : : * short definition to mark a function parameter unused
139 : : */
140 : : #ifdef RTE_TOOLCHAIN_MSVC
141 : : #define __rte_unused
142 : : #else
143 : : #define __rte_unused __attribute__((__unused__))
144 : : #endif
145 : :
146 : : /**
147 : : * Mark pointer as restricted with regard to pointer aliasing.
148 : : */
149 : : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
150 : : #define __rte_restrict __restrict
151 : : #else
152 : : #define __rte_restrict restrict
153 : : #endif
154 : :
155 : : /**
156 : : * definition to mark a variable or function parameter as used so
157 : : * as to avoid a compiler warning
158 : : */
159 : : #define RTE_SET_USED(x) (void)(x)
160 : :
161 : : /**
162 : : * Check format string and its arguments at compile-time.
163 : : *
164 : : * GCC on Windows assumes MS-specific format string by default,
165 : : * even if the underlying stdio implementation is ANSI-compliant,
166 : : * so this must be overridden.
167 : : */
168 : : #ifdef RTE_TOOLCHAIN_MSVC
169 : : #define __rte_format_printf(format_index, first_arg)
170 : : #else
171 : : #if RTE_CC_IS_GNU
172 : : #define __rte_format_printf(format_index, first_arg) \
173 : : __attribute__((format(gnu_printf, format_index, first_arg)))
174 : : #else
175 : : #define __rte_format_printf(format_index, first_arg) \
176 : : __attribute__((format(printf, format_index, first_arg)))
177 : : #endif
178 : : #endif
179 : :
180 : : /**
181 : : * Tells compiler that the function returns a value that points to
182 : : * memory, where the size is given by the one or two arguments.
183 : : * Used by compiler to validate object size.
184 : : */
185 : : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
186 : : #define __rte_alloc_size(...) \
187 : : __attribute__((alloc_size(__VA_ARGS__)))
188 : : #else
189 : : #define __rte_alloc_size(...)
190 : : #endif
191 : :
192 : : #define RTE_PRIORITY_LOG 101
193 : : #define RTE_PRIORITY_BUS 110
194 : : #define RTE_PRIORITY_CLASS 120
195 : : #define RTE_PRIORITY_LAST 65535
196 : :
197 : : #define RTE_PRIO(prio) \
198 : : RTE_PRIORITY_ ## prio
199 : :
200 : : /**
201 : : * Run function before main() with high priority.
202 : : *
203 : : * @param func
204 : : * Constructor function.
205 : : * @param prio
206 : : * Priority number must be above 100.
207 : : * Lowest number is the first to run.
208 : : */
209 : : #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
210 : : #ifndef RTE_TOOLCHAIN_MSVC
211 : : #define RTE_INIT_PRIO(func, prio) \
212 : : static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
213 : : #else
214 : : /* definition from the Microsoft CRT */
215 : : typedef int(__cdecl *_PIFV)(void);
216 : :
217 : : #define CTOR_SECTION_LOG ".CRT$XIB"
218 : : #define CTOR_SECTION_BUS ".CRT$XIC"
219 : : #define CTOR_SECTION_CLASS ".CRT$XID"
220 : : #define CTOR_SECTION_LAST ".CRT$XIY"
221 : :
222 : : #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
223 : :
224 : : #define RTE_INIT_PRIO(name, priority) \
225 : : static void name(void); \
226 : : static int __cdecl name ## _thunk(void) { name(); return 0; } \
227 : : __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
228 : : __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
229 : : _PIFV name ## _pointer = &name ## _thunk; \
230 : : __pragma(const_seg()) \
231 : : static void name(void)
232 : : #endif
233 : : #endif
234 : :
235 : : /**
236 : : * Run function before main() with low priority.
237 : : *
238 : : * The constructor will be run after prioritized constructors.
239 : : *
240 : : * @param func
241 : : * Constructor function.
242 : : */
243 : : #define RTE_INIT(func) \
244 : : RTE_INIT_PRIO(func, LAST)
245 : :
246 : : /**
247 : : * Run after main() with low priority.
248 : : *
249 : : * @param func
250 : : * Destructor function name.
251 : : * @param prio
252 : : * Priority number must be above 100.
253 : : * Lowest number is the last to run.
254 : : */
255 : : #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
256 : : #ifndef RTE_TOOLCHAIN_MSVC
257 : : #define RTE_FINI_PRIO(func, prio) \
258 : : static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
259 : : #else
260 : : #define DTOR_SECTION_LOG "mydtor$B"
261 : : #define DTOR_SECTION_BUS "mydtor$C"
262 : : #define DTOR_SECTION_CLASS "mydtor$D"
263 : : #define DTOR_SECTION_LAST "mydtor$Y"
264 : :
265 : : #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
266 : :
267 : : #define RTE_FINI_PRIO(name, priority) \
268 : : static void name(void); \
269 : : __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
270 : : __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
271 : : __pragma(const_seg()) \
272 : : static void name(void)
273 : : #endif
274 : : #endif
275 : :
276 : : /**
277 : : * Run after main() with high priority.
278 : : *
279 : : * The destructor will be run *before* prioritized destructors.
280 : : *
281 : : * @param func
282 : : * Destructor function name.
283 : : */
284 : : #define RTE_FINI(func) \
285 : : RTE_FINI_PRIO(func, LAST)
286 : :
287 : : /**
288 : : * Hint never returning function
289 : : */
290 : : #ifdef RTE_TOOLCHAIN_MSVC
291 : : #define __rte_noreturn
292 : : #else
293 : : #define __rte_noreturn __attribute__((noreturn))
294 : : #endif
295 : :
296 : : /**
297 : : * Issue a warning in case the function's return value is ignored.
298 : : *
299 : : * The use of this attribute should be restricted to cases where
300 : : * ignoring the marked function's return value is almost always a
301 : : * bug. With GCC, some effort is required to make clear that ignoring
302 : : * the return value is intentional. The usual void-casting method to
303 : : * mark something unused as used does not suppress the warning with
304 : : * this compiler.
305 : : *
306 : : * @code{.c}
307 : : * __rte_warn_unused_result int foo();
308 : : *
309 : : * void ignore_foo_result(void) {
310 : : * foo(); // generates a warning with all compilers
311 : : *
312 : : * (void)foo(); // still generates the warning with GCC (but not clang)
313 : : *
314 : : * int unused __rte_unused;
315 : : * unused = foo(); // does the trick with all compilers
316 : : * }
317 : : * @endcode
318 : : */
319 : : #ifdef RTE_TOOLCHAIN_MSVC
320 : : #define __rte_warn_unused_result
321 : : #else
322 : : #define __rte_warn_unused_result __attribute__((warn_unused_result))
323 : : #endif
324 : :
325 : : /**
326 : : * Force a function to be inlined
327 : : */
328 : : #ifdef RTE_TOOLCHAIN_MSVC
329 : : #define __rte_always_inline
330 : : #else
331 : : #define __rte_always_inline inline __attribute__((always_inline))
332 : : #endif
333 : :
334 : : /**
335 : : * Force a function to be noinlined
336 : : */
337 : : #define __rte_noinline __attribute__((noinline))
338 : :
339 : : /**
340 : : * Hint function in the hot path
341 : : */
342 : : #define __rte_hot __attribute__((hot))
343 : :
344 : : /**
345 : : * Hint function in the cold path
346 : : */
347 : : #ifdef RTE_TOOLCHAIN_MSVC
348 : : #define __rte_cold
349 : : #else
350 : : #define __rte_cold __attribute__((cold))
351 : : #endif
352 : :
353 : : /**
354 : : * Disable AddressSanitizer on some code
355 : : */
356 : : #ifdef RTE_MALLOC_ASAN
357 : : #ifdef RTE_CC_CLANG
358 : : #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
359 : : #else
360 : : #define __rte_no_asan __attribute__((no_sanitize_address))
361 : : #endif
362 : : #else /* ! RTE_MALLOC_ASAN */
363 : : #define __rte_no_asan
364 : : #endif
365 : :
366 : : /*********** Macros for pointer arithmetic ********/
367 : :
368 : : /**
369 : : * add a byte-value offset to a pointer
370 : : */
371 : : #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
372 : :
373 : : /**
374 : : * subtract a byte-value offset from a pointer
375 : : */
376 : : #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
377 : :
378 : : /**
379 : : * get the difference between two pointer values, i.e. how far apart
380 : : * in bytes are the locations they point two. It is assumed that
381 : : * ptr1 is greater than ptr2.
382 : : */
383 : : #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
384 : :
385 : : /**
386 : : * Workaround to cast a const field of a structure to non-const type.
387 : : */
388 : : #define RTE_CAST_FIELD(var, field, type) \
389 : : (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
390 : :
391 : : /*********** Macros/static functions for doing alignment ********/
392 : :
393 : :
394 : : /**
395 : : * Macro to align a pointer to a given power-of-two. The resultant
396 : : * pointer will be a pointer of the same type as the first parameter, and
397 : : * point to an address no higher than the first parameter. Second parameter
398 : : * must be a power-of-two value.
399 : : */
400 : : #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
401 : : ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
402 : :
403 : : /**
404 : : * Macro to align a value to a given power-of-two. The resultant value
405 : : * will be of the same type as the first parameter, and will be no
406 : : * bigger than the first parameter. Second parameter must be a
407 : : * power-of-two value.
408 : : */
409 : : #define RTE_ALIGN_FLOOR(val, align) \
410 : : (typeof(val))((val) & (~((typeof(val))((align) - 1))))
411 : :
412 : : /**
413 : : * Macro to align a pointer to a given power-of-two. The resultant
414 : : * pointer will be a pointer of the same type as the first parameter, and
415 : : * point to an address no lower than the first parameter. Second parameter
416 : : * must be a power-of-two value.
417 : : */
418 : : #define RTE_PTR_ALIGN_CEIL(ptr, align) \
419 : : RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
420 : :
421 : : /**
422 : : * Macro to align a value to a given power-of-two. The resultant value
423 : : * will be of the same type as the first parameter, and will be no lower
424 : : * than the first parameter. Second parameter must be a power-of-two
425 : : * value.
426 : : */
427 : : #define RTE_ALIGN_CEIL(val, align) \
428 : : RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
429 : :
430 : : /**
431 : : * Macro to align a pointer to a given power-of-two. The resultant
432 : : * pointer will be a pointer of the same type as the first parameter, and
433 : : * point to an address no lower than the first parameter. Second parameter
434 : : * must be a power-of-two value.
435 : : * This function is the same as RTE_PTR_ALIGN_CEIL
436 : : */
437 : : #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
438 : :
439 : : /**
440 : : * Macro to align a value to a given power-of-two. The resultant
441 : : * value will be of the same type as the first parameter, and
442 : : * will be no lower than the first parameter. Second parameter
443 : : * must be a power-of-two value.
444 : : * This function is the same as RTE_ALIGN_CEIL
445 : : */
446 : : #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
447 : :
448 : : /**
449 : : * Macro to align a value to the multiple of given value. The resultant
450 : : * value will be of the same type as the first parameter and will be no lower
451 : : * than the first parameter.
452 : : */
453 : : #define RTE_ALIGN_MUL_CEIL(v, mul) \
454 : : ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
455 : :
456 : : /**
457 : : * Macro to align a value to the multiple of given value. The resultant
458 : : * value will be of the same type as the first parameter and will be no higher
459 : : * than the first parameter.
460 : : */
461 : : #define RTE_ALIGN_MUL_FLOOR(v, mul) \
462 : : (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
463 : :
464 : : /**
465 : : * Macro to align value to the nearest multiple of the given value.
466 : : * The resultant value might be greater than or less than the first parameter
467 : : * whichever difference is the lowest.
468 : : */
469 : : #define RTE_ALIGN_MUL_NEAR(v, mul) \
470 : : __extension__ ({ \
471 : : typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
472 : : typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
473 : : (ceil - (v)) > ((v) - floor) ? floor : ceil; \
474 : : })
475 : :
476 : : /**
477 : : * Checks if a pointer is aligned to a given power-of-two value
478 : : *
479 : : * @param ptr
480 : : * The pointer whose alignment is to be checked
481 : : * @param align
482 : : * The power-of-two value to which the ptr should be aligned
483 : : *
484 : : * @return
485 : : * True(1) where the pointer is correctly aligned, false(0) otherwise
486 : : */
487 : : static inline int
488 : : rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
489 : : {
490 [ + + # # : 2 : return ((uintptr_t)ptr & (align - 1)) == 0;
# # ]
491 : : }
492 : :
493 : : /*********** Macros for compile type checks ********/
494 : :
495 : : /**
496 : : * Triggers an error at compilation time if the condition is true.
497 : : */
498 : : #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
499 : :
500 : : /*********** Cache line related macros ********/
501 : :
502 : : /** Cache line mask. */
503 : : #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
504 : :
505 : : /** Return the first cache-aligned value greater or equal to size. */
506 : : #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
507 : :
508 : : /** Cache line size in terms of log2 */
509 : : #if RTE_CACHE_LINE_SIZE == 64
510 : : #define RTE_CACHE_LINE_SIZE_LOG2 6
511 : : #elif RTE_CACHE_LINE_SIZE == 128
512 : : #define RTE_CACHE_LINE_SIZE_LOG2 7
513 : : #else
514 : : #error "Unsupported cache line size"
515 : : #endif
516 : :
517 : : /** Minimum Cache line size. */
518 : : #define RTE_CACHE_LINE_MIN_SIZE 64
519 : :
520 : : /** Force alignment to cache line. */
521 : : #ifdef RTE_TOOLCHAIN_MSVC
522 : : #define __rte_cache_aligned
523 : : #else
524 : : #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
525 : : #endif
526 : :
527 : : /** Force minimum cache line alignment. */
528 : : #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
529 : :
530 : : #define _RTE_CACHE_GUARD_HELPER2(unique) \
531 : : char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] \
532 : : __rte_cache_aligned
533 : : #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
534 : : /**
535 : : * Empty cache lines, to guard against false sharing-like effects
536 : : * on systems with a next-N-lines hardware prefetcher.
537 : : *
538 : : * Use as spacing between data accessed by different lcores,
539 : : * to prevent cache thrashing on hardware with speculative prefetching.
540 : : */
541 : : #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
542 : :
543 : : /*********** PA/IOVA type definitions ********/
544 : :
545 : : /** Physical address */
546 : : typedef uint64_t phys_addr_t;
547 : : #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
548 : :
549 : : /**
550 : : * IO virtual address type.
551 : : * When the physical addressing mode (IOVA as PA) is in use,
552 : : * the translation from an IO virtual address (IOVA) to a physical address
553 : : * is a direct mapping, i.e. the same value.
554 : : * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
555 : : */
556 : : typedef uint64_t rte_iova_t;
557 : : #define RTE_BAD_IOVA ((rte_iova_t)-1)
558 : :
559 : : /*********** Structure alignment markers ********/
560 : :
561 : : #ifndef RTE_TOOLCHAIN_MSVC
562 : :
563 : : /** Generic marker for any place in a structure. */
564 : : __extension__ typedef void *RTE_MARKER[0];
565 : : /** Marker for 1B alignment in a structure. */
566 : : __extension__ typedef uint8_t RTE_MARKER8[0];
567 : : /** Marker for 2B alignment in a structure. */
568 : : __extension__ typedef uint16_t RTE_MARKER16[0];
569 : : /** Marker for 4B alignment in a structure. */
570 : : __extension__ typedef uint32_t RTE_MARKER32[0];
571 : : /** Marker for 8B alignment in a structure. */
572 : : __extension__ typedef uint64_t RTE_MARKER64[0];
573 : :
574 : : #endif
575 : :
576 : : /*********** Macros for calculating min and max **********/
577 : :
578 : : /**
579 : : * Macro to return the minimum of two numbers
580 : : */
581 : : #define RTE_MIN(a, b) \
582 : : __extension__ ({ \
583 : : typeof (a) _a = (a); \
584 : : typeof (b) _b = (b); \
585 : : _a < _b ? _a : _b; \
586 : : })
587 : :
588 : : /**
589 : : * Macro to return the maximum of two numbers
590 : : */
591 : : #define RTE_MAX(a, b) \
592 : : __extension__ ({ \
593 : : typeof (a) _a = (a); \
594 : : typeof (b) _b = (b); \
595 : : _a > _b ? _a : _b; \
596 : : })
597 : :
598 : : /*********** Other general functions / macros ********/
599 : :
600 : : #ifndef offsetof
601 : : /** Return the offset of a field in a structure. */
602 : : #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
603 : : #endif
604 : :
605 : : /**
606 : : * Return pointer to the wrapping struct instance.
607 : : *
608 : : * Example:
609 : : *
610 : : * struct wrapper {
611 : : * ...
612 : : * struct child c;
613 : : * ...
614 : : * };
615 : : *
616 : : * struct child *x = obtain(...);
617 : : * struct wrapper *w = container_of(x, struct wrapper, c);
618 : : */
619 : : #ifndef container_of
620 : : #ifdef RTE_TOOLCHAIN_MSVC
621 : : #define container_of(ptr, type, member) \
622 : : ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
623 : : #else
624 : : #define container_of(ptr, type, member) __extension__ ({ \
625 : : const typeof(((type *)0)->member) *_ptr = (ptr); \
626 : : __rte_unused type *_target_ptr = \
627 : : (type *)(ptr); \
628 : : (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
629 : : })
630 : : #endif
631 : : #endif
632 : :
633 : : /** Swap two variables. */
634 : : #define RTE_SWAP(a, b) \
635 : : __extension__ ({ \
636 : : typeof (a) _a = a; \
637 : : a = b; \
638 : : b = _a; \
639 : : })
640 : :
641 : : /**
642 : : * Get the size of a field in a structure.
643 : : *
644 : : * @param type
645 : : * The type of the structure.
646 : : * @param field
647 : : * The field in the structure.
648 : : * @return
649 : : * The size of the field in the structure, in bytes.
650 : : */
651 : : #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
652 : :
653 : : #define _RTE_STR(x) #x
654 : : /** Take a macro value and get a string version of it */
655 : : #define RTE_STR(x) _RTE_STR(x)
656 : :
657 : : /**
658 : : * ISO C helpers to modify format strings using variadic macros.
659 : : * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
660 : : * An empty %s argument is appended to avoid a dangling comma.
661 : : */
662 : : #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
663 : : #define RTE_FMT_HEAD(fmt, ...) fmt
664 : : #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
665 : :
666 : : /** Mask value of type "tp" for the first "ln" bit set. */
667 : : #define RTE_LEN2MASK(ln, tp) \
668 : : ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
669 : :
670 : : /** Number of elements in the array. */
671 : : #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
672 : :
673 : : /**
674 : : * Converts a numeric string to the equivalent uint64_t value.
675 : : * As well as straight number conversion, also recognises the suffixes
676 : : * k, m and g for kilobytes, megabytes and gigabytes respectively.
677 : : *
678 : : * If a negative number is passed in i.e. a string with the first non-black
679 : : * character being "-", zero is returned. Zero is also returned in the case of
680 : : * an error with the strtoull call in the function.
681 : : *
682 : : * @param str
683 : : * String containing number to convert.
684 : : * @return
685 : : * Number.
686 : : */
687 : : uint64_t
688 : : rte_str_to_size(const char *str);
689 : :
690 : : /**
691 : : * Function to terminate the application immediately, printing an error
692 : : * message and returning the exit_code back to the shell.
693 : : *
694 : : * This function never returns
695 : : *
696 : : * @param exit_code
697 : : * The exit code to be returned by the application
698 : : * @param format
699 : : * The format string to be used for printing the message. This can include
700 : : * printf format characters which will be expanded using any further parameters
701 : : * to the function.
702 : : */
703 : : __rte_noreturn void
704 : : rte_exit(int exit_code, const char *format, ...)
705 : : __rte_format_printf(2, 3);
706 : :
707 : : #ifdef __cplusplus
708 : : }
709 : : #endif
710 : :
711 : : #endif
|