Thu, 28 Dec 2023 19:17:45 +0100
fix accidental generation of cxListIterator() symbol
universe@483 | 1 | /* |
universe@483 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@483 | 3 | * |
universe@483 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@483 | 5 | * |
universe@483 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@483 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@483 | 8 | * |
universe@483 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@483 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@483 | 11 | * |
universe@483 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@483 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@483 | 14 | * documentation and/or other materials provided with the distribution. |
universe@483 | 15 | * |
universe@483 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@483 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@483 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@483 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@483 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@483 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@483 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@483 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@483 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@483 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@483 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@483 | 27 | */ |
universe@483 | 28 | |
universe@483 | 29 | /** |
universe@483 | 30 | * \file utils.h |
universe@483 | 31 | * |
universe@483 | 32 | * \brief General purpose utility functions. |
universe@483 | 33 | * |
universe@483 | 34 | * \author Mike Becker |
universe@483 | 35 | * \author Olaf Wintermann |
universe@483 | 36 | * \copyright 2-Clause BSD License |
universe@483 | 37 | */ |
universe@483 | 38 | |
universe@483 | 39 | #ifndef UCX_UTILS_H |
universe@483 | 40 | #define UCX_UTILS_H |
universe@483 | 41 | |
universe@483 | 42 | #include "common.h" |
universe@483 | 43 | |
universe@483 | 44 | #ifdef __cplusplus |
universe@483 | 45 | extern "C" { |
universe@483 | 46 | #endif |
universe@483 | 47 | |
universe@527 | 48 | /** |
universe@527 | 49 | * Convenience macro for a for loop that counts from zero to n-1. |
universe@527 | 50 | */ |
universe@509 | 51 | #define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++) |
universe@509 | 52 | |
universe@646 | 53 | /** |
universe@646 | 54 | * Convenience macro for swapping two pointers. |
universe@646 | 55 | */ |
universe@646 | 56 | #ifdef __cplusplus |
universe@646 | 57 | #define cx_swap_ptr(left, right) do {auto cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) |
universe@646 | 58 | #else |
universe@646 | 59 | #define cx_swap_ptr(left, right) do {void *cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) |
universe@646 | 60 | #endif |
universe@646 | 61 | |
universe@628 | 62 | // cx_szmul() definition |
universe@483 | 63 | |
universe@483 | 64 | #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) |
universe@483 | 65 | #define CX_SZMUL_BUILTIN |
universe@483 | 66 | |
universe@483 | 67 | /** |
universe@651 | 68 | * Alias for \c __builtin_mul_overflow. |
universe@483 | 69 | * |
universe@483 | 70 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 71 | * |
universe@483 | 72 | * @param a first operand |
universe@483 | 73 | * @param b second operand |
universe@483 | 74 | * @param result a pointer to a size_t, where the result should |
universe@483 | 75 | * be stored |
universe@483 | 76 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 77 | * otherwise |
universe@483 | 78 | */ |
universe@651 | 79 | #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result) |
universe@483 | 80 | |
universe@628 | 81 | #else // no GNUC or clang bultin |
universe@483 | 82 | |
universe@483 | 83 | /** |
universe@483 | 84 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 85 | * |
universe@483 | 86 | * @param a first operand |
universe@483 | 87 | * @param b second operand |
universe@483 | 88 | * @param result a pointer to a size_t, where the result should |
universe@483 | 89 | * be stored |
universe@483 | 90 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 91 | * otherwise |
universe@483 | 92 | */ |
universe@483 | 93 | #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result) |
universe@483 | 94 | |
universe@483 | 95 | /** |
universe@483 | 96 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 97 | * |
universe@483 | 98 | * This is a custom implementation in case there is no compiler builtin |
universe@483 | 99 | * available. |
universe@483 | 100 | * |
universe@483 | 101 | * @param a first operand |
universe@483 | 102 | * @param b second operand |
universe@483 | 103 | * @param result a pointer to a size_t where the result should be stored |
universe@483 | 104 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 105 | * otherwise |
universe@483 | 106 | */ |
universe@483 | 107 | int cx_szmul_impl(size_t a, size_t b, size_t *result); |
universe@483 | 108 | |
universe@674 | 109 | #endif // cx_szmul |
universe@674 | 110 | |
universe@674 | 111 | |
universe@674 | 112 | /** |
universe@674 | 113 | * Reads data from a stream and writes it to another stream. |
universe@674 | 114 | * |
universe@674 | 115 | * @param src the source stream |
universe@674 | 116 | * @param dest the destination stream |
universe@674 | 117 | * @param rfnc the read function |
universe@674 | 118 | * @param wfnc the write function |
universe@674 | 119 | * @param buf a pointer to the copy buffer or \c NULL if a buffer |
universe@674 | 120 | * shall be implicitly created on the heap |
universe@674 | 121 | * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can |
universe@674 | 122 | * set this to zero to let the implementation decide |
universe@674 | 123 | * @param n the maximum number of bytes that shall be copied. |
universe@674 | 124 | * If this is larger than \p bufsize, the content is copied over multiple |
universe@674 | 125 | * iterations. |
universe@674 | 126 | * @return the total number of bytes copied |
universe@674 | 127 | */ |
universe@674 | 128 | __attribute__((__nonnull__(1, 2, 3, 4))) |
universe@674 | 129 | size_t cx_stream_bncopy( |
universe@674 | 130 | void *src, |
universe@674 | 131 | void *dest, |
universe@674 | 132 | cx_read_func rfnc, |
universe@674 | 133 | cx_write_func wfnc, |
universe@674 | 134 | char *buf, |
universe@674 | 135 | size_t bufsize, |
universe@674 | 136 | size_t n |
universe@674 | 137 | ); |
universe@674 | 138 | |
universe@674 | 139 | /** |
universe@674 | 140 | * Reads data from a stream and writes it to another stream. |
universe@674 | 141 | * |
universe@674 | 142 | * @param src the source stream |
universe@674 | 143 | * @param dest the destination stream |
universe@674 | 144 | * @param rfnc the read function |
universe@674 | 145 | * @param wfnc the write function |
universe@674 | 146 | * @param buf a pointer to the copy buffer or \c NULL if a buffer |
universe@674 | 147 | * shall be implicitly created on the heap |
universe@674 | 148 | * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can |
universe@674 | 149 | * set this to zero to let the implementation decide |
universe@674 | 150 | * @return total number of bytes copied |
universe@674 | 151 | */ |
universe@674 | 152 | #define cx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize) \ |
universe@674 | 153 | cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX) |
universe@674 | 154 | |
universe@674 | 155 | /** |
universe@674 | 156 | * Reads data from a stream and writes it to another stream. |
universe@674 | 157 | * |
universe@674 | 158 | * The data is temporarily stored in a stack allocated buffer. |
universe@674 | 159 | * |
universe@674 | 160 | * @param src the source stream |
universe@674 | 161 | * @param dest the destination stream |
universe@674 | 162 | * @param rfnc the read function |
universe@674 | 163 | * @param wfnc the write function |
universe@674 | 164 | * @param n the maximum number of bytes that shall be copied. |
universe@674 | 165 | * @return total number of bytes copied |
universe@674 | 166 | */ |
universe@674 | 167 | __attribute__((__nonnull__)) |
universe@674 | 168 | size_t cx_stream_ncopy( |
universe@674 | 169 | void *src, |
universe@674 | 170 | void *dest, |
universe@674 | 171 | cx_read_func rfnc, |
universe@674 | 172 | cx_write_func wfnc, |
universe@674 | 173 | size_t n |
universe@674 | 174 | ); |
universe@674 | 175 | |
universe@674 | 176 | /** |
universe@674 | 177 | * Reads data from a stream and writes it to another stream. |
universe@674 | 178 | * |
universe@674 | 179 | * The data is temporarily stored in a stack allocated buffer. |
universe@674 | 180 | * |
universe@674 | 181 | * @param src the source stream |
universe@674 | 182 | * @param dest the destination stream |
universe@674 | 183 | * @param rfnc the read function |
universe@674 | 184 | * @param wfnc the write function |
universe@674 | 185 | * @return total number of bytes copied |
universe@674 | 186 | */ |
universe@674 | 187 | #define cx_stream_copy(src, dest, rfnc, wfnc) \ |
universe@674 | 188 | cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX) |
universe@483 | 189 | |
universe@483 | 190 | #ifdef __cplusplus |
universe@483 | 191 | } |
universe@483 | 192 | #endif |
universe@483 | 193 | |
universe@628 | 194 | #endif // UCX_UTILS_H |