25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 /** |
29 /** |
30 * \file common.h |
30 * @file common.h |
31 * |
31 * |
32 * \brief Common definitions and feature checks. |
32 * @brief Common definitions and feature checks. |
33 * |
33 * |
34 * \author Mike Becker |
34 * @author Mike Becker |
35 * \author Olaf Wintermann |
35 * @author Olaf Wintermann |
36 * \copyright 2-Clause BSD License |
36 * @copyright 2-Clause BSD License |
37 * |
37 * |
38 * \mainpage UAP Common Extensions |
38 * @mainpage UAP Common Extensions |
39 * Library with common and useful functions, macros and data structures. |
39 * Library with common and useful functions, macros and data structures. |
40 * <p> |
40 * <p> |
41 * Latest available source:<br> |
41 * Latest available source:<br> |
42 * <a href="https://sourceforge.net/projects/ucx/files/">https://sourceforge.net/projects/ucx/files/</a> |
42 * <a href="https://sourceforge.net/projects/ucx/files/">https://sourceforge.net/projects/ucx/files/</a> |
43 * </p> |
43 * </p> |
100 |
100 |
101 // --------------------------------------------------------------------------- |
101 // --------------------------------------------------------------------------- |
102 // Architecture Detection |
102 // Architecture Detection |
103 // --------------------------------------------------------------------------- |
103 // --------------------------------------------------------------------------- |
104 |
104 |
|
105 #ifndef INTPTR_MAX |
|
106 #error Missing INTPTR_MAX definition |
|
107 #endif |
105 #if INTPTR_MAX == INT64_MAX |
108 #if INTPTR_MAX == INT64_MAX |
106 /** |
109 /** |
107 * The address width in bits on this platform. |
110 * The address width in bits on this platform. |
108 */ |
111 */ |
109 #define CX_WORDSIZE 64 |
112 #define CX_WORDSIZE 64 |
161 #define cx_attr_malloc __attribute__((__malloc__)) |
167 #define cx_attr_malloc __attribute__((__malloc__)) |
162 |
168 |
163 #ifndef __clang__ |
169 #ifndef __clang__ |
164 /** |
170 /** |
165 * The pointer returned by the attributed function is supposed to be freed |
171 * The pointer returned by the attributed function is supposed to be freed |
166 * by \p freefunc. |
172 * by @p freefunc. |
167 * |
173 * |
168 * @param freefunc the function that shall be used to free the memory |
174 * @param freefunc the function that shall be used to free the memory |
169 * @param freefunc_arg the index of the pointer argument in \p freefunc |
175 * @param freefunc_arg the index of the pointer argument in @p freefunc |
170 */ |
176 */ |
171 #define cx_attr_dealloc(freefunc, freefunc_arg) \ |
177 #define cx_attr_dealloc(freefunc, freefunc_arg) \ |
172 __attribute__((__malloc__(freefunc, freefunc_arg))) |
178 __attribute__((__malloc__(freefunc, freefunc_arg))) |
173 #else |
179 #else |
174 /** |
180 /** |
319 // szmul implementation |
325 // szmul implementation |
320 // --------------------------------------------------------------------------- |
326 // --------------------------------------------------------------------------- |
321 |
327 |
322 #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) |
328 #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) |
323 #define CX_SZMUL_BUILTIN |
329 #define CX_SZMUL_BUILTIN |
324 |
330 #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result) |
325 /** |
331 #else // no GNUC or clang bultin |
326 * Alias for \c __builtin_mul_overflow. |
332 /** |
327 * |
|
328 * Performs a multiplication of size_t values and checks for overflow. |
333 * Performs a multiplication of size_t values and checks for overflow. |
|
334 * |
|
335 * @param a (@c size_t) first operand |
|
336 * @param b (@c size_t) second operand |
|
337 * @param result (@c size_t*) a pointer to a variable, where the result should |
|
338 * be stored |
|
339 * @retval zero success |
|
340 * @retval non-zero the multiplication would overflow |
|
341 */ |
|
342 #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result) |
|
343 |
|
344 /** |
|
345 * Implementation of cx_szmul() when no compiler builtin is available. |
|
346 * |
|
347 * Do not use in application code. |
329 * |
348 * |
330 * @param a first operand |
349 * @param a first operand |
331 * @param b second operand |
350 * @param b second operand |
332 * @param result a pointer to a size_t, where the result should |
351 * @param result a pointer to a variable, where the result should |
333 * be stored |
352 * be stored |
334 * @return zero, if no overflow occurred and the result is correct, non-zero |
353 * @retval zero success |
335 * otherwise |
354 * @retval non-zero the multiplication would overflow |
336 */ |
|
337 #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result) |
|
338 |
|
339 #else // no GNUC or clang bultin |
|
340 |
|
341 /** |
|
342 * Performs a multiplication of size_t values and checks for overflow. |
|
343 * |
|
344 * @param a first operand |
|
345 * @param b second operand |
|
346 * @param result a pointer to a size_t, where the result should |
|
347 * be stored |
|
348 * @return zero, if no overflow occurred and the result is correct, non-zero |
|
349 * otherwise |
|
350 */ |
|
351 #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result) |
|
352 |
|
353 /** |
|
354 * Performs a multiplication of size_t values and checks for overflow. |
|
355 * |
|
356 * This is a custom implementation in case there is no compiler builtin |
|
357 * available. |
|
358 * |
|
359 * @param a first operand |
|
360 * @param b second operand |
|
361 * @param result a pointer to a size_t where the result should be stored |
|
362 * @return zero, if no overflow occurred and the result is correct, non-zero |
|
363 * otherwise |
|
364 */ |
355 */ |
365 #if __cplusplus |
356 #if __cplusplus |
366 extern "C" |
357 extern "C" |
367 #endif |
358 #endif |
368 int cx_szmul_impl(size_t a, size_t b, size_t *result); |
359 int cx_szmul_impl(size_t a, size_t b, size_t *result); |
369 |
|
370 #endif // cx_szmul |
360 #endif // cx_szmul |
371 |
361 |
372 |
362 |
373 // --------------------------------------------------------------------------- |
363 // --------------------------------------------------------------------------- |
374 // Fixes for MSVC incompatibilities |
364 // Fixes for MSVC incompatibilities |