1 #ifndef BASEDEFS_H 2 #define BASEDEFS_H 3 4 /************************************************************************************************** 5 * IOWOW library 6 * 7 * MIT License 8 * 9 * Copyright (c) 2012-2021 Softmotions Ltd <info@softmotions.com> 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a copy 12 * of this software and associated documentation files (the "Software"), to deal 13 * in the Software without restriction, including without limitation the rights 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 * copies of the Software, and to permit persons to whom the Software is 16 * furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included in all 19 * copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 * SOFTWARE. 28 *************************************************************************************************/ 29 30 /** 31 * @file 32 * @brief Very basic definitions. 33 * @author Anton Adamansky (adamansky@softmotions.com) 34 */ 35 36 #ifdef __cplusplus 37 #define IW_EXTERN_C_START extern "C" { 38 #define IW_EXTERN_C_END } 39 #else 40 #define IW_EXTERN_C_START 41 #define IW_EXTERN_C_END 42 #endif 43 44 #define IW_XSTR(s) IW_STR(s) 45 #define IW_STR(s) #s 46 47 #define IW_MAX(X__, Y__) ({ __typeof__(X__) x = (X__); __typeof__(Y__) y = (Y__); x < y ? y : x; }) 48 #define IW_MIN(X__, Y__) ({ __typeof__(X__) x = (X__); __typeof__(Y__) y = (Y__); x < y ? x : y; }) 49 50 51 #if (defined(_WIN32) || defined(_WIN64)) 52 #if (defined(IW_NODLL) || defined(IW_STATIC)) 53 #define IW_EXPORT 54 #else 55 #ifdef IW_API_EXPORTS 56 #define IW_EXPORT __declspec(dllexport) 57 #else 58 #define IW_EXPORT __declspec(dllimport) 59 #endif 60 #endif 61 #else 62 #if __GNUC__ >= 4 63 #define IW_EXPORT __attribute__((visibility("default"))) 64 #else 65 #define IW_EXPORT 66 #endif 67 #endif 68 69 #if defined(__GNUC__) 70 #define IW_INLINE static inline __attribute__((always_inline)) 71 #else 72 #define IW_INLINE static inline 73 #endif 74 75 #define IW_SOFT_INLINE static inline 76 77 #if __GNUC__ >= 4 78 #define WUR __attribute__((__warn_unused_result__)) 79 #define IW_ALLOC __attribute__((malloc)) __attribute__((warn_unused_result)) 80 #else 81 #define WUR 82 #define IW_ALLOC 83 #endif 84 85 #define IW_ARR_STATIC static 86 #define IW_ARR_CONST const 87 88 #ifdef _WIN32 89 #include <windows.h> 90 #define INVALIDHANDLE(_HNDL) \ 91 (((_HNDL) == INVALID_HANDLE_VALUE) || (_HNDL) == NULL) 92 #else 93 typedef int HANDLE; 94 #define INVALID_HANDLE_VALUE (-1) 95 #define INVALIDHANDLE(_HNDL) ((_HNDL) < 0 || (_HNDL) == UINT16_MAX) 96 #endif 97 98 #define IW_ERROR_START 70000 99 100 #ifdef _WIN32 101 #define IW_PATH_CHR '\\' 102 #define IW_PATH_STR "\\" 103 #define IW_LINE_SEP "\r\n" 104 #else 105 #define IW_PATH_CHR '/' 106 #define IW_PATH_STR "/" 107 #define IW_LINE_SEP "\n" 108 #endif 109 110 #define ZGO(label__, val__) \ 111 ({ __typeof__(val__) v__ = (val__); \ 112 if (!v__) goto label__; \ 113 v__; }) 114 115 #define ZRET(ret__, val__) \ 116 ({ __typeof__(val__) v__ = (val__); \ 117 if (!v__) return ret__; \ 118 v__; }) 119 120 #ifdef __GNUC__ 121 #define RCGO(rc__, label__) if (__builtin_expect((!!(rc__)), 0)) goto label__ 122 #else 123 #define RCGO(rc__, label__) if (rc__) goto label__ 124 #endif 125 126 #define RCIF(res__, rc__, rcv__, label__) \ 127 if (res__) { \ 128 rc__ = (rcv__); \ 129 goto label__; \ 130 } 131 132 #define RCHECK(rc__, label__, expr__) \ 133 rc__ = expr__; \ 134 RCGO(rc__, label__) 135 136 #define RCC(rc__, label__, expr__) RCHECK(rc__, label__, expr__) 137 138 #ifndef RCGA 139 #define RCGA(v__, label__) \ 140 if (!(v__)) { \ 141 rc = iwrc_set_errno(IW_ERROR_ALLOC, errno); \ 142 goto label__; \ 143 } 144 #endif 145 146 #ifndef RCA 147 #define RCA(v__, label__) RCGA(v__, label__) 148 #endif 149 150 #ifndef RCB 151 #define RCB(label__, v__) RCGA(v__, label__) 152 #endif 153 154 #ifndef RCN 155 #define RCN(label__, v__) \ 156 if ((v__) < 0) { \ 157 rc = iwrc_set_errno(IW_ERROR_ERRNO, errno); \ 158 goto label__; \ 159 } 160 #endif 161 162 #ifndef RCT 163 #define RCT(label__, val__) \ 164 ({ __typeof__(val__) v__ = (val__); \ 165 if (v__) { \ 166 rc = iwrc_set_errno(IW_ERROR_THREADING_ERRNO, v__); \ 167 goto label__; \ 168 } \ 169 }) 170 #endif 171 172 #ifdef __GNUC__ 173 #define RCRET(rc__) if (__builtin_expect((!!(rc__)), 0)) return (rc__) 174 #else 175 #define RCRET(rc__) if (rc__) return (rc__) 176 #endif 177 178 #define RCR(expr__) \ 179 ({ iwrc rc__ = (expr__); RCRET(rc__); 0; }) 180 181 #ifdef __GNUC__ 182 #define RCBREAK(rc__) if (__builtin_expect((!!(rc__)), 0)) break 183 #else 184 #define RCBREAK(rc__) if (rc__) break 185 #endif 186 187 #ifdef __GNUC__ 188 #define RCONT(rc__) if (__builtin_expect((!!(rc__)), 0)) continue 189 #else 190 #define RCONT(rc__) if (rc__) continue 191 #endif 192 193 #ifndef MIN 194 #define MIN(a_, b_) ((a_) < (b_) ? (a_) : (b_)) 195 #endif 196 197 #ifndef MAX 198 #define MAX(a_, b_) ((a_) > (b_) ? (a_) : (b_)) 199 #endif 200 201 #include <stdint.h> 202 #include <stddef.h> 203 #include <stdbool.h> 204 #include <sys/types.h> 205 206 #ifdef _WIN32 207 typedef _locale_t locale_t; 208 #endif 209 210 #if defined(__GNUC__) || defined(__clang__) 211 #define IW_DEPRECATED __attribute__((deprecated)) 212 #elif defined(_MSC_VER) 213 #define IW_DEPRECATED __declspec(deprecated) 214 #else 215 #define IW_DEPRECATED 216 #endif 217 218 /** 219 * @brief The operation result status code. 220 * 221 * Zero status code `0` indicates <em>operation success</em> 222 * 223 * Status code can embed an `errno` code as operation result. 224 * In this case `uint32_t iwrc_strip_errno(iwrc *rc)` used 225 * to fetch embedded errno. 226 * 227 * @see iwlog.h 228 */ 229 typedef uint64_t iwrc; 230 231 /** 232 * @brief A rational number. 233 */ 234 typedef struct IW_RNUM { 235 int32_t n; /**< Numerator */ 236 int32_t dn; /**< Denometator */ 237 } IW_RNUM; 238 239 #endif 240