universe@483: /* universe@483: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@483: * universe@483: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@483: * universe@483: * Redistribution and use in source and binary forms, with or without universe@483: * modification, are permitted provided that the following conditions are met: universe@483: * universe@483: * 1. Redistributions of source code must retain the above copyright universe@483: * notice, this list of conditions and the following disclaimer. universe@483: * universe@483: * 2. Redistributions in binary form must reproduce the above copyright universe@483: * notice, this list of conditions and the following disclaimer in the universe@483: * documentation and/or other materials provided with the distribution. universe@483: * universe@483: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@483: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@483: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@483: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@483: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@483: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@483: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@483: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@483: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@483: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@483: * POSSIBILITY OF SUCH DAMAGE. universe@483: */ universe@483: universe@483: /** universe@483: * \file utils.h universe@483: * universe@483: * \brief General purpose utility functions. universe@483: * universe@483: * \author Mike Becker universe@483: * \author Olaf Wintermann universe@483: * \copyright 2-Clause BSD License universe@483: */ universe@483: universe@483: #ifndef UCX_UTILS_H universe@483: #define UCX_UTILS_H universe@483: universe@483: #include "common.h" universe@483: universe@483: #ifdef __cplusplus universe@483: extern "C" { universe@483: #endif universe@483: universe@527: /** universe@527: * Convenience macro for a for loop that counts from zero to n-1. universe@527: */ universe@509: #define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++) universe@509: universe@646: /** universe@646: * Convenience macro for swapping two pointers. universe@646: */ universe@646: #ifdef __cplusplus universe@646: #define cx_swap_ptr(left, right) do {auto cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) universe@646: #else universe@646: #define cx_swap_ptr(left, right) do {void *cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) universe@646: #endif universe@646: universe@628: // cx_szmul() definition universe@483: universe@483: #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) universe@483: #define CX_SZMUL_BUILTIN universe@483: universe@483: /** universe@651: * Alias for \c __builtin_mul_overflow. universe@483: * universe@483: * Performs a multiplication of size_t values and checks for overflow. universe@483: * universe@483: * @param a first operand universe@483: * @param b second operand universe@483: * @param result a pointer to a size_t, where the result should universe@483: * be stored universe@483: * @return zero, if no overflow occurred and the result is correct, non-zero universe@483: * otherwise universe@483: */ universe@651: #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result) universe@483: universe@628: #else // no GNUC or clang bultin universe@483: universe@483: /** universe@483: * Performs a multiplication of size_t values and checks for overflow. universe@483: * universe@483: * @param a first operand universe@483: * @param b second operand universe@483: * @param result a pointer to a size_t, where the result should universe@483: * be stored universe@483: * @return zero, if no overflow occurred and the result is correct, non-zero universe@483: * otherwise universe@483: */ universe@483: #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result) universe@483: universe@483: /** universe@483: * Performs a multiplication of size_t values and checks for overflow. universe@483: * universe@483: * This is a custom implementation in case there is no compiler builtin universe@483: * available. universe@483: * universe@483: * @param a first operand universe@483: * @param b second operand universe@483: * @param result a pointer to a size_t where the result should be stored universe@483: * @return zero, if no overflow occurred and the result is correct, non-zero universe@483: * otherwise universe@483: */ universe@483: int cx_szmul_impl(size_t a, size_t b, size_t *result); universe@483: universe@674: #endif // cx_szmul universe@674: universe@674: universe@674: /** universe@674: * Reads data from a stream and writes it to another stream. universe@674: * universe@674: * @param src the source stream universe@674: * @param dest the destination stream universe@674: * @param rfnc the read function universe@674: * @param wfnc the write function universe@674: * @param buf a pointer to the copy buffer or \c NULL if a buffer universe@674: * shall be implicitly created on the heap universe@674: * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can universe@674: * set this to zero to let the implementation decide universe@674: * @param n the maximum number of bytes that shall be copied. universe@674: * If this is larger than \p bufsize, the content is copied over multiple universe@674: * iterations. universe@674: * @return the total number of bytes copied universe@674: */ universe@674: __attribute__((__nonnull__(1, 2, 3, 4))) universe@674: size_t cx_stream_bncopy( universe@674: void *src, universe@674: void *dest, universe@674: cx_read_func rfnc, universe@674: cx_write_func wfnc, universe@674: char *buf, universe@674: size_t bufsize, universe@674: size_t n universe@674: ); universe@674: universe@674: /** universe@674: * Reads data from a stream and writes it to another stream. universe@674: * universe@674: * @param src the source stream universe@674: * @param dest the destination stream universe@674: * @param rfnc the read function universe@674: * @param wfnc the write function universe@674: * @param buf a pointer to the copy buffer or \c NULL if a buffer universe@674: * shall be implicitly created on the heap universe@674: * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can universe@674: * set this to zero to let the implementation decide universe@674: * @return total number of bytes copied universe@674: */ universe@674: #define cx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize) \ universe@674: cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX) universe@674: universe@674: /** universe@674: * Reads data from a stream and writes it to another stream. universe@674: * universe@674: * The data is temporarily stored in a stack allocated buffer. universe@674: * universe@674: * @param src the source stream universe@674: * @param dest the destination stream universe@674: * @param rfnc the read function universe@674: * @param wfnc the write function universe@674: * @param n the maximum number of bytes that shall be copied. universe@674: * @return total number of bytes copied universe@674: */ universe@674: __attribute__((__nonnull__)) universe@674: size_t cx_stream_ncopy( universe@674: void *src, universe@674: void *dest, universe@674: cx_read_func rfnc, universe@674: cx_write_func wfnc, universe@674: size_t n universe@674: ); universe@674: universe@674: /** universe@674: * Reads data from a stream and writes it to another stream. universe@674: * universe@674: * The data is temporarily stored in a stack allocated buffer. universe@674: * universe@674: * @param src the source stream universe@674: * @param dest the destination stream universe@674: * @param rfnc the read function universe@674: * @param wfnc the write function universe@674: * @return total number of bytes copied universe@674: */ universe@674: #define cx_stream_copy(src, dest, rfnc, wfnc) \ universe@674: cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX) universe@483: universe@483: #ifdef __cplusplus universe@483: } universe@483: #endif universe@483: universe@628: #endif // UCX_UTILS_H