forked from ivmai/bdwgc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mark.c
2110 lines (1928 loc) · 77.1 KB
/
mark.c
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-1995 by Xerox Corporation. All rights reserved.
* Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
* Copyright (c) 2008-2022 Ivan Maidanski
*
* 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.
*/
#include "private/gc_pmark.h"
/* Make arguments appear live to compiler. Put here to minimize the */
/* risk of inlining. Used to minimize junk left in registers. */
GC_ATTR_NOINLINE
void GC_noop6(word arg1, word arg2, word arg3, word arg4, word arg5, word arg6)
{
UNUSED_ARG(arg1);
UNUSED_ARG(arg2);
UNUSED_ARG(arg3);
UNUSED_ARG(arg4);
UNUSED_ARG(arg5);
UNUSED_ARG(arg6);
/* Avoid GC_noop6 calls to be optimized away. */
# if defined(AO_HAVE_compiler_barrier) && !defined(BASE_ATOMIC_OPS_EMULATED)
AO_compiler_barrier(); /* to serve as a special side-effect */
# else
GC_noop1(0);
# endif
}
/* Make the argument appear live to compiler. This is similar */
/* to GC_noop6(), but with a single argument. Robust against */
/* whole program analysis. */
GC_API void GC_CALL GC_noop1(GC_word x)
{
# if defined(AO_HAVE_store) && defined(THREAD_SANITIZER)
AO_store(&GC_noop_sink, (AO_t)x);
# else
GC_noop_sink = x;
# endif
}
GC_API void GC_CALL GC_noop1_ptr(volatile void *p)
{
# if CPP_PTRSZ > CPP_WORDSZ
# if defined(AO_HAVE_store) && defined(THREAD_SANITIZER)
GC_cptr_store(&GC_noop_sink_ptr, (ptr_t)CAST_AWAY_VOLATILE_PVOID(p));
# else
GC_noop_sink_ptr = (ptr_t)CAST_AWAY_VOLATILE_PVOID(p);
# endif
# else
GC_noop1(ADDR(p));
# endif
}
GC_API void GC_keep_alive(GC_word x)
{
GC_reachable_here(x);
}
/* Initialize GC_obj_kinds properly and standard free lists properly. */
/* This must be done statically since they may be accessed before */
/* GC_init is called. It is done here, since we need to deal with mark */
/* descriptors. Note: GC_obj_kinds[NORMAL].ok_descriptor is adjusted */
/* in GC_init() for EXTRA_BYTES. */
GC_INNER struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
/* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
/* 0 | */ GC_DS_LENGTH, FALSE, FALSE
/*, */ OK_DISCLAIM_INITZ },
/* NORMAL */ { &GC_objfreelist[0], 0,
/* 0 | */ GC_DS_LENGTH, TRUE /* add length to descr */, TRUE
/*, */ OK_DISCLAIM_INITZ },
/* UNCOLLECTABLE */
{ &GC_uobjfreelist[0], 0,
/* 0 | */ GC_DS_LENGTH, TRUE /* add length to descr */, TRUE
/*, */ OK_DISCLAIM_INITZ },
# ifdef GC_ATOMIC_UNCOLLECTABLE
{ &GC_auobjfreelist[0], 0,
/* 0 | */ GC_DS_LENGTH, FALSE, FALSE
/*, */ OK_DISCLAIM_INITZ },
# endif
};
#ifndef INITIAL_MARK_STACK_SIZE
/* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a multiple of */
/* HBLKSIZE. The incremental collector actually likes a larger size, */
/* since it wants to push all marked dirty objects before marking */
/* anything new. Currently we let it grow dynamically. */
# define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
#endif /* !INITIAL_MARK_STACK_SIZE */
#if !defined(GC_DISABLE_INCREMENTAL)
/* Number of dirty pages we marked from, excluding pointer-free */
/* pages, etc. Used for logging only. */
STATIC word GC_n_rescuing_pages = 0;
#endif
GC_API void GC_CALL GC_set_pointer_mask(GC_word value)
{
# ifdef DYNAMIC_POINTER_MASK
GC_ASSERT(value >= 0xff); /* a simple sanity check */
GC_pointer_mask = value;
# else
if (value
# ifdef POINTER_MASK
!= (word)(POINTER_MASK)
# else
!= GC_WORD_MAX
# endif
) {
ABORT("Dynamic pointer mask/shift is unsupported");
}
# endif
}
GC_API GC_word GC_CALL GC_get_pointer_mask(void)
{
# ifdef DYNAMIC_POINTER_MASK
GC_word value = GC_pointer_mask;
if (0 == value) {
GC_ASSERT(!GC_is_initialized);
value = GC_WORD_MAX;
}
return value;
# elif defined(POINTER_MASK)
return POINTER_MASK;
# else
return GC_WORD_MAX;
# endif
}
GC_API void GC_CALL GC_set_pointer_shift(unsigned value)
{
# ifdef DYNAMIC_POINTER_MASK
GC_ASSERT(value < CPP_WORDSZ);
GC_pointer_shift = (unsigned char)value;
# else
if (value
# ifdef POINTER_SHIFT
!= (unsigned)(POINTER_SHIFT)
# endif /* else is not zero */
) {
ABORT("Dynamic pointer mask/shift is unsupported");
}
# endif
}
GC_API unsigned GC_CALL GC_get_pointer_shift(void)
{
# ifdef DYNAMIC_POINTER_MASK
return GC_pointer_shift;
# elif defined(POINTER_SHIFT)
GC_STATIC_ASSERT((unsigned)(POINTER_SHIFT) < CPP_WORDSZ);
return POINTER_SHIFT;
# else
return 0;
# endif
}
/* Is a collection in progress? Note that this can return true in the */
/* non-incremental case, if a collection has been abandoned and the */
/* mark state is now MS_INVALID. */
GC_INNER GC_bool GC_collection_in_progress(void)
{
return GC_mark_state != MS_NONE;
}
/* Clear all mark bits in the header. */
GC_INNER void GC_clear_hdr_marks(hdr *hhdr)
{
size_t last_bit;
# ifdef AO_HAVE_load
/* Atomic access is used to avoid racing with GC_realloc. */
last_bit = FINAL_MARK_BIT(AO_load(&(hhdr -> hb_sz)));
# else
/* No race as GC_realloc holds the allocator lock while updating hb_sz. */
last_bit = FINAL_MARK_BIT(hhdr -> hb_sz);
# endif
BZERO(CAST_AWAY_VOLATILE_PVOID(hhdr -> hb_marks),
sizeof(hhdr -> hb_marks));
set_mark_bit_from_hdr(hhdr, last_bit);
hhdr -> hb_n_marks = 0;
}
/* Set all mark bits in the header. Used for uncollectible blocks. */
GC_INNER void GC_set_hdr_marks(hdr *hhdr)
{
size_t i;
size_t sz = hhdr -> hb_sz;
size_t n_marks = FINAL_MARK_BIT(sz);
# ifdef USE_MARK_BYTES
for (i = 0; i <= n_marks; i += MARK_BIT_OFFSET(sz)) {
hhdr -> hb_marks[i] = 1;
}
# else
/* Note that all bits are set even in case of not MARK_BIT_PER_OBJ, */
/* instead of setting every n-th bit where n is MARK_BIT_OFFSET(sz). */
/* This is done for a performance reason. */
for (i = 0; i < divWORDSZ(n_marks); ++i) {
hhdr -> hb_marks[i] = GC_WORD_MAX;
}
/* Set the remaining bits near the end (plus one bit past the end). */
hhdr -> hb_marks[i] = ((((word)1 << modWORDSZ(n_marks)) - 1) << 1) | 1;
# endif
# ifdef MARK_BIT_PER_OBJ
hhdr -> hb_n_marks = n_marks;
# else
hhdr -> hb_n_marks = HBLK_OBJS(sz);
# endif
}
/* Clear all mark bits associated with block h. */
static void GC_CALLBACK clear_marks_for_block(struct hblk *h, void *dummy)
{
hdr * hhdr = HDR(h);
UNUSED_ARG(dummy);
if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) {
/* Mark bit for these is cleared only once the object is */
/* explicitly deallocated. This either frees the block, or the */
/* bit is cleared once the object is on the free list. */
return;
}
GC_clear_hdr_marks(hhdr);
# if defined(CPPCHECK)
GC_noop1_ptr(h);
# endif
}
/* Slow but general routines for setting/clearing/asking about mark bits. */
GC_API void GC_CALL GC_set_mark_bit(const void *p)
{
struct hblk *h = HBLKPTR(p);
hdr * hhdr = HDR(h);
size_t bit_no = MARK_BIT_NO((size_t)((ptr_t)p - (ptr_t)h), hhdr -> hb_sz);
if (!mark_bit_from_hdr(hhdr, bit_no)) {
set_mark_bit_from_hdr(hhdr, bit_no);
INCR_MARKS(hhdr);
}
}
GC_API void GC_CALL GC_clear_mark_bit(const void *p)
{
struct hblk *h = HBLKPTR(p);
hdr * hhdr = HDR(h);
size_t bit_no = MARK_BIT_NO((size_t)((ptr_t)p - (ptr_t)h), hhdr -> hb_sz);
if (mark_bit_from_hdr(hhdr, bit_no)) {
size_t n_marks = hhdr -> hb_n_marks;
GC_ASSERT(n_marks != 0);
clear_mark_bit_from_hdr(hhdr, bit_no);
n_marks--;
# ifdef PARALLEL_MARK
/* Don't decrement to zero. The counts are approximate due to */
/* concurrency issues, but we need to ensure that a count of */
/* zero implies an empty block. */
if (n_marks != 0 || !GC_parallel)
hhdr -> hb_n_marks = n_marks;
# else
hhdr -> hb_n_marks = n_marks;
# endif
}
}
GC_API int GC_CALL GC_is_marked(const void *p)
{
struct hblk *h = HBLKPTR(p);
hdr * hhdr = HDR(h);
size_t bit_no = MARK_BIT_NO((size_t)((ptr_t)p - (ptr_t)h), hhdr -> hb_sz);
return (int)mark_bit_from_hdr(hhdr, bit_no); /* 0 or 1 */
}
/* Clear mark bits in all allocated heap blocks. This invalidates the */
/* marker invariant, and sets GC_mark_state to reflect this. (This */
/* implicitly starts marking to reestablish the invariant.) */
GC_INNER void GC_clear_marks(void)
{
/* The initialization is needed for GC_push_roots(). */
GC_ASSERT(GC_is_initialized);
GC_apply_to_all_blocks(clear_marks_for_block, NULL);
GC_objects_are_marked = FALSE;
GC_mark_state = MS_INVALID;
GC_scan_ptr = NULL;
}
/* Initiate a garbage collection. Initiates a full collection if the */
/* mark state is invalid. */
GC_INNER void GC_initiate_gc(void)
{
GC_ASSERT(I_HOLD_LOCK());
GC_ASSERT(GC_is_initialized);
# ifndef GC_DISABLE_INCREMENTAL
if (GC_incremental) {
# ifdef CHECKSUMS
GC_read_dirty(FALSE);
GC_check_dirty();
# else
GC_read_dirty(GC_mark_state == MS_INVALID);
# endif
}
GC_n_rescuing_pages = 0;
# endif
if (GC_mark_state == MS_NONE) {
GC_mark_state = MS_PUSH_RESCUERS;
} else {
/* This is really a full collection, and mark bits are invalid. */
GC_ASSERT(GC_mark_state == MS_INVALID);
}
GC_scan_ptr = NULL;
}
#ifdef PARALLEL_MARK
/* Initiate parallel marking. */
STATIC void GC_do_parallel_mark(void);
#endif /* PARALLEL_MARK */
#ifdef GC_DISABLE_INCREMENTAL
# define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
#else
STATIC struct hblk * GC_push_next_marked_dirty(struct hblk *h);
#endif /* !GC_DISABLE_INCREMENTAL */
STATIC struct hblk * GC_push_next_marked(struct hblk *h);
STATIC struct hblk * GC_push_next_marked_uncollectable(struct hblk *h);
static void alloc_mark_stack(size_t);
static void push_roots_and_advance(GC_bool push_all, ptr_t cold_gc_frame)
{
if (GC_scan_ptr != NULL) {
/* Not ready to push. */
return;
}
GC_push_roots(push_all, cold_gc_frame);
GC_objects_are_marked = TRUE;
if (GC_mark_state != MS_INVALID)
GC_mark_state = MS_ROOTS_PUSHED;
}
STATIC GC_on_mark_stack_empty_proc GC_on_mark_stack_empty;
GC_API void GC_CALL GC_set_on_mark_stack_empty(GC_on_mark_stack_empty_proc fn)
{
LOCK();
GC_on_mark_stack_empty = fn;
UNLOCK();
}
GC_API GC_on_mark_stack_empty_proc GC_CALL GC_get_on_mark_stack_empty(void)
{
GC_on_mark_stack_empty_proc fn;
READER_LOCK();
fn = GC_on_mark_stack_empty;
READER_UNLOCK();
return fn;
}
/* Perform a small amount of marking. */
/* We try to touch roughly a page of memory. */
/* Return TRUE if we just finished a mark phase. */
/* Cold_gc_frame is an address inside a GC frame that */
/* remains valid until all marking is complete. */
/* A zero value indicates that it's OK to miss some */
/* register values. In the case of an incremental */
/* collection, the world may be running. */
#ifdef WRAP_MARK_SOME
/* For Win32, this is called after we establish a structured */
/* exception (or signal) handler, in case Windows unmaps one */
/* of our root segments. Note that this code should never */
/* generate an incremental GC write fault. */
STATIC GC_bool GC_mark_some_inner(ptr_t cold_gc_frame)
#else
GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame)
#endif
{
GC_ASSERT(I_HOLD_LOCK());
switch (GC_mark_state) {
case MS_NONE:
return TRUE;
case MS_PUSH_RESCUERS:
if (ADDR_GE((ptr_t)GC_mark_stack_top,
(ptr_t)(GC_mark_stack_limit - INITIAL_MARK_STACK_SIZE/2))) {
/* Go ahead and mark, even though that might cause us to */
/* see more marked dirty objects later on. Avoid this */
/* in the future. */
GC_mark_stack_too_small = TRUE;
MARK_FROM_MARK_STACK();
} else {
GC_scan_ptr = GC_push_next_marked_dirty(GC_scan_ptr);
# ifndef GC_DISABLE_INCREMENTAL
if (NULL == GC_scan_ptr) {
GC_COND_LOG_PRINTF("Marked from %lu dirty pages\n",
(unsigned long)GC_n_rescuing_pages);
}
# endif
push_roots_and_advance(FALSE, cold_gc_frame);
}
GC_ASSERT(GC_mark_state == MS_PUSH_RESCUERS
|| GC_mark_state == MS_ROOTS_PUSHED
|| GC_mark_state == MS_INVALID);
break;
case MS_PUSH_UNCOLLECTABLE:
if (ADDR_GE((ptr_t)GC_mark_stack_top,
(ptr_t)(GC_mark_stack + GC_mark_stack_size/4))) {
# ifdef PARALLEL_MARK
/* Avoid this, since we don't parallelize the marker */
/* here. */
if (GC_parallel) GC_mark_stack_too_small = TRUE;
# endif
MARK_FROM_MARK_STACK();
} else {
GC_scan_ptr = GC_push_next_marked_uncollectable(GC_scan_ptr);
push_roots_and_advance(TRUE, cold_gc_frame);
}
GC_ASSERT(GC_mark_state == MS_PUSH_UNCOLLECTABLE
|| GC_mark_state == MS_ROOTS_PUSHED
|| GC_mark_state == MS_INVALID);
break;
case MS_ROOTS_PUSHED:
# ifdef PARALLEL_MARK
/* Eventually, incremental marking should run */
/* asynchronously in multiple threads, without acquiring */
/* the allocator lock. */
/* For now, parallel marker is disabled if there is */
/* a chance that marking could be interrupted by */
/* a client-supplied time limit or custom stop function. */
if (GC_parallel && !GC_parallel_mark_disabled) {
GC_do_parallel_mark();
GC_ASSERT(ADDR_LT((ptr_t)GC_mark_stack_top,
GC_first_nonempty));
GC_mark_stack_top = GC_mark_stack - 1;
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
if (GC_mark_state == MS_ROOTS_PUSHED) {
GC_mark_state = MS_NONE;
return TRUE;
}
GC_ASSERT(GC_mark_state == MS_INVALID);
break;
}
# endif
if (ADDR_GE((ptr_t)GC_mark_stack_top, (ptr_t)GC_mark_stack)) {
MARK_FROM_MARK_STACK();
} else {
GC_on_mark_stack_empty_proc on_ms_empty =
GC_on_mark_stack_empty;
if (on_ms_empty != 0) {
GC_mark_stack_top = on_ms_empty(GC_mark_stack_top,
GC_mark_stack_limit);
/* If we pushed new items, we need to continue */
/* processing. */
if (ADDR_GE((ptr_t)GC_mark_stack_top,
(ptr_t)GC_mark_stack))
break;
}
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
GC_mark_state = MS_NONE;
return TRUE;
}
GC_ASSERT(GC_mark_state == MS_ROOTS_PUSHED
|| GC_mark_state == MS_INVALID);
break;
case MS_INVALID:
case MS_PARTIALLY_INVALID:
if (!GC_objects_are_marked) {
GC_mark_state = MS_PUSH_UNCOLLECTABLE;
break;
}
if (ADDR_GE((ptr_t)GC_mark_stack_top, (ptr_t)GC_mark_stack)) {
MARK_FROM_MARK_STACK();
GC_ASSERT(GC_mark_state == MS_PARTIALLY_INVALID
|| GC_mark_state == MS_INVALID);
break;
}
if (NULL == GC_scan_ptr && GC_mark_state == MS_INVALID) {
/* About to start a heap scan for marked objects. */
/* Mark stack is empty. OK to reallocate. */
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
GC_mark_state = MS_PARTIALLY_INVALID;
}
GC_scan_ptr = GC_push_next_marked(GC_scan_ptr);
if (GC_mark_state == MS_PARTIALLY_INVALID)
push_roots_and_advance(TRUE, cold_gc_frame);
GC_ASSERT(GC_mark_state == MS_ROOTS_PUSHED
|| GC_mark_state == MS_PARTIALLY_INVALID
|| GC_mark_state == MS_INVALID);
break;
default:
ABORT("GC_mark_some: bad state");
}
return FALSE;
}
#ifdef PARALLEL_MARK
GC_INNER GC_bool GC_parallel_mark_disabled = FALSE;
#endif
#ifdef WRAP_MARK_SOME
GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame)
{
GC_bool ret_val;
if (GC_no_dls) {
ret_val = GC_mark_some_inner(cold_gc_frame);
} else {
/* Windows appears to asynchronously create and remove */
/* writable memory mappings, for reasons we haven't yet */
/* understood. Since we look for writable regions to */
/* determine the root set, we may try to mark from an */
/* address range that disappeared since we started the */
/* collection. Thus we have to recover from faults here. */
/* This code seems to be necessary for WinCE (at least in */
/* the case we'd decide to add MEM_PRIVATE sections to */
/* data roots in GC_register_dynamic_libraries()). */
/* It's conceivable that this is the same issue as with */
/* terminating threads that we see with Linux and */
/* USE_PROC_FOR_LIBRARIES. */
# ifndef NO_SEH_AVAILABLE
__try {
ret_val = GC_mark_some_inner(cold_gc_frame);
} __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
goto handle_ex;
}
# else
# if defined(USE_PROC_FOR_LIBRARIES) && !defined(DEFAULT_VDB)
if (GC_auto_incremental) {
static GC_bool is_warned = FALSE;
if (!is_warned) {
is_warned = TRUE;
WARN("Incremental GC incompatible with /proc roots\n", 0);
}
/* I'm not sure if this could still work ... */
}
# endif
/* If USE_PROC_FOR_LIBRARIES, we are handling the case in */
/* which /proc is used for root finding, and we have threads. */
/* We may find a stack for a thread that is in the process of */
/* exiting, and disappears while we are marking it. */
/* This seems extremely difficult to avoid otherwise. */
GC_setup_temporary_fault_handler();
if (SETJMP(GC_jmp_buf) != 0) goto handle_ex;
ret_val = GC_mark_some_inner(cold_gc_frame);
GC_reset_fault_handler();
# endif
}
# if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
/* With DllMain-based thread tracking, a thread may have */
/* started while we were marking. This is logically equivalent */
/* to the exception case; our results are invalid and we have */
/* to start over. This cannot be prevented since we can't */
/* block in DllMain. */
if (GC_started_thread_while_stopped())
goto handle_thr_start;
# endif
return ret_val;
handle_ex:
/* Exception handler starts here for all cases. */
# if defined(NO_SEH_AVAILABLE)
GC_reset_fault_handler();
# endif
{
static word warned_gc_no;
/* Report caught ACCESS_VIOLATION, once per collection. */
if (warned_gc_no != GC_gc_no) {
GC_COND_LOG_PRINTF("Memory mapping disappeared at collection #%lu\n",
(unsigned long)GC_gc_no + 1);
warned_gc_no = GC_gc_no;
}
}
# if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
handle_thr_start:
# endif
/* We have bad roots on the mark stack - discard it. */
/* Rescan from marked objects. Redetermine roots. */
# ifdef REGISTER_LIBRARIES_EARLY
START_WORLD();
GC_cond_register_dynamic_libraries();
STOP_WORLD();
# endif
GC_invalidate_mark_state();
GC_scan_ptr = NULL;
return FALSE;
}
#endif /* WRAP_MARK_SOME */
GC_INNER void GC_invalidate_mark_state(void)
{
GC_mark_state = MS_INVALID;
GC_mark_stack_top = GC_mark_stack-1;
}
STATIC mse * GC_signal_mark_stack_overflow(mse *msp)
{
GC_mark_state = MS_INVALID;
# ifdef PARALLEL_MARK
/* We are using a local_mark_stack in parallel mode, so */
/* do not signal the global mark stack to be resized. */
/* That will be done if required in GC_return_mark_stack. */
if (!GC_parallel)
GC_mark_stack_too_small = TRUE;
# else
GC_mark_stack_too_small = TRUE;
# endif
GC_COND_LOG_PRINTF("Mark stack overflow; current size: %lu entries\n",
(unsigned long)GC_mark_stack_size);
# if defined(CPPCHECK)
GC_noop1_ptr(msp);
# endif
return msp - GC_MARK_STACK_DISCARDS;
}
/*
* Mark objects pointed to by the regions described by
* mark stack entries between mark_stack and mark_stack_top,
* inclusive. Assumes the upper limit of a mark stack entry
* is never 0. A mark stack entry never has size 0.
* We try to traverse on the order of a hblk of memory before we return.
* Caller is responsible for calling this until the mark stack is empty.
* Note that this is the most performance critical routine in the
* collector. Hence it contains all sorts of ugly hacks to speed
* things up. In particular, we avoid procedure calls on the common
* path, we take advantage of peculiarities of the mark descriptor
* encoding, we optionally maintain a cache for the block address to
* header mapping, we prefetch when an object is "grayed", etc.
*/
GC_ATTR_NO_SANITIZE_ADDR GC_ATTR_NO_SANITIZE_MEMORY GC_ATTR_NO_SANITIZE_THREAD
GC_INNER mse * GC_mark_from(mse *mark_stack_top, mse *mark_stack,
mse *mark_stack_limit)
{
signed_word credit = HBLKSIZE; /* remaining credit for marking work */
word descr;
ptr_t current_p; /* pointer to current candidate ptr */
ptr_t q; /* the candidate pointer */
ptr_t limit = 0; /* limit (incl.) of current candidate range */
ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
DECLARE_HDR_CACHE;
# define SPLIT_RANGE_PTRS 128 /* must be power of 2 */
GC_objects_are_marked = TRUE;
INIT_HDR_CACHE;
# if defined(OS2) || CPP_PTRSZ > CPP_WORDSZ
/* OS/2: use untweaked version to circumvent a compiler problem. */
while (ADDR_GE((ptr_t)mark_stack_top, (ptr_t)mark_stack) && credit >= 0)
# else
while (((((word)mark_stack_top - (word)mark_stack) | (word)credit)
& SIGNB) == 0)
# endif
{
current_p = mark_stack_top -> mse_start;
descr = mark_stack_top -> mse_descr;
retry:
/* current_p and descr describe the current object. */
/* (*mark_stack_top) is vacant. */
/* The following is 0 only for small objects described by a simple */
/* length descriptor. For many applications this is the common */
/* case, so we try to detect it quickly. */
if (descr & (~(word)(PTRS_TO_BYTES(SPLIT_RANGE_PTRS)-1) | GC_DS_TAGS)) {
word tag = descr & GC_DS_TAGS;
GC_STATIC_ASSERT(GC_DS_TAGS == 0x3);
switch (tag) {
case GC_DS_LENGTH:
/* Large length. Process part of the range to avoid pushing */
/* too much on the stack. */
/* Either it is a heap object or a region outside the heap. */
GC_ASSERT(descr < GC_greatest_real_heap_addr
- GC_least_real_heap_addr
|| GC_least_real_heap_addr + sizeof(ptr_t)
>= ADDR(current_p) + descr
|| ADDR(current_p) >= GC_greatest_real_heap_addr);
# ifdef PARALLEL_MARK
# define SHARE_BYTES 2048
if (descr > SHARE_BYTES && GC_parallel
&& ADDR_LT((ptr_t)mark_stack_top,
(ptr_t)(mark_stack_limit - 1))) {
word new_size = (descr >> 1) & ~(word)(sizeof(ptr_t)-1);
mark_stack_top -> mse_start = current_p;
/* This makes sure we handle misaligned pointers. */
mark_stack_top -> mse_descr
= (new_size + sizeof(ptr_t)) | GC_DS_LENGTH;
mark_stack_top++;
# ifdef ENABLE_TRACE
if (ADDR_INSIDE(GC_trace_ptr, current_p, current_p + descr)) {
GC_log_printf("GC #%lu: large section; start %p, len %lu,"
" splitting (parallel) at %p\n",
(unsigned long)GC_gc_no, (void *)current_p,
(unsigned long)descr,
(void *)(current_p + new_size));
}
# endif
current_p += new_size;
descr -= new_size;
goto retry;
}
# endif /* PARALLEL_MARK */
limit = current_p + PTRS_TO_BYTES(SPLIT_RANGE_PTRS-1);
mark_stack_top -> mse_start = limit;
mark_stack_top -> mse_descr
= descr - PTRS_TO_BYTES(SPLIT_RANGE_PTRS-1);
# ifdef ENABLE_TRACE
if (ADDR_INSIDE(GC_trace_ptr, current_p, current_p + descr)) {
GC_log_printf("GC #%lu: large section; start %p, len %lu,"
" splitting at %p\n",
(unsigned long)GC_gc_no, (void *)current_p,
(unsigned long)descr, (void *)limit);
}
# endif
/* Make sure that pointers overlapping the two ranges are */
/* considered. */
limit += sizeof(ptr_t) - ALIGNMENT;
break;
case GC_DS_BITMAP:
mark_stack_top--;
# ifdef ENABLE_TRACE
if (ADDR_INSIDE(GC_trace_ptr, current_p,
current_p + PTRS_TO_BYTES(BITMAP_BITS))) {
GC_log_printf("GC #%lu: tracing from %p bitmap descr 0x%lx\n",
(unsigned long)GC_gc_no, (void *)current_p,
(unsigned long)descr);
}
# endif
descr &= ~(word)GC_DS_TAGS;
credit -= (signed_word)PTRS_TO_BYTES(CPP_PTRSZ / 2); /* guess */
for (; descr != 0; descr <<= 1, current_p += sizeof(ptr_t)) {
if ((descr & SIGNB) == 0) continue;
LOAD_PTR_OR_CONTINUE(q, current_p);
FIXUP_POINTER(q);
if (ADDR_LT(least_ha, q) && ADDR_LT(q, greatest_ha)) {
PREFETCH(q);
# ifdef ENABLE_TRACE
if (GC_trace_ptr == current_p) {
GC_log_printf("GC #%lu: considering(3) %p -> %p\n",
(unsigned long)GC_gc_no, (void *)current_p,
(void *)q);
}
# endif
PUSH_CONTENTS(q, mark_stack_top, mark_stack_limit, current_p);
}
}
continue;
case GC_DS_PROC:
mark_stack_top--;
# ifdef ENABLE_TRACE
if (ADDR_GE(GC_trace_ptr, current_p)) {
const void *base = GC_base(current_p);
if (base != NULL && GC_base(GC_trace_ptr) == base) {
GC_log_printf("GC #%lu: tracing from %p, proc descr 0x%lx\n",
(unsigned long)GC_gc_no, (void *)current_p,
(unsigned long)descr);
}
}
# endif
credit -= GC_PROC_BYTES;
mark_stack_top = (*PROC(descr))((word *)current_p, mark_stack_top,
mark_stack_limit, ENV(descr));
continue;
case GC_DS_PER_OBJECT:
if (!(descr & SIGNB)) {
/* Descriptor is in the object. */
descr = *(word *)(current_p + descr - GC_DS_PER_OBJECT);
} else {
/* Descriptor is in the type descriptor pointed to by the */
/* first "pointer-sized" word of the object. */
ptr_t type_descr = *(ptr_t *)current_p;
/* type_descr is either a valid pointer to the descriptor */
/* structure, or this object was on a free list. */
/* If it was anything but the last object on the free list, */
/* we will misinterpret the next object on the free list as */
/* the type descriptor, and get a 0 GC descriptor, which */
/* is ideal. Unfortunately, we need to check for the last */
/* object case explicitly. */
if (EXPECT(NULL == type_descr, FALSE)) {
mark_stack_top--;
continue;
}
descr = *(word *)(type_descr
- ((signed_word)descr + (GC_INDIR_PER_OBJ_BIAS
- GC_DS_PER_OBJECT)));
}
if (0 == descr) {
/* Can happen either because we generated a 0 descriptor */
/* or we saw a pointer to a free object. */
mark_stack_top--;
continue;
}
goto retry;
}
} else {
/* Small object with length descriptor. */
mark_stack_top--;
# ifndef SMALL_CONFIG
if (descr < sizeof(ptr_t))
continue;
# endif
# ifdef ENABLE_TRACE
if (ADDR_INSIDE(GC_trace_ptr, current_p, current_p + descr)) {
GC_log_printf("GC #%lu: small object; start %p, len %lu\n",
(unsigned long)GC_gc_no, (void *)current_p,
(unsigned long)descr);
}
# endif
limit = current_p + descr;
}
/* The simple case in which we're scanning a range. */
GC_ASSERT((ADDR(current_p) & (ALIGNMENT-1)) == 0);
credit -= limit - current_p;
limit -= sizeof(ptr_t);
{
# define PREF_DIST 4
# if !defined(SMALL_CONFIG) && !defined(USE_PTR_HWTAG)
ptr_t deferred;
/* Try to prefetch the next pointer to be examined ASAP. */
/* Empirically, this also seems to help slightly without */
/* prefetches, at least on Linux/i686. Presumably this loop */
/* ends up with less register pressure, and gcc thus ends up */
/* generating slightly better code. Overall gcc code quality */
/* for this loop is still not great. */
for (;;) {
PREFETCH(limit - PREF_DIST*CACHE_LINE_SIZE);
GC_ASSERT(ADDR_GE(limit, current_p));
deferred = *(ptr_t *)limit;
FIXUP_POINTER(deferred);
limit -= ALIGNMENT;
if (ADDR_LT(least_ha, deferred) && ADDR_LT(deferred, greatest_ha)) {
PREFETCH(deferred);
break;
}
if (ADDR_LT(limit, current_p)) goto next_object;
/* Unroll once, so we don't do too many of the prefetches */
/* based on limit. */
deferred = *(ptr_t *)limit;
FIXUP_POINTER(deferred);
limit -= ALIGNMENT;
if (ADDR_LT(least_ha, deferred) && ADDR_LT(deferred, greatest_ha)) {
PREFETCH(deferred);
break;
}
if (ADDR_LT(limit, current_p)) goto next_object;
}
# endif
for (; ADDR_GE(limit, current_p); current_p += ALIGNMENT) {
/* Empirically, unrolling this loop doesn't help a lot. */
/* Since PUSH_CONTENTS expands to a lot of code, */
/* we don't. */
LOAD_PTR_OR_CONTINUE(q, current_p);
FIXUP_POINTER(q);
PREFETCH(current_p + PREF_DIST*CACHE_LINE_SIZE);
if (ADDR_LT(least_ha, q) && ADDR_LT(q, greatest_ha)) {
/* Prefetch the content of the object we just pushed. It is */
/* likely we will need them soon. */
PREFETCH(q);
# ifdef ENABLE_TRACE
if (GC_trace_ptr == current_p) {
GC_log_printf("GC #%lu: considering(1) %p -> %p\n",
(unsigned long)GC_gc_no, (void *)current_p,
(void *)q);
}
# endif
PUSH_CONTENTS(q, mark_stack_top, mark_stack_limit, current_p);
}
}
# if !defined(SMALL_CONFIG) && !defined(USE_PTR_HWTAG)
/* We still need to mark the entry we previously prefetched. */
/* We already know that it passes the preliminary pointer */
/* validity test. */
# ifdef ENABLE_TRACE
if (GC_trace_ptr == current_p) {
GC_log_printf("GC #%lu: considering(2) %p -> %p\n",
(unsigned long)GC_gc_no, (void *)current_p,
(void *)deferred);
}
# endif
PUSH_CONTENTS(deferred, mark_stack_top, mark_stack_limit, current_p);
next_object:;
# endif
}
}
return mark_stack_top;
}
#ifdef PARALLEL_MARK
/* Note: this is protected by the mark lock. */
STATIC GC_bool GC_help_wanted = FALSE;
/* Number of running helpers. Protected by the mark lock. */
STATIC unsigned GC_helper_count = 0;
/* Number of active helpers. May increase and decrease within each */
/* mark cycle; but once it returns to 0, it stays zero for the cycle. */
/* Protected by the mark lock. */
STATIC unsigned GC_active_count = 0;
GC_INNER word GC_mark_no = 0;
#ifdef LINT2
# define LOCAL_MARK_STACK_SIZE (HBLKSIZE / 8)
#else
/* Under normal circumstances, this is big enough to guarantee we do */
/* not overflow half of it in a single call to GC_mark_from. */
# define LOCAL_MARK_STACK_SIZE HBLKSIZE
#endif
/* Wait all markers to finish initialization (i.e. store */
/* marker_[b]sp, marker_mach_threads, GC_marker_Id). */
GC_INNER void GC_wait_for_markers_init(void)
{
signed_word count;
GC_ASSERT(I_HOLD_LOCK());
if (GC_markers_m1 == 0)
return;
/* Allocate the local mark stack for the thread that holds */
/* the allocator lock. */
# ifndef CAN_HANDLE_FORK
GC_ASSERT(NULL == GC_main_local_mark_stack);
# else
if (NULL == GC_main_local_mark_stack)
# endif
{
size_t bytes_to_get =
ROUNDUP_PAGESIZE_IF_MMAP(LOCAL_MARK_STACK_SIZE * sizeof(mse));
GC_ASSERT(GC_page_size != 0);
GC_main_local_mark_stack = (mse *)GC_os_get_mem(bytes_to_get);
if (NULL == GC_main_local_mark_stack)
ABORT("Insufficient memory for main local_mark_stack");
}
/* Reuse the mark lock and builders count to synchronize */
/* marker threads startup. */
GC_acquire_mark_lock();
GC_fl_builder_count += GC_markers_m1;
count = GC_fl_builder_count;
GC_release_mark_lock();
if (count != 0) {
GC_ASSERT(count > 0);
GC_wait_for_reclaim();
}
}
/* Steal mark stack entries starting at mse low into mark stack local */
/* until we either steal mse high, or we have n_to_get entries. */
/* Return a pointer to the top of the local mark stack. */
/* (*next) is replaced by a pointer to the next unscanned mark stack */
/* entry. */
STATIC mse * GC_steal_mark_stack(mse * low, mse * high, mse * local,
size_t n_to_get, mse **next)
{
mse *p;
mse *top = local - 1;
size_t i = 0;
GC_ASSERT(ADDR_GE((ptr_t)high, (ptr_t)(low - 1))
&& (word)(high - low + 1) <= GC_mark_stack_size);
for (p = low; ADDR_GE((ptr_t)high, (ptr_t)p) && i <= n_to_get; ++p) {
word descr = AO_load(&(p -> mse_descr));