forked from nothings/stb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stb.h
14454 lines (12695 loc) · 455 KB
/
stb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* stb.h - v2.37 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h
no warranty is offered or implied; use this code at your own risk
This is a single header file with a bunch of useful utilities
for getting stuff done in C/C++.
Documentation: http://nothings.org/stb/stb_h.html
Unit tests: http://nothings.org/stb/stb.c
============================================================================
You MUST
#define STB_DEFINE
in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
include, like this:
#define STB_DEFINE
#include "stb.h"
All other files should just #include "stb.h" without the #define.
============================================================================
Version History
2.36 various fixes
2.35 fix clang-cl issues with swprintf
2.34 fix warnings
2.33 more fixes to random numbers
2.32 stb_intcmprev, stb_uidict, fix random numbers on Linux
2.31 stb_ucharcmp
2.30 MinGW fix
2.29 attempt to fix use of swprintf()
2.28 various new functionality
2.27 test _WIN32 not WIN32 in STB_THREADS
2.26 various warning & bugfixes
2.25 various warning & bugfixes
2.24 various warning & bugfixes
2.23 fix 2.22
2.22 64-bit fixes from '!='; fix stb_sdict_copy() to have preferred name
2.21 utf-8 decoder rejects "overlong" encodings; attempted 64-bit improvements
2.20 fix to hash "copy" function--reported by someone with handle "!="
2.19 ???
2.18 stb_readdir_subdirs_mask
2.17 stb_cfg_dir
2.16 fix stb_bgio_, add stb_bgio_stat(); begin a streaming wrapper
2.15 upgraded hash table template to allow:
- aggregate keys (explicit comparison func for EMPTY and DEL keys)
- "static" implementations (so they can be culled if unused)
2.14 stb_mprintf
2.13 reduce identifiable strings in STB_NO_STB_STRINGS
2.12 fix STB_ONLY -- lots of uint32s, TRUE/FALSE things had crept in
2.11 fix bug in stb_dirtree_get() which caused "c://path" sorts of stuff
2.10 STB_F(), STB_I() inline constants (also KI,KU,KF,KD)
2.09 stb_box_face_vertex_axis_side
2.08 bugfix stb_trimwhite()
2.07 colored printing in windows (why are we in 1985?)
2.06 comparison functions are now functions-that-return-functions and
accept a struct-offset as a parameter (not thread-safe)
2.05 compile and pass tests under Linux (but no threads); thread cleanup
2.04 stb_cubic_bezier_1d, smoothstep, avoid dependency on registry
2.03 ?
2.02 remove integrated documentation
2.01 integrate various fixes; stb_force_uniprocessor
2.00 revised stb_dupe to use multiple hashes
1.99 stb_charcmp
1.98 stb_arr_deleten, stb_arr_insertn
1.97 fix stb_newell_normal()
1.96 stb_hash_number()
1.95 hack stb__rec_max; clean up recursion code to use new functions
1.94 stb_dirtree; rename stb_extra to stb_ptrmap
1.93 stb_sem_new() API cleanup (no blockflag-starts blocked; use 'extra')
1.92 stb_threadqueue--multi reader/writer queue, fixed size or resizeable
1.91 stb_bgio_* for reading disk asynchronously
1.90 stb_mutex uses CRITICAL_REGION; new stb_sync primitive for thread
joining; workqueue supports stb_sync instead of stb_semaphore
1.89 support ';' in constant-string wildcards; stb_mutex wrapper (can
implement with EnterCriticalRegion eventually)
1.88 portable threading API (only for win32 so far); worker thread queue
1.87 fix wildcard handling in stb_readdir_recursive
1.86 support ';' in wildcards
1.85 make stb_regex work with non-constant strings;
beginnings of stb_introspect()
1.84 (forgot to make notes)
1.83 whoops, stb_keep_if_different wasn't deleting the temp file
1.82 bring back stb_compress from stb_file.h for cmirror
1.81 various bugfixes, STB_FASTMALLOC_INIT inits FASTMALLOC in release
1.80 stb_readdir returns utf8; write own utf8-utf16 because lib was wrong
1.79 stb_write
1.78 calloc() support for malloc wrapper, STB_FASTMALLOC
1.77 STB_FASTMALLOC
1.76 STB_STUA - Lua-like language; (stb_image, stb_csample, stb_bilinear)
1.75 alloc/free array of blocks; stb_hheap bug; a few stb_ps_ funcs;
hash*getkey, hash*copy; stb_bitset; stb_strnicmp; bugfix stb_bst
1.74 stb_replaceinplace; use stdlib C function to convert utf8 to UTF-16
1.73 fix performance bug & leak in stb_ischar (C++ port lost a 'static')
1.72 remove stb_block, stb_block_manager, stb_decompress (to stb_file.h)
1.71 stb_trimwhite, stb_tokens_nested, etc.
1.70 back out 1.69 because it might problemize mixed builds; stb_filec()
1.69 (stb_file returns 'char *' in C++)
1.68 add a special 'tree root' data type for stb_bst; stb_arr_end
1.67 full C++ port. (stb_block_manager)
1.66 stb_newell_normal
1.65 stb_lex_item_wild -- allow wildcard items which MUST match entirely
1.64 stb_data
1.63 stb_log_name
1.62 stb_define_sort; C++ cleanup
1.61 stb_hash_fast -- Paul Hsieh's hash function (beats Bob Jenkins'?)
1.60 stb_delete_directory_recursive
1.59 stb_readdir_recursive
1.58 stb_bst variant with parent pointer for O(1) iteration, not O(log N)
1.57 replace LCG random with Mersenne Twister (found a public domain one)
1.56 stb_perfect_hash, stb_ischar, stb_regex
1.55 new stb_bst API allows multiple BSTs per node (e.g. secondary keys)
1.54 bugfix: stb_define_hash, stb_wildmatch, regexp
1.53 stb_define_hash; recoded stb_extra, stb_sdict use it
1.52 stb_rand_define, stb_bst, stb_reverse
1.51 fix 'stb_arr_setlen(NULL, 0)'
1.50 stb_wordwrap
1.49 minor improvements to enable the scripting language
1.48 better approach for stb_arr using stb_malloc; more invasive, clearer
1.47 stb_lex (lexes stb.h at 1.5ML/s on 3Ghz P4; 60/70% of optimal/flex)
1.46 stb_wrapper_*, STB_MALLOC_WRAPPER
1.45 lightly tested DFA acceleration of regexp searching
1.44 wildcard matching & searching; regexp matching & searching
1.43 stb_temp
1.42 allow stb_arr to use stb_malloc/realloc; note this is global
1.41 make it compile in C++; (disable stb_arr in C++)
1.40 stb_dupe tweak; stb_swap; stb_substr
1.39 stb_dupe; improve stb_file_max to be less stupid
1.38 stb_sha1_file: generate sha1 for file, even > 4GB
1.37 stb_file_max; partial support for utf8 filenames in Windows
1.36 remove STB__NO_PREFIX - poor interaction with IDE, not worth it
streamline stb_arr to make it separately publishable
1.35 bugfixes for stb_sdict, stb_malloc(0), stristr
1.34 (streaming interfaces for stb_compress)
1.33 stb_alloc; bug in stb_getopt; remove stb_overflow
1.32 (stb_compress returns, smaller&faster; encode window & 64-bit len)
1.31 stb_prefix_count
1.30 (STB__NO_PREFIX - remove stb_ prefixes for personal projects)
1.29 stb_fput_varlen64, etc.
1.28 stb_sha1
1.27 ?
1.26 stb_extra
1.25 ?
1.24 stb_copyfile
1.23 stb_readdir
1.22 ?
1.21 ?
1.20 ?
1.19 ?
1.18 ?
1.17 ?
1.16 ?
1.15 stb_fixpath, stb_splitpath, stb_strchr2
1.14 stb_arr
1.13 ?stb, stb_log, stb_fatal
1.12 ?stb_hash2
1.11 miniML
1.10 stb_crc32, stb_adler32
1.09 stb_sdict
1.08 stb_bitreverse, stb_ispow2, stb_big32
stb_fopen, stb_fput_varlen, stb_fput_ranged
stb_fcmp, stb_feq
1.07 (stb_encompress)
1.06 stb_compress
1.05 stb_tokens, (stb_hheap)
1.04 stb_rand
1.03 ?(s-strings)
1.02 ?stb_filelen, stb_tokens
1.01 stb_tolower
1.00 stb_hash, stb_intcmp
stb_file, stb_stringfile, stb_fgets
stb_prefix, stb_strlower, stb_strtok
stb_image
(stb_array), (stb_arena)
Parenthesized items have since been removed.
LICENSE
See end of file for license information.
CREDITS
Written by Sean Barrett.
Fixes:
Philipp Wiesemann
Robert Nix
r-lyeh
blackpawn
github:Mojofreem
Ryan Whitworth
Vincent Isambart
Mike Sartain
Eugene Opalev
Tim Sjostrand
github:infatum
Dave Butler (Croepha)
Ethan Lee (flibitijibibo)
Brian Collins
Kyle Langley
*/
#include <stdarg.h>
#ifndef STB__INCLUDE_STB_H
#define STB__INCLUDE_STB_H
#define STB_VERSION 1
#ifdef STB_INTROSPECT
#define STB_DEFINE
#endif
#ifdef STB_DEFINE_THREADS
#ifndef STB_DEFINE
#define STB_DEFINE
#endif
#ifndef STB_THREADS
#define STB_THREADS
#endif
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#ifndef _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_NON_CONFORMING_SWPRINTFS
#endif
#if !defined(_MSC_VER) || _MSC_VER > 1700
#include <intrin.h> // _BitScanReverse
#endif
#endif
#include <stdlib.h> // stdlib could have min/max
#include <stdio.h> // need FILE
#include <string.h> // stb_define_hash needs memcpy/memset
#include <time.h> // stb_dirtree
#ifdef __MINGW32__
#include <fcntl.h> // O_RDWR
#endif
#ifdef STB_PERSONAL
typedef int Bool;
#define False 0
#define True 1
#endif
#ifdef STB_MALLOC_WRAPPER_PAGED
#define STB_MALLOC_WRAPPER_DEBUG
#endif
#ifdef STB_MALLOC_WRAPPER_DEBUG
#define STB_MALLOC_WRAPPER
#endif
#ifdef STB_MALLOC_WRAPPER_FASTMALLOC
#define STB_FASTMALLOC
#define STB_MALLOC_WRAPPER
#endif
#ifdef STB_FASTMALLOC
#ifndef _WIN32
#undef STB_FASTMALLOC
#endif
#endif
#ifdef STB_DEFINE
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <ctype.h>
#include <math.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <io.h> // _mktemp
#include <direct.h> // _rmdir
#endif
#include <sys/types.h> // stat()/_stat()
#include <sys/stat.h> // stat()/_stat()
#endif
#define stb_min(a,b) ((a) < (b) ? (a) : (b))
#define stb_max(a,b) ((a) > (b) ? (a) : (b))
#ifndef STB_ONLY
#if !defined(__cplusplus) && !defined(min) && !defined(max)
#define min(x,y) stb_min(x,y)
#define max(x,y) stb_max(x,y)
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef deg2rad
#define deg2rad(a) ((a)*(M_PI/180))
#endif
#ifndef rad2deg
#define rad2deg(a) ((a)*(180/M_PI))
#endif
#ifndef swap
#ifndef __cplusplus
#define swap(TYPE,a,b) \
do { TYPE stb__t; stb__t = (a); (a) = (b); (b) = stb__t; } while (0)
#endif
#endif
typedef unsigned char uint8 ;
typedef signed char int8 ;
typedef unsigned short uint16;
typedef signed short int16;
#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
typedef unsigned long uint32;
typedef signed long int32;
#else
typedef unsigned int uint32;
typedef signed int int32;
#endif
typedef unsigned char uchar ;
typedef unsigned short ushort;
typedef unsigned int uint ;
typedef unsigned long ulong ;
// produce compile errors if the sizes aren't right
typedef char stb__testsize16[sizeof(int16)==2];
typedef char stb__testsize32[sizeof(int32)==4];
#endif
#ifndef STB_TRUE
#define STB_TRUE 1
#define STB_FALSE 0
#endif
// if we're STB_ONLY, can't rely on uint32 or even uint, so all the
// variables we'll use herein need typenames prefixed with 'stb':
typedef unsigned char stb_uchar;
typedef unsigned char stb_uint8;
typedef unsigned int stb_uint;
typedef unsigned short stb_uint16;
typedef short stb_int16;
typedef signed char stb_int8;
#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
typedef unsigned long stb_uint32;
typedef long stb_int32;
#else
typedef unsigned int stb_uint32;
typedef int stb_int32;
#endif
typedef char stb__testsize2_16[sizeof(stb_uint16)==2 ? 1 : -1];
typedef char stb__testsize2_32[sizeof(stb_uint32)==4 ? 1 : -1];
#ifdef _MSC_VER
typedef unsigned __int64 stb_uint64;
typedef __int64 stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ui64)
#define STB_IMM_INT64(literali64) (literali64##i64)
#else
// ??
typedef unsigned long long stb_uint64;
typedef long long stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ULL)
#define STB_IMM_INT64(literali64) (literali64##LL)
#endif
typedef char stb__testsize2_64[sizeof(stb_uint64)==8 ? 1 : -1];
// add platform-specific ways of checking for sizeof(char*) == 8,
// and make those define STB_PTR64
#if defined(_WIN64) || defined(__x86_64__) || defined(__ia64__) || defined(__LP64__)
#define STB_PTR64
#endif
#ifdef STB_PTR64
typedef char stb__testsize2_ptr[sizeof(char *) == 8];
typedef stb_uint64 stb_uinta;
typedef stb_int64 stb_inta;
#else
typedef char stb__testsize2_ptr[sizeof(char *) == 4];
typedef stb_uint32 stb_uinta;
typedef stb_int32 stb_inta;
#endif
typedef char stb__testsize2_uinta[sizeof(stb_uinta)==sizeof(char*) ? 1 : -1];
// if so, we should define an int type that is the pointer size. until then,
// we'll have to make do with this (which is not the same at all!)
typedef union
{
unsigned int i;
void * p;
} stb_uintptr;
#ifdef __cplusplus
#define STB_EXTERN extern "C"
#else
#define STB_EXTERN extern
#endif
// check for well-known debug defines
#if defined(DEBUG) || defined(_DEBUG) || defined(DBG)
#ifndef NDEBUG
#define STB_DEBUG
#endif
#endif
#ifdef STB_DEBUG
#include <assert.h>
#endif
//////////////////////////////////////////////////////////////////////////////
//
// C library function platform handling
//
#ifdef STB_DEFINE
#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
static FILE * stb_p_fopen(const char *filename, const char *mode)
{
FILE *f;
if (0 == fopen_s(&f, filename, mode))
return f;
else
return NULL;
}
static FILE * stb_p_wfopen(const wchar_t *filename, const wchar_t *mode)
{
FILE *f;
if (0 == _wfopen_s(&f, filename, mode))
return f;
else
return NULL;
}
static char *stb_p_strcpy_s(char *a, size_t size, const char *b)
{
strcpy_s(a,size,b);
return a;
}
static char *stb_p_strncpy_s(char *a, size_t size, const char *b, size_t count)
{
strncpy_s(a,size,b,count);
return a;
}
#define stb_p_mktemp(s) (_mktemp_s(s, strlen(s)+1) == 0)
#define stb_p_sprintf sprintf_s
#define stb_p_size(x) ,(x)
#else
#define stb_p_fopen fopen
#define stb_p_wfopen _wfopen
#define stb_p_strcpy_s(a,s,b) strcpy(a,b)
#define stb_p_strncpy_s(a,s,b,c) strncpy(a,b,c)
#define stb_p_mktemp(s) (mktemp(s) != NULL)
#define stb_p_sprintf sprintf
#define stb_p_size(x)
#endif
#if defined(_WIN32)
#define stb_p_vsnprintf _vsnprintf
#else
#define stb_p_vsnprintf vsnprintf
#endif
#endif // STB_DEFINE
#if defined(_WIN32) && (_MSC_VER >= 1300)
#define stb_p_stricmp _stricmp
#define stb_p_strnicmp _strnicmp
#define stb_p_strdup _strdup
#else
#define stb_p_strdup strdup
#define stb_p_stricmp stricmp
#define stb_p_strnicmp strnicmp
#endif
STB_EXTERN void stb_wrapper_malloc(void *newp, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_free(void *oldp, char *file, int line);
STB_EXTERN void stb_wrapper_realloc(void *oldp, void *newp, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_calloc(size_t num, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_listall(void (*func)(void *ptr, size_t sz, char *file, int line));
STB_EXTERN void stb_wrapper_dump(char *filename);
STB_EXTERN size_t stb_wrapper_allocsize(void *oldp);
STB_EXTERN void stb_wrapper_check(void *oldp);
#ifdef STB_DEFINE
// this is a special function used inside malloc wrapper
// to do allocations that aren't tracked (to avoid
// reentrancy). Of course if someone _else_ wraps realloc,
// this breaks, but if they're doing that AND the malloc
// wrapper they need to explicitly check for reentrancy.
//
// only define realloc_raw() and we do realloc(NULL,sz)
// for malloc() and realloc(p,0) for free().
static void * stb__realloc_raw(void *p, int sz)
{
if (p == NULL) return malloc(sz);
if (sz == 0) { free(p); return NULL; }
return realloc(p,sz);
}
#endif
#ifdef _WIN32
STB_EXTERN void * stb_smalloc(size_t sz);
STB_EXTERN void stb_sfree(void *p);
STB_EXTERN void * stb_srealloc(void *p, size_t sz);
STB_EXTERN void * stb_scalloc(size_t n, size_t sz);
STB_EXTERN char * stb_sstrdup(char *s);
#endif
#ifdef STB_FASTMALLOC
#define malloc stb_smalloc
#define free stb_sfree
#define realloc stb_srealloc
#define strdup stb_sstrdup
#define calloc stb_scalloc
#endif
#ifndef STB_MALLOC_ALLCHECK
#define stb__check(p) 1
#else
#ifndef STB_MALLOC_WRAPPER
#error STB_MALLOC_ALLCHECK requires STB_MALLOC_WRAPPER
#else
#define stb__check(p) stb_mcheck(p)
#endif
#endif
#ifdef STB_MALLOC_WRAPPER
STB_EXTERN void * stb__malloc(size_t, char *, int);
STB_EXTERN void * stb__realloc(void *, size_t, char *, int);
STB_EXTERN void * stb__calloc(size_t n, size_t s, char *, int);
STB_EXTERN void stb__free(void *, char *file, int);
STB_EXTERN char * stb__strdup(char *s, char *file, int);
STB_EXTERN void stb_malloc_checkall(void);
STB_EXTERN void stb_malloc_check_counter(int init_delay, int rep_delay);
#ifndef STB_MALLOC_WRAPPER_DEBUG
#define stb_mcheck(p) 1
#else
STB_EXTERN int stb_mcheck(void *);
#endif
#ifdef STB_DEFINE
#ifdef STB_MALLOC_WRAPPER_DEBUG
#define STB__PAD 32
#define STB__BIAS 16
#define STB__SIG 0x51b01234
#define STB__FIXSIZE(sz) (((sz+3) & ~3) + STB__PAD)
#define STB__ptr(x,y) ((char *) (x) + (y))
#else
#define STB__ptr(x,y) (x)
#define STB__FIXSIZE(sz) (sz)
#endif
#ifdef STB_MALLOC_WRAPPER_DEBUG
int stb_mcheck(void *p)
{
unsigned int sz;
if (p == NULL) return 1;
p = ((char *) p) - STB__BIAS;
sz = * (unsigned int *) p;
assert(* (unsigned int *) STB__ptr(p,4) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,8) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,12) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,sz-4) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-8) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-12) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-16) == STB__SIG+1);
stb_wrapper_check(STB__ptr(p, STB__BIAS));
return 1;
}
static void stb__check2(void *p, size_t sz, char *file, int line)
{
stb_mcheck(p);
}
void stb_malloc_checkall(void)
{
stb_wrapper_listall(stb__check2);
}
#else
void stb_malloc_checkall(void) { }
#endif
static int stb__malloc_wait=(1 << 30), stb__malloc_next_wait = (1 << 30), stb__malloc_iter;
void stb_malloc_check_counter(int init_delay, int rep_delay)
{
stb__malloc_wait = init_delay;
stb__malloc_next_wait = rep_delay;
}
void stb_mcheck_all(void)
{
#ifdef STB_MALLOC_WRAPPER_DEBUG
++stb__malloc_iter;
if (--stb__malloc_wait <= 0) {
stb_malloc_checkall();
stb__malloc_wait = stb__malloc_next_wait;
}
#endif
}
#ifdef STB_MALLOC_WRAPPER_PAGED
#define STB__WINDOWS_PAGE (1 << 12)
#ifndef _WINDOWS_
STB_EXTERN __declspec(dllimport) void * __stdcall VirtualAlloc(void *p, unsigned long size, unsigned long type, unsigned long protect);
STB_EXTERN __declspec(dllimport) int __stdcall VirtualFree(void *p, unsigned long size, unsigned long freetype);
#endif
#endif
static void *stb__malloc_final(size_t sz)
{
#ifdef STB_MALLOC_WRAPPER_PAGED
size_t aligned = (sz + STB__WINDOWS_PAGE - 1) & ~(STB__WINDOWS_PAGE-1);
char *p = VirtualAlloc(NULL, aligned + STB__WINDOWS_PAGE, 0x2000, 0x04); // RESERVE, READWRITE
if (p == NULL) return p;
VirtualAlloc(p, aligned, 0x1000, 0x04); // COMMIT, READWRITE
return p;
#else
return malloc(sz);
#endif
}
static void stb__free_final(void *p)
{
#ifdef STB_MALLOC_WRAPPER_PAGED
VirtualFree(p, 0, 0x8000); // RELEASE
#else
free(p);
#endif
}
int stb__malloc_failure;
#ifdef STB_MALLOC_WRAPPER_PAGED
static void *stb__realloc_final(void *p, size_t sz, size_t old_sz)
{
void *q = stb__malloc_final(sz);
if (q == NULL)
return ++stb__malloc_failure, q;
// @TODO: deal with p being smaller!
memcpy(q, p, sz < old_sz ? sz : old_sz);
stb__free_final(p);
return q;
}
#endif
void stb__free(void *p, char *file, int line)
{
stb_mcheck_all();
if (!p) return;
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_mcheck(p);
#endif
stb_wrapper_free(p,file,line);
#ifdef STB_MALLOC_WRAPPER_DEBUG
p = STB__ptr(p,-STB__BIAS);
* (unsigned int *) STB__ptr(p,0) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,4) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,8) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,12) = 0xdeadbeef;
#endif
stb__free_final(p);
}
void * stb__malloc(size_t sz, char *file, int line)
{
void *p;
stb_mcheck_all();
if (sz == 0) return NULL;
p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) {
++stb__malloc_failure;
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_malloc_checkall();
#endif
return p;
}
#ifdef STB_MALLOC_WRAPPER_DEBUG
* (int *) STB__ptr(p,0) = STB__FIXSIZE(sz);
* (unsigned int *) STB__ptr(p,4) = STB__SIG;
* (unsigned int *) STB__ptr(p,8) = STB__SIG;
* (unsigned int *) STB__ptr(p,12) = STB__SIG;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-4) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-8) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-12) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-16) = STB__SIG+1;
p = STB__ptr(p, STB__BIAS);
#endif
stb_wrapper_malloc(p,sz,file,line);
return p;
}
void * stb__realloc(void *p, size_t sz, char *file, int line)
{
void *q;
stb_mcheck_all();
if (p == NULL) return stb__malloc(sz,file,line);
if (sz == 0 ) { stb__free(p,file,line); return NULL; }
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_mcheck(p);
p = STB__ptr(p,-STB__BIAS);
#endif
#ifdef STB_MALLOC_WRAPPER_PAGED
{
size_t n = stb_wrapper_allocsize(STB__ptr(p,STB__BIAS));
if (!n)
stb_wrapper_check(STB__ptr(p,STB__BIAS));
q = stb__realloc_final(p, STB__FIXSIZE(sz), STB__FIXSIZE(n));
}
#else
q = realloc(p, STB__FIXSIZE(sz));
#endif
if (q == NULL)
return ++stb__malloc_failure, q;
#ifdef STB_MALLOC_WRAPPER_DEBUG
* (int *) STB__ptr(q,0) = STB__FIXSIZE(sz);
* (unsigned int *) STB__ptr(q,4) = STB__SIG;
* (unsigned int *) STB__ptr(q,8) = STB__SIG;
* (unsigned int *) STB__ptr(q,12) = STB__SIG;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-4) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-8) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-12) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-16) = STB__SIG+1;
q = STB__ptr(q, STB__BIAS);
p = STB__ptr(p, STB__BIAS);
#endif
stb_wrapper_realloc(p,q,sz,file,line);
return q;
}
STB_EXTERN int stb_log2_ceil(size_t);
static void *stb__calloc(size_t n, size_t sz, char *file, int line)
{
void *q;
stb_mcheck_all();
if (n == 0 || sz == 0) return NULL;
if (stb_log2_ceil(n) + stb_log2_ceil(sz) >= 32) return NULL;
q = stb__malloc(n*sz, file, line);
if (q) memset(q, 0, n*sz);
return q;
}
char * stb__strdup(char *s, char *file, int line)
{
char *p;
stb_mcheck_all();
p = stb__malloc(strlen(s)+1, file, line);
if (!p) return p;
stb_p_strcpy_s(p, strlen(s)+1, s);
return p;
}
#endif // STB_DEFINE
#ifdef STB_FASTMALLOC
#undef malloc
#undef realloc
#undef free
#undef strdup
#undef calloc
#endif
// include everything that might define these, BEFORE making macros
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define malloc(s) stb__malloc ( s, __FILE__, __LINE__)
#define realloc(p,s) stb__realloc(p,s, __FILE__, __LINE__)
#define calloc(n,s) stb__calloc (n,s, __FILE__, __LINE__)
#define free(p) stb__free (p, __FILE__, __LINE__)
#define strdup(p) stb__strdup (p, __FILE__, __LINE__)
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Windows pretty display
//
STB_EXTERN void stbprint(const char *fmt, ...);
STB_EXTERN char *stb_sprintf(const char *fmt, ...);
STB_EXTERN char *stb_mprintf(const char *fmt, ...);
STB_EXTERN int stb_snprintf(char *s, size_t n, const char *fmt, ...);
STB_EXTERN int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v);
#ifdef STB_DEFINE
int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v)
{
int res;
#ifdef _WIN32
#ifdef __STDC_WANT_SECURE_LIB__
res = _vsnprintf_s(s, n, _TRUNCATE, fmt, v);
#else
res = stb_p_vsnprintf(s,n,fmt,v);
#endif
#else
res = vsnprintf(s,n,fmt,v);
#endif
if (n) s[n-1] = 0;
// Unix returns length output would require, Windows returns negative when truncated.
return (res >= (int) n || res < 0) ? -1 : res;
}
int stb_snprintf(char *s, size_t n, const char *fmt, ...)
{
int res;
va_list v;
va_start(v,fmt);
res = stb_vsnprintf(s, n, fmt, v);
va_end(v);
return res;
}
char *stb_sprintf(const char *fmt, ...)
{
static char buffer[1024];
va_list v;
va_start(v,fmt);
stb_vsnprintf(buffer,1024,fmt,v);
va_end(v);
return buffer;
}
char *stb_mprintf(const char *fmt, ...)
{
static char buffer[1024];
va_list v;
va_start(v,fmt);
stb_vsnprintf(buffer,1024,fmt,v);
va_end(v);
return stb_p_strdup(buffer);
}
#ifdef _WIN32
#ifndef _WINDOWS_
STB_EXTERN __declspec(dllimport) int __stdcall WriteConsoleA(void *, const void *, unsigned int, unsigned int *, void *);
STB_EXTERN __declspec(dllimport) void * __stdcall GetStdHandle(unsigned int);
STB_EXTERN __declspec(dllimport) int __stdcall SetConsoleTextAttribute(void *, unsigned short);
#endif
static void stb__print_one(void *handle, char *s, ptrdiff_t len)
{
if (len)
if (0==WriteConsoleA(handle, s, (unsigned) len, NULL,NULL))
// if it fails, maybe redirected, so output normally...
// but it's supriously reporting failure now on Win7 and later
{}//fwrite(s, 1, (unsigned) len, stdout);
}
static void stb__print(char *s)
{
void *handle = GetStdHandle((unsigned int) -11); // STD_OUTPUT_HANDLE
int pad=0; // number of padding characters to add
char *t = s;
while (*s) {
int lpad;
while (*s && *s != '{') {
if (pad) {
if (*s == '\r' || *s == '\n')
pad = 0;
else if (s[0] == ' ' && s[1] == ' ') {
stb__print_one(handle, t, s-t);
t = s;
while (pad) {
stb__print_one(handle, t, 1);
--pad;
}
}
}
++s;
}
if (!*s) break;
stb__print_one(handle, t, s-t);
if (s[1] == '{') {
++s;
continue;
}
if (s[1] == '#') {
t = s+3;
if (isxdigit(s[2]))
if (isdigit(s[2]))
SetConsoleTextAttribute(handle, s[2] - '0');
else
SetConsoleTextAttribute(handle, tolower(s[2]) - 'a' + 10);
else {
SetConsoleTextAttribute(handle, 0x0f);
t=s+2;
}
} else if (s[1] == '!') {
SetConsoleTextAttribute(handle, 0x0c);
t = s+2;
} else if (s[1] == '@') {
SetConsoleTextAttribute(handle, 0x09);
t = s+2;
} else if (s[1] == '$') {
SetConsoleTextAttribute(handle, 0x0a);
t = s+2;
} else {
SetConsoleTextAttribute(handle, 0x08); // 0,7,8,15 => shades of grey
t = s+1;
}
lpad = (int) (t-s);
s = t;
while (*s && *s != '}') ++s;
if (!*s) break;
stb__print_one(handle, t, s-t);
if (s[1] == '}') {
t = s+2;
} else {
pad += 1+lpad;
t = s+1;
}
s=t;
SetConsoleTextAttribute(handle, 0x07);
}
stb__print_one(handle, t, s-t);
SetConsoleTextAttribute(handle, 0x07);
}
void stbprint(const char *fmt, ...)
{
int res;
char buffer[1024];
char *tbuf = buffer;
va_list v;
va_start(v,fmt);
res = stb_vsnprintf(buffer, sizeof(buffer), fmt, v);
va_end(v);
if (res < 0) {
tbuf = (char *) malloc(16384);
va_start(v,fmt);
res = stb_vsnprintf(tbuf,16384, fmt, v);
va_end(v);
tbuf[16383] = 0;
}
stb__print(tbuf);
if (tbuf != buffer)
free(tbuf);
}
#else // _WIN32
void stbprint(const char *fmt, ...)
{
va_list v;
va_start(v,fmt);
vprintf(fmt,v);
va_end(v);
}
#endif // _WIN32
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// Windows UTF8 filename handling
//
// Windows stupidly treats 8-bit filenames as some dopey code page,
// rather than utf-8. If we want to use utf8 filenames, we have to
// convert them to WCHAR explicitly and call WCHAR versions of the
// file functions. So, ok, we do.
#ifdef _WIN32
#define stb__fopen(x,y) stb_p_wfopen((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
#define stb__windows(x,y) x
#else
#define stb__fopen(x,y) stb_p_fopen(x,y)
#define stb__windows(x,y) y
#endif
typedef unsigned short stb__wchar;