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