2 * sgen-bridge.c: Simple generational GC.
4 * Copyright 2011 Novell, Inc (http://www.novell.com)
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
16 * Copyright 2001-2003 Ximian, Inc
17 * Copyright 2003-2010 Novell, Inc.
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 #include "sgen-bridge.h"
55 #define DYN_ARRAY_REF(da,i) ((void*)((da)->data + (i) * (da)->elem_size))
56 #define DYN_ARRAY_PTR_REF(da,i) (((void**)(da)->data) [(i)])
57 #define DYN_ARRAY_INT_REF(da,i) (((int*)(da)->data) [(i)])
60 dyn_array_init (DynArray *da, int elem_size)
63 da->elem_size = elem_size;
69 dyn_array_ptr_init (DynArray *da)
71 dyn_array_init (da, sizeof (void*));
75 dyn_array_int_init (DynArray *da)
77 dyn_array_init (da, sizeof (int));
81 dyn_array_uninit (DynArray *da)
83 if (da->capacity <= 0)
86 mono_sgen_free_internal_dynamic (da->data, da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
91 dyn_array_ensure_capacity (DynArray *da, int capacity)
95 if (capacity <= da->capacity)
98 if (da->capacity == 0)
100 while (capacity > da->capacity)
103 new_data = mono_sgen_alloc_internal_dynamic (da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
104 memcpy (new_data, da->data, da->elem_size * da->size);
105 mono_sgen_free_internal_dynamic (da->data, da->elem_size * da->size, INTERNAL_MEM_BRIDGE_DATA);
110 dyn_array_add (DynArray *da)
114 dyn_array_ensure_capacity (da, da->size + 1);
116 p = DYN_ARRAY_REF (da, da->size);
122 dyn_array_ptr_add (DynArray *da, void *ptr)
124 void **p = dyn_array_add (da);
128 #define dyn_array_ptr_push dyn_array_ptr_add
131 dyn_array_ptr_pop (DynArray *da)
134 g_assert (da->size > 0);
135 p = DYN_ARRAY_PTR_REF (da, da->size - 1);
141 dyn_array_int_add (DynArray *da, int x)
143 int *p = dyn_array_add (da);
149 dyn_array_ptr_contains (DynArray *da, void *ptr)
152 for (i = 0; i < da->size; ++i)
153 if (DYN_ARRAY_PTR_REF (da, i) == ptr)
160 dyn_array_int_contains (DynArray *da, int x)
163 for (i = 0; i < da->size; ++i)
164 if (DYN_ARRAY_INT_REF (da, i) == x)
170 dyn_array_append (DynArray *dst, DynArray *src)
172 g_assert (dst->elem_size == src->elem_size);
174 dyn_array_ensure_capacity (dst, dst->size + src->size);
175 memcpy (DYN_ARRAY_REF (dst, dst->size), DYN_ARRAY_REF (src, 0), src->size * src->elem_size);
176 dst->size += src->size;
180 * FIXME: Optimizations:
182 * Don't allocate a scrs array for just one source. Most objects have
183 * just one source, so use the srcs pointer itself.
185 typedef struct _HashEntry {
196 struct _HashEntry *next;
199 typedef struct _SCC {
202 int num_bridge_entries;
203 DynArray xrefs; /* these are incoming, not outgoing */
206 static int num_hash_entries = 0;
207 static int hash_size = 0;
208 static HashEntry **hash_table = NULL;
210 static MonoGCBridgeCallbacks bridge_callbacks;
212 static int current_time;
215 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
217 bridge_callbacks = *callbacks;
221 mono_sgen_need_bridge_processing (void)
223 return bridge_callbacks.cross_references != NULL;
227 alloc_hash_table (int size)
230 table = mono_sgen_alloc_internal_dynamic (sizeof (HashEntry*) * size, INTERNAL_MEM_BRIDGE_DATA);
231 memset (table, 0, sizeof (HashEntry*) * size);
238 HashEntry **new_table;
239 int new_size = hash_size << 1;
242 new_table = alloc_hash_table (new_size);
243 for (i = 0; i < hash_size; ++i) {
244 HashEntry *entry = hash_table [i];
245 while (entry != NULL) {
246 HashEntry *next = entry->next;
247 int hash = ((mword)entry->obj >> 4) & (new_size - 1);
248 entry->next = new_table [hash];
249 new_table [hash] = entry;
254 mono_sgen_free_internal_dynamic (hash_table, sizeof (HashEntry*) * hash_size, INTERNAL_MEM_BRIDGE_DATA);
256 hash_table = new_table;
257 hash_size = new_size;
261 lookup_hash_entry (MonoObject *obj)
263 int hash = (mword)obj >> 4;
267 g_assert (hash_size == 0 && num_hash_entries == 0);
269 hash_table = alloc_hash_table (hash_size);
272 hash &= hash_size - 1;
273 for (entry = hash_table [hash]; entry != NULL; entry = entry->next) {
274 if (entry->obj == obj)
282 get_hash_entry (MonoObject *obj)
284 HashEntry *entry = lookup_hash_entry (obj);
290 entry = mono_sgen_alloc_internal_dynamic (sizeof (HashEntry), INTERNAL_MEM_BRIDGE_DATA);
291 memset (entry, 0, sizeof (HashEntry));
294 dyn_array_ptr_init (&entry->srcs);
295 entry->finishing_time = -1;
296 entry->scc_index = -1;
298 hash = ((mword)obj >> 4) & (hash_size - 1);
299 entry->next = hash_table [hash];
300 hash_table [hash] = entry;
304 if (num_hash_entries > hash_size >> 1)
311 add_source (HashEntry *entry, HashEntry *src)
313 dyn_array_ptr_add (&entry->srcs, src);
323 if (hash_table == NULL)
326 for (i = 0; i < hash_size; ++i) {
327 HashEntry *entry = hash_table [i];
328 while (entry != NULL) {
329 HashEntry *next = entry->next;
330 total_srcs += entry->srcs.size;
331 if (entry->srcs.size > max_srcs)
332 max_srcs = entry->srcs.size;
333 dyn_array_uninit (&entry->srcs);
334 mono_sgen_free_internal_dynamic (entry, sizeof (HashEntry), INTERNAL_MEM_BRIDGE_DATA);
339 mono_sgen_free_internal_dynamic (hash_table, sizeof (HashEntry*) * hash_size, INTERNAL_MEM_BRIDGE_DATA);
342 num_hash_entries = 0;
345 //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
349 register_bridge_object (MonoObject *obj)
351 HashEntry *entry = get_hash_entry (obj);
352 g_assert (!entry->is_bridge);
353 entry->is_bridge = TRUE;
357 register_finishing_time (HashEntry *entry, int t)
359 g_assert (entry->finishing_time < 0);
360 entry->finishing_time = t;
364 object_is_live (MonoObject **objp)
366 MonoObject *obj = *objp;
367 MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
370 return lookup_hash_entry (fwd) == NULL;
372 if (!mono_sgen_object_is_live (obj))
374 return lookup_hash_entry (obj) == NULL;
377 static DynArray dfs_stack;
380 #define HANDLE_PTR(ptr,obj) do { \
381 MonoObject *dst = (MonoObject*)*(ptr); \
382 if (dst && !object_is_live (&dst)) { \
383 dyn_array_ptr_push (&dfs_stack, obj_entry); \
384 dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst)); \
389 dfs1 (HashEntry *obj_entry, HashEntry *src)
391 g_assert (dfs_stack.size == 0);
393 dyn_array_ptr_push (&dfs_stack, src);
394 dyn_array_ptr_push (&dfs_stack, obj_entry);
400 obj_entry = dyn_array_ptr_pop (&dfs_stack);
402 src = dyn_array_ptr_pop (&dfs_stack);
404 obj = obj_entry->obj;
408 //g_print ("link %s -> %s\n", mono_sgen_safe_name (src->obj), mono_sgen_safe_name (obj));
409 add_source (obj_entry, src);
411 //g_print ("starting with %s\n", mono_sgen_safe_name (obj));
414 if (obj_entry->is_visited)
417 obj_entry->is_visited = TRUE;
419 dyn_array_ptr_push (&dfs_stack, obj_entry);
420 /* NULL marks that the next entry is to be finished */
421 dyn_array_ptr_push (&dfs_stack, NULL);
423 #include "sgen-scan-object.h"
425 obj_entry = dyn_array_ptr_pop (&dfs_stack);
427 //g_print ("finish %s\n", mono_sgen_safe_name (obj_entry->obj));
428 register_finishing_time (obj_entry, current_time++);
430 } while (dfs_stack.size > 0);
434 scc_add_xref (SCC *src, SCC *dst)
436 g_assert (src != dst);
437 g_assert (src->index != dst->index);
439 if (dyn_array_int_contains (&dst->xrefs, src->index))
441 if (src->num_bridge_entries) {
442 dyn_array_int_add (&dst->xrefs, src->index);
446 dyn_array_append (&dst->xrefs, &src->xrefs);
447 for (i = 0; i < dst->xrefs.size; ++i)
448 g_assert (DYN_ARRAY_INT_REF (&dst->xrefs, i) != dst->index);
453 scc_add_entry (SCC *scc, HashEntry *entry)
455 g_assert (entry->scc_index < 0);
456 entry->scc_index = scc->index;
457 if (entry->is_bridge)
458 ++scc->num_bridge_entries;
461 static DynArray sccs;
462 static SCC *current_scc;
465 dfs2 (HashEntry *entry)
469 g_assert (dfs_stack.size == 0);
471 dyn_array_ptr_push (&dfs_stack, entry);
474 entry = dyn_array_ptr_pop (&dfs_stack);
476 if (entry->scc_index >= 0) {
477 if (entry->scc_index != current_scc->index)
478 scc_add_xref (DYN_ARRAY_REF (&sccs, entry->scc_index), current_scc);
482 scc_add_entry (current_scc, entry);
484 for (i = 0; i < entry->srcs.size; ++i)
485 dyn_array_ptr_push (&dfs_stack, DYN_ARRAY_PTR_REF (&entry->srcs, i));
486 } while (dfs_stack.size > 0);
490 compare_hash_entries (const void *ep1, const void *ep2)
492 HashEntry *e1 = *(HashEntry**)ep1;
493 HashEntry *e2 = *(HashEntry**)ep2;
494 return e2->finishing_time - e1->finishing_time;
498 mono_sgen_bridge_processing (int num_objs, MonoObject **objs)
500 HashEntry **all_entries;
502 int num_sccs, num_xrefs;
503 int max_entries, max_xrefs;
505 MonoGCBridgeSCC **api_sccs;
506 MonoGCBridgeXRef *api_xrefs;
508 g_assert (mono_sgen_need_bridge_processing ());
510 //g_print ("%d finalized objects\n", num_objs);
512 /* remove objects that are not bridge objects */
514 for (i = 0; i < num_objs; ++i) {
515 MonoObject *obj = objs [i];
516 //g_assert (!mono_sgen_object_is_live (obj));
517 if (bridge_callbacks.is_bridge_object (obj)) {
518 register_bridge_object (obj);
524 //g_print ("%d bridge objects\n", num_objs);
528 dyn_array_ptr_init (&dfs_stack);
531 for (i = 0; i < num_objs; ++i)
532 dfs1 (get_hash_entry (objs [i]), NULL);
534 //g_print ("%d entries - hash size %d\n", num_hash_entries, hash_size);
536 /* alloc and fill array of all entries */
538 all_entries = mono_sgen_alloc_internal_dynamic (sizeof (HashEntry*) * num_hash_entries, INTERNAL_MEM_BRIDGE_DATA);
542 for (i = 0; i < hash_size; ++i) {
545 for (entry = hash_table [i]; entry != NULL; entry = entry->next) {
546 g_assert (entry->finishing_time >= 0);
547 all_entries [j++] = entry;
550 if (length > max_entries)
551 max_entries = length;
553 g_assert (j == num_hash_entries);
555 //g_print ("max hash bucket length %d\n", max_entries);
557 /* sort array according to decreasing finishing time */
559 qsort (all_entries, num_hash_entries, sizeof (HashEntry*), compare_hash_entries);
561 /* second DFS pass */
563 dyn_array_init (&sccs, sizeof (SCC));
564 for (i = 0; i < num_hash_entries; ++i) {
565 HashEntry *entry = all_entries [i];
566 if (entry->scc_index < 0) {
567 int index = sccs.size;
568 current_scc = dyn_array_add (&sccs);
569 current_scc->index = index;
570 current_scc->num_bridge_entries = 0;
571 current_scc->api_index = -1;
572 dyn_array_int_init (¤t_scc->xrefs);
578 //g_print ("%d sccs\n", sccs.size);
580 dyn_array_uninit (&dfs_stack);
582 /* init data for callback */
585 for (i = 0; i < sccs.size; ++i) {
586 SCC *scc = DYN_ARRAY_REF (&sccs, i);
587 g_assert (scc->index == i);
588 if (scc->num_bridge_entries)
592 api_sccs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
595 for (i = 0; i < sccs.size; ++i) {
596 SCC *scc = DYN_ARRAY_REF (&sccs, i);
597 if (!scc->num_bridge_entries)
600 api_sccs [j] = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA);
601 api_sccs [j]->num_objs = scc->num_bridge_entries;
602 scc->num_bridge_entries = 0;
603 scc->api_index = j++;
605 num_xrefs += scc->xrefs.size;
608 for (i = 0; i < hash_size; ++i) {
610 for (entry = hash_table [i]; entry != NULL; entry = entry->next) {
612 if (!entry->is_bridge)
614 scc = DYN_ARRAY_REF (&sccs, entry->scc_index);
615 api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = entry->obj;
619 api_xrefs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
621 for (i = 0; i < sccs.size; ++i) {
623 SCC *scc = DYN_ARRAY_REF (&sccs, i);
624 if (!scc->num_bridge_entries)
626 for (k = 0; k < scc->xrefs.size; ++k) {
627 SCC *src_scc = DYN_ARRAY_REF (&sccs, DYN_ARRAY_INT_REF (&scc->xrefs, k));
628 if (!src_scc->num_bridge_entries)
630 api_xrefs [j].src_scc_index = src_scc->api_index;
631 api_xrefs [j].dst_scc_index = scc->api_index;
639 max_entries = max_xrefs = 0;
640 for (i = 0; i < sccs.size; ++i) {
641 SCC *scc = DYN_ARRAY_REF (&sccs, i);
642 if (scc->num_bridge_entries)
644 if (scc->num_bridge_entries > max_entries)
645 max_entries = scc->num_bridge_entries;
646 if (scc->xrefs.size > max_xrefs)
647 max_xrefs = scc->xrefs.size;
648 dyn_array_uninit (&scc->xrefs);
651 dyn_array_uninit (&sccs);
653 mono_sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * num_hash_entries, INTERNAL_MEM_BRIDGE_DATA);
657 //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
661 bridge_callbacks.cross_references (num_sccs, api_sccs, num_xrefs, api_xrefs);
663 /* free callback data */
665 for (i = 0; i < num_sccs; ++i) {
666 mono_sgen_free_internal_dynamic (api_sccs [i],
667 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
668 INTERNAL_MEM_BRIDGE_DATA);
670 mono_sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
672 mono_sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
676 bridge_test_is_bridge_object (MonoObject *obj)
682 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
685 for (i = 0; i < num_sccs; ++i) {
687 g_print ("--- SCC %d\n", i);
688 for (j = 0; j < sccs [i]->num_objs; ++j)
689 g_print (" %s\n", mono_sgen_safe_name (sccs [i]->objs [j]));
691 for (i = 0; i < num_xrefs; ++i) {
692 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
693 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
694 g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
700 mono_sgen_register_test_bridge_callbacks (void)
702 MonoGCBridgeCallbacks callbacks;
703 callbacks.is_bridge_object = bridge_test_is_bridge_object;
704 callbacks.cross_references = bridge_test_cross_reference;
705 mono_gc_register_bridge_callbacks (&callbacks);