2 * sgen-bridge.c: Simple generational GC.
4 * Copyright 2011 Novell, Inc (http://www.novell.com)
5 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 * Copyright 2001-2003 Ximian, Inc
18 * Copyright 2003-2010 Novell, Inc.
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 #include "sgen-bridge.h"
48 #include "sgen-hash-table.h"
49 #include "sgen-qsort.h"
50 #include "utils/mono-logger-internal.h"
51 #include "utils/mono-time.h"
52 #include "utils/mono-compiler.h"
77 * FIXME: Optimizations:
79 * Don't allocate a scrs array for just one source. Most objects have
80 * just one source, so use the srcs pointer itself.
82 typedef struct _HashEntry {
83 MonoObject *obj; /* This is a duplicate - it's already stored in the hash table */
98 int num_bridge_entries;
99 DynIntArray xrefs; /* these are incoming, not outgoing */
102 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
104 static MonoGCBridgeCallbacks bridge_callbacks;
106 static int current_time;
108 gboolean bridge_processing_in_progress = FALSE;
118 dyn_array_init (DynArray *da)
126 dyn_array_uninit (DynArray *da, int elem_size)
128 if (da->capacity <= 0)
131 sgen_free_internal_dynamic (da->data, elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
136 dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
138 int old_capacity = da->capacity;
141 if (capacity <= old_capacity)
144 if (da->capacity == 0)
146 while (capacity > da->capacity)
149 new_data = sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
150 memcpy (new_data, da->data, elem_size * da->size);
151 sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
156 dyn_array_add (DynArray *da, int elem_size)
160 dyn_array_ensure_capacity (da, da->size + 1, elem_size);
162 p = da->data + da->size * elem_size;
169 dyn_array_int_init (DynIntArray *da)
171 dyn_array_init (&da->array);
175 dyn_array_int_uninit (DynIntArray *da)
177 dyn_array_uninit (&da->array, sizeof (int));
181 dyn_array_int_size (DynIntArray *da)
183 return da->array.size;
187 dyn_array_int_set_size (DynIntArray *da, int size)
189 da->array.size = size;
193 dyn_array_int_add (DynIntArray *da, int x)
195 int *p = dyn_array_add (&da->array, sizeof (int));
200 dyn_array_int_get (DynIntArray *da, int x)
202 return ((int*)da->array.data)[x];
206 dyn_array_int_set (DynIntArray *da, int idx, int val)
208 ((int*)da->array.data)[idx] = val;
212 dyn_array_int_ensure_capacity (DynIntArray *da, int capacity)
214 dyn_array_ensure_capacity (&da->array, capacity, sizeof (int));
218 dyn_array_int_set_all (DynIntArray *dst, DynIntArray *src)
220 dyn_array_int_ensure_capacity (dst, src->array.size);
221 memcpy (dst->array.data, src->array.data, src->array.size * sizeof (int));
222 dst->array.size = src->array.size;
228 dyn_array_ptr_init (DynPtrArray *da)
230 dyn_array_init (&da->array);
234 dyn_array_ptr_uninit (DynPtrArray *da)
236 dyn_array_uninit (&da->array, sizeof (void*));
240 dyn_array_ptr_size (DynPtrArray *da)
242 return da->array.size;
246 dyn_array_ptr_set_size (DynPtrArray *da, int size)
248 da->array.size = size;
252 dyn_array_ptr_get (DynPtrArray *da, int x)
254 return ((void**)da->array.data)[x];
258 dyn_array_ptr_add (DynPtrArray *da, void *ptr)
260 void **p = dyn_array_add (&da->array, sizeof (void*));
264 #define dyn_array_ptr_push dyn_array_ptr_add
267 dyn_array_ptr_pop (DynPtrArray *da)
270 int size = da->array.size;
272 p = dyn_array_ptr_get (da, size - 1);
280 dyn_array_scc_init (DynSCCArray *da)
282 dyn_array_init (&da->array);
286 dyn_array_scc_uninit (DynSCCArray *da)
288 dyn_array_uninit (&da->array, sizeof (SCC));
292 dyn_array_scc_size (DynSCCArray *da)
294 return da->array.size;
298 dyn_array_scc_add (DynSCCArray *da)
300 return dyn_array_add (&da->array, sizeof (SCC));
304 dyn_array_scc_get_ptr (DynSCCArray *da, int x)
306 return &((SCC*)da->array.data)[x];
311 static DynIntArray merge_array;
314 dyn_array_int_contains (DynIntArray *da, int x)
317 for (i = 0; i < dyn_array_int_size (da); ++i)
318 if (dyn_array_int_get (da, i) == x)
325 dyn_array_int_merge (DynIntArray *dst, DynIntArray *src)
329 dyn_array_int_ensure_capacity (&merge_array, dyn_array_int_size (dst) + dyn_array_int_size (src));
330 dyn_array_int_set_size (&merge_array, 0);
332 for (i = j = 0; i < dyn_array_int_size (dst) || j < dyn_array_int_size (src); ) {
333 if (i < dyn_array_int_size (dst) && j < dyn_array_int_size (src)) {
334 int a = dyn_array_int_get (dst, i);
335 int b = dyn_array_int_get (src, j);
337 dyn_array_int_add (&merge_array, a);
340 dyn_array_int_add (&merge_array, a);
344 dyn_array_int_add (&merge_array, b);
347 } else if (i < dyn_array_int_size (dst)) {
348 dyn_array_int_add (&merge_array, dyn_array_int_get (dst, i));
351 dyn_array_int_add (&merge_array, dyn_array_int_get (src, j));
356 if (dyn_array_int_size (&merge_array) > dyn_array_int_size (dst)) {
357 dyn_array_int_set_all (dst, &merge_array);
362 dyn_array_int_merge_one (DynIntArray *array, int value)
366 int size = dyn_array_int_size (array);
368 for (i = 0; i < size; ++i) {
369 if (dyn_array_int_get (array, i) == value)
371 else if (dyn_array_int_get (array, i) > value)
375 dyn_array_int_ensure_capacity (array, size + 1);
378 tmp = dyn_array_int_get (array, i);
379 for (; i < size; ++i) {
380 dyn_array_int_set (array, i, value);
382 tmp = dyn_array_int_get (array, i + 1);
384 dyn_array_int_set (array, size, value);
386 dyn_array_int_set (array, size, value);
389 dyn_array_int_set_size (array, size + 1);
393 mono_gc_wait_for_bridge_processing (void)
395 if (!bridge_processing_in_progress)
398 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE waiting for bridge processing to finish");
405 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
407 if (callbacks->bridge_version != SGEN_BRIDGE_VERSION)
408 g_error ("Invalid bridge callback version. Expected %d but got %d\n", SGEN_BRIDGE_VERSION, callbacks->bridge_version);
410 bridge_callbacks = *callbacks;
414 sgen_is_bridge_object (MonoObject *obj)
416 if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) != SGEN_GC_BIT_BRIDGE_OBJECT)
418 return bridge_callbacks.is_bridge_object (obj);
421 MonoGCBridgeObjectKind
422 sgen_bridge_class_kind (MonoClass *class)
424 return bridge_callbacks.bridge_class_kind (class);
428 sgen_need_bridge_processing (void)
430 return bridge_callbacks.cross_references != NULL;
434 get_hash_entry (MonoObject *obj, gboolean *existing)
436 HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
447 memset (&new_entry, 0, sizeof (HashEntry));
450 dyn_array_ptr_init (&new_entry.srcs);
451 new_entry.finishing_time = -1;
452 new_entry.scc_index = -1;
454 sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
456 return sgen_hash_table_lookup (&hash_table, obj);
460 add_source (HashEntry *entry, HashEntry *src)
462 dyn_array_ptr_add (&entry->srcs, src);
473 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
474 int entry_size = dyn_array_ptr_size (&entry->srcs);
475 total_srcs += entry_size;
476 if (entry_size > max_srcs)
477 max_srcs = entry_size;
478 dyn_array_ptr_uninit (&entry->srcs);
479 } SGEN_HASH_TABLE_FOREACH_END;
481 sgen_hash_table_clean (&hash_table);
483 dyn_array_int_uninit (&merge_array);
484 //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
488 register_bridge_object (MonoObject *obj)
490 HashEntry *entry = get_hash_entry (obj, NULL);
491 entry->is_bridge = TRUE;
496 register_finishing_time (HashEntry *entry, int t)
498 g_assert (entry->finishing_time < 0);
499 entry->finishing_time = t;
503 object_is_live (MonoObject **objp)
505 MonoObject *obj = *objp;
506 MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
509 return sgen_hash_table_lookup (&hash_table, fwd) == NULL;
511 if (!sgen_object_is_live (obj))
513 return sgen_hash_table_lookup (&hash_table, obj) == NULL;
516 static DynPtrArray registered_bridges;
517 static DynPtrArray dfs_stack;
519 static int dsf1_passes, dsf2_passes;
523 #define HANDLE_PTR(ptr,obj) do { \
524 MonoObject *dst = (MonoObject*)*(ptr); \
525 if (dst && !object_is_live (&dst)) { \
526 dyn_array_ptr_push (&dfs_stack, obj_entry); \
527 dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst, NULL)); \
532 dfs1 (HashEntry *obj_entry)
535 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
537 dyn_array_ptr_push (&dfs_stack, NULL);
538 dyn_array_ptr_push (&dfs_stack, obj_entry);
545 obj_entry = dyn_array_ptr_pop (&dfs_stack);
547 src = dyn_array_ptr_pop (&dfs_stack);
549 obj = obj_entry->obj;
553 //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
554 add_source (obj_entry, src);
556 //g_print ("starting with %s\n", sgen_safe_name (obj));
559 if (obj_entry->is_visited)
562 obj_entry->is_visited = TRUE;
564 dyn_array_ptr_push (&dfs_stack, obj_entry);
565 /* NULL marks that the next entry is to be finished */
566 dyn_array_ptr_push (&dfs_stack, NULL);
568 #include "sgen-scan-object.h"
570 obj_entry = dyn_array_ptr_pop (&dfs_stack);
572 //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
573 register_finishing_time (obj_entry, current_time++);
575 } while (dyn_array_ptr_size (&dfs_stack) > 0);
579 scc_add_xref (SCC *src, SCC *dst)
581 g_assert (src != dst);
582 g_assert (src->index != dst->index);
584 if (dyn_array_int_contains (&dst->xrefs, src->index))
586 if (src->num_bridge_entries) {
587 dyn_array_int_merge_one (&dst->xrefs, src->index);
590 dyn_array_int_merge (&dst->xrefs, &src->xrefs);
591 for (i = 0; i < dyn_array_int_size (&dst->xrefs); ++i)
592 g_assert (dyn_array_int_get (&dst->xrefs, i) != dst->index);
597 scc_add_entry (SCC *scc, HashEntry *entry)
599 g_assert (entry->scc_index < 0);
600 entry->scc_index = scc->index;
601 if (entry->is_bridge)
602 ++scc->num_bridge_entries;
605 static DynSCCArray sccs;
606 static SCC *current_scc;
609 dfs2 (HashEntry *entry)
613 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
615 dyn_array_ptr_push (&dfs_stack, entry);
618 entry = dyn_array_ptr_pop (&dfs_stack);
621 if (entry->scc_index >= 0) {
622 if (entry->scc_index != current_scc->index)
623 scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->scc_index), current_scc);
627 scc_add_entry (current_scc, entry);
629 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
630 dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
631 } while (dyn_array_ptr_size (&dfs_stack) > 0);
635 compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
637 return e2->finishing_time - e1->finishing_time;
640 DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
642 static unsigned long step_1, step_2, step_3, step_4, step_5, step_6, step_7, step_8;
643 static int fist_pass_links, second_pass_links, sccs_links;
644 static int max_sccs_links = 0;
647 sgen_bridge_register_finalized_object (MonoObject *obj)
649 g_assert (sgen_need_bridge_processing ());
650 dyn_array_ptr_push (®istered_bridges, obj);
654 sgen_bridge_reset_data (void)
656 dyn_array_ptr_set_size (®istered_bridges, 0);
660 sgen_bridge_processing_stw_step (void)
664 SGEN_TV_DECLARE (atv);
665 SGEN_TV_DECLARE (btv);
667 if (!dyn_array_ptr_size (®istered_bridges))
671 * bridge_processing_in_progress must be set with the world
672 * stopped. If not there would be race conditions.
674 bridge_processing_in_progress = TRUE;
676 SGEN_TV_GETTIME (btv);
680 dyn_array_ptr_init (&dfs_stack);
681 dyn_array_int_init (&merge_array);
685 First we insert all bridges into the hash table and then we do dfs1.
687 It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
688 which means that we can have entry N pointing to entry N + 1.
690 If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
691 pass and not create the required xref between the two.
693 bridge_count = dyn_array_ptr_size (®istered_bridges);
694 for (i = 0; i < bridge_count ; ++i)
695 register_bridge_object (dyn_array_ptr_get (®istered_bridges, i));
697 for (i = 0; i < bridge_count; ++i)
698 dfs1 (get_hash_entry (dyn_array_ptr_get (®istered_bridges, i), NULL));
700 SGEN_TV_GETTIME (atv);
701 step_2 = SGEN_TV_ELAPSED (btv, atv);
705 is_bridge_object_alive (MonoObject *obj, void *data)
707 SgenHashTable *table = data;
708 unsigned char *value = sgen_hash_table_lookup (table, obj);
715 sgen_bridge_processing_finish (int generation)
718 int num_sccs, num_xrefs;
719 int max_entries, max_xrefs;
720 int hash_table_size, sccs_size;
723 int num_registered_bridges;
724 HashEntry **all_entries;
725 MonoGCBridgeSCC **api_sccs;
726 MonoGCBridgeXRef *api_xrefs;
727 SgenHashTable alive_hash = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE, INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY, 1, mono_aligned_addr_hash, NULL);
728 SGEN_TV_DECLARE (atv);
729 SGEN_TV_DECLARE (btv);
731 if (!dyn_array_ptr_size (®istered_bridges))
734 g_assert (bridge_processing_in_progress);
736 SGEN_TV_GETTIME (atv);
738 /* alloc and fill array of all entries */
740 all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
743 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
744 g_assert (entry->finishing_time >= 0);
745 all_entries [j++] = entry;
746 fist_pass_links += dyn_array_ptr_size (&entry->srcs);
747 } SGEN_HASH_TABLE_FOREACH_END;
748 g_assert (j == hash_table.num_entries);
749 hash_table_size = hash_table.num_entries;
751 /* sort array according to decreasing finishing time */
752 qsort_hash_entries (all_entries, hash_table.num_entries);
754 SGEN_TV_GETTIME (btv);
755 step_3 = SGEN_TV_ELAPSED (atv, btv);
757 /* second DFS pass */
759 dyn_array_scc_init (&sccs);
760 for (i = 0; i < hash_table.num_entries; ++i) {
761 HashEntry *entry = all_entries [i];
762 if (entry->scc_index < 0) {
763 int index = dyn_array_scc_size (&sccs);
764 current_scc = dyn_array_scc_add (&sccs);
765 current_scc->index = index;
766 current_scc->num_bridge_entries = 0;
767 current_scc->api_index = -1;
768 dyn_array_int_init (¤t_scc->xrefs);
774 sccs_size = dyn_array_scc_size (&sccs);
776 for (i = 0; i < hash_table.num_entries; ++i) {
777 HashEntry *entry = all_entries [i];
778 second_pass_links += dyn_array_ptr_size (&entry->srcs);
781 SGEN_TV_GETTIME (atv);
782 step_4 = SGEN_TV_ELAPSED (btv, atv);
784 //g_print ("%d sccs\n", sccs.size);
786 dyn_array_ptr_uninit (&dfs_stack);
788 /* init data for callback */
791 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
792 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
793 g_assert (scc->index == i);
794 if (scc->num_bridge_entries)
796 sccs_links += dyn_array_int_size (&scc->xrefs);
797 max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->xrefs));
800 api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
803 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
804 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
805 if (!scc->num_bridge_entries)
808 api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
809 api_sccs [j]->is_alive = FALSE;
810 api_sccs [j]->num_objs = scc->num_bridge_entries;
811 scc->num_bridge_entries = 0;
812 scc->api_index = j++;
814 num_xrefs += dyn_array_int_size (&scc->xrefs);
817 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
818 if (entry->is_bridge) {
819 SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->scc_index);
820 api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = entry->obj;
822 } SGEN_HASH_TABLE_FOREACH_END;
824 api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
826 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
828 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
829 if (!scc->num_bridge_entries)
831 for (k = 0; k < dyn_array_int_size (&scc->xrefs); ++k) {
832 SCC *src_scc = dyn_array_scc_get_ptr (&sccs, dyn_array_int_get (&scc->xrefs, k));
833 if (!src_scc->num_bridge_entries)
835 api_xrefs [j].src_scc_index = src_scc->api_index;
836 api_xrefs [j].dst_scc_index = scc->api_index;
841 SGEN_TV_GETTIME (btv);
842 step_5 = SGEN_TV_ELAPSED (atv, btv);
847 max_entries = max_xrefs = 0;
848 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
849 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
850 if (scc->num_bridge_entries)
852 if (scc->num_bridge_entries > max_entries)
853 max_entries = scc->num_bridge_entries;
854 if (dyn_array_int_size (&scc->xrefs) > max_xrefs)
855 max_xrefs = dyn_array_int_size (&scc->xrefs);
856 dyn_array_int_uninit (&scc->xrefs);
859 dyn_array_scc_uninit (&sccs);
861 sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
864 /* Empty the registered bridges array */
865 num_registered_bridges = dyn_array_ptr_size (®istered_bridges);
866 dyn_array_ptr_set_size (®istered_bridges, 0);
868 SGEN_TV_GETTIME (atv);
869 step_6 = SGEN_TV_ELAPSED (btv, atv);
871 //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
875 bridge_callbacks.cross_references (num_sccs, api_sccs, num_xrefs, api_xrefs);
877 /* Release for finalization those objects we no longer care. */
878 SGEN_TV_GETTIME (btv);
879 step_7 = SGEN_TV_ELAPSED (atv, btv);
881 for (i = 0; i < num_sccs; ++i) {
882 unsigned char alive = api_sccs [i]->is_alive ? 1 : 0;
883 for (j = 0; j < api_sccs [i]->num_objs; ++j) {
884 /* Build hash table for nulling weak links. */
885 sgen_hash_table_replace (&alive_hash, api_sccs [i]->objs [j], &alive, NULL);
887 /* Release for finalization those objects we no longer care. */
888 if (!api_sccs [i]->is_alive)
889 sgen_mark_bridge_object (api_sccs [i]->objs [j]);
893 /* Null weak links to dead objects. */
894 sgen_null_links_with_predicate (GENERATION_NURSERY, is_bridge_object_alive, &alive_hash);
895 if (generation == GENERATION_OLD)
896 sgen_null_links_with_predicate (GENERATION_OLD, is_bridge_object_alive, &alive_hash);
898 sgen_hash_table_clean (&alive_hash);
900 /* free callback data */
902 for (i = 0; i < num_sccs; ++i) {
903 sgen_free_internal_dynamic (api_sccs [i],
904 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
905 INTERNAL_MEM_BRIDGE_DATA);
907 sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
909 sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
911 SGEN_TV_GETTIME (atv);
912 step_8 = SGEN_TV_ELAPSED (btv, atv);
914 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE num-objects %d num_hash_entries %d sccs size %d init %.2fms df1 %.2fms sort %.2fms dfs2 %.2fms setup-cb %.2fms free-data %.2fms user-cb %.2fms clenanup %.2fms links %d/%d/%d/%d dfs passes %d/%d",
915 num_registered_bridges, hash_table_size, dyn_array_scc_size (&sccs),
924 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
925 dsf1_passes, dsf2_passes);
927 step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
929 bridge_processing_in_progress = FALSE;
933 sgen_bridge_describe_pointer (MonoObject *obj)
938 for (i = 0; i < dyn_array_ptr_size (®istered_bridges); ++i) {
939 if (obj == dyn_array_ptr_get (®istered_bridges, i)) {
940 printf ("Pointer is a registered bridge object.\n");
945 entry = sgen_hash_table_lookup (&hash_table, obj);
949 printf ("Bridge hash table entry %p:\n", entry);
950 printf (" is bridge: %d\n", (int)entry->is_bridge);
951 printf (" is visited: %d\n", (int)entry->is_visited);
954 static const char *bridge_class;
956 static MonoGCBridgeObjectKind
957 bridge_test_bridge_class_kind (MonoClass *class)
959 if (!strcmp (bridge_class, class->name))
960 return GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS;
961 return GC_BRIDGE_TRANSPARENT_CLASS;
965 bridge_test_is_bridge_object (MonoObject *object)
971 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
974 for (i = 0; i < num_sccs; ++i) {
976 // g_print ("--- SCC %d\n", i);
977 for (j = 0; j < sccs [i]->num_objs; ++j) {
978 // g_print (" %s\n", sgen_safe_name (sccs [i]->objs [j]));
979 if (i & 1) /*retain half of the bridged objects */
980 sccs [i]->is_alive = TRUE;
983 for (i = 0; i < num_xrefs; ++i) {
984 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
985 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
986 // g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
990 static MonoClassField *mono_bridge_test_field;
1000 test_scc (MonoGCBridgeSCC *scc, int i)
1002 int status = BRIDGE_DEAD;
1003 mono_field_get_value (scc->objs [i], mono_bridge_test_field, &status);
1008 mark_scc (MonoGCBridgeSCC *scc, int value)
1011 for (i = 0; i < scc->num_objs; ++i) {
1012 if (!test_scc (scc, i)) {
1014 mono_field_set_value (scc->objs [i], mono_bridge_test_field, &status);
1020 bridge_test_cross_reference2 (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
1025 if (!mono_bridge_test_field) {
1026 mono_bridge_test_field = mono_class_get_field_from_name (mono_object_get_class (sccs[0]->objs [0]), "__test");
1027 g_assert (mono_bridge_test_field);
1030 /*We mark all objects in a scc with live objects as reachable by scc*/
1031 for (i = 0; i < num_sccs; ++i) {
1033 gboolean live = FALSE;
1034 for (j = 0; j < sccs [i]->num_objs; ++j) {
1035 if (test_scc (sccs [i], j)) {
1042 for (j = 0; j < sccs [i]->num_objs; ++j) {
1043 if (!test_scc (sccs [i], j)) {
1044 int status = BRIDGE_SAME_SCC;
1045 mono_field_set_value (sccs [i]->objs [j], mono_bridge_test_field, &status);
1050 /*Now we mark the transitive closure of reachable objects from the xrefs*/
1054 /* Mark all objects that are brought to life due to xrefs*/
1055 for (i = 0; i < num_xrefs; ++i) {
1056 MonoGCBridgeXRef ref = xrefs [i];
1057 if (test_scc (sccs [ref.src_scc_index], 0) && !test_scc (sccs [ref.dst_scc_index], 0)) {
1059 mark_scc (sccs [ref.dst_scc_index], BRIDGE_XREF);
1064 /* keep everything in memory, all we want to do is test persistence */
1065 for (i = 0; i < num_sccs; ++i)
1066 sccs [i]->is_alive = TRUE;
1070 sgen_register_test_bridge_callbacks (const char *bridge_class_name)
1072 MonoGCBridgeCallbacks callbacks;
1073 callbacks.bridge_version = SGEN_BRIDGE_VERSION;
1074 callbacks.bridge_class_kind = bridge_test_bridge_class_kind;
1075 callbacks.is_bridge_object = bridge_test_is_bridge_object;
1076 callbacks.cross_references = bridge_class_name[0] == '2' ? bridge_test_cross_reference2 : bridge_test_cross_reference;
1077 mono_gc_register_bridge_callbacks (&callbacks);
1078 bridge_class = bridge_class_name + (bridge_class_name[0] == '2' ? 1 : 0);