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