2024-04-18 12:59:11 +00:00
|
|
|
#pragma once
|
|
|
|
#include "dqn.h"
|
|
|
|
|
2024-03-25 05:11:57 +00:00
|
|
|
/*
|
2024-01-31 12:49:23 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// $$$$$$$$\ $$\ $$\ $$$$$$$$\ $$$$$$$$\ $$$$$$$\ $$\ $$\ $$$$$$\ $$\
|
|
|
|
// $$ _____|$$ | $$ |\__$$ __|$$ _____|$$ __$$\ $$$\ $$ |$$ __$$\ $$ |
|
|
|
|
// $$ | \$$\ $$ | $$ | $$ | $$ | $$ |$$$$\ $$ |$$ / $$ |$$ |
|
|
|
|
// $$$$$\ \$$$$ / $$ | $$$$$\ $$$$$$$ |$$ $$\$$ |$$$$$$$$ |$$ |
|
|
|
|
// $$ __| $$ $$< $$ | $$ __| $$ __$$< $$ \$$$$ |$$ __$$ |$$ |
|
|
|
|
// $$ | $$ /\$$\ $$ | $$ | $$ | $$ |$$ |\$$$ |$$ | $$ |$$ |
|
|
|
|
// $$$$$$$$\ $$ / $$ | $$ | $$$$$$$$\ $$ | $$ |$$ | \$$ |$$ | $$ |$$$$$$$$\
|
|
|
|
// \________|\__| \__| \__| \________|\__| \__|\__| \__|\__| \__|\________|
|
|
|
|
//
|
|
|
|
// dqn_external.h -- Third party dependencies
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2024-03-25 05:11:57 +00:00
|
|
|
*/
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
// NOTE: [$OS_H] OS Headers ////////////////////////////////////////////////////////////////////////
|
|
|
|
#if !defined(DQN_OS_WIN32) || defined(DQN_OS_WIN32_USE_PTHREADS)
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#endif
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
#if defined(DQN_OS_UNIX) || defined(DQN_PLATFORM_EMSCRIPTEN)
|
2023-08-16 11:59:38 +00:00
|
|
|
#include <errno.h> // errno
|
|
|
|
#include <fcntl.h> // O_RDONLY ... etc
|
|
|
|
#include <sys/ioctl.h> // ioctl
|
|
|
|
#include <sys/types.h> // pid_t
|
2023-10-24 13:11:48 +00:00
|
|
|
#include <sys/wait.h> // waitpid
|
2023-08-16 11:59:38 +00:00
|
|
|
#include <sys/random.h> // getrandom
|
|
|
|
#include <sys/stat.h> // stat
|
|
|
|
#include <sys/mman.h> // mmap
|
|
|
|
#include <time.h> // clock_gettime, nanosleep
|
2023-10-24 13:11:48 +00:00
|
|
|
#include <unistd.h> // access, gettid, write
|
|
|
|
|
|
|
|
#if defined(DQN_PLATFORM_EMSCRIPTEN)
|
|
|
|
#else
|
|
|
|
#include <sys/sendfile.h> // sendfile
|
|
|
|
#include <linux/fs.h> // FICLONE
|
|
|
|
#endif
|
2023-08-16 11:59:38 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
#if defined(DQN_PLATFORM_EMSCRIPTEN)
|
|
|
|
#include <emscripten/fetch.h> // emscripten_fetch (for Dqn_OSHttpResponse)
|
2023-08-31 04:33:07 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
// NOTE: [$STBS] stb_sprintf ///////////////////////////////////////////////////////////////////////
|
|
|
|
#if defined(DQN_USE_STD_PRINTF)
|
|
|
|
#include <stdio.h>
|
|
|
|
#define DQN_SPRINTF(...) sprintf(__VA_ARGS__)
|
|
|
|
#define DQN_SNPRINTF(...) snprintf(__VA_ARGS__)
|
|
|
|
#define DQN_VSPRINTF(...) vsprintf(__VA_ARGS__)
|
|
|
|
#define DQN_VSNPRINTF(...) vsnprintf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DQN_SPRINTF(...) STB_SPRINTF_DECORATE(sprintf)(__VA_ARGS__)
|
|
|
|
#define DQN_SNPRINTF(...) STB_SPRINTF_DECORATE(snprintf)(__VA_ARGS__)
|
|
|
|
#define DQN_VSPRINTF(...) STB_SPRINTF_DECORATE(vsprintf)(__VA_ARGS__)
|
|
|
|
#define DQN_VSNPRINTF(...) STB_SPRINTF_DECORATE(vsnprintf)(__VA_ARGS__)
|
|
|
|
|
|
|
|
#if (DQN_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)) && defined(DQN_COMPILER_MSVC)
|
2024-03-25 05:11:57 +00:00
|
|
|
#error The STB implementation of sprintf triggers MSVCs implementation of ASAN. Compiling ASAN with STB sprintf is not supported.
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
// NOTE: stb_sprintf assumes c-string literals are 4 byte aligned which is
|
|
|
|
// always true, however, reading past the end of a string whose size is not
|
|
|
|
// a multiple of 4 is UB causing ASAN to complain. This is practically safe
|
|
|
|
// and guaranteed by all compilers so we mute this.
|
|
|
|
//
|
|
|
|
// ==12072==ERROR: AddressSanitizer: global-buffer-overflow on address
|
|
|
|
// READ of size 4 at 0x7ff6f442a0d8 thread T0
|
|
|
|
// #0 0x7ff6f42d3be8 in stbsp_vsprintfcb C:\Home\Code\dqn\dqn_external.cpp:199
|
|
|
|
|
|
|
|
#define STBSP__ASAN __declspec(no_sanitize_address)
|
|
|
|
#endif
|
|
|
|
|
2023-08-16 11:59:38 +00:00
|
|
|
// stb_sprintf - v1.10 - public domain snprintf() implementation
|
|
|
|
// originally by Jeff Roberts / RAD Game Tools, 2015/10/20
|
|
|
|
// http://github.com/nothings/stb
|
|
|
|
//
|
|
|
|
// allowed types: sc uidBboXx p AaGgEef n
|
|
|
|
// lengths : hh h ll j z t I64 I32 I
|
|
|
|
//
|
|
|
|
// Contributors:
|
|
|
|
// Fabian "ryg" Giesen (reformatting)
|
|
|
|
// github:aganm (attribute format)
|
|
|
|
//
|
|
|
|
// Contributors (bugfixes):
|
|
|
|
// github:d26435
|
|
|
|
// github:trex78
|
|
|
|
// github:account-login
|
|
|
|
// Jari Komppa (SI suffixes)
|
|
|
|
// Rohit Nirmal
|
|
|
|
// Marcin Wojdyr
|
|
|
|
// Leonard Ritter
|
|
|
|
// Stefano Zanotti
|
|
|
|
// Adam Allison
|
|
|
|
// Arvid Gerstmann
|
|
|
|
// Markus Kolb
|
|
|
|
//
|
|
|
|
// LICENSE:
|
|
|
|
//
|
|
|
|
// See end of file for license information.
|
|
|
|
|
|
|
|
#ifndef STB_SPRINTF_H_INCLUDE
|
|
|
|
#define STB_SPRINTF_H_INCLUDE
|
|
|
|
|
|
|
|
/*
|
|
|
|
Single file sprintf replacement.
|
|
|
|
|
|
|
|
Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.
|
|
|
|
Hereby placed in public domain.
|
|
|
|
|
|
|
|
This is a full sprintf replacement that supports everything that
|
|
|
|
the C runtime sprintfs support, including float/double, 64-bit integers,
|
|
|
|
hex floats, field parameters (%*.*d stuff), length reads backs, etc.
|
|
|
|
|
|
|
|
Why would you need this if sprintf already exists? Well, first off,
|
|
|
|
it's *much* faster (see below). It's also much smaller than the CRT
|
|
|
|
versions code-space-wise. We've also added some simple improvements
|
|
|
|
that are super handy (commas in thousands, callbacks at buffer full,
|
|
|
|
for example). Finally, the format strings for MSVC and GCC differ
|
|
|
|
for 64-bit integers (among other small things), so this lets you use
|
|
|
|
the same format strings in cross platform code.
|
|
|
|
|
|
|
|
It uses the standard single file trick of being both the header file
|
|
|
|
and the source itself. If you just include it normally, you just get
|
|
|
|
the header file function definitions. To get the code, you include
|
|
|
|
it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first.
|
|
|
|
|
|
|
|
It only uses va_args macros from the C runtime to do it's work. It
|
|
|
|
does cast doubles to S64s and shifts and divides U64s, which does
|
|
|
|
drag in CRT code on most platforms.
|
|
|
|
|
|
|
|
It compiles to roughly 8K with float support, and 4K without.
|
|
|
|
As a comparison, when using MSVC static libs, calling sprintf drags
|
|
|
|
in 16K.
|
|
|
|
|
|
|
|
API:
|
2024-01-31 12:49:23 +00:00
|
|
|
////
|
2023-08-16 11:59:38 +00:00
|
|
|
int stbsp_sprintf( char * buf, char const * fmt, ... )
|
|
|
|
int stbsp_snprintf( char * buf, int count, char const * fmt, ... )
|
|
|
|
Convert an arg list into a buffer. stbsp_snprintf always returns
|
|
|
|
a zero-terminated string (unlike regular snprintf).
|
|
|
|
|
|
|
|
int stbsp_vsprintf( char * buf, char const * fmt, va_list va )
|
|
|
|
int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va )
|
|
|
|
Convert a va_list arg list into a buffer. stbsp_vsnprintf always returns
|
|
|
|
a zero-terminated string (unlike regular snprintf).
|
|
|
|
|
|
|
|
int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )
|
|
|
|
typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len );
|
|
|
|
Convert into a buffer, calling back every STB_SPRINTF_MIN chars.
|
|
|
|
Your callback can then copy the chars out, print them or whatever.
|
|
|
|
This function is actually the workhorse for everything else.
|
|
|
|
The buffer you pass in must hold at least STB_SPRINTF_MIN characters.
|
|
|
|
// you return the next buffer to use or 0 to stop converting
|
|
|
|
|
|
|
|
void stbsp_set_separators( char comma, char period )
|
|
|
|
Set the comma and period characters to use.
|
|
|
|
|
|
|
|
FLOATS/DOUBLES:
|
2024-01-31 12:49:23 +00:00
|
|
|
///////////////
|
2023-08-16 11:59:38 +00:00
|
|
|
This code uses a internal float->ascii conversion method that uses
|
|
|
|
doubles with error correction (double-doubles, for ~105 bits of
|
|
|
|
precision). This conversion is round-trip perfect - that is, an atof
|
|
|
|
of the values output here will give you the bit-exact double back.
|
|
|
|
|
|
|
|
One difference is that our insignificant digits will be different than
|
|
|
|
with MSVC or GCC (but they don't match each other either). We also
|
|
|
|
don't attempt to find the minimum length matching float (pre-MSVC15
|
|
|
|
doesn't either).
|
|
|
|
|
|
|
|
If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT
|
|
|
|
and you'll save 4K of code space.
|
|
|
|
|
|
|
|
64-BIT INTS:
|
2024-01-31 12:49:23 +00:00
|
|
|
////////////
|
2023-08-16 11:59:38 +00:00
|
|
|
This library also supports 64-bit integers and you can use MSVC style or
|
|
|
|
GCC style indicators (%I64d or %lld). It supports the C99 specifiers
|
|
|
|
for size_t and ptr_diff_t (%jd %zd) as well.
|
|
|
|
|
|
|
|
EXTRAS:
|
2024-01-31 12:49:23 +00:00
|
|
|
///////
|
2023-08-16 11:59:38 +00:00
|
|
|
Like some GCCs, for integers and floats, you can use a ' (single quote)
|
|
|
|
specifier and commas will be inserted on the thousands: "%'d" on 12345
|
|
|
|
would print 12,345.
|
|
|
|
|
|
|
|
For integers and floats, you can use a "$" specifier and the number
|
|
|
|
will be converted to float and then divided to get kilo, mega, giga or
|
|
|
|
tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is
|
|
|
|
"2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn
|
|
|
|
2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three
|
|
|
|
$:s: "%$$$d" -> "2.42 M". To remove the space between the number and the
|
|
|
|
suffix, add "_" specifier: "%_$d" -> "2.53M".
|
|
|
|
|
|
|
|
In addition to octal and hexadecimal conversions, you can print
|
|
|
|
integers in binary: "%b" for 256 would print 100.
|
|
|
|
|
|
|
|
PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
|
2024-01-31 12:49:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////
|
2023-08-16 11:59:38 +00:00
|
|
|
"%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)
|
|
|
|
"%24d" across all 32-bit ints (4.5x/4.2x faster)
|
|
|
|
"%x" across all 32-bit ints (4.5x/3.8x faster)
|
|
|
|
"%08x" across all 32-bit ints (4.3x/3.8x faster)
|
|
|
|
"%f" across e-10 to e+10 floats (7.3x/6.0x faster)
|
|
|
|
"%e" across e-10 to e+10 floats (8.1x/6.0x faster)
|
|
|
|
"%g" across e-10 to e+10 floats (10.0x/7.1x faster)
|
|
|
|
"%f" for values near e-300 (7.9x/6.5x faster)
|
|
|
|
"%f" for values near e+300 (10.0x/9.1x faster)
|
|
|
|
"%e" for values near e-300 (10.1x/7.0x faster)
|
|
|
|
"%e" for values near e+300 (9.2x/6.0x faster)
|
|
|
|
"%.320f" for values near e-300 (12.6x/11.2x faster)
|
|
|
|
"%a" for random values (8.6x/4.3x faster)
|
|
|
|
"%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)
|
|
|
|
"%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)
|
|
|
|
"%s%s%s" for 64 char strings (7.1x/7.3x faster)
|
|
|
|
"...512 char string..." ( 35.0x/32.5x faster!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#if defined(__has_feature) && defined(__has_attribute)
|
|
|
|
#if __has_feature(address_sanitizer)
|
|
|
|
#if __has_attribute(__no_sanitize__)
|
|
|
|
#define STBSP__ASAN __attribute__((__no_sanitize__("address")))
|
|
|
|
#elif __has_attribute(__no_sanitize_address__)
|
|
|
|
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
|
|
|
|
#elif __has_attribute(__no_address_safety_analysis__)
|
|
|
|
#define STBSP__ASAN __attribute__((__no_address_safety_analysis__))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#elif defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
|
|
|
|
#if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__
|
|
|
|
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef STBSP__ASAN
|
|
|
|
#define STBSP__ASAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef STB_SPRINTF_STATIC
|
|
|
|
#define STBSP__PUBLICDEC static
|
|
|
|
#define STBSP__PUBLICDEF static STBSP__ASAN
|
|
|
|
#else
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#define STBSP__PUBLICDEC extern "C"
|
|
|
|
#define STBSP__PUBLICDEF extern "C" STBSP__ASAN
|
|
|
|
#else
|
|
|
|
#define STBSP__PUBLICDEC extern
|
|
|
|
#define STBSP__PUBLICDEF STBSP__ASAN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__has_attribute)
|
|
|
|
#if __has_attribute(format)
|
|
|
|
#define STBSP__ATTRIBUTE_FORMAT(fmt,va) __attribute__((format(printf,fmt,va)))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef STBSP__ATTRIBUTE_FORMAT
|
|
|
|
#define STBSP__ATTRIBUTE_FORMAT(fmt,va)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define STBSP__NOTUSED(v) (void)(v)
|
|
|
|
#else
|
|
|
|
#define STBSP__NOTUSED(v) (void)sizeof(v)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdarg.h> // for va_arg(), va_list()
|
|
|
|
#include <stddef.h> // size_t, ptrdiff_t
|
|
|
|
|
|
|
|
#ifndef STB_SPRINTF_MIN
|
|
|
|
#define STB_SPRINTF_MIN 512 // how many characters per callback
|
|
|
|
#endif
|
|
|
|
typedef char *STBSP_SPRINTFCB(const char *buf, void *user, int len);
|
|
|
|
|
|
|
|
#ifndef STB_SPRINTF_DECORATE
|
|
|
|
#define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names
|
|
|
|
#endif
|
|
|
|
|
|
|
|
STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va);
|
|
|
|
STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va);
|
|
|
|
STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(2,3);
|
|
|
|
STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(3,4);
|
|
|
|
|
|
|
|
STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va);
|
|
|
|
STBSP__PUBLICDEC void STB_SPRINTF_DECORATE(set_separators)(char comma, char period);
|
|
|
|
#endif // STB_SPRINTF_H_INCLUDE
|
2024-01-31 12:49:23 +00:00
|
|
|
#endif // !defined(DQN_USE_STD_PRINTF)
|