lib: use SPDX tag for Intel copyright files
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7
8 /**
9  * @file
10  *
11  * Generic, commonly-used macro and inline function definitions
12  * for DPDK.
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #ifndef typeof
26 #define typeof __typeof__
27 #endif
28
29 #ifndef asm
30 #define asm __asm__
31 #endif
32
33 /** C extension macro for environments lacking C11 features. */
34 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
35 #define RTE_STD_C11 __extension__
36 #else
37 #define RTE_STD_C11
38 #endif
39
40 /** Define GCC_VERSION **/
41 #ifdef RTE_TOOLCHAIN_GCC
42 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
43                 __GNUC_PATCHLEVEL__)
44 #endif
45
46 #ifdef RTE_ARCH_STRICT_ALIGN
47 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
48 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
49 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
50 #else
51 typedef uint64_t unaligned_uint64_t;
52 typedef uint32_t unaligned_uint32_t;
53 typedef uint16_t unaligned_uint16_t;
54 #endif
55
56 /**
57  * Force alignment
58  */
59 #define __rte_aligned(a) __attribute__((__aligned__(a)))
60
61 /**
62  * Force a structure to be packed
63  */
64 #define __rte_packed __attribute__((__packed__))
65
66 /******* Macro to mark functions and fields scheduled for removal *****/
67 #define __rte_deprecated        __attribute__((__deprecated__))
68
69 /*********** Macros to eliminate unused variable warnings ********/
70
71 /**
72  * short definition to mark a function parameter unused
73  */
74 #define __rte_unused __attribute__((__unused__))
75
76 /**
77  * definition to mark a variable or function parameter as used so
78  * as to avoid a compiler warning
79  */
80 #define RTE_SET_USED(x) (void)(x)
81
82 /**
83  * Run function before main() with low priority.
84  *
85  * The constructor will be run after prioritized constructors.
86  *
87  * @param func
88  *   Constructor function.
89  */
90 #define RTE_INIT(func) \
91 static void __attribute__((constructor, used)) func(void)
92
93 /**
94  * Run function before main() with high priority.
95  *
96  * @param func
97  *   Constructor function.
98  * @param prio
99  *   Priority number must be above 100.
100  *   Lowest number is the first to run.
101  */
102 #define RTE_INIT_PRIO(func, prio) \
103 static void __attribute__((constructor(prio), used)) func(void)
104
105 /**
106  * Force a function to be inlined
107  */
108 #define __rte_always_inline inline __attribute__((always_inline))
109
110 /**
111  * Force a function to be noinlined
112  */
113 #define __rte_noinline  __attribute__((noinline))
114
115 /*********** Macros for pointer arithmetic ********/
116
117 /**
118  * add a byte-value offset from a pointer
119  */
120 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
121
122 /**
123  * subtract a byte-value offset from a pointer
124  */
125 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
126
127 /**
128  * get the difference between two pointer values, i.e. how far apart
129  * in bytes are the locations they point two. It is assumed that
130  * ptr1 is greater than ptr2.
131  */
132 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
133
134 /*********** Macros/static functions for doing alignment ********/
135
136
137 /**
138  * Macro to align a pointer to a given power-of-two. The resultant
139  * pointer will be a pointer of the same type as the first parameter, and
140  * point to an address no higher than the first parameter. Second parameter
141  * must be a power-of-two value.
142  */
143 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
144         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
145
146 /**
147  * Macro to align a value to a given power-of-two. The resultant value
148  * will be of the same type as the first parameter, and will be no
149  * bigger than the first parameter. Second parameter must be a
150  * power-of-two value.
151  */
152 #define RTE_ALIGN_FLOOR(val, align) \
153         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
154
155 /**
156  * Macro to align a pointer to a given power-of-two. The resultant
157  * pointer will be a pointer of the same type as the first parameter, and
158  * point to an address no lower than the first parameter. Second parameter
159  * must be a power-of-two value.
160  */
161 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
162         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
163
164 /**
165  * Macro to align a value to a given power-of-two. The resultant value
166  * will be of the same type as the first parameter, and will be no lower
167  * than the first parameter. Second parameter must be a power-of-two
168  * value.
169  */
170 #define RTE_ALIGN_CEIL(val, align) \
171         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
172
173 /**
174  * Macro to align a pointer to a given power-of-two. The resultant
175  * pointer will be a pointer of the same type as the first parameter, and
176  * point to an address no lower than the first parameter. Second parameter
177  * must be a power-of-two value.
178  * This function is the same as RTE_PTR_ALIGN_CEIL
179  */
180 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
181
182 /**
183  * Macro to align a value to a given power-of-two. The resultant
184  * value will be of the same type as the first parameter, and
185  * will be no lower than the first parameter. Second parameter
186  * must be a power-of-two value.
187  * This function is the same as RTE_ALIGN_CEIL
188  */
189 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
190
191 /**
192  * Checks if a pointer is aligned to a given power-of-two value
193  *
194  * @param ptr
195  *   The pointer whose alignment is to be checked
196  * @param align
197  *   The power-of-two value to which the ptr should be aligned
198  *
199  * @return
200  *   True(1) where the pointer is correctly aligned, false(0) otherwise
201  */
202 static inline int
203 rte_is_aligned(void *ptr, unsigned align)
204 {
205         return RTE_PTR_ALIGN(ptr, align) == ptr;
206 }
207
208 /*********** Macros for compile type checks ********/
209
210 /**
211  * Triggers an error at compilation time if the condition is true.
212  */
213 #ifndef __OPTIMIZE__
214 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
215 #else
216 extern int RTE_BUILD_BUG_ON_detected_error;
217 #define RTE_BUILD_BUG_ON(condition) do {             \
218         ((void)sizeof(char[1 - 2*!!(condition)]));   \
219         if (condition)                               \
220                 RTE_BUILD_BUG_ON_detected_error = 1; \
221 } while(0)
222 #endif
223
224 /*********** Macros to work with powers of 2 ********/
225
226 /**
227  * Returns true if n is a power of 2
228  * @param n
229  *     Number to check
230  * @return 1 if true, 0 otherwise
231  */
232 static inline int
233 rte_is_power_of_2(uint32_t n)
234 {
235         return n && !(n & (n - 1));
236 }
237
238 /**
239  * Aligns input parameter to the next power of 2
240  *
241  * @param x
242  *   The integer value to algin
243  *
244  * @return
245  *   Input parameter aligned to the next power of 2
246  */
247 static inline uint32_t
248 rte_align32pow2(uint32_t x)
249 {
250         x--;
251         x |= x >> 1;
252         x |= x >> 2;
253         x |= x >> 4;
254         x |= x >> 8;
255         x |= x >> 16;
256
257         return x + 1;
258 }
259
260 /**
261  * Aligns 64b input parameter to the next power of 2
262  *
263  * @param v
264  *   The 64b value to align
265  *
266  * @return
267  *   Input parameter aligned to the next power of 2
268  */
269 static inline uint64_t
270 rte_align64pow2(uint64_t v)
271 {
272         v--;
273         v |= v >> 1;
274         v |= v >> 2;
275         v |= v >> 4;
276         v |= v >> 8;
277         v |= v >> 16;
278         v |= v >> 32;
279
280         return v + 1;
281 }
282
283 /*********** Macros for calculating min and max **********/
284
285 /**
286  * Macro to return the minimum of two numbers
287  */
288 #define RTE_MIN(a, b) \
289         __extension__ ({ \
290                 typeof (a) _a = (a); \
291                 typeof (b) _b = (b); \
292                 _a < _b ? _a : _b; \
293         })
294
295 /**
296  * Macro to return the maximum of two numbers
297  */
298 #define RTE_MAX(a, b) \
299         __extension__ ({ \
300                 typeof (a) _a = (a); \
301                 typeof (b) _b = (b); \
302                 _a > _b ? _a : _b; \
303         })
304
305 /*********** Other general functions / macros ********/
306
307 /**
308  * Searches the input parameter for the least significant set bit
309  * (starting from zero).
310  * If a least significant 1 bit is found, its bit index is returned.
311  * If the content of the input parameter is zero, then the content of the return
312  * value is undefined.
313  * @param v
314  *     input parameter, should not be zero.
315  * @return
316  *     least significant set bit in the input parameter.
317  */
318 static inline uint32_t
319 rte_bsf32(uint32_t v)
320 {
321         return __builtin_ctz(v);
322 }
323
324 /**
325  * Return the rounded-up log2 of a integer.
326  *
327  * @param v
328  *     The input parameter.
329  * @return
330  *     The rounded-up log2 of the input, or 0 if the input is 0.
331  */
332 static inline uint32_t
333 rte_log2_u32(uint32_t v)
334 {
335         if (v == 0)
336                 return 0;
337         v = rte_align32pow2(v);
338         return rte_bsf32(v);
339 }
340
341 #ifndef offsetof
342 /** Return the offset of a field in a structure. */
343 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
344 #endif
345
346 /**
347  * Return pointer to the wrapping struct instance.
348  *
349  * Example:
350  *
351  *  struct wrapper {
352  *      ...
353  *      struct child c;
354  *      ...
355  *  };
356  *
357  *  struct child *x = obtain(...);
358  *  struct wrapper *w = container_of(x, struct wrapper, c);
359  */
360 #ifndef container_of
361 #define container_of(ptr, type, member) __extension__ ({                \
362                         const typeof(((type *)0)->member) *_ptr = (ptr); \
363                         __attribute__((unused)) type *_target_ptr =     \
364                                 (type *)(ptr);                          \
365                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
366                 })
367 #endif
368
369 #define _RTE_STR(x) #x
370 /** Take a macro value and get a string version of it */
371 #define RTE_STR(x) _RTE_STR(x)
372
373 /**
374  * ISO C helpers to modify format strings using variadic macros.
375  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
376  * An empty %s argument is appended to avoid a dangling comma.
377  */
378 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
379 #define RTE_FMT_HEAD(fmt, ...) fmt
380 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
381
382 /** Mask value of type "tp" for the first "ln" bit set. */
383 #define RTE_LEN2MASK(ln, tp)    \
384         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
385
386 /** Number of elements in the array. */
387 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
388
389 /**
390  * Converts a numeric string to the equivalent uint64_t value.
391  * As well as straight number conversion, also recognises the suffixes
392  * k, m and g for kilobytes, megabytes and gigabytes respectively.
393  *
394  * If a negative number is passed in  i.e. a string with the first non-black
395  * character being "-", zero is returned. Zero is also returned in the case of
396  * an error with the strtoull call in the function.
397  *
398  * @param str
399  *     String containing number to convert.
400  * @return
401  *     Number.
402  */
403 static inline uint64_t
404 rte_str_to_size(const char *str)
405 {
406         char *endptr;
407         unsigned long long size;
408
409         while (isspace((int)*str))
410                 str++;
411         if (*str == '-')
412                 return 0;
413
414         errno = 0;
415         size = strtoull(str, &endptr, 0);
416         if (errno)
417                 return 0;
418
419         if (*endptr == ' ')
420                 endptr++; /* allow 1 space gap */
421
422         switch (*endptr){
423         case 'G': case 'g': size *= 1024; /* fall-through */
424         case 'M': case 'm': size *= 1024; /* fall-through */
425         case 'K': case 'k': size *= 1024; /* fall-through */
426         default:
427                 break;
428         }
429         return size;
430 }
431
432 /**
433  * Function to terminate the application immediately, printing an error
434  * message and returning the exit_code back to the shell.
435  *
436  * This function never returns
437  *
438  * @param exit_code
439  *     The exit code to be returned by the application
440  * @param format
441  *     The format string to be used for printing the message. This can include
442  *     printf format characters which will be expanded using any further parameters
443  *     to the function.
444  */
445 void
446 rte_exit(int exit_code, const char *format, ...)
447         __attribute__((noreturn))
448         __attribute__((format(printf, 2, 3)));
449
450 #ifdef __cplusplus
451 }
452 #endif
453
454 #endif