-
Notifications
You must be signed in to change notification settings - Fork 407
/
gc_priv.h
2742 lines (2452 loc) · 115 KB
/
gc_priv.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
/*
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
* Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
*
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*/
#ifndef GC_PRIVATE_H
#define GC_PRIVATE_H
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifndef GC_BUILD
# define GC_BUILD
#endif
#if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
&& !defined(_GNU_SOURCE)
/* Can't test LINUX, since this must be defined before other includes. */
# define _GNU_SOURCE 1
#endif
#if defined(__INTERIX) && !defined(_ALL_SOURCE)
# define _ALL_SOURCE 1
#endif
#if (defined(DGUX) && defined(GC_THREADS) || defined(DGUX386_THREADS) \
|| defined(GC_DGUX386_THREADS)) && !defined(_USING_POSIX4A_DRAFT10)
# define _USING_POSIX4A_DRAFT10 1
#endif
#if defined(__MINGW32__) && !defined(__MINGW_EXCPT_DEFINE_PSDK) \
&& defined(__i386__) && defined(GC_EXTERN) /* defined in gc.c */
/* See the description in mark.c. */
# define __MINGW_EXCPT_DEFINE_PSDK 1
#endif
# if defined(NO_DEBUGGING) && !defined(GC_ASSERTIONS) && !defined(NDEBUG)
/* To turn off assertion checking (in atomic_ops.h). */
# define NDEBUG 1
# endif
#ifndef GC_H
# include "../gc.h"
#endif
#include <stdlib.h>
#if !defined(sony_news)
# include <stddef.h>
#endif
#ifdef DGUX
# include <sys/types.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif /* DGUX */
#ifdef BSD_TIME
# include <sys/types.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif /* BSD_TIME */
#ifdef PARALLEL_MARK
# define AO_REQUIRE_CAS
# if !defined(__GNUC__) && !defined(AO_ASSUME_WINDOWS98)
# define AO_ASSUME_WINDOWS98
# endif
#endif
#include "../gc_tiny_fl.h"
#include "../gc_mark.h"
typedef GC_word word;
typedef GC_signed_word signed_word;
typedef unsigned int unsigned32;
typedef int GC_bool;
#define TRUE 1
#define FALSE 0
#ifndef PTR_T_DEFINED
typedef char * ptr_t; /* A generic pointer to which we can add */
/* byte displacements and which can be used */
/* for address comparisons. */
# define PTR_T_DEFINED
#endif
#ifndef SIZE_MAX
# include <limits.h>
#endif
#if defined(SIZE_MAX) && !defined(CPPCHECK)
# define GC_SIZE_MAX ((size_t)SIZE_MAX)
/* Extra cast to workaround some buggy SIZE_MAX definitions. */
#else
# define GC_SIZE_MAX (~(size_t)0)
#endif
#if GC_GNUC_PREREQ(3, 0) && !defined(LINT2)
# define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
/* Equivalent to (expr), but predict that usually (expr)==outcome. */
#else
# define EXPECT(expr, outcome) (expr)
#endif /* __GNUC__ */
/* Saturated addition of size_t values. Used to avoid value wrap */
/* around on overflow. The arguments should have no side effects. */
#define SIZET_SAT_ADD(a, b) \
(EXPECT((a) < GC_SIZE_MAX - (b), TRUE) ? (a) + (b) : GC_SIZE_MAX)
#include "gcconfig.h"
#if !defined(GC_ATOMIC_UNCOLLECTABLE) && defined(ATOMIC_UNCOLLECTABLE)
/* For compatibility with old-style naming. */
# define GC_ATOMIC_UNCOLLECTABLE
#endif
#ifndef GC_INNER
/* This tagging macro must be used at the start of every variable */
/* definition which is declared with GC_EXTERN. Should be also used */
/* for the GC-scope function definitions and prototypes. Must not be */
/* used in gcconfig.h. Shouldn't be used for the debugging-only */
/* functions. Currently, not used for the functions declared in or */
/* called from the "dated" source files (located in "extra" folder). */
# if defined(GC_DLL) && defined(__GNUC__) && !defined(MSWIN32) \
&& !defined(MSWINCE) && !defined(CYGWIN32)
# if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
/* See the corresponding GC_API definition. */
# define GC_INNER __attribute__((__visibility__("hidden")))
# else
/* The attribute is unsupported. */
# define GC_INNER /* empty */
# endif
# else
# define GC_INNER /* empty */
# endif
# define GC_EXTERN extern GC_INNER
/* Used only for the GC-scope variables (prefixed with "GC_") */
/* declared in the header files. Must not be used for thread-local */
/* variables. Must not be used in gcconfig.h. Shouldn't be used for */
/* the debugging-only or profiling-only variables. Currently, not */
/* used for the variables accessed from the "dated" source files */
/* (specific.c/h, and in the "extra" folder). */
/* The corresponding variable definition must start with GC_INNER. */
#endif /* !GC_INNER */
#ifdef __cplusplus
/* Register storage specifier is deprecated in C++11. */
# define REGISTER /* empty */
#else
/* Used only for several local variables in the performance-critical */
/* functions. Should not be used for new code. */
# define REGISTER register
#endif
#ifndef HEADERS_H
# include "gc_hdrs.h"
#endif
#ifndef GC_ATTR_NO_SANITIZE_ADDR
# ifndef ADDRESS_SANITIZER
# define GC_ATTR_NO_SANITIZE_ADDR /* empty */
# elif GC_CLANG_PREREQ(3, 8)
# define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize("address")))
# else
# define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize_address))
# endif
#endif /* !GC_ATTR_NO_SANITIZE_ADDR */
#ifndef GC_ATTR_NO_SANITIZE_MEMORY
# ifndef MEMORY_SANITIZER
# define GC_ATTR_NO_SANITIZE_MEMORY /* empty */
# elif GC_CLANG_PREREQ(3, 8)
# define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
# else
# define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
# endif
#endif /* !GC_ATTR_NO_SANITIZE_MEMORY */
#ifndef GC_ATTR_NO_SANITIZE_THREAD
# ifndef THREAD_SANITIZER
# define GC_ATTR_NO_SANITIZE_THREAD /* empty */
# elif GC_CLANG_PREREQ(3, 8)
# define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
# else
# define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
# endif
#endif /* !GC_ATTR_NO_SANITIZE_THREAD */
#ifndef GC_ATTR_UNUSED
# if GC_GNUC_PREREQ(3, 4)
# define GC_ATTR_UNUSED __attribute__((__unused__))
# else
# define GC_ATTR_UNUSED /* empty */
# endif
#endif /* !GC_ATTR_UNUSED */
#ifdef HAVE_CONFIG_H
/* The "inline" keyword is determined by Autoconf AC_C_INLINE. */
# define GC_INLINE static inline
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__DMC__) \
|| (GC_GNUC_PREREQ(3, 0) && defined(__STRICT_ANSI__)) \
|| defined(__WATCOMC__)
# define GC_INLINE static __inline
#elif GC_GNUC_PREREQ(3, 0) || defined(__sun)
# define GC_INLINE static inline
#else
# define GC_INLINE static
#endif
#ifndef GC_ATTR_NOINLINE
# if GC_GNUC_PREREQ(4, 0)
# define GC_ATTR_NOINLINE __attribute__((__noinline__))
# elif _MSC_VER >= 1400
# define GC_ATTR_NOINLINE __declspec(noinline)
# else
# define GC_ATTR_NOINLINE /* empty */
# endif
#endif
#ifndef GC_API_OSCALL
/* This is used to identify GC routines called by name from OS. */
# if defined(__GNUC__)
# if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
/* Same as GC_API if GC_DLL. */
# define GC_API_OSCALL extern __attribute__((__visibility__("default")))
# else
/* The attribute is unsupported. */
# define GC_API_OSCALL extern
# endif
# else
# define GC_API_OSCALL GC_API
# endif
#endif
#ifndef GC_API_PRIV
# define GC_API_PRIV GC_API
#endif
#if defined(THREADS) && !defined(NN_PLATFORM_CTR) && !defined(SN_TARGET_PSP2)
# include "gc_atomic_ops.h"
# ifndef AO_HAVE_compiler_barrier
# define AO_HAVE_compiler_barrier 1
# endif
#endif
#include "gc_locks.h"
#define GC_WORD_MAX (~(word)0)
# ifdef STACK_GROWS_DOWN
# define COOLER_THAN >
# define HOTTER_THAN <
# define MAKE_COOLER(x,y) if ((word)((x) + (y)) > (word)(x)) {(x) += (y);} \
else (x) = (ptr_t)GC_WORD_MAX
# define MAKE_HOTTER(x,y) (x) -= (y)
# else
# define COOLER_THAN <
# define HOTTER_THAN >
# define MAKE_COOLER(x,y) if ((word)((x) - (y)) < (word)(x)) {(x) -= (y);} \
else (x) = 0
# define MAKE_HOTTER(x,y) (x) += (y)
# endif
#if defined(AMIGA) && defined(__SASC)
# define GC_FAR __far
#else
# define GC_FAR
#endif
/*********************************/
/* */
/* Definitions for conservative */
/* collector */
/* */
/*********************************/
/*********************************/
/* */
/* Easily changeable parameters */
/* */
/*********************************/
/* #define ALL_INTERIOR_POINTERS */
/* Forces all pointers into the interior of an */
/* object to be considered valid. Also causes the */
/* sizes of all objects to be inflated by at least */
/* one byte. This should suffice to guarantee */
/* that in the presence of a compiler that does */
/* not perform garbage-collector-unsafe */
/* optimizations, all portable, strictly ANSI */
/* conforming C programs should be safely usable */
/* with malloc replaced by GC_malloc and free */
/* calls removed. There are several disadvantages: */
/* 1. There are probably no interesting, portable, */
/* strictly ANSI conforming C programs. */
/* 2. This option makes it hard for the collector */
/* to allocate space that is not "pointed to" */
/* by integers, etc. Under SunOS 4.X with a */
/* statically linked libc, we empirically */
/* observed that it would be difficult to */
/* allocate individual objects larger than 100K. */
/* Even if only smaller objects are allocated, */
/* more swap space is likely to be needed. */
/* Fortunately, much of this will never be */
/* touched. */
/* If you can easily avoid using this option, do. */
/* If not, try to keep individual objects small. */
/* This is now really controlled at startup, */
/* through GC_all_interior_pointers. */
EXTERN_C_BEGIN
#ifndef GC_NO_FINALIZATION
# define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
GC_INNER void GC_notify_or_invoke_finalizers(void);
/* If GC_finalize_on_demand is not set, invoke */
/* eligible finalizers. Otherwise: */
/* Call *GC_finalizer_notifier if there are */
/* finalizers to be run, and we haven't called */
/* this procedure yet this GC cycle. */
GC_INNER void GC_finalize(void);
/* Perform all indicated finalization actions */
/* on unmarked objects. */
/* Unreachable finalizable objects are enqueued */
/* for processing by GC_invoke_finalizers. */
/* Invoked with lock. */
# ifndef GC_TOGGLE_REFS_NOT_NEEDED
GC_INNER void GC_process_togglerefs(void);
/* Process the toggle-refs before GC starts. */
# endif
# ifndef SMALL_CONFIG
GC_INNER void GC_print_finalization_stats(void);
# endif
#else
# define GC_INVOKE_FINALIZERS() (void)0
#endif /* GC_NO_FINALIZATION */
#if !defined(DONT_ADD_BYTE_AT_END)
# ifdef LINT2
/* Explicitly instruct the code analysis tool that */
/* GC_all_interior_pointers is assumed to have only 0 or 1 value. */
# define EXTRA_BYTES ((size_t)(GC_all_interior_pointers? 1 : 0))
# else
# define EXTRA_BYTES (size_t)GC_all_interior_pointers
# endif
# define MAX_EXTRA_BYTES 1
#else
# define EXTRA_BYTES 0
# define MAX_EXTRA_BYTES 0
#endif
# ifndef LARGE_CONFIG
# define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
/* Must be multiple of largest page size. */
# define MAXHINCR 2048 /* Maximum heap increment, in blocks */
# else
# define MINHINCR 64
# define MAXHINCR 4096
# endif
# define BL_LIMIT GC_black_list_spacing
/* If we need a block of N bytes, and we have */
/* a block of N + BL_LIMIT bytes available, */
/* and N > BL_LIMIT, */
/* but all possible positions in it are */
/* blacklisted, we just use it anyway (and */
/* print a warning, if warnings are enabled). */
/* This risks subsequently leaking the block */
/* due to a false reference. But not using */
/* the block risks unreasonable immediate */
/* heap growth. */
/*********************************/
/* */
/* Stack saving for debugging */
/* */
/*********************************/
#ifdef NEED_CALLINFO
struct callinfo {
word ci_pc; /* Caller, not callee, pc */
# if NARGS > 0
word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
# endif
# if (NFRAMES * (NARGS + 1)) % 2 == 1
/* Likely alignment problem. */
word ci_dummy;
# endif
};
#endif
#ifdef SAVE_CALL_CHAIN
/* Fill in the pc and argument information for up to NFRAMES of my */
/* callers. Ignore my frame and my callers frame. */
GC_INNER void GC_save_callers(struct callinfo info[NFRAMES]);
GC_INNER void GC_print_callers(struct callinfo info[NFRAMES]);
#endif
EXTERN_C_END
/*********************************/
/* */
/* OS interface routines */
/* */
/*********************************/
#ifndef NO_CLOCK
#ifdef BSD_TIME
# undef CLOCK_TYPE
# undef GET_TIME
# undef MS_TIME_DIFF
# define CLOCK_TYPE struct timeval
# define CLOCK_TYPE_INITIALIZER { 0, 0 }
# define GET_TIME(x) \
do { \
struct rusage rusage; \
getrusage(RUSAGE_SELF, &rusage); \
x = rusage.ru_utime; \
} while (0)
# define MS_TIME_DIFF(a,b) ((unsigned long)((long)(a.tv_sec-b.tv_sec) * 1000 \
+ (long)(a.tv_usec - b.tv_usec) / 1000 \
- (a.tv_usec < b.tv_usec \
&& (long)(a.tv_usec - b.tv_usec) % 1000 != 0 ? 1 : 0)))
/* "a" time is expected to be not earlier than */
/* "b" one; the result has unsigned long type. */
# define NS_FRAC_TIME_DIFF(a, b) ((unsigned long) \
((a.tv_usec < b.tv_usec \
&& (long)(a.tv_usec - b.tv_usec) % 1000 != 0 ? 1000L : 0) \
+ (long)(a.tv_usec - b.tv_usec) % 1000) * 1000)
/* The total time difference could be computed as */
/* MS_TIME_DIFF(a,b)*1000000+NS_FRAC_TIME_DIFF(a,b).*/
#elif defined(MSWIN32) || defined(MSWINCE) || defined(WINXP_USE_PERF_COUNTER)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif
# define NOSERVICE
# include <windows.h>
# include <winbase.h>
# if defined(MSWINRT_FLAVOR) || defined(WINXP_USE_PERF_COUNTER)
# define CLOCK_TYPE ULONGLONG
# define GET_TIME(x) \
do { \
LARGE_INTEGER freq, t; \
if (!QueryPerformanceFrequency(&freq) \
|| !QueryPerformanceCounter(&t)) \
ABORT("QueryPerformanceCounter requires WinXP+"); \
x = (CLOCK_TYPE)((double)t.QuadPart/freq.QuadPart * 1e9); \
} while (0)
/* TODO: Call QueryPerformanceFrequency once at GC init. */
# define MS_TIME_DIFF(a, b) ((unsigned long)(((a) - (b)) / 1000000UL))
# define NS_FRAC_TIME_DIFF(a, b) ((unsigned long)(((a) - (b)) % 1000000UL))
# else
# define CLOCK_TYPE DWORD
# define GET_TIME(x) (void)(x = GetTickCount())
# define MS_TIME_DIFF(a, b) ((unsigned long)((a) - (b)))
# define NS_FRAC_TIME_DIFF(a, b) 0UL
# endif /* !WINXP_USE_PERF_COUNTER */
#elif defined(NN_PLATFORM_CTR)
# define CLOCK_TYPE long long
EXTERN_C_BEGIN
CLOCK_TYPE n3ds_get_system_tick(void);
CLOCK_TYPE n3ds_convert_tick_to_ms(CLOCK_TYPE tick);
EXTERN_C_END
# define GET_TIME(x) (void)(x = n3ds_get_system_tick())
# define MS_TIME_DIFF(a,b) ((unsigned long)n3ds_convert_tick_to_ms((a)-(b)))
# define NS_FRAC_TIME_DIFF(a, b) 0UL /* TODO: implement it */
#else /* !BSD_TIME && !NN_PLATFORM_CTR && !MSWIN32 && !MSWINCE */
# include <time.h>
# if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
# include <machine/limits.h>
# define CLOCKS_PER_SEC CLK_TCK
# endif
# if !defined(CLOCKS_PER_SEC)
# define CLOCKS_PER_SEC 1000000
/* This is technically a bug in the implementation. */
/* ANSI requires that CLOCKS_PER_SEC be defined. But at least */
/* under SunOS 4.1.1, it isn't. Also note that the combination of */
/* ANSI C and POSIX is incredibly gross here. The type clock_t */
/* is used by both clock() and times(). But on some machines */
/* these use different notions of a clock tick, CLOCKS_PER_SEC */
/* seems to apply only to clock. Hence we use it here. On many */
/* machines, including SunOS, clock actually uses units of */
/* microseconds (which are not really clock ticks). */
# endif
# define CLOCK_TYPE clock_t
# define GET_TIME(x) (void)(x = clock())
# define MS_TIME_DIFF(a,b) (CLOCKS_PER_SEC % 1000 == 0 ? \
(unsigned long)((a) - (b)) / (unsigned long)(CLOCKS_PER_SEC / 1000) \
: ((unsigned long)((a) - (b)) * 1000) / (unsigned long)CLOCKS_PER_SEC)
/* Avoid using double type since some targets (like ARM) might */
/* require -lm option for double-to-long conversion. */
# define NS_FRAC_TIME_DIFF(a, b) (CLOCKS_PER_SEC <= 1000 ? 0UL \
: (unsigned long)(CLOCKS_PER_SEC <= (clock_t)1000000UL \
? (((a) - (b)) * ((clock_t)1000000UL / CLOCKS_PER_SEC) % 1000) * 1000 \
: (CLOCKS_PER_SEC <= (clock_t)1000000UL * 1000 \
? ((a) - (b)) * ((clock_t)1000000UL * 1000 / CLOCKS_PER_SEC) \
: (((a) - (b)) * (clock_t)1000000UL * 1000) / CLOCKS_PER_SEC) \
% (clock_t)1000000UL))
#endif /* !BSD_TIME && !MSWIN32 */
# ifndef CLOCK_TYPE_INITIALIZER
/* This is used to initialize CLOCK_TYPE variables (to some value) */
/* to avoid "variable might be uninitialized" compiler warnings. */
# define CLOCK_TYPE_INITIALIZER 0
# endif
#endif /* !NO_CLOCK */
/* We use bzero and bcopy internally. They may not be available. */
# if defined(SPARC) && defined(SUNOS4) \
|| (defined(M68K) && defined(NEXT)) || defined(VAX)
# define BCOPY_EXISTS
# elif defined(AMIGA) || defined(DARWIN)
# include <string.h>
# define BCOPY_EXISTS
# elif defined(MACOS) && defined(POWERPC)
# include <MacMemory.h>
# define bcopy(x,y,n) BlockMoveData(x, y, n)
# define bzero(x,n) BlockZero(x, n)
# define BCOPY_EXISTS
# endif
# if !defined(BCOPY_EXISTS) || defined(CPPCHECK)
# include <string.h>
# define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
# define BZERO(x,n) memset(x, 0, (size_t)(n))
# else
# define BCOPY(x,y,n) bcopy((void *)(x),(void *)(y),(size_t)(n))
# define BZERO(x,n) bzero((void *)(x),(size_t)(n))
# endif
#ifdef PCR
# include "th/PCR_ThCtl.h"
#endif
EXTERN_C_BEGIN
/*
* Stop and restart mutator threads.
*/
# ifdef PCR
# define STOP_WORLD() \
PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
PCR_allSigsBlocked, \
PCR_waitForever)
# define START_WORLD() \
PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
PCR_allSigsBlocked, \
PCR_waitForever)
# else
# if defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \
|| defined(GC_WIN32_THREADS) || defined(GC_PTHREADS)
GC_INNER void GC_stop_world(void);
GC_INNER void GC_start_world(void);
# define STOP_WORLD() GC_stop_world()
# define START_WORLD() GC_start_world()
# else
/* Just do a sanity check: we are not inside GC_do_blocking(). */
# define STOP_WORLD() GC_ASSERT(GC_blocked_sp == NULL)
# define START_WORLD()
# endif
# endif
#ifdef THREADS
GC_EXTERN GC_on_thread_event_proc GC_on_thread_event;
#endif
/* Abandon ship */
# if defined(SMALL_CONFIG) || defined(PCR)
# define GC_on_abort(msg) (void)0 /* be silent on abort */
# else
GC_API_PRIV GC_abort_func GC_on_abort;
# endif
# if defined(CPPCHECK)
# define ABORT(msg) { GC_on_abort(msg); abort(); }
# elif defined(PCR)
# define ABORT(s) PCR_Base_Panic(s)
# else
# if defined(MSWIN_XBOX1) && !defined(DebugBreak)
# define DebugBreak() __debugbreak()
# elif defined(MSWINCE) && !defined(DebugBreak) \
&& (!defined(UNDER_CE) || (defined(__MINGW32CE__) && !defined(ARM32)))
/* This simplifies linking for WinCE (and, probably, doesn't */
/* hurt debugging much); use -DDebugBreak=DebugBreak to override */
/* this behavior if really needed. This is also a workaround for */
/* x86mingw32ce toolchain (if it is still declaring DebugBreak() */
/* instead of defining it as a macro). */
# define DebugBreak() _exit(-1) /* there is no abort() in WinCE */
# endif
# if defined(MSWIN32) && (defined(NO_DEBUGGING) || defined(LINT2))
/* A more user-friendly abort after showing fatal message. */
# define ABORT(msg) (GC_on_abort(msg), _exit(-1))
/* Exit on error without running "at-exit" callbacks. */
# elif defined(MSWINCE) && defined(NO_DEBUGGING)
# define ABORT(msg) (GC_on_abort(msg), ExitProcess(-1))
# elif defined(MSWIN32) || defined(MSWINCE)
# if defined(_CrtDbgBreak) && defined(_DEBUG) && defined(_MSC_VER)
# define ABORT(msg) { GC_on_abort(msg); \
_CrtDbgBreak() /* __debugbreak() */; }
# else
# define ABORT(msg) { GC_on_abort(msg); DebugBreak(); }
/* Note that: on a WinCE box, this could be silently */
/* ignored (i.e., the program is not aborted); */
/* DebugBreak is a statement in some toolchains. */
# endif
# else
# define ABORT(msg) (GC_on_abort(msg), abort())
# endif /* !MSWIN32 */
# endif /* !PCR */
/* For abort message with 1-3 arguments. C_msg and C_fmt should be */
/* literals. C_msg should not contain format specifiers. Arguments */
/* should match their format specifiers. */
#define ABORT_ARG1(C_msg, C_fmt, arg1) \
do { \
GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1); \
ABORT(C_msg); \
} while (0)
#define ABORT_ARG2(C_msg, C_fmt, arg1, arg2) \
do { \
GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1, arg2); \
ABORT(C_msg); \
} while (0)
#define ABORT_ARG3(C_msg, C_fmt, arg1, arg2, arg3) \
do { \
GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", \
arg1, arg2, arg3); \
ABORT(C_msg); \
} while (0)
/* Same as ABORT but does not have 'no-return' attribute. */
/* ABORT on a dummy condition (which is always true). */
#define ABORT_RET(msg) \
if ((signed_word)GC_current_warn_proc == -1) {} else ABORT(msg)
/* Exit abnormally, but without making a mess (e.g. out of memory) */
# ifdef PCR
# define EXIT() PCR_Base_Exit(1,PCR_waitForever)
# else
# define EXIT() (GC_on_abort(NULL), exit(1 /* EXIT_FAILURE */))
# endif
/* Print warning message, e.g. almost out of memory. */
/* The argument (if any) format specifier should be: */
/* "%s", "%p" or "%"WARN_PRIdPTR. */
#define WARN(msg, arg) \
(*GC_current_warn_proc)((/* no const */ char *)("GC Warning: " msg), \
(word)(arg))
GC_EXTERN GC_warn_proc GC_current_warn_proc;
/* Print format type macro for decimal signed_word value passed WARN(). */
/* This could be redefined for Win64 or LLP64, but typically should */
/* not be done as the WARN format string is, possibly, processed on the */
/* client side, so non-standard print type modifiers (like MS "I64d") */
/* should be avoided here if possible. */
#ifndef WARN_PRIdPTR
/* Assume sizeof(void *) == sizeof(long) (or a little-endian machine) */
# define WARN_PRIdPTR "ld"
#endif
/* A tagging macro (for a code static analyzer) to indicate that the */
/* string obtained from an untrusted source (e.g., argv[], getenv) is */
/* safe to use in a vulnerable operation (e.g., open, exec). */
#define TRUSTED_STRING(s) (char*)COVERT_DATAFLOW(s)
/* Get environment entry */
#ifdef GC_READ_ENV_FILE
GC_INNER char * GC_envfile_getenv(const char *name);
# define GETENV(name) GC_envfile_getenv(name)
#elif defined(NO_GETENV) && !defined(CPPCHECK)
# define GETENV(name) NULL
#elif defined(EMPTY_GETENV_RESULTS)
/* Workaround for a reputed Wine bug. */
GC_INLINE char * fixed_getenv(const char *name)
{
char *value = getenv(name);
return value != NULL && *value != '\0' ? value : NULL;
}
# define GETENV(name) fixed_getenv(name)
#else
# define GETENV(name) getenv(name)
#endif
EXTERN_C_END
#if defined(DARWIN)
# include <mach/thread_status.h>
# ifndef MAC_OS_X_VERSION_MAX_ALLOWED
# include <AvailabilityMacros.h>
/* Include this header just to import the above macro. */
# endif
# if defined(POWERPC)
# if CPP_WORDSZ == 32
# define GC_THREAD_STATE_T ppc_thread_state_t
# else
# define GC_THREAD_STATE_T ppc_thread_state64_t
# define GC_MACH_THREAD_STATE PPC_THREAD_STATE64
# define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE64_COUNT
# endif
# elif defined(I386) || defined(X86_64)
# if CPP_WORDSZ == 32
# if defined(i386_THREAD_STATE_COUNT) && !defined(x86_THREAD_STATE32_COUNT)
/* Use old naming convention for 32-bit x86. */
# define GC_THREAD_STATE_T i386_thread_state_t
# define GC_MACH_THREAD_STATE i386_THREAD_STATE
# define GC_MACH_THREAD_STATE_COUNT i386_THREAD_STATE_COUNT
# else
# define GC_THREAD_STATE_T x86_thread_state32_t
# define GC_MACH_THREAD_STATE x86_THREAD_STATE32
# define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
# endif
# else
# define GC_THREAD_STATE_T x86_thread_state64_t
# define GC_MACH_THREAD_STATE x86_THREAD_STATE64
# define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
# endif
# elif defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE) \
&& !defined(CPPCHECK)
# define GC_THREAD_STATE_T arm_unified_thread_state_t
# define GC_MACH_THREAD_STATE ARM_UNIFIED_THREAD_STATE
# define GC_MACH_THREAD_STATE_COUNT ARM_UNIFIED_THREAD_STATE_COUNT
# elif defined(ARM32)
# define GC_THREAD_STATE_T arm_thread_state_t
# ifdef ARM_MACHINE_THREAD_STATE_COUNT
# define GC_MACH_THREAD_STATE ARM_MACHINE_THREAD_STATE
# define GC_MACH_THREAD_STATE_COUNT ARM_MACHINE_THREAD_STATE_COUNT
# endif
# elif defined(AARCH64)
# define GC_THREAD_STATE_T arm_thread_state64_t
# define GC_MACH_THREAD_STATE ARM_THREAD_STATE64
# define GC_MACH_THREAD_STATE_COUNT ARM_THREAD_STATE64_COUNT
# elif !defined(CPPCHECK)
# error define GC_THREAD_STATE_T
# endif
# ifndef GC_MACH_THREAD_STATE
# define GC_MACH_THREAD_STATE MACHINE_THREAD_STATE
# define GC_MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT
# endif
# if CPP_WORDSZ == 32
# define GC_MACH_HEADER mach_header
# define GC_MACH_SECTION section
# define GC_GETSECTBYNAME getsectbynamefromheader
# else
# define GC_MACH_HEADER mach_header_64
# define GC_MACH_SECTION section_64
# define GC_GETSECTBYNAME getsectbynamefromheader_64
# endif
/* Try to work out the right way to access thread state structure */
/* members. The structure has changed its definition in different */
/* Darwin versions. This now defaults to the (older) names */
/* without __, thus hopefully, not breaking any existing */
/* Makefile.direct builds. */
# if __DARWIN_UNIX03
# define THREAD_FLD_NAME(x) __ ## x
# else
# define THREAD_FLD_NAME(x) x
# endif
# if defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE)
# define THREAD_FLD(x) ts_32.THREAD_FLD_NAME(x)
# else
# define THREAD_FLD(x) THREAD_FLD_NAME(x)
# endif
#endif /* DARWIN */
#include "../gc_tiny_fl.h"
#include <setjmp.h>
#if __STDC_VERSION__ >= 201112L
# include <assert.h> /* for static_assert */
#endif
EXTERN_C_BEGIN
/*********************************/
/* */
/* Word-size-dependent defines */
/* */
/*********************************/
#if CPP_WORDSZ == 32
# define WORDS_TO_BYTES(x) ((x)<<2)
# define BYTES_TO_WORDS(x) ((x)>>2)
# define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
# define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
# if ALIGNMENT != 4
# define UNALIGNED_PTRS
# endif
#endif
#if CPP_WORDSZ == 64
# define WORDS_TO_BYTES(x) ((x)<<3)
# define BYTES_TO_WORDS(x) ((x)>>3)
# define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
# define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
# if ALIGNMENT != 8
# define UNALIGNED_PTRS
# endif
#endif
/* The first TINY_FREELISTS free lists correspond to the first */
/* TINY_FREELISTS multiples of GRANULE_BYTES, i.e. we keep */
/* separate free lists for each multiple of GRANULE_BYTES */
/* up to (TINY_FREELISTS-1) * GRANULE_BYTES. After that they */
/* may be spread out further. */
#define GRANULE_BYTES GC_GRANULE_BYTES
#define TINY_FREELISTS GC_TINY_FREELISTS
#define WORDSZ ((word)CPP_WORDSZ)
#define SIGNB ((word)1 << (WORDSZ-1))
#define BYTES_PER_WORD ((word)(sizeof (word)))
#define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
#if GRANULE_BYTES == 8
# define BYTES_TO_GRANULES(n) ((n)>>3)
# define GRANULES_TO_BYTES(n) ((n)<<3)
# if CPP_WORDSZ == 64
# define GRANULES_TO_WORDS(n) (n)
# elif CPP_WORDSZ == 32
# define GRANULES_TO_WORDS(n) ((n)<<1)
# else
# define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
# endif
#elif GRANULE_BYTES == 16
# define BYTES_TO_GRANULES(n) ((n)>>4)
# define GRANULES_TO_BYTES(n) ((n)<<4)
# if CPP_WORDSZ == 64
# define GRANULES_TO_WORDS(n) ((n)<<1)
# elif CPP_WORDSZ == 32
# define GRANULES_TO_WORDS(n) ((n)<<2)
# else
# define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
# endif
#else
# error Bad GRANULE_BYTES value
#endif
/*********************/
/* */
/* Size Parameters */
/* */
/*********************/
/* Heap block size, bytes. Should be power of 2. */
/* Incremental GC with MPROTECT_VDB currently requires the */
/* page size to be a multiple of HBLKSIZE. Since most modern */
/* architectures support variable page sizes down to 4K, and */
/* X86 is generally 4K, we now default to 4K, except for */
/* Alpha: Seems to be used with 8K pages. */
/* SMALL_CONFIG: Want less block-level fragmentation. */
#ifndef HBLKSIZE
# if defined(LARGE_CONFIG) || !defined(SMALL_CONFIG)
# ifdef ALPHA
# define CPP_LOG_HBLKSIZE 13
# elif defined(SN_TARGET_ORBIS) || defined(SN_TARGET_PSP2)
# define CPP_LOG_HBLKSIZE 16 /* page size is set to 64K */
# else
# define CPP_LOG_HBLKSIZE 12
# endif
# else
# define CPP_LOG_HBLKSIZE 10
# endif
#else
# if HBLKSIZE == 512
# define CPP_LOG_HBLKSIZE 9
# elif HBLKSIZE == 1024
# define CPP_LOG_HBLKSIZE 10
# elif HBLKSIZE == 2048
# define CPP_LOG_HBLKSIZE 11
# elif HBLKSIZE == 4096
# define CPP_LOG_HBLKSIZE 12
# elif HBLKSIZE == 8192
# define CPP_LOG_HBLKSIZE 13
# elif HBLKSIZE == 16384
# define CPP_LOG_HBLKSIZE 14
# elif !defined(CPPCHECK)
# error Bad HBLKSIZE value
# endif
# undef HBLKSIZE
#endif
# define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
# define LOG_HBLKSIZE ((size_t)CPP_LOG_HBLKSIZE)
# define HBLKSIZE ((size_t)CPP_HBLKSIZE)
#define GC_SQRT_SIZE_MAX ((((size_t)1) << (WORDSZ / 2)) - 1)
/* Max size objects supported by freelist (larger objects are */
/* allocated directly with allchblk(), by rounding to the next */
/* multiple of HBLKSIZE). */
#define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
#define MAXOBJBYTES ((size_t)CPP_MAXOBJBYTES)
#define CPP_MAXOBJWORDS BYTES_TO_WORDS(CPP_MAXOBJBYTES)
#define MAXOBJWORDS ((size_t)CPP_MAXOBJWORDS)
#define CPP_MAXOBJGRANULES BYTES_TO_GRANULES(CPP_MAXOBJBYTES)
#define MAXOBJGRANULES ((size_t)CPP_MAXOBJGRANULES)
# define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
# define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
/* Equivalent to subtracting 2 hblk pointers. */
/* We do it this way because a compiler should */
/* find it hard to use an integer division */
/* instead of a shift. The bundled SunOS 4.1 */
/* o.w. sometimes pessimizes the subtraction to */
/* involve a call to .div. */
# define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
# define HBLKPTR(objptr) ((struct hblk *)(((word)(objptr)) \
& ~(word)(HBLKSIZE-1)))
# define HBLKDISPL(objptr) (((size_t) (objptr)) & (HBLKSIZE-1))
/* Round up allocation size (in bytes) to a multiple of a granule. */
#define ROUNDUP_GRANULE_SIZE(lb) /* lb should have no side-effect */ \
(SIZET_SAT_ADD(lb, GRANULE_BYTES - 1) & ~(GRANULE_BYTES - 1))
/* Round up byte allocation requests to integral number of words, etc. */
# define ROUNDED_UP_GRANULES(lb) /* lb should have no side-effect */ \
BYTES_TO_GRANULES(SIZET_SAT_ADD(lb, GRANULE_BYTES - 1 + EXTRA_BYTES))
# if MAX_EXTRA_BYTES == 0
# define SMALL_OBJ(bytes) EXPECT((bytes) <= (MAXOBJBYTES), TRUE)
# else
# define SMALL_OBJ(bytes) \
(EXPECT((bytes) <= (MAXOBJBYTES - MAX_EXTRA_BYTES), TRUE) \
|| (bytes) <= MAXOBJBYTES - EXTRA_BYTES)
/* This really just tests bytes <= MAXOBJBYTES - EXTRA_BYTES. */
/* But we try to avoid looking up EXTRA_BYTES. */
# endif
# define ADD_SLOP(lb) /* lb should have no side-effect */ \
SIZET_SAT_ADD(lb, EXTRA_BYTES)
/*
* Hash table representation of sets of pages.
* Implements a map from aligned HBLKSIZE chunks of the address space to one
* bit each.
* This assumes it is OK to spuriously set bits, e.g. because multiple
* addresses are represented by a single location.
* Used by black-listing code, and perhaps by dirty bit maintenance code.
*/
# ifdef LARGE_CONFIG
# if CPP_WORDSZ == 32
# define LOG_PHT_ENTRIES 20 /* Collisions likely at 1M blocks, */
/* which is >= 4GB. Each table takes */
/* 128KB, some of which may never be */
/* touched. */
# else
# define LOG_PHT_ENTRIES 21 /* Collisions likely at 2M blocks, */
/* which is >= 8GB. Each table takes */
/* 256KB, some of which may never be */
/* touched. */
# endif
# elif !defined(SMALL_CONFIG)
# define LOG_PHT_ENTRIES 18 /* Collisions are likely if heap grows */
/* to more than 256K hblks >= 1GB. */
/* Each hash table occupies 32K bytes. */
/* Even for somewhat smaller heaps, */
/* say half that, collisions may be an */
/* issue because we blacklist */
/* addresses outside the heap. */
# else
# define LOG_PHT_ENTRIES 15 /* Collisions are likely if heap grows */
/* to more than 32K hblks = 128MB. */
/* Each hash table occupies 4K bytes. */
# endif
# define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
# define PHT_SIZE (PHT_ENTRIES >> LOGWL)
typedef word page_hash_table[PHT_SIZE];
# define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
# define get_pht_entry_from_index(bl, index) \
(((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
# define set_pht_entry_from_index(bl, index) \
(void)((bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index))
#if defined(THREADS) && defined(AO_HAVE_or)
/* And, one more version for GC_add_to_black_list_normal/stack */
/* (invoked indirectly by GC_do_local_mark) and */
/* async_set_pht_entry_from_index (invoked by GC_dirty or the write */