33#ifndef _SIR_PLATFORM_H_INCLUDED
34# define _SIR_PLATFORM_H_INCLUDED
36# if defined(_MSC_VER) && !defined(__clang__)
38# undef _USE_ATTRIBUTES_FOR_SAL
39# define _USE_ATTRIBUTES_FOR_SAL 1
41# define PRINTF_FORMAT _Printf_format_string_
42# define PRINTF_FORMAT_ATTR(fmt_p, va_p)
45# if (defined(__MINGW32__) || defined(__MINGW64__)) && \
46 !defined(__clang_version__)
47# if !defined(__USE_MINGW_ANSI_STDIO)
48# define __USE_MINGW_ANSI_STDIO 1
50# define PRINTF_FORMAT_ATTR(fmt_p, va_p) \
51 __attribute__((format (gnu_printf, fmt_p, va_p)))
53# if !defined(__SUNPRO_C) && !defined(__SUNPRO_CC) && \
54 !defined(_CH_) && !defined(__CH__)
55# define PRINTF_FORMAT_ATTR(fmt_p, va_p) \
56 __attribute__((format (printf, fmt_p, va_p)))
58# define PRINTF_FORMAT_ATTR(fmt_p, va_p)
64# if defined __has_attribute && (defined(__clang__) || defined(__GNUC__))
65# define HAS_ATTRIBUTE(atr) __has_attribute(atr)
67# define HAS_ATTRIBUTE(atr) 0
71# if defined __has_include
72# define HAS_INCLUDE(inc) __has_include(inc)
74# define HAS_INCLUDE(inc) 0
78# if defined __has_feature
79# define HAS_FEATURE(fea) __has_feature(fea)
81# define HAS_FEATURE(fea) 0
84# undef SANITIZE_SUPPRESS
85# if HAS_ATTRIBUTE(no_sanitize)
86# define SANITIZE_SUPPRESS(str) __attribute__((no_sanitize(str)))
88# if !defined(SANITIZE_SUPPRESS)
89# define SANITIZE_SUPPRESS(str)
92# if HAS_FEATURE(safe_stack) && !defined(SIR_NO_PLUGINS)
93# error "linking DSOs with SafeStack is unsupported; disable SafeStack or enable SIR_NO_PLUGINS"
96# if defined(__OPENOSC_H__)
97# define SIR_IMPL_STRNLEN
101# if defined(__STDC_NO_ATOMICS__)
102# undef __HAVE_ATOMIC_H__
104# define __HAVE_ATOMIC_H__
106# if defined(__GNUC__) && __GNUC__ <= 4
107# if defined(__GNUC_MINOR__) && __GNUC_MINOR__ <= 8
108# if !defined(__clang_version__)
109# undef __HAVE_ATOMIC_H__
113# if defined(__DragonFly__)
114# if defined(__clang__) && defined(__clang_version__)
115# undef __HAVE_ATOMIC_H__
118# if defined(__circle_lang__)
119# undef __HAVE_ATOMIC_H__
122# undef __HAVE_ATOMIC_H__
124# if (defined(__INTEL_COMPILER) && !defined(__llvm__)) || \
125 defined(__NVCOMPILER) || defined(__COVERITY__)
126# if !defined(_BITS_FLOATN_H)
127# define _BITS_FLOATN_H
130# if defined(__IMPORTC__)
131# include "sir/platform_importc.h"
133# if !defined(__open_xl__) && defined(__ibmxl__) && defined(__ibmxl_release__)
135# if __ibmxl_release__ <= 1
136# undef __HAVE_ATOMIC_H__
140# if !defined(__open_xl__) && defined(__xlC_ver__) && defined(__IBMC__)
142# undef __HAVE_ATOMIC_H__
146# if !defined(__STDC_WANT_LIB_EXT1__)
147# define __STDC_WANT_LIB_EXT1__ 1
149# if !defined(__STDC_WANT_LIB_EXT2__)
150# define __STDC_WANT_LIB_EXT2__ 1
152# if defined(__APPLE__) && defined(__MACH__)
154# undef _DARWIN_C_SOURCE
155# define _DARWIN_C_SOURCE
156# elif defined(__serenity__)
157# define SIR_PTHREAD_GETNAME_NP
158# elif defined(__OpenBSD__)
160# define __FreeBSD_PTHREAD_NP_11_3__
161# elif defined(__NetBSD__)
163# if !defined(_NETBSD_SOURCE)
164# define _NETBSD_SOURCE 1
166# define SIR_PTHREAD_GETNAME_NP
167# define __USE_HEX_TIDS__
168# elif defined(__FreeBSD__) || defined(__DragonFly__)
171# if !defined(_DEFAULT_SOURCE)
172# define _DEFAULT_SOURCE
174# include <sys/param.h>
175# if __FreeBSD_version >= 1202500
176# define __FreeBSD_PTHREAD_NP_12_2__
177# elif __FreeBSD_version >= 1103500
178# define __FreeBSD_PTHREAD_NP_11_3__
179# elif __DragonFly_version >= 400907
180# define __DragonFly_getthreadid__
182# if defined(__DragonFly__)
183# define SIR_PTHREAD_GETNAME_NP
186# if defined(__EMSCRIPTEN__)
188# define _GNU_SOURCE 1
189# undef SIR_NO_THREAD_NAMES
190# define SIR_NO_THREAD_NAMES
191# include <emscripten.h>
193# if defined(__HAIKU__)
194# if !defined(__USE_GNU)
197# if !defined(_GNU_SOURCE)
198# define _GNU_SOURCE 1
200# if defined(__clang__) && !defined(_GNU_PTHREAD_H_)
203int pthread_getname_np(pthread_t thread,
char* buffer,
size_t length);
205# define SIR_PTHREAD_GETNAME_NP
207# if defined(__GNU__) && !defined(__linux__)
208# undef SIR_NO_THREAD_NAMES
209# define SIR_NO_THREAD_NAMES
210# if !defined(__HURD__)
214# if defined(__linux__)
215# if !defined(_GNU_SOURCE)
216# define _GNU_SOURCE 1
218# include <features.h>
219# if defined(__GLIBC__)
221# define GLIBC_VERSION (((0 + __GLIBC__) * 10000) + ((0 + __GLIBC_MINOR__) * 100))
223# if !defined(GLIBC_VERSION)
224# define GLIBC_VERSION 0
226# if defined(__GLIBC__) && GLIBC_VERSION >= 21200
227# define SIR_PTHREAD_GETNAME_NP
229# if defined(__GLIBC__) && GLIBC_VERSION > 0 && GLIBC_VERSION < 21200
230# undef SIR_NO_THREAD_NAMES
231# define SIR_NO_THREAD_NAMES
233# if defined(__GLIBC__) && GLIBC_VERSION > 0 && \
234 !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
235# include <sys/sysinfo.h>
238# if defined(__CYGWIN__)
239# if !defined(_GNU_SOURCE)
240# define _GNU_SOURCE 1
242# define SIR_PTHREAD_GETNAME_NP
243# include <sys/features.h>
245# if defined(__ANDROID__) && defined(__ANDROID_API__)
246# if __ANDROID_API__ < 26
247# undef SIR_PTHREAD_GETNAME_NP
248# undef SIR_NO_THREAD_NAMES
249# define SIR_NO_THREAD_NAMES
252# if defined(__illumos__) || ((defined(__sun) || defined(__sun__)) && \
253 (defined(__SVR4) || defined(__svr4__)))
255# define SIR_PTHREAD_GETNAME_NP
256# if !defined(_ATFILE_SOURCE)
257# define _ATFILE_SOURCE 1
259# if !defined(__EXTENSIONS__)
260# define __EXTENSIONS__
263# if !defined(_POSIX_C_SOURCE)
264# define _POSIX_C_SOURCE 200809L
266# if !defined(_DEFAULT_SOURCE)
267# define _DEFAULT_SOURCE
269# if !defined(_XOPEN_SOURCE)
270# define _XOPEN_SOURCE 700
275# define __WANT_STDC_SECURE_LIB__ 1
276# define WIN32_LEAN_AND_MEAN
278# define WINVER 0x0A00
280# define _WIN32_WINNT 0x0A00
283# if defined(__ORANGEC__)
284# include "sir/platform_orangec.h"
286# if defined(__MINGW32__) || defined(__MINGW64__)
287# define SIR_PTHREAD_GETNAME_NP
291# include <synchapi.h>
292# include <processthreadsapi.h>
294# include <winsock2.h>
297# include <evntprov.h>
298# undef __HAVE_ATOMIC_H__
299# if defined(_MSC_VER) && _MSC_VER >= 1933 && \
300 !defined(__cplusplus) && !defined(__IMPORTC__)
301# include <stdatomic.h>
302# define __HAVE_ATOMIC_H__
304# if defined(__MINGW32__) || defined(__MINGW64__)
305# undef __USE_MINGW_ANSI_STDIO
306# define __USE_MINGW_ANSI_STDIO 1
309void (__cdecl* _invalid_parameter_handler)(
310 wchar_t const*,
wchar_t const*,
wchar_t const*,
311 unsigned int, uintptr_t);
312_invalid_parameter_handler
313_set_thread_local_invalid_parameter_handler(
314 _invalid_parameter_handler pNew);
316# include "sir/platform_embarcadero.h"
319# if !defined(__MACOS__) && !defined(__BSD__) && !defined(__SOLARIS__) && \
320 !defined(__HAIKU__) && !(defined(__GLIBC__) && GLIBC_VERSION >= 23800)
321# define SIR_IMPL_STRLCPY 1
322# define SIR_IMPL_STRLCAT 1
326# define SIR_IMPL_STRNDUP 1
330# define SIR_IMPL_STRCASESTR 1
333# if defined(__MINGW64__)
334# define PID_CAST (int)
339# if defined(_AIX) || defined(__CYGWIN__)
340# define CLOCK_CAST (int)
346# if !defined(_ALL_SOURCE)
349# if defined(_GNU_SOURCE)
352# if !defined(_THREAD_SAFE)
357# if defined(SIR_ASSERT_ENABLED)
359# define SIR_ASSERT(...) assert(__VA_ARGS__)
361# if defined(SIR_SELFLOG)
362# define SIR_ASSERT(...) \
364 if (!(__VA_ARGS__)) { \
365 _sir_selflog(SIR_BRED("!!! would be asserting: " #__VA_ARGS__ "")); \
369# define SIR_ASSERT(...)
373# if defined(__cplusplus) && !defined(restrict)
382# include <inttypes.h>
385# if defined(__cplusplus) && defined(__NVCOMPILER) && defined(__FLOAT128__)
386# define NVIDIA_FLOAT128 __FLOAT128__
392# if defined(NVIDIA_FLOAT128)
393# define __FLOAT128__ NVIDIA_FLOAT128
394# undef NVIDIA_FLOAT128
403# include <sys/stat.h>
404# include <sys/types.h>
408# if !defined(SIR_NO_SYSTEM_LOGGERS)
409# if defined(__MACOS__) && !defined(__IMPORTC__) && \
410 ((defined(__clang__) || defined(__clang_version__)) && \
411 !(defined(__INTEL_COMPILER) && !defined(__llvm__)) && \
412 defined(__clang_major__) && defined(__clang_minor__) && defined(__clang_patchlevel__))
413# undef SIR_OS_LOG_ENABLED
414# define SIR_OS_LOG_ENABLED
415# elif defined(__WIN__)
416# undef SIR_EVENTLOG_ENABLED
417# if defined(__EMBARCADEROC__) || defined(__ORANGEC__)
418# define SIR_NO_SYSTEM_LOGGERS
420# define SIR_EVENTLOG_ENABLED
423# undef SIR_OS_LOG_ENABLED
424# define SIR_SYSLOG_ENABLED
427# undef SIR_OS_LOG_ENABLED
428# undef SIR_SYSLOG_ENABLED
431# define SIR_MAXHOST 256
433# if !defined(__WIN__)
434# if !defined(SIR_NO_PLUGINS)
437# if !defined(_CH_) && !defined(__CH__)
441# include <ch/pthread.h>
442# undef __HAVE_ATOMIC_H__
444# if defined(__illumos__)
445# include <sys/fcntl.h>
447# if defined(__linux__) && defined(__USE_GNU) && \
448 (defined(__SUNPRO_C) || defined(__SUNPRO_CC))
456# if defined(__MACOS__)
457# include <sys/sysctl.h>
459# if !defined(__CYGWIN__) && !defined(__HAIKU__) && \
460 !defined(__serenity__) && !defined(_AIX) && \
461 !defined(_CH_) && !defined(__CH__)
462# include <sys/syscall.h>
464# include <sys/time.h>
469# if defined(__HAVE_ATOMIC_H__) && !defined(__cplusplus)
470# include <stdatomic.h>
472# if defined(SIR_SYSLOG_ENABLED)
473# if !defined(__cplusplus)
477# if defined(__CYGWIN__)
478# undef SIR_NO_THREAD_NAMES
479# define SIR_NO_THREAD_NAMES
482# include <procinfo.h>
483# include <sys/procfs.h>
484# include <sys/systemcfg.h>
485# undef SIR_NO_THREAD_NAMES
486# define SIR_NO_THREAD_NAMES
489# if !defined(__NetBSD__)
490# include <pthread_np.h>
492# include <sys/sysctl.h>
493# elif defined(__linux__)
494# if defined(__GLIBC__)
495# include <linux/limits.h>
497# elif defined(__HAIKU__)
499# include <FindDirectory.h>
500# elif defined(__MACOS__)
501# include <mach-o/dyld.h>
502# if defined(SIR_OS_LOG_ENABLED)
504# include <os/trace.h>
505# include <os/activity.h>
509# if defined(PATH_MAX)
510# define SIR_MAXPATH PATH_MAX
511# elif defined(MAXPATHLEN)
512# define SIR_MAXPATH MAXPATHLEN
514# define SIR_MAXPATH 1024
517# if !defined(SIR_MAXPID) && defined(HAIKU) && defined(B_OS_NAME_LENGTH)
518# define SIR_MAXPID B_OS_NAME_LENGTH
520# if !defined(SIR_MAXPID) && defined(PTHREAD_MAX_NAMELEN_NP)
521# define SIR_MAXPID PTHREAD_MAX_NAMELEN_NP
523# if !defined(SIR_MAXPID) && defined(_NTO_THREAD_NAME_MAX)
524# define SIR_MAXPID _NTO_THREAD_NAME_MAX
526# if !defined(SIR_MAXPID) && defined(__APPLE__)
527# define SIR_MAXPID 64
529# if !defined(SIR_MAXPID) && defined(__OpenBSD__)
530# define SIR_MAXPID 32
532# if !defined(SIR_MAXPID) && defined(__SOLARIS__)
533# define SIR_MAXPID 31
535# if !defined(SIR_MAXPID) && defined(__linux__)
536# define SIR_MAXPID 16
538# if !defined(SIR_MAXPID)
539# define SIR_MAXPID 15
542# if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) || \
543 defined(__MACOS__) || defined(__OpenBSD__)
544# define SIR_MSEC_TIMER
545# define SIR_MSEC_POSIX
547# undef SIR_MSEC_TIMER
551# if defined(CLOCK_REALTIME_FAST)
552# define SIR_WALLCLOCK CLOCK_REALTIME_FAST
553# elif defined(CLOCK_REALTIME_COARSE)
554# define SIR_WALLCLOCK CLOCK_REALTIME_COARSE
556# define SIR_WALLCLOCK CLOCK_REALTIME
560# if defined(CLOCK_UPTIME)
561# define SIR_INTERVALCLOCK CLOCK_UPTIME
562# elif defined(CLOCK_BOOTTIME) && !defined(__EMSCRIPTEN__)
563# define SIR_INTERVALCLOCK CLOCK_BOOTTIME
564# elif defined(CLOCK_HIGHRES)
565# define SIR_INTERVALCLOCK CLOCK_HIGHRES
566# elif defined(CLOCK_MONOTONIC)
567# define SIR_INTERVALCLOCK CLOCK_MONOTONIC
569# define SIR_INTERVALCLOCK CLOCK_REALTIME
573typedef void* sir_pluginhandle;
576typedef void (*sir_pluginexport)(void);
579typedef pthread_mutex_t sir_mutex;
582typedef pthread_t sir_thread;
585typedef pthread_cond_t sir_condition;
588typedef struct timespec sir_wait;
591typedef pthread_once_t sir_once;
594typedef void (*sir_once_fn)(void);
597# define SIR_ONCE_INIT PTHREAD_ONCE_INIT
600# define SIR_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
604# define SIR_MAXPID 64
606# define SIR_MAXPATH MAX_PATH
608# define SIR_MSEC_TIMER
609# define SIR_MSEC_WIN32
610# define SIR_WALLCLOCK 0
611# define SIR_INTERVALCLOCK 1
614typedef HMODULE sir_pluginhandle;
617typedef FARPROC sir_pluginexport;
620typedef CRITICAL_SECTION sir_mutex;
623typedef HANDLE sir_thread;
626typedef CONDITION_VARIABLE sir_condition;
629typedef DWORD sir_wait;
632typedef INIT_ONCE sir_once;
635# if !defined(__MINGW64__) && !defined(__MINGW32__) && !defined(__ORANGEC__)
640typedef BOOL(CALLBACK* sir_once_fn)(PINIT_ONCE, PVOID, PVOID*);
643# define SIR_ONCE_INIT INIT_ONCE_STATIC_INIT
646# define SIR_MUTEX_INIT {0}
650# if !defined(_sir_thread_local)
651# if (__STDC_VERSION__ >= 201112 && !defined(__STDC_NO_THREADS__)) || \
652 (defined(__SUNPRO_C) || defined(__SUNPRO_CC))
653# if defined(_AIX) && defined(__GNUC__)
654# define _sir_thread_local __thread
656# define _sir_thread_local _Thread_local
658# elif defined(__WIN__)
659# define _sir_thread_local __declspec(thread)
660# elif defined(__GNUC__) || (defined(_AIX) && (defined(__xlC_ver__) || defined(__ibmxl__)))
661# define _sir_thread_local __thread
663# if !defined(_CH_) && !defined(__CH__)
664# error "unable to resolve thread local attribute; please contact the developers"
669# if defined(__WIN__) && defined(__STDC_SECURE_LIB__)
670# define __HAVE_STDC_SECURE_OR_EXT1__
671# elif defined(__STDC_LIB_EXT1__)
672# define __HAVE_STDC_SECURE_OR_EXT1__
673# elif defined(__STDC_ALLOC_LIB__)
674# define __HAVE_STDC_EXT2__
677# if (defined(__clang__) || defined(__GNUC__)) && defined(__FILE_NAME__)
678# define __file__ __FILE_NAME__
679# elif defined(__BASE_FILE__)
680# define __file__ __BASE_FILE__
682# define __file__ __FILE__
690#if defined(SIR_LINT) && (defined(__linux__) && defined(__GLIBC__)) && \
691 (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) && \
692 (defined(__clang__) || defined(__clang_version__))