Ruby 4.1.0dev (2026-03-20 revision eb04ab9117336f2b3613244cfe8a528c52faf6d6)
defines.h
Go to the documentation of this file.
1
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
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
36#ifndef PRISM_DEPTH_MAXIMUM
37 #define PRISM_DEPTH_MAXIMUM 10000
38#endif
39
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
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
80#if defined(__GNUC__)
81# define PRISM_ATTRIBUTE_UNUSED __attribute__((unused))
82#else
83# define PRISM_ATTRIBUTE_UNUSED
84#endif
85
90#if defined(_MSC_VER) && !defined(inline)
91# define inline __inline
92#endif
93
98#if defined(_MSC_VER)
99# define PRISM_FORCE_INLINE __forceinline
100#elif defined(__GNUC__) || defined(__clang__)
101# define PRISM_FORCE_INLINE inline __attribute__((always_inline))
102#else
103# define PRISM_FORCE_INLINE inline
104#endif
105
110#if !defined(snprintf) && defined(_MSC_VER) && (_MSC_VER < 1900)
111# define snprintf _snprintf
112#endif
113
118#define PM_CONCATENATE(left, right) left ## right
119
125#if defined(_Static_assert)
126# define PM_STATIC_ASSERT(line, condition, message) _Static_assert(condition, message)
127#else
128# define PM_STATIC_ASSERT(line, condition, message) typedef char PM_CONCATENATE(static_assert_, line)[(condition) ? 1 : -1]
129#endif
130
136#ifdef _WIN32
137# define PRISM_HAS_MMAP
138#else
139# include <unistd.h>
140# ifdef _POSIX_MAPPED_FILES
141# define PRISM_HAS_MMAP
142# endif
143#endif
144
150#ifndef PRISM_HAS_NO_FILESYSTEM
151# define PRISM_HAS_FILESYSTEM
152#endif
153
159#ifdef __MINGW64__
160 #include <float.h>
161 #define PRISM_ISINF(x) (!_finite(x))
162#else
163 #define PRISM_ISINF(x) isinf(x)
164#endif
165
185#ifdef PRISM_XALLOCATOR
186 #include "prism_xallocator.h"
187#else
188 #ifndef xmalloc
193 #define xmalloc malloc
194 #endif
195
196 #ifndef xrealloc
201 #define xrealloc realloc
202 #endif
203
204 #ifndef xcalloc
209 #define xcalloc calloc
210 #endif
211
212 #ifndef xfree
217 #define xfree free
218 #endif
219#endif
220
221#ifndef xfree_sized
227 #define xfree_sized(p, s) xfree(((void)(s), (p)))
228#endif
229
230#ifndef xrealloc_sized
236 #define xrealloc_sized(p, ns, os) xrealloc((p), ((void)(os), (ns)))
237#endif
238
239#ifdef PRISM_BUILD_DEBUG
240 #include "prism/debug_allocator.h"
241#endif
242
247#ifdef PRISM_BUILD_MINIMAL
249 #define PRISM_EXCLUDE_SERIALIZATION
250
252 #define PRISM_EXCLUDE_JSON
253
255 #define PRISM_EXCLUDE_PRETTYPRINT
256
258 #define PRISM_ENCODING_EXCLUDE_FULL
259#endif
260
265#if defined(__GNUC__) || defined(__clang__)
267 #define PRISM_LIKELY(x) __builtin_expect(!!(x), 1)
268
270 #define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0)
271#else
273 #define PRISM_LIKELY(x) (x)
274
276 #define PRISM_UNLIKELY(x) (x)
277#endif
278
283#if (defined(__aarch64__) && defined(__ARM_NEON)) || (defined(_MSC_VER) && defined(_M_ARM64))
284 #define PRISM_HAS_NEON
285#elif (defined(__x86_64__) && defined(__SSSE3__)) || (defined(_MSC_VER) && defined(_M_X64))
286 #define PRISM_HAS_SSSE3
287#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
288 #define PRISM_HAS_SWAR
289#endif
290
298#if defined(__GNUC__) || defined(__clang__)
299 #define pm_ctzll(v) ((unsigned) __builtin_ctzll(v))
300#elif defined(_MSC_VER)
301 #include <intrin.h>
302 static inline unsigned pm_ctzll(uint64_t v) {
303 unsigned long index;
304 _BitScanForward64(&index, v);
305 return (unsigned) index;
306 }
307#else
308 static inline unsigned
309 pm_ctzll(uint64_t v) {
310 unsigned c = 0;
311 v &= (uint64_t) (-(int64_t) v);
312 if (v & 0x00000000FFFFFFFFULL) c += 0; else c += 32;
313 if (v & 0x0000FFFF0000FFFFULL) c += 0; else c += 16;
314 if (v & 0x00FF00FF00FF00FFULL) c += 0; else c += 8;
315 if (v & 0x0F0F0F0F0F0F0F0FULL) c += 0; else c += 4;
316 if (v & 0x3333333333333333ULL) c += 0; else c += 2;
317 if (v & 0x5555555555555555ULL) c += 0; else c += 1;
318 return c;
319 }
320#endif
321
326#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later
327 #define PRISM_FALLTHROUGH [[fallthrough]];
328#elif defined(__GNUC__) || defined(__clang__)
329 #define PRISM_FALLTHROUGH __attribute__((fallthrough));
330#elif defined(_MSC_VER)
331 #define PRISM_FALLTHROUGH __fallthrough;
332#else
333 #define PRISM_FALLTHROUGH
334#endif
335
340#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
341 #define PM_FLEX_ARY_LEN /* data[] */
342#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
343 #define PM_FLEX_ARY_LEN 0 /* data[0] */
344#else
345 #define PM_FLEX_ARY_LEN 1 /* data[1] */
346#endif
347
353#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or later */
355 #define PRISM_ALIGNAS _Alignas
356
358 #define PRISM_ALIGNOF _Alignof
359#elif defined(__GNUC__) || defined(__clang__)
361 #define PRISM_ALIGNAS(size) __attribute__((aligned(size)))
362
364 #define PRISM_ALIGNOF(type) __alignof__(type)
365#elif defined(_MSC_VER)
367 #define PRISM_ALIGNAS(size) __declspec(align(size))
368
370 #define PRISM_ALIGNOF(type) __alignof(type)
371#else
373 #define PRISM_ALIGNAS(size)
374
376 #define PRISM_ALIGNOF(type) sizeof(type)
377#endif
378
379#endif
Decorate allocation function to ensure sizes are correct.
#define pm_ctzll(v)
Platform detection for SIMD / fast-path implementations.
Definition defines.h:299
Defines old _.