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
133# if defined(__IMPORTC__)
134# include "sir/platform_importc.h"
136# if !defined(__open_xl__) && defined(__ibmxl__) && defined(__ibmxl_release__)
138# if __ibmxl_release__ <= 1
139# undef __HAVE_ATOMIC_H__
143# if !defined(__open_xl__) && defined(__xlC_ver__) && defined(__IBMC__)
145# undef __HAVE_ATOMIC_H__
149# if !defined(__STDC_WANT_LIB_EXT1__)
150# define __STDC_WANT_LIB_EXT1__ 1
152# if !defined(__STDC_WANT_LIB_EXT2__)
153# define __STDC_WANT_LIB_EXT2__ 1
155# if defined(__APPLE__) && defined(__MACH__)
157# undef _DARWIN_C_SOURCE
158# define _DARWIN_C_SOURCE
159# elif defined(__serenity__)
160# define SIR_PTHREAD_GETNAME_NP
161# elif defined(__OpenBSD__)
163# define __FreeBSD_PTHREAD_NP_11_3__
164# elif defined(__NetBSD__)
166# if !defined(_NETBSD_SOURCE)
167# define _NETBSD_SOURCE 1
169# define SIR_PTHREAD_GETNAME_NP
170# define __USE_HEX_TIDS__
171# elif defined(__FreeBSD__) || defined(__DragonFly__)
174# if !defined(_DEFAULT_SOURCE)
175# define _DEFAULT_SOURCE
177# include <sys/param.h>
178# if __FreeBSD_version >= 1202500
179# define __FreeBSD_PTHREAD_NP_12_2__
180# elif __FreeBSD_version >= 1103500
181# define __FreeBSD_PTHREAD_NP_11_3__
182# elif __DragonFly_version >= 400907
183# define __DragonFly_getthreadid__
185# if defined(__DragonFly__)
186# define SIR_PTHREAD_GETNAME_NP
189# if defined(__EMSCRIPTEN__)
191# define _GNU_SOURCE 1
192# undef SIR_NO_THREAD_NAMES
193# define SIR_NO_THREAD_NAMES
194# include <emscripten.h>
196# if defined(__HAIKU__)
197# if !defined(__USE_GNU)
200# if !defined(_GNU_SOURCE)
201# define _GNU_SOURCE 1
203# if defined(__clang__) && !defined(_GNU_PTHREAD_H_)
206int pthread_getname_np(pthread_t thread,
char* buffer,
size_t length);
208# define SIR_PTHREAD_GETNAME_NP
210# if defined(__GNU__) && !defined(__linux__)
211# undef SIR_NO_THREAD_NAMES
212# define SIR_NO_THREAD_NAMES
213# if !defined(__HURD__)
217# if defined(__linux__)
218# if !defined(_GNU_SOURCE)
219# define _GNU_SOURCE 1
221# include <features.h>
222# if defined(__GLIBC__)
224# define GLIBC_VERSION (((0 + __GLIBC__) * 10000) + ((0 + __GLIBC_MINOR__) * 100))
226# if !defined(GLIBC_VERSION)
227# define GLIBC_VERSION 0
229# if defined(__GLIBC__) && GLIBC_VERSION >= 21200
230# define SIR_PTHREAD_GETNAME_NP
232# if defined(__GLIBC__) && GLIBC_VERSION > 0 && GLIBC_VERSION < 21200
233# undef SIR_NO_THREAD_NAMES
234# define SIR_NO_THREAD_NAMES
236# if defined(__GLIBC__) && GLIBC_VERSION > 0 && \
237 !defined(__SUNPRO_C) && !defined(__SUNPRO_CC)
238# include <sys/sysinfo.h>
241# if defined(__CYGWIN__)
242# if !defined(_GNU_SOURCE)
243# define _GNU_SOURCE 1
245# define SIR_PTHREAD_GETNAME_NP
246# include <sys/features.h>
248# if defined(__ANDROID__) && defined(__ANDROID_API__)
249# if __ANDROID_API__ < 26
250# undef SIR_PTHREAD_GETNAME_NP
251# undef SIR_NO_THREAD_NAMES
252# define SIR_NO_THREAD_NAMES
255# if defined(__illumos__) || ((defined(__sun) || defined(__sun__)) && \
256 (defined(__SVR4) || defined(__svr4__)))
258# define SIR_PTHREAD_GETNAME_NP
259# if !defined(_ATFILE_SOURCE)
260# define _ATFILE_SOURCE 1
262# if !defined(__EXTENSIONS__)
263# define __EXTENSIONS__
266# if !defined(_POSIX_C_SOURCE)
267# define _POSIX_C_SOURCE 200809L
269# if !defined(_DEFAULT_SOURCE)
270# define _DEFAULT_SOURCE
272# if !defined(_XOPEN_SOURCE)
273# define _XOPEN_SOURCE 700
278# define __WANT_STDC_SECURE_LIB__ 1
279# define WIN32_LEAN_AND_MEAN
281# define WINVER 0x0A00
283# define _WIN32_WINNT 0x0A00
286# if defined(__ORANGEC__)
287# include "sir/platform_orangec.h"
289# if defined(__MINGW32__) || defined(__MINGW64__)
290# define SIR_PTHREAD_GETNAME_NP
294# include <synchapi.h>
295# include <processthreadsapi.h>
297# include <winsock2.h>
300# include <evntprov.h>
301# undef __HAVE_ATOMIC_H__
302# if defined(_MSC_VER) && _MSC_VER >= 1933 && \
303 !defined(__cplusplus) && !defined(__IMPORTC__)
304# include <stdatomic.h>
305# define __HAVE_ATOMIC_H__
307# if defined(__MINGW32__) || defined(__MINGW64__)
308# undef __USE_MINGW_ANSI_STDIO
309# define __USE_MINGW_ANSI_STDIO 1
312void (__cdecl* _invalid_parameter_handler)(
313 wchar_t const*,
wchar_t const*,
wchar_t const*,
314 unsigned int, uintptr_t);
315_invalid_parameter_handler
316_set_thread_local_invalid_parameter_handler(
317 _invalid_parameter_handler pNew);
319# include "sir/platform_embarcadero.h"
322# if !defined(__MACOS__) && !defined(__BSD__) && !defined(__SOLARIS__) && \
323 !defined(__HAIKU__) && !(defined(__GLIBC__) && GLIBC_VERSION >= 23800)
324# define SIR_IMPL_STRLCPY 1
325# define SIR_IMPL_STRLCAT 1
329# define SIR_IMPL_STRNDUP 1
333# define SIR_IMPL_STRCASESTR 1
336# if defined(__MINGW64__)
337# define PID_CAST (int)
342# if defined(_AIX) || defined(__CYGWIN__)
343# define CLOCK_CAST (int)
349# if !defined(_ALL_SOURCE)
352# if defined(_GNU_SOURCE)
355# if !defined(_THREAD_SAFE)
360# if defined(SIR_ASSERT_ENABLED)
362# define SIR_ASSERT(...) assert(__VA_ARGS__)
364# if defined(SIR_SELFLOG)
365# define SIR_ASSERT(...) \
367 if (!(__VA_ARGS__)) { \
368 _sir_selflog(SIR_BRED("!!! would be asserting: " #__VA_ARGS__ "")); \
372# define SIR_ASSERT(...)
376# if defined(__cplusplus) && !defined(restrict)
385# include <inttypes.h>
388# if defined(__cplusplus) && defined(__NVCOMPILER) && defined(__FLOAT128__)
389# define NVIDIA_FLOAT128 __FLOAT128__
395# if defined(NVIDIA_FLOAT128)
396# define __FLOAT128__ NVIDIA_FLOAT128
397# undef NVIDIA_FLOAT128
406# include <sys/stat.h>
407# include <sys/types.h>
411# if !defined(SIR_NO_SYSTEM_LOGGERS)
412# if defined(__MACOS__) && !defined(__IMPORTC__) && \
413 ((defined(__clang__) || defined(__clang_version__)) && \
414 !(defined(__INTEL_COMPILER) && !defined(__llvm__)) && \
415 defined(__clang_major__) && defined(__clang_minor__) && defined(__clang_patchlevel__))
416# undef SIR_OS_LOG_ENABLED
417# define SIR_OS_LOG_ENABLED
418# elif defined(__WIN__)
419# undef SIR_EVENTLOG_ENABLED
420# if defined(__EMBARCADEROC__) || defined(__ORANGEC__)
421# define SIR_NO_SYSTEM_LOGGERS
423# define SIR_EVENTLOG_ENABLED
426# undef SIR_OS_LOG_ENABLED
427# define SIR_SYSLOG_ENABLED
430# undef SIR_OS_LOG_ENABLED
431# undef SIR_SYSLOG_ENABLED
434# define SIR_MAXHOST 256
436# if !defined(__WIN__)
437# if !defined(SIR_NO_PLUGINS)
440# if !defined(_CH_) && !defined(__CH__)
444# include <ch/pthread.h>
445# undef __HAVE_ATOMIC_H__
447# if defined(__illumos__)
448# include <sys/fcntl.h>
450# if defined(__linux__) && defined(__USE_GNU) && \
451 (defined(__SUNPRO_C) || defined(__SUNPRO_CC))
459# if defined(__MACOS__)
460# include <sys/sysctl.h>
462# if !defined(__CYGWIN__) && !defined(__HAIKU__) && \
463 !defined(__serenity__) && !defined(_AIX) && \
464 !defined(_CH_) && !defined(__CH__) && !defined(__QNX__)
465# include <sys/syscall.h>
468# include <sys/syspage.h>
470# include <sys/time.h>
475# if defined(__HAVE_ATOMIC_H__) && !defined(__cplusplus)
476# include <stdatomic.h>
478# if defined(SIR_SYSLOG_ENABLED)
479# if !defined(__cplusplus)
483# if defined(__CYGWIN__)
484# undef SIR_NO_THREAD_NAMES
485# define SIR_NO_THREAD_NAMES
488# include <procinfo.h>
489# include <sys/procfs.h>
490# include <sys/systemcfg.h>
491# undef SIR_NO_THREAD_NAMES
492# define SIR_NO_THREAD_NAMES
495# if !defined(__NetBSD__)
496# include <pthread_np.h>
498# include <sys/sysctl.h>
499# elif defined(__linux__)
500# if defined(__GLIBC__)
501# include <linux/limits.h>
503# elif defined(__HAIKU__)
505# include <FindDirectory.h>
506# elif defined(__MACOS__)
507# include <mach-o/dyld.h>
508# if defined(SIR_OS_LOG_ENABLED)
510# include <os/trace.h>
511# include <os/activity.h>
515# if defined(PATH_MAX)
516# define SIR_MAXPATH PATH_MAX
517# elif defined(MAXPATHLEN)
518# define SIR_MAXPATH MAXPATHLEN
520# define SIR_MAXPATH 1024
523# if !defined(SIR_MAXPID) && defined(HAIKU) && defined(B_OS_NAME_LENGTH)
524# define SIR_MAXPID B_OS_NAME_LENGTH
526# if !defined(SIR_MAXPID) && defined(PTHREAD_MAX_NAMELEN_NP)
527# define SIR_MAXPID PTHREAD_MAX_NAMELEN_NP
529# if !defined(SIR_MAXPID) && defined(_NTO_THREAD_NAME_MAX)
530# define SIR_MAXPID _NTO_THREAD_NAME_MAX
532# if !defined(SIR_MAXPID) && defined(__APPLE__)
533# define SIR_MAXPID 64
535# if !defined(SIR_MAXPID) && defined(__OpenBSD__)
536# define SIR_MAXPID 32
538# if !defined(SIR_MAXPID) && defined(__SOLARIS__)
539# define SIR_MAXPID 31
541# if !defined(SIR_MAXPID) && defined(__linux__)
542# define SIR_MAXPID 16
544# if !defined(SIR_MAXPID)
545# define SIR_MAXPID 15
548# if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) || \
549 defined(__MACOS__) || defined(__OpenBSD__)
550# define SIR_MSEC_TIMER
551# define SIR_MSEC_POSIX
553# undef SIR_MSEC_TIMER
557# if defined(CLOCK_REALTIME_FAST)
558# define SIR_WALLCLOCK CLOCK_REALTIME_FAST
559# elif defined(CLOCK_REALTIME_COARSE)
560# define SIR_WALLCLOCK CLOCK_REALTIME_COARSE
562# define SIR_WALLCLOCK CLOCK_REALTIME
566# if defined(CLOCK_UPTIME)
567# define SIR_INTERVALCLOCK CLOCK_UPTIME
568# elif defined(CLOCK_BOOTTIME) && !defined(__EMSCRIPTEN__)
569# define SIR_INTERVALCLOCK CLOCK_BOOTTIME
570# elif defined(CLOCK_HIGHRES)
571# define SIR_INTERVALCLOCK CLOCK_HIGHRES
572# elif defined(CLOCK_MONOTONIC)
573# define SIR_INTERVALCLOCK CLOCK_MONOTONIC
575# define SIR_INTERVALCLOCK CLOCK_REALTIME
579typedef void* sir_pluginhandle;
582typedef void (*sir_pluginexport)(void);
585typedef pthread_mutex_t sir_mutex;
588typedef pthread_t sir_thread;
591typedef pthread_cond_t sir_condition;
594typedef struct timespec sir_wait;
597typedef pthread_once_t sir_once;
600typedef void (*sir_once_fn)(void);
603# define SIR_ONCE_INIT PTHREAD_ONCE_INIT
606# define SIR_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
610# define SIR_MAXPID 64
612# define SIR_MAXPATH MAX_PATH
614# define SIR_MSEC_TIMER
615# define SIR_MSEC_WIN32
616# define SIR_WALLCLOCK 0
617# define SIR_INTERVALCLOCK 1
620typedef HMODULE sir_pluginhandle;
623typedef FARPROC sir_pluginexport;
626typedef CRITICAL_SECTION sir_mutex;
629typedef HANDLE sir_thread;
632typedef CONDITION_VARIABLE sir_condition;
635typedef DWORD sir_wait;
638typedef INIT_ONCE sir_once;
641# if !defined(__MINGW64__) && !defined(__MINGW32__) && !defined(__ORANGEC__)
646typedef BOOL(CALLBACK* sir_once_fn)(PINIT_ONCE, PVOID, PVOID*);
649# define SIR_ONCE_INIT INIT_ONCE_STATIC_INIT
652# define SIR_MUTEX_INIT {0}
656# if !defined(_sir_thread_local)
657# if (__STDC_VERSION__ >= 201112 && !defined(__STDC_NO_THREADS__)) || \
658 (defined(__SUNPRO_C) || defined(__SUNPRO_CC))
659# if defined(_AIX) && defined(__GNUC__)
660# define _sir_thread_local __thread
662# define _sir_thread_local _Thread_local
664# elif defined(__WIN__)
665# define _sir_thread_local __declspec(thread)
666# elif defined(__GNUC__) || (defined(_AIX) && (defined(__xlC_ver__) || defined(__ibmxl__)))
667# define _sir_thread_local __thread
669# if !defined(_CH_) && !defined(__CH__)
670# error "unable to resolve thread local attribute; please contact the developers"
675# if defined(__WIN__) && defined(__STDC_SECURE_LIB__)
676# define __HAVE_STDC_SECURE_OR_EXT1__
677# elif defined(__STDC_LIB_EXT1__)
678# define __HAVE_STDC_SECURE_OR_EXT1__
679# elif defined(__STDC_ALLOC_LIB__)
680# define __HAVE_STDC_EXT2__
683# if (defined(__clang__) || defined(__GNUC__)) && defined(__FILE_NAME__)
684# define __file__ __FILE_NAME__
685# elif defined(__BASE_FILE__)
686# define __file__ __BASE_FILE__
688# define __file__ __FILE__
696#if defined(SIR_LINT) && (defined(__linux__) && defined(__GLIBC__)) && \
697 (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) && \
698 (defined(__clang__) || defined(__clang_version__))