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