universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@5: * universe@363: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@5: */ universe@114: /** universe@114: * Main UCX Header providing most common definitions. universe@114: * universe@114: * @file ucx.h universe@115: * @author Mike Becker universe@114: * @author Olaf Wintermann universe@114: */ olaf@5: olaf@5: #ifndef UCX_H olaf@5: #define UCX_H olaf@5: universe@151: /** Major UCX version as integer constant. */ universe@306: #define UCX_VERSION_MAJOR 2 universe@151: universe@151: /** Minor UCX version as integer constant. */ universe@334: #define UCX_VERSION_MINOR 1 universe@259: universe@259: /** Version constant which ensures to increase monotonically. */ universe@259: #define UCX_VERSION (((UCX_VERSION_MAJOR)<<16)|UCX_VERSION_MINOR) universe@151: olaf@5: #include universe@243: #include olaf@5: universe@127: #ifdef _WIN32 universe@132: #if !(defined __ssize_t_defined || defined _SSIZE_T_) universe@127: #include universe@127: typedef SSIZE_T ssize_t; universe@127: #define __ssize_t_defined universe@132: #define _SSIZE_T_ universe@132: #endif /* __ssize_t_defined and _SSIZE_T */ universe@127: #else /* !_WIN32 */ universe@127: #include universe@127: #endif /* _WIN32 */ universe@127: olaf@5: #ifdef __cplusplus olaf@5: extern "C" { olaf@5: #endif universe@209: universe@209: universe@209: /** universe@209: * A function pointer to a destructor function. universe@209: * @see ucx_mempool_setdestr() universe@209: * @see ucx_mempool_regdestr() universe@209: */ universe@209: typedef void(*ucx_destructor)(void*); olaf@5: universe@114: /** universe@114: * Function pointer to a compare function. universe@114: * universe@114: * The compare function shall take three arguments: the two values that shall be universe@114: * compared and optional additional data. universe@114: * The function shall then return -1 if the first argument is less than the universe@114: * second argument, 1 if the first argument is greater than the second argument universe@114: * and 0 if both arguments are equal. If the third argument is universe@114: * NULL, it shall be ignored. universe@114: */ universe@244: typedef int(*cmp_func)(const void*,const void*,void*); universe@18: universe@114: /** universe@243: * Function pointer to a distance function. universe@243: * universe@243: * The distance function shall take three arguments: the two values for which universe@243: * the distance shall be computed and optional additional data. universe@243: * The function shall then return the signed distance as integer value. universe@243: */ universe@244: typedef intmax_t(*distance_func)(const void*,const void*,void*); universe@243: universe@243: /** universe@114: * Function pointer to a copy function. universe@114: * universe@114: * The copy function shall create a copy of the first argument and may use universe@114: * additional data provided by the second argument. If the second argument is universe@114: * NULL, it shall be ignored. universe@114: universe@114: * Attention: if pointers returned by functions of this type may be universe@114: * passed to free() depends on the implementation of the universe@114: * respective copy_func. universe@114: */ universe@244: typedef void*(*copy_func)(const void*,void*); universe@18: universe@114: /** universe@114: * Function pointer to a write function. universe@114: * universe@114: * The signature of the write function shall be compatible to the signature universe@114: * of standard fwrite, though it may use arbitrary data types for universe@114: * source and destination. universe@114: * universe@114: * The arguments shall contain (in ascending order): a pointer to the source, universe@116: * the length of one element, the element count and a pointer to the universe@116: * destination. universe@114: */ olaf@76: typedef size_t(*write_func)(const void*, size_t, size_t, void*); olaf@76: universe@114: /** universe@114: * Function pointer to a read function. universe@114: * universe@114: * The signature of the read function shall be compatible to the signature universe@114: * of standard fread, though it may use arbitrary data types for universe@114: * source and destination. universe@114: * universe@114: * The arguments shall contain (in ascending order): a pointer to the universe@116: * destination, the length of one element, the element count and a pointer to universe@116: * the source. universe@114: */ olaf@76: typedef size_t(*read_func)(void*, size_t, size_t, void*); olaf@76: olaf@270: olaf@270: olaf@331: #if __GNUC__ >= 5 || defined(__clang__) olaf@270: #define UCX_MUL_BUILTIN universe@303: universe@303: #if __WORDSIZE == 32 universe@303: /** universe@303: * Alias for __builtin_umul_overflow. universe@303: * universe@303: * Performs a multiplication of size_t values and checks for overflow. universe@303: * universe@303: * @param a first operand universe@303: * @param b second operand universe@303: * @param result a pointer to a size_t, where the result should universe@303: * be stored universe@303: * @return zero, if no overflow occurred and the result is correct, non-zero universe@303: * otherwise universe@303: */ universe@303: #define ucx_szmul(a, b, result) __builtin_umul_overflow(a, b, result) universe@303: #else /* __WORDSIZE != 32 */ universe@303: /** universe@303: * Alias for __builtin_umull_overflow. universe@303: * universe@303: * Performs a multiplication of size_t values and checks for overflow. universe@303: * universe@303: * @param a first operand universe@303: * @param b second operand universe@303: * @param result a pointer to a size_t, where the result should universe@303: * be stored universe@303: * @return zero, if no overflow occurred and the result is correct, non-zero universe@303: * otherwise universe@303: */ olaf@270: #define ucx_szmul(a, b, result) __builtin_umull_overflow(a, b, result) universe@303: #endif /* __WORDSIZE */ universe@303: universe@303: #else /* no GNUC or clang bultin */ universe@303: universe@303: /** universe@303: * Performs a multiplication of size_t values and checks for overflow. universe@373: * universe@303: * @param a first operand universe@303: * @param b second operand universe@303: * @param result a pointer to a size_t, where the result should universe@303: * be stored universe@303: * @return zero, if no overflow occurred and the result is correct, non-zero universe@303: * otherwise universe@303: */ olaf@331: #define ucx_szmul(a, b, result) ucx_szmul_impl(a, b, result) olaf@331: universe@373: /** universe@373: * Performs a multiplication of size_t values and checks for overflow. universe@373: * universe@373: * This is a custom implementation in case there is no compiler builtin universe@373: * available. universe@373: * universe@373: * @param a first operand universe@373: * @param b second operand universe@373: * @param result a pointer to a size_t where the result should be stored universe@373: * @return zero, if no overflow occurred and the result is correct, non-zero universe@373: * otherwise universe@373: */ olaf@331: int ucx_szmul_impl(size_t a, size_t b, size_t *result); universe@303: olaf@270: #endif olaf@270: olaf@5: #ifdef __cplusplus olaf@5: } olaf@5: #endif olaf@5: olaf@5: #endif /* UCX_H */ olaf@5: