forked from ivmai/bdwgc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalize.c
1465 lines (1320 loc) · 50.8 KB
/
finalize.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-1996 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
* Copyright (c) 2007 Free Software Foundation, Inc.
* 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"
#ifndef GC_NO_FINALIZATION
# include "gc/javaxfc.h" /* to get GC_finalize_all() as extern "C" */
/* Type of mark procedure used for marking from finalizable object. */
/* This procedure normally does not mark the object, only its */
/* descendants. */
typedef void (* finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */);
#define HASH3(addr,size,log_size) \
((size_t)((ADDR(addr) >> 3) ^ (ADDR(addr) >> (3 + (log_size)))) \
& ((size)-1))
#define HASH2(addr,log_size) HASH3(addr, (size_t)1 << (log_size), log_size)
struct hash_chain_entry {
GC_hidden_pointer hidden_key;
struct hash_chain_entry * next;
};
struct disappearing_link {
struct hash_chain_entry prolog;
# define dl_hidden_link prolog.hidden_key /* field to be cleared */
# define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
# define dl_set_next(x, y) \
(void)((x)->prolog.next = (struct hash_chain_entry *)(y))
GC_hidden_pointer dl_hidden_obj; /* pointer to object base */
};
struct finalizable_object {
struct hash_chain_entry prolog;
/* Pointer to object base. No longer hidden once object is on */
/* finalize_now queue. */
# define fo_hidden_base prolog.hidden_key
# define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
# define fo_set_next(x,y) ((x)->prolog.next = (struct hash_chain_entry *)(y))
GC_finalization_proc fo_fn; /* finalizer */
finalization_mark_proc fo_mark_proc; /* mark-through procedure */
ptr_t fo_client_data;
size_t fo_object_sz; /* in bytes */
};
#ifdef AO_HAVE_store
/* Update finalize_now atomically as GC_should_invoke_finalizers does */
/* not acquire the allocator lock. */
# define SET_FINALIZE_NOW(fo) \
GC_cptr_store((volatile ptr_t *)&GC_fnlz_roots.finalize_now, \
(ptr_t)(fo))
#else
# define SET_FINALIZE_NOW(fo) (void)(GC_fnlz_roots.finalize_now = (fo))
#endif /* !THREADS */
GC_API void GC_CALL GC_push_finalizer_structures(void)
{
GC_ASSERT(ADDR(&GC_dl_hashtbl.head) % sizeof(ptr_t) == 0);
GC_ASSERT(ADDR(&GC_fnlz_roots) % sizeof(ptr_t) == 0);
# ifndef GC_LONG_REFS_NOT_NEEDED
GC_ASSERT(ADDR(&GC_ll_hashtbl.head) % sizeof(ptr_t) == 0);
GC_PUSH_ALL_SYM(GC_ll_hashtbl.head);
# endif
GC_PUSH_ALL_SYM(GC_dl_hashtbl.head);
GC_PUSH_ALL_SYM(GC_fnlz_roots);
/* GC_toggleref_arr is pushed specially by GC_mark_togglerefs. */
}
/* Threshold of log_size to initiate full collection before growing */
/* a hash table. */
#ifndef GC_ON_GROW_LOG_SIZE_MIN
# define GC_ON_GROW_LOG_SIZE_MIN LOG_HBLKSIZE
#endif
/* Double the size of a hash table. *log_size_ptr is the log of its */
/* current size. May be a no-op. *table_ptr is a pointer to an array */
/* of hash headers. We update both *table_ptr and *log_size_ptr on */
/* success. */
STATIC void GC_grow_table(struct hash_chain_entry ***table_ptr,
unsigned *log_size_ptr, const size_t *entries_ptr)
{
size_t i;
struct hash_chain_entry *p;
unsigned log_old_size = *log_size_ptr;
unsigned log_new_size = log_old_size + 1;
size_t old_size = NULL == *table_ptr ? 0 : (size_t)1 << log_old_size;
size_t new_size = (size_t)1 << log_new_size;
/* FIXME: Power of 2 size often gets rounded up to one more page. */
struct hash_chain_entry **new_table;
GC_ASSERT(I_HOLD_LOCK());
/* Avoid growing the table in case of at least 25% of entries can */
/* be deleted by enforcing a collection. Ignored for small tables. */
/* In incremental mode we skip this optimization, as we want to */
/* avoid triggering a full GC whenever possible. */
if (log_old_size >= (unsigned)GC_ON_GROW_LOG_SIZE_MIN && !GC_incremental) {
IF_CANCEL(int cancel_state;)
DISABLE_CANCEL(cancel_state);
GC_gcollect_inner();
RESTORE_CANCEL(cancel_state);
/* GC_finalize might decrease entries value. */
if (*entries_ptr < ((size_t)1 << log_old_size) - (*entries_ptr >> 2))
return;
}
new_table = (struct hash_chain_entry **)
GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
new_size * sizeof(struct hash_chain_entry *), NORMAL);
if (NULL == new_table) {
if (NULL == *table_ptr) {
ABORT("Insufficient space for initial table allocation");
} else {
return;
}
}
for (i = 0; i < old_size; i++) {
for (p = (*table_ptr)[i]; p != NULL;) {
ptr_t real_key = (ptr_t)GC_REVEAL_POINTER(p -> hidden_key);
struct hash_chain_entry *next = p -> next;
size_t new_hash = HASH3(real_key, new_size, log_new_size);
p -> next = new_table[new_hash];
GC_dirty(p);
new_table[new_hash] = p;
p = next;
}
}
*log_size_ptr = log_new_size;
*table_ptr = new_table;
GC_dirty(new_table); /* entire object */
}
GC_API int GC_CALL GC_register_disappearing_link(void * * link)
{
ptr_t base;
base = (ptr_t)GC_base(link);
if (base == 0)
ABORT("Bad arg to GC_register_disappearing_link");
return GC_general_register_disappearing_link(link, base);
}
STATIC int GC_register_disappearing_link_inner(
struct dl_hashtbl_s *dl_hashtbl, void **link,
const void *obj, const char *tbl_log_name)
{
struct disappearing_link *curr_dl;
size_t index;
struct disappearing_link * new_dl;
GC_ASSERT(GC_is_initialized);
if (EXPECT(GC_find_leak, FALSE)) return GC_UNIMPLEMENTED;
# ifdef GC_ASSERTIONS
GC_noop1_ptr(*link); /* check accessibility */
# endif
LOCK();
GC_ASSERT(obj != NULL && GC_base_C(obj) == obj);
if (EXPECT(NULL == dl_hashtbl -> head, FALSE)
|| EXPECT(dl_hashtbl -> entries
> ((size_t)1 << dl_hashtbl -> log_size), FALSE)) {
GC_grow_table((struct hash_chain_entry ***)&dl_hashtbl -> head,
&dl_hashtbl -> log_size, &dl_hashtbl -> entries);
GC_COND_LOG_PRINTF("Grew %s table to %u entries\n", tbl_log_name,
1U << dl_hashtbl -> log_size);
}
index = HASH2(link, dl_hashtbl -> log_size);
for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
curr_dl = dl_next(curr_dl)) {
if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
/* Alternatively, GC_HIDE_NZ_POINTER() could be used instead. */
curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
UNLOCK();
return GC_DUPLICATE;
}
}
new_dl = (struct disappearing_link *)
GC_INTERNAL_MALLOC(sizeof(struct disappearing_link), NORMAL);
if (EXPECT(NULL == new_dl, FALSE)) {
GC_oom_func oom_fn = GC_oom_fn;
UNLOCK();
new_dl = (struct disappearing_link *)
(*oom_fn)(sizeof(struct disappearing_link));
if (0 == new_dl) {
return GC_NO_MEMORY;
}
/* It's not likely we'll make it here, but ... */
LOCK();
/* Recalculate index since the table may grow. */
index = HASH2(link, dl_hashtbl -> log_size);
/* Check again that our disappearing link not in the table. */
for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
curr_dl = dl_next(curr_dl)) {
if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
UNLOCK();
# ifndef DBG_HDRS_ALL
/* Free unused new_dl returned by GC_oom_fn(). */
GC_free(new_dl);
# endif
return GC_DUPLICATE;
}
}
}
new_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
new_dl -> dl_hidden_link = GC_HIDE_POINTER(link);
dl_set_next(new_dl, dl_hashtbl -> head[index]);
GC_dirty(new_dl);
dl_hashtbl -> head[index] = new_dl;
dl_hashtbl -> entries++;
GC_dirty(dl_hashtbl->head + index);
UNLOCK();
return GC_SUCCESS;
}
GC_API int GC_CALL GC_general_register_disappearing_link(void * * link,
const void * obj)
{
if ((ADDR(link) & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link))
ABORT("Bad arg to GC_general_register_disappearing_link");
return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj,
"dl");
}
#ifdef DBG_HDRS_ALL
# define FREE_DL_ENTRY(curr_dl) dl_set_next(curr_dl, NULL)
#else
# define FREE_DL_ENTRY(curr_dl) GC_free(curr_dl)
#endif
/* Unregisters given link and returns the link entry to free. */
GC_INLINE struct disappearing_link *GC_unregister_disappearing_link_inner(
struct dl_hashtbl_s *dl_hashtbl, void **link)
{
struct disappearing_link *curr_dl;
struct disappearing_link *prev_dl = NULL;
size_t index;
GC_ASSERT(I_HOLD_LOCK());
if (EXPECT(NULL == dl_hashtbl -> head, FALSE)) return NULL;
index = HASH2(link, dl_hashtbl -> log_size);
for (curr_dl = dl_hashtbl -> head[index]; curr_dl;
curr_dl = dl_next(curr_dl)) {
if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
/* Remove found entry from the table. */
if (NULL == prev_dl) {
dl_hashtbl -> head[index] = dl_next(curr_dl);
GC_dirty(dl_hashtbl->head + index);
} else {
dl_set_next(prev_dl, dl_next(curr_dl));
GC_dirty(prev_dl);
}
dl_hashtbl -> entries--;
break;
}
prev_dl = curr_dl;
}
return curr_dl;
}
GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
{
struct disappearing_link *curr_dl;
if ((ADDR(link) & (ALIGNMENT-1)) != 0) {
/* Nothing to do. */
return 0;
}
LOCK();
curr_dl = GC_unregister_disappearing_link_inner(&GC_dl_hashtbl, link);
UNLOCK();
if (NULL == curr_dl) return 0;
FREE_DL_ENTRY(curr_dl);
return 1;
}
/* Mark from one finalizable object using the specified mark proc. */
/* May not mark the object pointed to by real_ptr (i.e, it is the job */
/* of the caller, if appropriate). Note that this is called with the */
/* mutator running. This is safe only if the mutator (client) gets */
/* the allocator lock to reveal hidden pointers. */
GC_INLINE void GC_mark_fo(ptr_t real_ptr, finalization_mark_proc fo_mark_proc)
{
GC_ASSERT(I_HOLD_LOCK());
fo_mark_proc(real_ptr);
/* Process objects pushed by the mark procedure. */
while (!GC_mark_stack_empty())
MARK_FROM_MARK_STACK();
}
/* Complete a collection in progress, if any. */
GC_INLINE void GC_complete_ongoing_collection(void) {
if (EXPECT(GC_collection_in_progress(), FALSE)) {
while (!GC_mark_some(NULL)) { /* empty */ }
}
}
/* Toggle-ref support. */
#ifndef GC_TOGGLE_REFS_NOT_NEEDED
typedef union toggle_ref_u GCToggleRef;
STATIC GC_toggleref_func GC_toggleref_callback = 0;
GC_INNER void GC_process_togglerefs(void)
{
size_t i;
size_t new_size = 0;
GC_bool needs_barrier = FALSE;
GC_ASSERT(I_HOLD_LOCK());
for (i = 0; i < GC_toggleref_array_size; ++i) {
GCToggleRef *r = &GC_toggleref_arr[i];
void *obj = r -> strong_ref;
if ((ADDR(obj) & 1) != 0) {
obj = GC_REVEAL_POINTER(r -> weak_ref);
GC_ASSERT((ADDR(obj) & 1) == 0);
}
if (NULL == obj) continue;
switch (GC_toggleref_callback(obj)) {
case GC_TOGGLE_REF_DROP:
break;
case GC_TOGGLE_REF_STRONG:
GC_toggleref_arr[new_size++].strong_ref = obj;
needs_barrier = TRUE;
break;
case GC_TOGGLE_REF_WEAK:
GC_toggleref_arr[new_size++].weak_ref = GC_HIDE_POINTER(obj);
break;
default:
ABORT("Bad toggle-ref status returned by callback");
}
}
if (new_size < GC_toggleref_array_size) {
BZERO(&GC_toggleref_arr[new_size],
(GC_toggleref_array_size - new_size) * sizeof(GCToggleRef));
GC_toggleref_array_size = new_size;
}
if (needs_barrier)
GC_dirty(GC_toggleref_arr); /* entire object */
}
STATIC void GC_normal_finalize_mark_proc(ptr_t);
STATIC void GC_mark_togglerefs(void)
{
size_t i;
GC_ASSERT(I_HOLD_LOCK());
if (NULL == GC_toggleref_arr)
return;
GC_set_mark_bit(GC_toggleref_arr);
for (i = 0; i < GC_toggleref_array_size; ++i) {
void *obj = GC_toggleref_arr[i].strong_ref;
if (obj != NULL && (ADDR(obj) & 1) == 0) {
/* Push and mark the object. */
GC_mark_fo((ptr_t)obj, GC_normal_finalize_mark_proc);
GC_set_mark_bit(obj);
GC_complete_ongoing_collection();
}
}
}
STATIC void GC_clear_togglerefs(void)
{
size_t i;
GC_ASSERT(I_HOLD_LOCK());
for (i = 0; i < GC_toggleref_array_size; ++i) {
GCToggleRef *r = &GC_toggleref_arr[i];
if ((ADDR(r -> strong_ref) & 1) != 0) {
if (!GC_is_marked(GC_REVEAL_POINTER(r -> weak_ref))) {
r -> weak_ref = 0;
} else {
/* No need to copy, BDWGC is a non-moving collector. */
}
}
}
}
GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func fn)
{
LOCK();
GC_toggleref_callback = fn;
UNLOCK();
}
GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void)
{
GC_toggleref_func fn;
READER_LOCK();
fn = GC_toggleref_callback;
READER_UNLOCK();
return fn;
}
static GC_bool ensure_toggleref_capacity(size_t capacity_inc)
{
GC_ASSERT(I_HOLD_LOCK());
if (NULL == GC_toggleref_arr) {
GC_toggleref_array_capacity = 32; /* initial capacity */
GC_toggleref_arr = (GCToggleRef *)GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
GC_toggleref_array_capacity * sizeof(GCToggleRef),
NORMAL);
if (NULL == GC_toggleref_arr)
return FALSE;
}
if (GC_toggleref_array_size + capacity_inc
>= GC_toggleref_array_capacity) {
GCToggleRef *new_array;
while (GC_toggleref_array_capacity
< GC_toggleref_array_size + capacity_inc) {
GC_toggleref_array_capacity *= 2;
if ((GC_toggleref_array_capacity
& ((size_t)1 << (sizeof(size_t) * 8 - 1))) != 0)
return FALSE; /* overflow */
}
new_array = (GCToggleRef *)GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
GC_toggleref_array_capacity * sizeof(GCToggleRef),
NORMAL);
if (NULL == new_array)
return FALSE;
if (EXPECT(GC_toggleref_array_size > 0, TRUE))
BCOPY(GC_toggleref_arr, new_array,
GC_toggleref_array_size * sizeof(GCToggleRef));
GC_INTERNAL_FREE(GC_toggleref_arr);
GC_toggleref_arr = new_array;
}
return TRUE;
}
GC_API int GC_CALL GC_toggleref_add(void *obj, int is_strong_ref)
{
int res = GC_SUCCESS;
GC_ASSERT(NONNULL_ARG_NOT_NULL(obj));
LOCK();
GC_ASSERT((ADDR(obj) & 1) == 0 && obj == GC_base(obj));
if (GC_toggleref_callback != 0) {
if (!ensure_toggleref_capacity(1)) {
res = GC_NO_MEMORY;
} else {
GCToggleRef *r = &GC_toggleref_arr[GC_toggleref_array_size];
if (is_strong_ref) {
r -> strong_ref = obj;
GC_dirty(GC_toggleref_arr + GC_toggleref_array_size);
} else {
r -> weak_ref = GC_HIDE_POINTER(obj);
GC_ASSERT((r -> weak_ref & 1) != 0);
}
GC_toggleref_array_size++;
}
}
UNLOCK();
return res;
}
#endif /* !GC_TOGGLE_REFS_NOT_NEEDED */
/* Finalizer callback support. */
STATIC GC_await_finalize_proc GC_object_finalized_proc = 0;
GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc fn)
{
LOCK();
GC_object_finalized_proc = fn;
UNLOCK();
}
GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void)
{
GC_await_finalize_proc fn;
READER_LOCK();
fn = GC_object_finalized_proc;
READER_UNLOCK();
return fn;
}
#ifndef GC_LONG_REFS_NOT_NEEDED
GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj)
{
if ((ADDR(link) & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link))
ABORT("Bad arg to GC_register_long_link");
return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj,
"long dl");
}
GC_API int GC_CALL GC_unregister_long_link(void * * link)
{
struct disappearing_link *curr_dl;
if ((ADDR(link) & (ALIGNMENT-1)) != 0) {
/* Nothing to do. */
return 0;
}
LOCK();
curr_dl = GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
UNLOCK();
if (NULL == curr_dl) return 0;
FREE_DL_ENTRY(curr_dl);
return 1;
}
#endif /* !GC_LONG_REFS_NOT_NEEDED */
#ifndef GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED
STATIC int GC_move_disappearing_link_inner(
struct dl_hashtbl_s *dl_hashtbl,
void **link, void **new_link)
{
struct disappearing_link *curr_dl, *new_dl;
struct disappearing_link *prev_dl = NULL;
size_t curr_index, new_index;
GC_hidden_pointer curr_hidden_link, new_hidden_link;
# ifdef GC_ASSERTIONS
GC_noop1_ptr(*new_link);
# endif
GC_ASSERT(I_HOLD_LOCK());
if (EXPECT(NULL == dl_hashtbl -> head, FALSE)) return GC_NOT_FOUND;
/* Find current link. */
curr_index = HASH2(link, dl_hashtbl -> log_size);
curr_hidden_link = GC_HIDE_POINTER(link);
for (curr_dl = dl_hashtbl -> head[curr_index]; curr_dl;
curr_dl = dl_next(curr_dl)) {
if (curr_dl -> dl_hidden_link == curr_hidden_link)
break;
prev_dl = curr_dl;
}
if (EXPECT(NULL == curr_dl, FALSE)) {
return GC_NOT_FOUND;
} else if (link == new_link) {
/* Nothing to do. */
return GC_SUCCESS;
}
/* link is found; now check new_link not present. */
new_index = HASH2(new_link, dl_hashtbl -> log_size);
new_hidden_link = GC_HIDE_POINTER(new_link);
for (new_dl = dl_hashtbl -> head[new_index]; new_dl;
new_dl = dl_next(new_dl)) {
if (new_dl -> dl_hidden_link == new_hidden_link) {
/* Target already registered; bail out. */
return GC_DUPLICATE;
}
}
/* Remove from old, add to new, update link. */
if (NULL == prev_dl) {
dl_hashtbl -> head[curr_index] = dl_next(curr_dl);
} else {
dl_set_next(prev_dl, dl_next(curr_dl));
GC_dirty(prev_dl);
}
curr_dl -> dl_hidden_link = new_hidden_link;
dl_set_next(curr_dl, dl_hashtbl -> head[new_index]);
dl_hashtbl -> head[new_index] = curr_dl;
GC_dirty(curr_dl);
GC_dirty(dl_hashtbl->head); /* entire object */
return GC_SUCCESS;
}
GC_API int GC_CALL GC_move_disappearing_link(void **link, void **new_link)
{
int result;
if ((ADDR(new_link) & (ALIGNMENT-1)) != 0
|| !NONNULL_ARG_NOT_NULL(new_link))
ABORT("Bad new_link arg to GC_move_disappearing_link");
if ((ADDR(link) & (ALIGNMENT-1)) != 0) {
/* Nothing to do. */
return GC_NOT_FOUND;
}
LOCK();
result = GC_move_disappearing_link_inner(&GC_dl_hashtbl, link, new_link);
UNLOCK();
return result;
}
# ifndef GC_LONG_REFS_NOT_NEEDED
GC_API int GC_CALL GC_move_long_link(void **link, void **new_link)
{
int result;
if ((ADDR(new_link) & (ALIGNMENT-1)) != 0
|| !NONNULL_ARG_NOT_NULL(new_link))
ABORT("Bad new_link arg to GC_move_long_link");
if ((ADDR(link) & (ALIGNMENT-1)) != 0) {
/* Nothing to do. */
return GC_NOT_FOUND;
}
LOCK();
result = GC_move_disappearing_link_inner(&GC_ll_hashtbl, link, new_link);
UNLOCK();
return result;
}
# endif /* !GC_LONG_REFS_NOT_NEEDED */
#endif /* !GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED */
/* Possible finalization_marker procedures. Note that mark stack */
/* overflow is handled by the caller, and is not a disaster. */
#if defined(_MSC_VER) && defined(I386)
GC_ATTR_NOINLINE
/* Otherwise some optimizer bug is tickled in VC for x86 (v19, at least). */
#endif
STATIC void GC_normal_finalize_mark_proc(ptr_t p)
{
GC_mark_stack_top = GC_push_obj(p, HDR(p), GC_mark_stack_top,
GC_mark_stack + GC_mark_stack_size);
}
/* This only pays very partial attention to the mark descriptor. */
/* It does the right thing for normal and atomic objects, and treats */
/* most others as normal. */
STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
{
const hdr *hhdr = HDR(p);
word descr = hhdr -> hb_descr;
ptr_t current_p;
ptr_t scan_limit;
ptr_t target_limit = p + hhdr -> hb_sz - 1;
if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
scan_limit = p + descr - sizeof(ptr_t);
} else {
scan_limit = target_limit + 1 - sizeof(ptr_t);
}
for (current_p = p; ADDR_GE(scan_limit, current_p);
current_p += ALIGNMENT) {
ptr_t q;
LOAD_PTR_OR_CONTINUE(q, current_p);
if (ADDR_LT(q, p) || ADDR_LT(target_limit, q)) {
GC_PUSH_ONE_HEAP(q, current_p, GC_mark_stack_top);
}
}
}
STATIC void GC_null_finalize_mark_proc(ptr_t p)
{
UNUSED_ARG(p);
}
/* Possible finalization_marker procedures. Note that mark stack */
/* overflow is handled by the caller, and is not a disaster. */
/* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
/* but it is explicitly tested for, and triggers different */
/* behavior. Objects registered in this way are not finalized */
/* if they are reachable by other finalizable objects, even if those */
/* other objects specify no ordering. */
STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
{
/* A dummy comparison to ensure the compiler not to optimize two */
/* identical functions into a single one (thus, to ensure a unique */
/* address of each). Alternatively, GC_noop1_ptr(p) could be used. */
if (EXPECT(NULL == p, FALSE)) return;
GC_normal_finalize_mark_proc(p);
}
/* Avoid the work if unreachable finalizable objects are not used. */
/* TODO: turn need_unreachable_finalization into a counter */
static GC_bool need_unreachable_finalization = FALSE;
/* Register a finalization function. See gc.h for details. */
/* The last parameter is a procedure that determines */
/* marking for finalization ordering. Any objects marked */
/* by that procedure will be guaranteed to not have been */
/* finalized when this finalizer is invoked. */
STATIC void GC_register_finalizer_inner(void * obj,
GC_finalization_proc fn, void *cd,
GC_finalization_proc *ofn, void **ocd,
finalization_mark_proc mp)
{
struct finalizable_object * curr_fo;
size_t index;
struct finalizable_object *new_fo = 0;
const hdr *hhdr = NULL; /* initialized to prevent warning. */
GC_ASSERT(GC_is_initialized);
if (EXPECT(GC_find_leak, FALSE)) {
/* No-op. *ocd and *ofn remain unchanged. */
return;
}
LOCK();
GC_ASSERT(obj != NULL && GC_base_C(obj) == obj);
if (mp == GC_unreachable_finalize_mark_proc)
need_unreachable_finalization = TRUE;
if (EXPECT(NULL == GC_fnlz_roots.fo_head, FALSE)
|| EXPECT(GC_fo_entries
> ((size_t)1 << GC_log_fo_table_size), FALSE)) {
GC_grow_table((struct hash_chain_entry ***)&GC_fnlz_roots.fo_head,
&GC_log_fo_table_size, &GC_fo_entries);
GC_COND_LOG_PRINTF("Grew fo table to %u entries\n",
1U << GC_log_fo_table_size);
}
for (;;) {
struct finalizable_object *prev_fo = NULL;
GC_oom_func oom_fn;
index = HASH2(obj, GC_log_fo_table_size);
curr_fo = GC_fnlz_roots.fo_head[index];
while (curr_fo != NULL) {
GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
if (curr_fo -> fo_hidden_base == GC_HIDE_POINTER(obj)) {
/* Interruption by a signal in the middle of this */
/* should be safe. The client may see only *ocd */
/* updated, but we'll declare that to be his problem. */
if (ocd) *ocd = curr_fo -> fo_client_data;
if (ofn) *ofn = curr_fo -> fo_fn;
/* Delete the structure for obj. */
if (prev_fo == 0) {
GC_fnlz_roots.fo_head[index] = fo_next(curr_fo);
} else {
fo_set_next(prev_fo, fo_next(curr_fo));
GC_dirty(prev_fo);
}
if (fn == 0) {
GC_fo_entries--;
/* May not happen if we get a signal. But a high */
/* estimate will only make the table larger than */
/* necessary. */
# if !defined(THREADS) && !defined(DBG_HDRS_ALL)
GC_free(curr_fo);
# endif
} else {
curr_fo -> fo_fn = fn;
curr_fo -> fo_client_data = (ptr_t)cd;
curr_fo -> fo_mark_proc = mp;
GC_dirty(curr_fo);
/* Reinsert it. We deleted it first to maintain */
/* consistency in the event of a signal. */
if (prev_fo == 0) {
GC_fnlz_roots.fo_head[index] = curr_fo;
} else {
fo_set_next(prev_fo, curr_fo);
GC_dirty(prev_fo);
}
}
if (NULL == prev_fo)
GC_dirty(GC_fnlz_roots.fo_head + index);
UNLOCK();
# ifndef DBG_HDRS_ALL
/* Free unused new_fo returned by GC_oom_fn() */
GC_free(new_fo);
# endif
return;
}
prev_fo = curr_fo;
curr_fo = fo_next(curr_fo);
}
if (EXPECT(new_fo != 0, FALSE)) {
/* new_fo is returned by GC_oom_fn(). */
GC_ASSERT(fn != 0);
# ifdef LINT2
if (NULL == hhdr) ABORT("Bad hhdr in GC_register_finalizer_inner");
# endif
break;
}
if (fn == 0) {
if (ocd) *ocd = 0;
if (ofn) *ofn = 0;
UNLOCK();
return;
}
GET_HDR(obj, hhdr);
if (EXPECT(NULL == hhdr, FALSE)) {
/* We won't collect it, hence finalizer wouldn't be run. */
if (ocd) *ocd = 0;
if (ofn) *ofn = 0;
UNLOCK();
return;
}
new_fo = (struct finalizable_object *)
GC_INTERNAL_MALLOC(sizeof(struct finalizable_object), NORMAL);
if (EXPECT(new_fo != 0, TRUE))
break;
oom_fn = GC_oom_fn;
UNLOCK();
new_fo = (struct finalizable_object *)
(*oom_fn)(sizeof(struct finalizable_object));
if (0 == new_fo) {
/* No enough memory. *ocd and *ofn remain unchanged. */
return;
}
/* It's not likely we'll make it here, but ... */
LOCK();
/* Recalculate index since the table may grow and */
/* check again that our finalizer is not in the table. */
}
GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
if (ocd) *ocd = 0;
if (ofn) *ofn = 0;
new_fo -> fo_hidden_base = GC_HIDE_POINTER(obj);
new_fo -> fo_fn = fn;
new_fo -> fo_client_data = (ptr_t)cd;
new_fo -> fo_object_sz = hhdr -> hb_sz;
new_fo -> fo_mark_proc = mp;
fo_set_next(new_fo, GC_fnlz_roots.fo_head[index]);
GC_dirty(new_fo);
GC_fo_entries++;
GC_fnlz_roots.fo_head[index] = new_fo;
GC_dirty(GC_fnlz_roots.fo_head + index);
UNLOCK();
}
GC_API void GC_CALL GC_register_finalizer(void * obj,
GC_finalization_proc fn, void * cd,
GC_finalization_proc *ofn, void ** ocd)
{
GC_register_finalizer_inner(obj, fn, cd, ofn,
ocd, GC_normal_finalize_mark_proc);
}
GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
GC_finalization_proc fn, void * cd,
GC_finalization_proc *ofn, void ** ocd)
{
GC_register_finalizer_inner(obj, fn, cd, ofn,
ocd, GC_ignore_self_finalize_mark_proc);
}
GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
GC_finalization_proc fn, void * cd,
GC_finalization_proc *ofn, void ** ocd)
{
GC_register_finalizer_inner(obj, fn, cd, ofn,
ocd, GC_null_finalize_mark_proc);
}
GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
GC_finalization_proc fn, void * cd,
GC_finalization_proc *ofn, void ** ocd)
{
GC_ASSERT(GC_java_finalization);
GC_register_finalizer_inner(obj, fn, cd, ofn,
ocd, GC_unreachable_finalize_mark_proc);
}
#ifndef NO_DEBUGGING
STATIC void GC_dump_finalization_links(
const struct dl_hashtbl_s *dl_hashtbl)
{
size_t dl_size = (size_t)1 << dl_hashtbl -> log_size;
size_t i;
if (NULL == dl_hashtbl -> head) {
/* The table is empty. */
return;
}
for (i = 0; i < dl_size; i++) {
struct disappearing_link *curr_dl;
for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
curr_dl = dl_next(curr_dl)) {
ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_dl -> dl_hidden_obj);
ptr_t real_link = (ptr_t)GC_REVEAL_POINTER(curr_dl -> dl_hidden_link);
GC_printf("Object: %p, link value: %p, link addr: %p\n",
(void *)real_ptr, *(void **)real_link, (void *)real_link);
}
}
}
GC_API void GC_CALL GC_dump_finalization(void)
{
struct finalizable_object * curr_fo;
size_t i;
size_t fo_size = GC_fnlz_roots.fo_head == NULL ? 0 :
(size_t)1 << GC_log_fo_table_size;
GC_printf("\n***Disappearing (short) links:\n");
GC_dump_finalization_links(&GC_dl_hashtbl);
# ifndef GC_LONG_REFS_NOT_NEEDED
GC_printf("\n***Disappearing long links:\n");
GC_dump_finalization_links(&GC_ll_hashtbl);
# endif
GC_printf("\n***Finalizers:\n");
for (i = 0; i < fo_size; i++) {
for (curr_fo = GC_fnlz_roots.fo_head[i];
curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
GC_printf("Finalizable object: %p\n", (void *)real_ptr);
}
}
}
#endif /* !NO_DEBUGGING */
#ifndef SMALL_CONFIG
STATIC size_t GC_old_dl_entries = 0; /* for stats printing */
# ifndef GC_LONG_REFS_NOT_NEEDED
STATIC size_t GC_old_ll_entries = 0;
# endif
#endif /* !SMALL_CONFIG */
#ifndef THREADS
/* Global variables to minimize the level of recursion when a client */
/* finalizer allocates memory. (Only the lowest byte of */
/* GC_finalizer_nested is used, the rest of the variable is padding */
/* for the proper global data alignment required for some compilers */
/* like Watcom.) */
STATIC int GC_finalizer_nested = 0;
STATIC unsigned GC_finalizer_skipped = 0;
/* Checks and updates the level of finalizers recursion. */
/* Returns NULL if GC_invoke_finalizers() should not be called by the */
/* collector (to minimize the risk of a deep finalizers recursion), */
/* otherwise returns a pointer to GC_finalizer_nested. */
STATIC unsigned char *GC_check_finalizer_nested(void)
{
unsigned nesting_level = *(unsigned char *)&GC_finalizer_nested;
if (nesting_level) {
/* We are inside another GC_invoke_finalizers(). */
/* Skip some implicitly-called GC_invoke_finalizers() */
/* depending on the nesting (recursion) level. */
if (++GC_finalizer_skipped < (1U << nesting_level)) return NULL;
GC_finalizer_skipped = 0;
}
*(char *)&GC_finalizer_nested = (char)(nesting_level + 1);
return (unsigned char *)&GC_finalizer_nested;
}
#endif /* !THREADS */
GC_INLINE void GC_make_disappearing_links_disappear(
struct dl_hashtbl_s* dl_hashtbl,
GC_bool is_remove_dangling)
{
size_t i;
size_t dl_size = (size_t)1 << dl_hashtbl -> log_size;
GC_bool needs_barrier = FALSE;
GC_ASSERT(I_HOLD_LOCK());
if (NULL == dl_hashtbl -> head) {
/* The table is empty. */
return;
}
for (i = 0; i < dl_size; i++) {
struct disappearing_link *curr_dl, *next_dl;
struct disappearing_link *prev_dl = NULL;
for (curr_dl = dl_hashtbl->head[i]; curr_dl != NULL; curr_dl = next_dl) {
next_dl = dl_next(curr_dl);
# if defined(GC_ASSERTIONS) && !defined(THREAD_SANITIZER)
/* Check accessibility of the location pointed by link. */
GC_noop1_ptr(*(ptr_t *)GC_REVEAL_POINTER(curr_dl -> dl_hidden_link));
# endif
if (is_remove_dangling) {
ptr_t real_link = (ptr_t)GC_base(GC_REVEAL_POINTER(
curr_dl -> dl_hidden_link));
if (NULL == real_link || EXPECT(GC_is_marked(real_link), TRUE)) {
prev_dl = curr_dl;
continue;
}
} else {
if (EXPECT(GC_is_marked((ptr_t)GC_REVEAL_POINTER(
curr_dl -> dl_hidden_obj)), TRUE)) {
prev_dl = curr_dl;
continue;
}
*(ptr_t *)GC_REVEAL_POINTER(curr_dl -> dl_hidden_link) = NULL;
}
/* Delete curr_dl entry from dl_hashtbl. */
if (NULL == prev_dl) {
dl_hashtbl -> head[i] = next_dl;
needs_barrier = TRUE;
} else {
dl_set_next(prev_dl, next_dl);
GC_dirty(prev_dl);
}
GC_clear_mark_bit(curr_dl);
dl_hashtbl -> entries--;