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