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