|
25 | 25 | #define __STDC_FORMAT_MACROS
|
26 | 26 | #include <inttypes.h>
|
27 | 27 |
|
| 28 | +/** |
| 29 | + * When we are parsing using recursive descent, we want to protect against |
| 30 | + * malicious payloads that could attempt to crash our parser. We do this by |
| 31 | + * specifying a maximum depth to which we are allowed to recurse. |
| 32 | + */ |
| 33 | +#ifndef PRISM_DEPTH_MAXIMUM |
| 34 | + #define PRISM_DEPTH_MAXIMUM 1000 |
| 35 | +#endif |
| 36 | + |
28 | 37 | /**
|
29 | 38 | * By default, we compile with -fvisibility=hidden. When this is enabled, we
|
30 | 39 | * need to mark certain functions as being publically-visible. This macro does
|
|
212 | 221 | #define PRISM_ENCODING_EXCLUDE_FULL
|
213 | 222 | #endif
|
214 | 223 |
|
| 224 | +/** |
| 225 | + * Support PRISM_LIKELY and PRISM_UNLIKELY to help the compiler optimize its |
| 226 | + * branch predication. |
| 227 | + */ |
| 228 | +#if defined(__GNUC__) || defined(__clang__) |
| 229 | + /** The compiler should predicate that this branch will be taken. */ |
| 230 | + #define PRISM_LIKELY(x) __builtin_expect(!!(x), 1) |
| 231 | + |
| 232 | + /** The compiler should predicate that this branch will not be taken. */ |
| 233 | + #define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 234 | +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 235 | + /** The compiler should predicate that this branch will be taken. */ |
| 236 | + #define PRISM_LIKELY(x) __assume((x)) |
| 237 | + |
| 238 | + /** The compiler should predicate that this branch will not be taken. */ |
| 239 | + #define PRISM_UNLIKELY(x) __assume(!(x)) |
| 240 | +#else |
| 241 | + /** Void because this platform does not support branch prediction hints. */ |
| 242 | + #define PRISM_LIKELY(x) (x) |
| 243 | + |
| 244 | + /** Void because this platform does not support branch prediction hints. */ |
| 245 | + #define PRISM_UNLIKELY(x) (x) |
| 246 | +#endif |
| 247 | + |
215 | 248 | #endif
|
0 commit comments