ucx/cx/common.h

changeset 888
af685cc9d623
parent 854
1c8401ece69e
equal deleted inserted replaced
877:b60487c3ec36 888:af685cc9d623
44 * 44 *
45 * <p> 45 * <p>
46 * Repositories:<br> 46 * Repositories:<br>
47 * <a href="https://sourceforge.net/p/ucx/code">https://sourceforge.net/p/ucx/code</a> 47 * <a href="https://sourceforge.net/p/ucx/code">https://sourceforge.net/p/ucx/code</a>
48 * -&nbsp;or&nbsp;- 48 * -&nbsp;or&nbsp;-
49 * <a href="https://develop.uap-core.de/hg/ucx">https://develop.uap-core.de/hg/ucx</a> 49 * <a href="https://uap-core.de/hg/ucx">https://uap-core.de/hg/ucx</a>
50 * </p> 50 * </p>
51 * 51 *
52 * <h2>LICENCE</h2> 52 * <h2>LICENSE</h2>
53 * 53 *
54 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 54 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
55 * 55 *
56 * Redistribution and use in source and binary forms, with or without 56 * Redistribution and use in source and binary forms, with or without
57 * modification, are permitted provided that the following conditions are met: 57 * modification, are permitted provided that the following conditions are met:
129 */ 129 */
130 #define __attribute__(x) 130 #define __attribute__(x)
131 #endif 131 #endif
132 132
133 /** 133 /**
134 * Inform the compiler that falling through a switch case is intentional.
135 */
136 #define cx_attr_fallthrough __attribute__((__fallthrough__))
137
138 /**
134 * All pointer arguments must be non-NULL. 139 * All pointer arguments must be non-NULL.
135 */ 140 */
136 #define cx_attr_nonnull __attribute__((__nonnull__)) 141 #define cx_attr_nonnull __attribute__((__nonnull__))
137 142
138 /** 143 /**
148 /** 153 /**
149 * The attributed function always returns freshly allocated memory. 154 * The attributed function always returns freshly allocated memory.
150 */ 155 */
151 #define cx_attr_malloc __attribute__((__malloc__)) 156 #define cx_attr_malloc __attribute__((__malloc__))
152 157
153 #ifndef __clang__ 158 #if !defined(__clang__) && __GNUC__ >= 11
154 /** 159 /**
155 * The pointer returned by the attributed function is supposed to be freed 160 * The pointer returned by the attributed function is supposed to be freed
156 * by @p freefunc. 161 * by @p freefunc.
157 * 162 *
158 * @param freefunc the function that shall be used to free the memory 163 * @param freefunc the function that shall be used to free the memory
182 /** 187 /**
183 * No support for @c null_terminated_string_arg in clang or GCC below 14. 188 * No support for @c null_terminated_string_arg in clang or GCC below 14.
184 */ 189 */
185 #define cx_attr_cstr_arg(idx) 190 #define cx_attr_cstr_arg(idx)
186 /** 191 /**
187 * No support for access attribute in clang. 192 * No support for the access attribute in clang.
188 */ 193 */
189 #define cx_attr_access(mode, ...) 194 #define cx_attr_access(mode, ...)
190 #else 195 #else
191 #if __GNUC__ < 10 196 #if __GNUC__ < 10
192 /** 197 /**
238 * Takes one or two arguments: the index of the pointer and (optionally) the 243 * Takes one or two arguments: the index of the pointer and (optionally) the
239 * index of another argument specifying the maximum number of accessed bytes. 244 * index of another argument specifying the maximum number of accessed bytes.
240 */ 245 */
241 #define cx_attr_access_w(...) cx_attr_access(__write_only__, __VA_ARGS__) 246 #define cx_attr_access_w(...) cx_attr_access(__write_only__, __VA_ARGS__)
242 247
243 #if __STDC_VERSION__ >= 202300L
244
245 /** 248 /**
246 * Do not warn about unused variable. 249 * Do not warn about unused variable.
247 */ 250 */
248 #define cx_attr_unused [[maybe_unused]] 251 #define cx_attr_unused __attribute__((__unused__))
249 252
250 /** 253 /**
251 * Warn about discarded return value. 254 * Warn about discarded return value.
252 */ 255 */
253 #define cx_attr_nodiscard [[nodiscard]]
254
255 #else // no C23
256
257 /**
258 * Do not warn about unused variable.
259 */
260 #define cx_attr_unused __attribute__((__unused__))
261
262 /**
263 * Warn about discarded return value.
264 */
265 #define cx_attr_nodiscard __attribute__((__warn_unused_result__)) 256 #define cx_attr_nodiscard __attribute__((__warn_unused_result__))
266
267 #endif // __STDC_VERSION__
268 257
269 258
270 // --------------------------------------------------------------------------- 259 // ---------------------------------------------------------------------------
271 // MSVC specifics 260 // MSVC specifics
272 // --------------------------------------------------------------------------- 261 // ---------------------------------------------------------------------------
274 #ifdef _MSC_VER 263 #ifdef _MSC_VER
275 // fix missing _Thread_local support 264 // fix missing _Thread_local support
276 #define _Thread_local __declspec(thread) 265 #define _Thread_local __declspec(thread)
277 #endif // _MSC_VER 266 #endif // _MSC_VER
278 267
268 // ---------------------------------------------------------------------------
269 // Exported and inlined functions
270 // ---------------------------------------------------------------------------
271
279 #if defined(CX_WINDLL_EXPORT) 272 #if defined(CX_WINDLL_EXPORT)
280 #define cx_attr_export __declspec(dllexport) 273 #define CX_EXPORT __declspec(dllexport)
281 #elif defined(CX_WINDLL) 274 #elif defined(CX_WINDLL)
282 #define cx_attr_export __declspec(dllimport) 275 #define CX_EXPORT __declspec(dllimport)
283 #else 276 #else
284 /** Only used for building Windows DLLs. */ 277 /** Only used for building Windows DLLs. */
285 #define cx_attr_export 278 #define CX_EXPORT
286 #endif // CX_WINDLL / CX_WINDLL_EXPORT 279 #endif // CX_WINDLL / CX_WINDLL_EXPORT
287 280
281 #ifdef __GNUC__
282 /**
283 * Declares a function to be inlined.
284 */
285 #define CX_INLINE __attribute__((always_inline)) static inline
286 #else
287 #define CX_INLINE static inline
288 #endif
289 /**
290 * Declares a compatibility function for C++ builds.
291 */
292 #define CX_CPPDECL static inline
293
288 // --------------------------------------------------------------------------- 294 // ---------------------------------------------------------------------------
289 // Useful function pointers 295 // Useful function pointers
290 // --------------------------------------------------------------------------- 296 // ---------------------------------------------------------------------------
291 297
292 /** 298 /**
293 * Function pointer compatible with fwrite-like functions. 299 * Function pointer compatible with fwrite-like functions.
294 */ 300 */
295 typedef size_t (*cx_write_func)( 301 typedef size_t (*cx_write_func)(const void*, size_t, size_t, void*);
296 const void *,
297 size_t,
298 size_t,
299 void *
300 );
301 302
302 /** 303 /**
303 * Function pointer compatible with fread-like functions. 304 * Function pointer compatible with fread-like functions.
304 */ 305 */
305 typedef size_t (*cx_read_func)( 306 typedef size_t (*cx_read_func)(void*, size_t, size_t, void*);
306 void *,
307 size_t,
308 size_t,
309 void *
310 );
311 307
312 // --------------------------------------------------------------------------- 308 // ---------------------------------------------------------------------------
313 // Utility macros 309 // Utility macros
314 // --------------------------------------------------------------------------- 310 // ---------------------------------------------------------------------------
315 311
357 * @retval non-zero the multiplication would overflow 353 * @retval non-zero the multiplication would overflow
358 */ 354 */
359 #if __cplusplus 355 #if __cplusplus
360 extern "C" 356 extern "C"
361 #endif 357 #endif
362 cx_attr_export int cx_szmul_impl(size_t a, size_t b, size_t *result); 358 CX_EXPORT int cx_szmul_impl(size_t a, size_t b, size_t *result);
363 #endif // cx_szmul 359 #endif // cx_szmul
364 360
365
366
367 #endif // UCX_COMMON_H 361 #endif // UCX_COMMON_H

mercurial