Merge pull request #231 from linquize/a853199c497bb0977970974303fac7e42080809d
[mono.git] / mono / metadata / sgen-bridge.c
1 /*
2  * sgen-bridge.c: Simple generational GC.
3  *
4  * Copyright 2011 Novell, Inc (http://www.novell.com)
5  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
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.
15  *
16  *
17  * Copyright 2001-2003 Ximian, Inc
18  * Copyright 2003-2010 Novell, Inc.
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #include "config.h"
41
42 #ifdef HAVE_SGEN_GC
43
44 #include <stdlib.h>
45
46 #include "sgen-gc.h"
47 #include "sgen-bridge.h"
48 #include "utils/mono-logger-internal.h"
49 #include "utils/mono-time.h"
50
51
52 typedef struct {
53         int size;
54         int elem_size;
55         int capacity;
56         char *data;
57 } DynArray;
58
59 #define DYN_ARRAY_REF(da,i)     ((void*)((da)->data + (i) * (da)->elem_size))
60 #define DYN_ARRAY_PTR_REF(da,i) (((void**)(da)->data) [(i)])
61 #define DYN_ARRAY_INT_REF(da,i) (((int*)(da)->data) [(i)])
62 #define DYN_ARRAY_PTR_STATIC_INITIALIZER { 0, sizeof (void*), 0, NULL }
63 #define DYN_ARRAY_INT_STATIC_INITIALIZER { 0, sizeof (int), 0, NULL }
64
65 static void
66 dyn_array_init (DynArray *da, int elem_size)
67 {
68         da->size = 0;
69         da->elem_size = elem_size;
70         da->capacity = 0;
71         da->data = NULL;
72 }
73
74 static void
75 dyn_array_ptr_init (DynArray *da)
76 {
77         dyn_array_init (da, sizeof (void*));
78 }
79
80 static void
81 dyn_array_int_init (DynArray *da)
82 {
83         dyn_array_init (da, sizeof (int));
84 }
85
86 static void
87 dyn_array_uninit (DynArray *da)
88 {
89         if (da->capacity <= 0)
90                 return;
91
92         mono_sgen_free_internal_dynamic (da->data, da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
93         da->data = NULL;
94 }
95
96 static void
97 dyn_array_ensure_capacity (DynArray *da, int capacity)
98 {
99         int old_capacity = da->capacity;
100         char *new_data;
101
102         if (capacity <= old_capacity)
103                 return;
104
105         if (da->capacity == 0)
106                 da->capacity = 2;
107         while (capacity > da->capacity)
108                 da->capacity *= 2;
109
110         new_data = mono_sgen_alloc_internal_dynamic (da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
111         memcpy (new_data, da->data, da->elem_size * da->size);
112         mono_sgen_free_internal_dynamic (da->data, da->elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
113         da->data = new_data;
114 }
115
116 static void*
117 dyn_array_add (DynArray *da)
118 {
119         void *p;
120
121         dyn_array_ensure_capacity (da, da->size + 1);
122
123         p = DYN_ARRAY_REF (da, da->size);
124         ++da->size;
125         return p;
126 }
127
128 static void
129 dyn_array_ptr_add (DynArray *da, void *ptr)
130 {
131         void **p = dyn_array_add (da);
132         *p = ptr;
133 }
134
135 #define dyn_array_ptr_push dyn_array_ptr_add
136
137 static void*
138 dyn_array_ptr_pop (DynArray *da)
139 {
140         void *p;
141         g_assert (da->size > 0);
142         p = DYN_ARRAY_PTR_REF (da, da->size - 1);
143         --da->size;
144         return p;
145 }
146
147 static void
148 dyn_array_int_add (DynArray *da, int x)
149 {
150         int *p = dyn_array_add (da);
151         *p = x;
152 }
153
154 /*
155 static gboolean
156 dyn_array_ptr_contains (DynArray *da, void *ptr)
157 {
158         int i;
159         for (i = 0; i < da->size; ++i)
160                 if (DYN_ARRAY_PTR_REF (da, i) == ptr)
161                         return TRUE;
162         return FALSE;
163 }
164 */
165
166 static gboolean
167 dyn_array_int_contains (DynArray *da, int x)
168 {
169         int i;
170         for (i = 0; i < da->size; ++i)
171                 if (DYN_ARRAY_INT_REF (da, i) == x)
172                         return TRUE;
173         return FALSE;
174 }
175
176 static DynArray merge_array;
177
178 static void
179 dyn_array_int_merge (DynArray *dst, DynArray *src)
180 {
181         int i, j;
182
183         dyn_array_ensure_capacity (&merge_array, dst->size + src->size);
184         merge_array.size = 0;
185
186         for (i = j = 0; i < dst->size || j < src->size; ) {
187                 if (i < dst->size && j < src->size) {
188                         int a = DYN_ARRAY_INT_REF (dst, i); 
189                         int b = DYN_ARRAY_INT_REF (src, j); 
190                         if (a < b) {
191                                 dyn_array_int_add (&merge_array, a);
192                                 ++i;
193                         } else if (a == b) {
194                                 dyn_array_int_add (&merge_array, a);
195                                 ++i;
196                                 ++j;    
197                         } else {
198                                 dyn_array_int_add (&merge_array, b);
199                                 ++j;
200                         }
201                 } else if (i < dst->size) {
202                         dyn_array_int_add (&merge_array, DYN_ARRAY_INT_REF (dst, i));
203                         ++i;
204                 } else {
205                         dyn_array_int_add (&merge_array, DYN_ARRAY_INT_REF (src, j));
206                         ++j;
207                 }
208         }
209
210         if (merge_array.size > dst->size) {
211                 dyn_array_ensure_capacity (dst, merge_array.size);
212                 memcpy (DYN_ARRAY_REF (dst, 0), DYN_ARRAY_REF (&merge_array, 0), merge_array.size * merge_array.elem_size);
213                 dst->size = merge_array.size;
214         }
215 }
216
217 static void
218 dyn_array_int_merge_one (DynArray *array, int value)
219 {
220         int i;
221         int tmp;
222         int end = array->size;
223
224         for (i = 0; i < end; ++i) {
225                 if (DYN_ARRAY_INT_REF (array, i) == value)
226                         return;
227                 else if (DYN_ARRAY_INT_REF (array, i) > value)
228                         break;
229         }
230
231         dyn_array_ensure_capacity (array, array->size + 1);
232
233         if (i < end) {
234                 tmp = DYN_ARRAY_INT_REF (array, i);
235                 for (; i <= end; ++i) {
236                         DYN_ARRAY_INT_REF (array, i) = value;
237                         value = tmp;
238                         tmp = DYN_ARRAY_INT_REF (array, i + 1);
239                 }
240                 DYN_ARRAY_INT_REF (array, end + 1) = tmp;
241         } else {
242                 DYN_ARRAY_INT_REF (array, end) = value;
243         }
244         ++array->size;
245 }
246
247 /*
248  * FIXME: Optimizations:
249  *
250  * Don't allocate a scrs array for just one source.  Most objects have
251  * just one source, so use the srcs pointer itself.
252  */
253 typedef struct _HashEntry {
254         MonoObject *obj;        /* This is a duplicate - it's already stored in the hash table */
255
256         gboolean is_bridge;
257         gboolean is_visited;
258
259         int finishing_time;
260
261         DynArray srcs;
262
263         int scc_index;
264 } HashEntry;
265
266 typedef struct _SCC {
267         int index;
268         int api_index;
269         int num_bridge_entries;
270         DynArray xrefs;         /* these are incoming, not outgoing */
271 } SCC;
272
273 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_DATA, INTERNAL_MEM_BRIDGE_DATA, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
274
275 static MonoGCBridgeCallbacks bridge_callbacks;
276
277 static int current_time;
278
279 void
280 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
281 {
282         if (callbacks->bridge_version != MONO_SGEN_BRIDGE_VERSION) {
283                 fprintf (stderr, "Invalid bridge callback version. Expected %d but got %d\n", MONO_SGEN_BRIDGE_VERSION, callbacks->bridge_version);
284                 exit (1);
285         }
286         bridge_callbacks = *callbacks;
287 }
288
289 gboolean
290 mono_sgen_is_bridge_object (MonoObject *obj)
291 {
292         return (obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) == SGEN_GC_BIT_BRIDGE_OBJECT;
293 }
294
295 gboolean
296 mono_sgen_is_bridge_class (MonoClass *class)
297 {
298         return bridge_callbacks.is_bridge_class (class);
299 }
300
301 gboolean
302 mono_sgen_need_bridge_processing (void)
303 {
304         return bridge_callbacks.cross_references != NULL;
305 }
306
307 static HashEntry*
308 get_hash_entry (MonoObject *obj, gboolean *existing)
309 {
310         HashEntry *entry = mono_sgen_hash_table_lookup (&hash_table, obj);
311         HashEntry new_entry;
312
313         if (entry) {
314                 if (existing)
315                         *existing = TRUE;
316                 return entry;
317         }
318         if (existing)
319                 *existing = FALSE;
320
321         memset (&new_entry, 0, sizeof (HashEntry));
322
323         new_entry.obj = obj;
324         dyn_array_ptr_init (&new_entry.srcs);
325         new_entry.finishing_time = -1;
326         new_entry.scc_index = -1;
327
328         mono_sgen_hash_table_replace (&hash_table, obj, &new_entry);
329
330         return mono_sgen_hash_table_lookup (&hash_table, obj);
331 }
332
333 static void
334 add_source (HashEntry *entry, HashEntry *src)
335 {
336         dyn_array_ptr_add (&entry->srcs, src);
337 }
338
339 static void
340 free_data (void)
341 {
342         MonoObject *obj;
343         HashEntry *entry;
344         int total_srcs = 0;
345         int max_srcs = 0;
346
347         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
348                 total_srcs += entry->srcs.size;
349                 if (entry->srcs.size > max_srcs)
350                         max_srcs = entry->srcs.size;
351                 dyn_array_uninit (&entry->srcs);
352         } SGEN_HASH_TABLE_FOREACH_END;
353
354         mono_sgen_hash_table_clean (&hash_table);
355
356         dyn_array_uninit (&merge_array);
357         //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
358 }
359
360 static HashEntry*
361 register_bridge_object (MonoObject *obj)
362 {
363         HashEntry *entry = get_hash_entry (obj, NULL);
364         entry->is_bridge = TRUE;
365         return entry;
366 }
367
368 static void
369 register_finishing_time (HashEntry *entry, int t)
370 {
371         g_assert (entry->finishing_time < 0);
372         entry->finishing_time = t;
373 }
374
375 static gboolean
376 object_is_live (MonoObject **objp)
377 {
378         MonoObject *obj = *objp;
379         MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
380         if (fwd) {
381                 *objp = fwd;
382                 return mono_sgen_hash_table_lookup (&hash_table, fwd) == NULL;
383         }
384         if (!mono_sgen_object_is_live (obj))
385                 return FALSE;
386         return mono_sgen_hash_table_lookup (&hash_table, obj) == NULL;
387 }
388
389 static DynArray registered_bridges = DYN_ARRAY_PTR_STATIC_INITIALIZER;
390 static DynArray dfs_stack;
391
392 static int dsf1_passes, dsf2_passes;
393
394
395 #undef HANDLE_PTR
396 #define HANDLE_PTR(ptr,obj)     do {                                    \
397                 MonoObject *dst = (MonoObject*)*(ptr);                  \
398                 if (dst && !object_is_live (&dst)) {                    \
399                         dyn_array_ptr_push (&dfs_stack, obj_entry);     \
400                         dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst, NULL)); \
401                 }                                                       \
402         } while (0)
403
404 static void
405 dfs1 (HashEntry *obj_entry, HashEntry *src)
406 {
407         g_assert (dfs_stack.size == 0);
408
409         dyn_array_ptr_push (&dfs_stack, src);
410         dyn_array_ptr_push (&dfs_stack, obj_entry);
411
412         do {
413                 MonoObject *obj;
414                 char *start;
415                 ++dsf1_passes;
416
417                 obj_entry = dyn_array_ptr_pop (&dfs_stack);
418                 if (obj_entry) {
419                         src = dyn_array_ptr_pop (&dfs_stack);
420
421                         obj = obj_entry->obj;
422                         start = (char*)obj;
423
424                         if (src) {
425                                 //g_print ("link %s -> %s\n", mono_sgen_safe_name (src->obj), mono_sgen_safe_name (obj));
426                                 add_source (obj_entry, src);
427                         } else {
428                                 //g_print ("starting with %s\n", mono_sgen_safe_name (obj));
429                         }
430
431                         if (obj_entry->is_visited)
432                                 continue;
433
434                         obj_entry->is_visited = TRUE;
435
436                         dyn_array_ptr_push (&dfs_stack, obj_entry);
437                         /* NULL marks that the next entry is to be finished */
438                         dyn_array_ptr_push (&dfs_stack, NULL);
439
440 #include "sgen-scan-object.h"
441                 } else {
442                         obj_entry = dyn_array_ptr_pop (&dfs_stack);
443
444                         //g_print ("finish %s\n", mono_sgen_safe_name (obj_entry->obj));
445                         register_finishing_time (obj_entry, current_time++);
446                 }
447         } while (dfs_stack.size > 0);
448 }
449
450 static void
451 scc_add_xref (SCC *src, SCC *dst)
452 {
453         g_assert (src != dst);
454         g_assert (src->index != dst->index);
455
456         if (dyn_array_int_contains (&dst->xrefs, src->index))
457                 return;
458         if (src->num_bridge_entries) {
459                 dyn_array_int_merge_one (&dst->xrefs, src->index);
460         } else {
461                 int i;
462                 dyn_array_int_merge (&dst->xrefs, &src->xrefs);
463                 for (i = 0; i < dst->xrefs.size; ++i)
464                         g_assert (DYN_ARRAY_INT_REF (&dst->xrefs, i) != dst->index);
465         }
466 }
467
468 static void
469 scc_add_entry (SCC *scc, HashEntry *entry)
470 {
471         g_assert (entry->scc_index < 0);
472         entry->scc_index = scc->index;
473         if (entry->is_bridge)
474                 ++scc->num_bridge_entries;
475 }
476
477 static DynArray sccs;
478 static SCC *current_scc;
479
480 static void
481 dfs2 (HashEntry *entry)
482 {
483         int i;
484
485         g_assert (dfs_stack.size == 0);
486
487         dyn_array_ptr_push (&dfs_stack, entry);
488
489         do {
490                 entry = dyn_array_ptr_pop (&dfs_stack);
491                 ++dsf2_passes;
492
493                 if (entry->scc_index >= 0) {
494                         if (entry->scc_index != current_scc->index)
495                                 scc_add_xref (DYN_ARRAY_REF (&sccs, entry->scc_index), current_scc);
496                         continue;
497                 }
498
499                 scc_add_entry (current_scc, entry);
500
501                 for (i = 0; i < entry->srcs.size; ++i)
502                         dyn_array_ptr_push (&dfs_stack, DYN_ARRAY_PTR_REF (&entry->srcs, i));
503         } while (dfs_stack.size > 0);
504 }
505
506 static int
507 compare_hash_entries (const void *ep1, const void *ep2)
508 {
509         HashEntry *e1 = *(HashEntry**)ep1;
510         HashEntry *e2 = *(HashEntry**)ep2;
511         return e2->finishing_time - e1->finishing_time;
512 }
513
514 static unsigned long step_1, step_2, step_3, step_4, step_5, step_6, step_7, step_8;
515 static int fist_pass_links, second_pass_links, sccs_links;
516 static int max_sccs_links = 0;
517
518 void
519 mono_sgen_bridge_register_finalized_object (MonoObject *obj)
520 {
521         g_assert (mono_sgen_need_bridge_processing ());
522         dyn_array_ptr_push (&registered_bridges, obj);
523 }
524
525 void
526 mono_sgen_bridge_reset_data (void)
527 {
528         registered_bridges.size = 0;
529 }
530
531 void
532 mono_sgen_bridge_processing_stw_step (void)
533 {
534         int i;
535         SGEN_TV_DECLARE (atv);
536         SGEN_TV_DECLARE (btv);
537
538         if (!registered_bridges.size)
539                 return;
540
541         SGEN_TV_GETTIME (btv);
542
543         /* first DFS pass */
544
545         dyn_array_ptr_init (&dfs_stack);
546         dyn_array_int_init (&merge_array);
547
548         current_time = 0;
549         for (i = 0; i < registered_bridges.size; ++i)
550                 dfs1 (register_bridge_object (DYN_ARRAY_PTR_REF (&registered_bridges, i)), NULL);
551
552         SGEN_TV_GETTIME (atv);
553         step_2 = SGEN_TV_ELAPSED (btv, atv);
554 }
555
556 void
557 mono_sgen_bridge_processing_finish (void)
558 {
559         int i, j;
560         int num_sccs, num_xrefs;
561         int max_entries, max_xrefs;
562         int hash_table_size, sccs_size;
563         MonoObject *obj;
564         HashEntry *entry;
565         int num_registered_bridges;
566         HashEntry **all_entries;
567         MonoGCBridgeSCC **api_sccs;
568         MonoGCBridgeXRef *api_xrefs;
569         SGEN_TV_DECLARE (atv);
570         SGEN_TV_DECLARE (btv);
571
572         if (!registered_bridges.size)
573                 return;
574
575         SGEN_TV_GETTIME (atv);
576
577         /* alloc and fill array of all entries */
578
579         all_entries = mono_sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
580
581         j = 0;
582         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
583                 g_assert (entry->finishing_time >= 0);
584                 all_entries [j++] = entry;
585                 fist_pass_links += entry->srcs.size;
586         } SGEN_HASH_TABLE_FOREACH_END;
587         g_assert (j == hash_table.num_entries);
588         hash_table_size = hash_table.num_entries;
589
590         /* sort array according to decreasing finishing time */
591
592         qsort (all_entries, hash_table.num_entries, sizeof (HashEntry*), compare_hash_entries);
593
594         SGEN_TV_GETTIME (btv);
595         step_3 = SGEN_TV_ELAPSED (atv, btv);
596
597         /* second DFS pass */
598
599         dyn_array_init (&sccs, sizeof (SCC));
600         for (i = 0; i < hash_table.num_entries; ++i) {
601                 HashEntry *entry = all_entries [i];
602                 if (entry->scc_index < 0) {
603                         int index = sccs.size;
604                         current_scc = dyn_array_add (&sccs);
605                         current_scc->index = index;
606                         current_scc->num_bridge_entries = 0;
607                         current_scc->api_index = -1;
608                         dyn_array_int_init (&current_scc->xrefs);
609
610                         dfs2 (entry);
611                 }
612         }
613
614         sccs_size = sccs.size;
615
616         for (i = 0; i < hash_table.num_entries; ++i) {
617                 HashEntry *entry = all_entries [i];
618                 second_pass_links += entry->srcs.size;
619         }
620
621         SGEN_TV_GETTIME (atv);
622         step_4 = SGEN_TV_ELAPSED (btv, atv);
623
624         //g_print ("%d sccs\n", sccs.size);
625
626         dyn_array_uninit (&dfs_stack);
627
628         /* init data for callback */
629
630         num_sccs = 0;
631         for (i = 0; i < sccs.size; ++i) {
632                 SCC *scc = DYN_ARRAY_REF (&sccs, i);
633                 g_assert (scc->index == i);
634                 if (scc->num_bridge_entries)
635                         ++num_sccs;
636                 sccs_links += scc->xrefs.size;
637                 max_sccs_links = MAX (max_sccs_links, scc->xrefs.size);
638         }
639
640         api_sccs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
641         num_xrefs = 0;
642         j = 0;
643         for (i = 0; i < sccs.size; ++i) {
644                 SCC *scc = DYN_ARRAY_REF (&sccs, i);
645                 if (!scc->num_bridge_entries)
646                         continue;
647
648                 api_sccs [j] = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA);
649                 api_sccs [j]->num_objs = scc->num_bridge_entries;
650                 scc->num_bridge_entries = 0;
651                 scc->api_index = j++;
652
653                 num_xrefs += scc->xrefs.size;
654         }
655
656         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
657                 if (entry->is_bridge) {
658                         SCC *scc = DYN_ARRAY_REF (&sccs, entry->scc_index);
659                         api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = entry->obj;
660                 }
661         } SGEN_HASH_TABLE_FOREACH_END;
662
663         api_xrefs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
664         j = 0;
665         for (i = 0; i < sccs.size; ++i) {
666                 int k;
667                 SCC *scc = DYN_ARRAY_REF (&sccs, i);
668                 if (!scc->num_bridge_entries)
669                         continue;
670                 for (k = 0; k < scc->xrefs.size; ++k) {
671                         SCC *src_scc = DYN_ARRAY_REF (&sccs, DYN_ARRAY_INT_REF (&scc->xrefs, k));
672                         if (!src_scc->num_bridge_entries)
673                                 continue;
674                         api_xrefs [j].src_scc_index = src_scc->api_index;
675                         api_xrefs [j].dst_scc_index = scc->api_index;
676                         ++j;
677                 }
678         }
679
680         SGEN_TV_GETTIME (btv);
681         step_5 = SGEN_TV_ELAPSED (atv, btv);
682
683         /* free data */
684
685         j = 0;
686         max_entries = max_xrefs = 0;
687         for (i = 0; i < sccs.size; ++i) {
688                 SCC *scc = DYN_ARRAY_REF (&sccs, i);
689                 if (scc->num_bridge_entries)
690                         ++j;
691                 if (scc->num_bridge_entries > max_entries)
692                         max_entries = scc->num_bridge_entries;
693                 if (scc->xrefs.size > max_xrefs)
694                         max_xrefs = scc->xrefs.size;
695                 dyn_array_uninit (&scc->xrefs);
696
697         }
698         dyn_array_uninit (&sccs);
699
700         mono_sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
701
702         free_data ();
703         /* Empty the registered bridges array */
704         num_registered_bridges = registered_bridges.size;
705         registered_bridges.size = 0;
706
707         SGEN_TV_GETTIME (atv);
708         step_6 = SGEN_TV_ELAPSED (btv, atv);
709
710         //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
711
712         /* callback */
713
714         bridge_callbacks.cross_references (num_sccs, api_sccs, num_xrefs, api_xrefs);
715
716 /*Release for finalization those objects we no longer care. */
717         SGEN_TV_GETTIME (btv);
718         step_7 = SGEN_TV_ELAPSED (atv, btv);
719
720         for (i = 0; i < num_sccs; ++i) {
721                 if (!api_sccs [i]->objs [0])
722                         continue;
723                 for (j = 0; j < api_sccs [i]->num_objs; ++j)
724                         mono_sgen_mark_bridge_object (api_sccs [i]->objs [j]);
725         }
726
727         /* free callback data */
728
729         for (i = 0; i < num_sccs; ++i) {
730                 mono_sgen_free_internal_dynamic (api_sccs [i],
731                                 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
732                                 INTERNAL_MEM_BRIDGE_DATA);
733         }
734         mono_sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
735
736         mono_sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
737
738         SGEN_TV_GETTIME (atv);
739         step_8 = SGEN_TV_ELAPSED (btv, atv);
740
741         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",
742                 num_registered_bridges, hash_table_size, sccs.size,
743                 step_1 / 1000.0f,
744                 step_2 / 1000.0f,
745                 step_3 / 1000.0f,
746                 step_4 / 1000.0f,
747                 step_5 / 1000.0f,
748                 step_6 / 1000.0f,
749                 step_7 / 1000.0f,
750                 step_8 / 1000.f,
751                 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
752                 dsf1_passes, dsf2_passes);
753
754         step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
755 }
756
757 static const char *bridge_class;
758
759 static gboolean
760 bridge_test_is_bridge_class (MonoClass *class)
761 {
762         return !strcmp (bridge_class, class->name);
763 }
764
765 static void
766 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
767 {
768         int i;
769         for (i = 0; i < num_sccs; ++i) {
770                 int j;
771         //      g_print ("--- SCC %d\n", i);
772                 for (j = 0; j < sccs [i]->num_objs; ++j) {
773         //              g_print ("  %s\n", mono_sgen_safe_name (sccs [i]->objs [j]));
774                         if (i & 1) /*retain half of the bridged objects */
775                                 sccs [i]->objs [0] = NULL;
776                 }
777         }
778         for (i = 0; i < num_xrefs; ++i) {
779                 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
780                 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
781         //      g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
782         }
783 }
784
785
786 void
787 mono_sgen_register_test_bridge_callbacks (const char *bridge_class_name)
788 {
789         MonoGCBridgeCallbacks callbacks;
790         callbacks.bridge_version = MONO_SGEN_BRIDGE_VERSION;
791         callbacks.is_bridge_class = bridge_test_is_bridge_class;
792         callbacks.cross_references = bridge_test_cross_reference;
793         mono_gc_register_bridge_callbacks (&callbacks);
794         bridge_class = bridge_class_name;
795 }
796
797 #endif