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