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