Debian Codegraph @

include/prism/defines.h

raw symbols SCIP
1 /**
2 * @file defines.h
3 *
4 * Macro definitions used throughout the prism library.
5 *
6 * This file should be included first by any *.h or *.c in prism for consistency
7 * and to ensure that the macros are defined before they are used.
8 */
9 #ifndef PRISM_DEFINES_H
10 #define PRISM_DEFINES_H
11
12 #include <ctype.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdarg.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 /**
22 * We want to be able to use the PRI* macros for printing out integers, but on
23 * some platforms they aren't included unless this is already defined.
24 */
25 #define __STDC_FORMAT_MACROS
26 // Include sys/types.h before inttypes.h to work around issue with
27 // certain versions of GCC and newlib which causes omission of PRIx64
28 #include <sys/types.h>
29 #include <inttypes.h>
30
31 /**
32 * When we are parsing using recursive descent, we want to protect against
33 * malicious payloads that could attempt to crash our parser. We do this by
34 * specifying a maximum depth to which we are allowed to recurse.
35 */
36 #ifndef PRISM_DEPTH_MAXIMUM
37 #define PRISM_DEPTH_MAXIMUM 10000
38 #endif
39
40 /**
41 * By default, we compile with -fvisibility=hidden. When this is enabled, we
42 * need to mark certain functions as being publically-visible. This macro does
43 * that in a compiler-agnostic way.
44 */
45 #ifndef PRISM_EXPORTED_FUNCTION
46 # ifdef PRISM_EXPORT_SYMBOLS
47 # ifdef _WIN32
48 # define PRISM_EXPORTED_FUNCTION __declspec(dllexport) extern
49 # else
50 # define PRISM_EXPORTED_FUNCTION __attribute__((__visibility__("default"))) extern
51 # endif
52 # else
53 # define PRISM_EXPORTED_FUNCTION
54 # endif
55 #endif
56
57 /**
58 * Certain compilers support specifying that a function accepts variadic
59 * parameters that look like printf format strings to provide a better developer
60 * experience when someone is using the function. This macro does that in a
61 * compiler-agnostic way.
62 */
63 #if defined(__GNUC__)
64 # if defined(__MINGW_PRINTF_FORMAT)
65 # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
66 # else
67 # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
68 # endif
69 #elif defined(__clang__)
70 # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
71 #else
72 # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index)
73 #endif
74
75 /**
76 * GCC will warn if you specify a function or parameter that is unused at
77 * runtime. This macro allows you to mark a function or parameter as unused in a
78 * compiler-agnostic way.
79 */
80 #if defined(__GNUC__)
81 # define PRISM_ATTRIBUTE_UNUSED __attribute__((unused))
82 #else
83 # define PRISM_ATTRIBUTE_UNUSED
84 #endif
85
86 /**
87 * Old Visual Studio versions do not support the inline keyword, so we need to
88 * define it to be __inline.
89 */
90 #if defined(_MSC_VER) && !defined(inline)
91 # define inline __inline
92 #endif
93
94 /**
95 * Old Visual Studio versions before 2015 do not implement sprintf, but instead
96 * implement _snprintf. We standard that here.
97 */
98 #if !defined(snprintf) && defined(_MSC_VER) && (_MSC_VER < 1900)
99 # define snprintf _snprintf
100 #endif
101
102 /**
103 * A simple utility macro to concatenate two tokens together, necessary when one
104 * of the tokens is itself a macro.
105 */
106 #define PM_CONCATENATE(left, right) left ## right
107
108 /**
109 * We want to be able to use static assertions, but they weren't standardized
110 * until C11. As such, we polyfill it here by making a hacky typedef that will
111 * fail to compile due to a negative array size if the condition is false.
112 */
113 #if defined(_Static_assert)
114 # define PM_STATIC_ASSERT(line, condition, message) _Static_assert(condition, message)
115 #else
116 # define PM_STATIC_ASSERT(line, condition, message) typedef char PM_CONCATENATE(static_assert_, line)[(condition) ? 1 : -1]
117 #endif
118
119 /**
120 * In general, libc for embedded systems does not support memory-mapped files.
121 * If the target platform is POSIX or Windows, we can map a file in memory and
122 * read it in a more efficient manner.
123 */
124 #ifdef _WIN32
125 # define PRISM_HAS_MMAP
126 #else
127 # include <unistd.h>
128 # ifdef _POSIX_MAPPED_FILES
129 # define PRISM_HAS_MMAP
130 # endif
131 #endif
132
133 /**
134 * If PRISM_HAS_NO_FILESYSTEM is defined, then we want to exclude all filesystem
135 * related code from the library. All filesystem related code should be guarded
136 * by PRISM_HAS_FILESYSTEM.
137 */
138 #ifndef PRISM_HAS_NO_FILESYSTEM
139 # define PRISM_HAS_FILESYSTEM
140 #endif
141
142 /**
143 * isinf on POSIX systems it accepts a float, a double, or a long double.
144 * But mingw didn't provide an isinf macro, only an isinf function that only
145 * accepts floats, so we need to use _finite instead.
146 */
147 #ifdef __MINGW64__
148 #include <float.h>
149 #define PRISM_ISINF(x) (!_finite(x))
150 #else
151 #define PRISM_ISINF(x) isinf(x)
152 #endif
153
154 /**
155 * If you build prism with a custom allocator, configure it with
156 * "-D PRISM_XALLOCATOR" to use your own allocator that defines xmalloc,
157 * xrealloc, xcalloc, and xfree.
158 *
159 * For example, your `prism_xallocator.h` file could look like this:
160 *
161 * ```
162 * #ifndef PRISM_XALLOCATOR_H
163 * #define PRISM_XALLOCATOR_H
164 * #define xmalloc my_malloc
165 * #define xrealloc my_realloc
166 * #define xcalloc my_calloc
167 * #define xfree my_free
168 * #endif
169 * ```
170 */
171 #ifdef PRISM_XALLOCATOR
172 #include "prism_xallocator.h"
173 #else
174 #ifndef xmalloc
175 /**
176 * The malloc function that should be used. This can be overridden with
177 * the PRISM_XALLOCATOR define.
178 */
179 #define xmalloc malloc
180 #endif
181
182 #ifndef xrealloc
183 /**
184 * The realloc function that should be used. This can be overridden with
185 * the PRISM_XALLOCATOR define.
186 */
187 #define xrealloc realloc
188 #endif
189
190 #ifndef xcalloc
191 /**
192 * The calloc function that should be used. This can be overridden with
193 * the PRISM_XALLOCATOR define.
194 */
195 #define xcalloc calloc
196 #endif
197
198 #ifndef xfree
199 /**
200 * The free function that should be used. This can be overridden with the
201 * PRISM_XALLOCATOR define.
202 */
203 #define xfree free
204 #endif
205 #endif
206
207 /**
208 * If PRISM_BUILD_MINIMAL is defined, then we're going to define every possible
209 * switch that will turn off certain features of prism.
210 */
211 #ifdef PRISM_BUILD_MINIMAL
212 /** Exclude the serialization API. */
213 #define PRISM_EXCLUDE_SERIALIZATION
214
215 /** Exclude the JSON serialization API. */
216 #define PRISM_EXCLUDE_JSON
217
218 /** Exclude the Array#pack parser API. */
219 #define PRISM_EXCLUDE_PACK
220
221 /** Exclude the prettyprint API. */
222 #define PRISM_EXCLUDE_PRETTYPRINT
223
224 /** Exclude the full set of encodings, using the minimal only. */
225 #define PRISM_ENCODING_EXCLUDE_FULL
226 #endif
227
228 /**
229 * Support PRISM_LIKELY and PRISM_UNLIKELY to help the compiler optimize its
230 * branch predication.
231 */
232 #if defined(__GNUC__) || defined(__clang__)
233 /** The compiler should predicate that this branch will be taken. */
234 #define PRISM_LIKELY(x) __builtin_expect(!!(x), 1)
235
236 /** The compiler should predicate that this branch will not be taken. */
237 #define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0)
238 #else
239 /** Void because this platform does not support branch prediction hints. */
240 #define PRISM_LIKELY(x) (x)
241
242 /** Void because this platform does not support branch prediction hints. */
243 #define PRISM_UNLIKELY(x) (x)
244 #endif
245
246 /**
247 * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch.
248 * Use PRISM_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional.
249 */
250 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later
251 #define PRISM_FALLTHROUGH [[fallthrough]];
252 #elif defined(__GNUC__) || defined(__clang__)
253 #define PRISM_FALLTHROUGH __attribute__((fallthrough));
254 #elif defined(_MSC_VER)
255 #define PRISM_FALLTHROUGH __fallthrough;
256 #else
257 #define PRISM_FALLTHROUGH
258 #endif
259
260 #endif