UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2014-2023 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef UTILS_MACROS_H_1982D373_8862_4453_ADFB_33AECC853E48
39#define UTILS_MACROS_H_1982D373_8862_4453_ADFB_33AECC853E48
40
41#ifdef __cplusplus
42#include <cctype>
43#else
44#include <ctype.h>
45#endif
46
47#define MERGE(a,b) a##b
48#define STRINGIFY(A) #A
49#define TOSTRING(A) STRINGIFY(A) // https://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
50
51#define IF_NOT_NULL_ELSE(val, default_val) ((val) ? (val) : (default_val))
52#define UNDEF -1
53#define IF_NOT_UNDEF_ELSE(val, default_val) ((val) != UNDEF ? (val) : (default_val))
54
55#define DIV_ROUNDED_UP(value, div) ((((value) % (div)) != 0) ? ((value) / (div) + 1) : ((value) / (div)))
56
57#define SWAP(a, b) do { b ^= a; a ^= b; b ^= a; } while (0)
58#define SWAP_PTR(a, b) do { void *tmp = (a); (a) = (b); (b) = tmp; } while(0)
59
60#define ARR_COUNT(arr) (sizeof (arr) / sizeof (arr)[0])
61
62#undef MIN
63#define MIN(a,b) (((a)<(b))?(a):(b))
64#undef MAX
65#define MAX(a,b) (((a)>(b))?(a):(b))
66#undef CLAMP
67#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
68
69#ifndef EXTERN_C
70#ifdef __cplusplus
71#define EXTERN_C extern "C"
72#else
73#define EXTERN_C
74#endif
75#endif // defined EXTERN_C
76
78#define UG_ASSERT(cond) \
79 do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
80 if (!(cond)) { \
81 fprintf(stderr, \
82 "%s:%d: %s: Assertion `" #cond "' failed.\n", \
83 __FILE__, __LINE__, __func__); \
84 abort(); \
85 } \
86 } while (0)
87
90#define UG_ASSERT_NO_FATAL(cond) \
91 do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
92 if (!(cond)) { \
93 fprintf(stderr, \
94 "%s:%d: %s: Assertion `" #cond "' failed.\n", \
95 __FILE__, __LINE__, __func__); \
96 handle_error(EXIT_FAILURE); \
97 } \
98 } while (0)
99
101#define snprintf_ch(str, ...) snprintf(str, sizeof str, __VA_ARGS__)
102#define STARTS_WITH(str, token) !strncmp(str, token, strlen(token))
103
111#ifdef WORDS_BIGENDIAN
112#define to_fourcc(a,b,c,d) (((uint32_t)(d)) | ((uint32_t)(c)<<8U) | ((uint32_t)(b)<<16U) | ((uint32_t)(a)<<24U))
113#else
114#define to_fourcc(a,b,c,d) (((uint32_t)(a)) | ((uint32_t)(b)<<8U) | ((uint32_t)(c)<<16U) | ((uint32_t)(d)<<24U))
115#endif
116#define IS_FCC(val) (isprint((val) >> 24U & 0xFFU) && isprint((val) >> 16U & 0xFFU) && isprint((val) >> 8U & 0xFFU) && isprint((val) & 0xFFU))
117
118
119/* Use following macro only if there are no dependencies between loop
120 * iterations (GCC), perhals the same holds also for clang. */
121#define __NL__
122#if defined __clang__ // try clang first - on macOS, clang defines both __clang__ and __GNUC__
123#define OPTIMIZED_FOR _Pragma("clang loop vectorize(assume_safety) interleave(enable)") __NL__ for
124#elif defined __GNUC__
125#define OPTIMIZED_FOR _Pragma("GCC ivdep") __NL__ for
126#else
127#define OPTIMIZED_FOR for
128#endif
129
131enum {
134 STR_LEN = 2048,
135};
136
138#define IS_KEY_PREFIX(tok, key) \
139 (strchr((tok), '=') != 0 && \
140 strncmp(key, tok, strchr((tok), '=') - (tok)) == 0)
142#define IS_PREFIX(tok, key) \
143 (IS_KEY_PREFIX(tok, key) || strncmp(tok, key, strlen(tok)) == 0)
144
145#endif // !defined UTILS_MACROS_H_1982D373_8862_4453_ADFB_33AECC853E48
146
@ MAX_CPU_CORES
maximal expected CPU core count
Definition macros.h:132
@ SHORT_STR
Definition macros.h:133
@ STR_LEN
"standard" string length placeholder
Definition macros.h:134