2 * sgen-toggleref.c: toggleref support for sgen
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * Copyright 2011 Xamarin, Inc.
8 * Copyright (C) 2012 Xamarin Inc
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License 2.0 as published by the Free Software Foundation;
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License 2.0 along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "sgen-toggleref.h"
32 /*only one of the two can be non null at a given time*/
38 static MonoToggleRefStatus (*toggleref_callback) (MonoObject *obj);
39 static MonoGCToggleRef *toggleref_array;
40 static int toggleref_array_size;
41 static int toggleref_array_capacity;
44 sgen_process_togglerefs (void)
47 int toggle_ref_counts [3] = { 0, 0, 0 };
49 SGEN_LOG (4, "Proccessing ToggleRefs %d", toggleref_array_size);
51 for (i = w = 0; i < toggleref_array_size; ++i) {
53 MonoGCToggleRef r = toggleref_array [i];
64 res = toggleref_callback (obj);
65 ++toggle_ref_counts [res];
67 case MONO_TOGGLE_REF_DROP:
69 case MONO_TOGGLE_REF_STRONG:
70 toggleref_array [w].strong_ref = obj;
71 toggleref_array [w].weak_ref = NULL;
74 case MONO_TOGGLE_REF_WEAK:
75 toggleref_array [w].strong_ref = NULL;
76 toggleref_array [w].weak_ref = obj;
80 g_assert_not_reached ();
84 toggleref_array_size = w;
86 SGEN_LOG (4, "Done Proccessing ToggleRefs dropped %d strong %d weak %d final size %d",
87 toggle_ref_counts [MONO_TOGGLE_REF_DROP],
88 toggle_ref_counts [MONO_TOGGLE_REF_STRONG],
89 toggle_ref_counts [MONO_TOGGLE_REF_WEAK],
94 sgen_scan_togglerefs (char *start, char *end, ScanCopyContext ctx)
96 CopyOrMarkObjectFunc copy_func = ctx.copy_func;
97 SgenGrayQueue *queue = ctx.queue;
100 SGEN_LOG (4, "Scanning ToggleRefs %d", toggleref_array_size);
102 for (i = 0; i < toggleref_array_size; ++i) {
103 if (toggleref_array [i].strong_ref) {
104 char *object = toggleref_array [i].strong_ref;
105 if (object >= start && object < end) {
106 SGEN_LOG (6, "\tcopying strong slot %d", i);
107 copy_func (&toggleref_array [i].strong_ref, queue);
109 } else if (toggleref_array [i].weak_ref) {
110 char *object = toggleref_array [i].weak_ref;
112 if (object >= start && object < end) {
113 if (sgen_gc_is_object_ready_for_finalization (object)) {
114 SGEN_LOG (6, "\tcleaning weak slot %d", i);
115 toggleref_array [i].weak_ref = NULL; /* We defer compaction to only happen on the callback step. */
117 SGEN_LOG (6, "\tkeeping weak slot %d", i);
118 copy_func (&toggleref_array [i].weak_ref, queue);
126 ensure_toggleref_capacity (int capacity)
128 if (!toggleref_array) {
129 toggleref_array_capacity = 32;
130 toggleref_array = sgen_alloc_internal_dynamic (
131 toggleref_array_capacity * sizeof (MonoGCToggleRef),
132 INTERNAL_MEM_TOGGLEREF_DATA,
135 if (toggleref_array_size + capacity >= toggleref_array_capacity) {
136 MonoGCToggleRef *tmp;
137 int old_capacity = toggleref_array_capacity;
138 while (toggleref_array_capacity < toggleref_array_size + capacity)
139 toggleref_array_size *= 2;
141 tmp = sgen_alloc_internal_dynamic (
142 toggleref_array_capacity * sizeof (MonoGCToggleRef),
143 INTERNAL_MEM_TOGGLEREF_DATA,
146 memcpy (tmp, toggleref_array, toggleref_array_size * sizeof (MonoGCToggleRef));
148 sgen_free_internal_dynamic (toggleref_array, old_capacity * sizeof (MonoGCToggleRef), INTERNAL_MEM_TOGGLEREF_DATA);
149 toggleref_array = tmp;
154 * mono_gc_toggleref_add:
155 * @object object to register for toggleref processing
156 * @strong_ref if true the object is registered with a strong ref, a weak one otherwise
158 * Register a given object for toggleref processing. It will be stored internally and the toggleref callback will be called
159 * on it until it returns MONO_TOGGLE_REF_DROP or is collected.
162 mono_gc_toggleref_add (MonoObject *object, mono_bool strong_ref)
164 if (!toggleref_callback)
167 SGEN_LOG (4, "Adding toggleref %p %d", object, strong_ref);
171 ensure_toggleref_capacity (1);
172 toggleref_array [toggleref_array_size].strong_ref = strong_ref ? object : NULL;
173 toggleref_array [toggleref_array_size].weak_ref = strong_ref ? NULL : object;
174 ++toggleref_array_size;
180 * mono_gc_toggleref_register_callback:
181 * @callback callback used to determine the new state of the given object.
183 * The callback must decide the status of a given object. It must return one of the values in the MONO_TOGGLE_REF_ enum.
184 * This function is called with the world running but with the GC locked. This means that you can do everything that doesn't
185 * require GC interaction. This includes, but not limited to, allocating objects, (de)registering for finalization, manipulating
186 *gchandles, storing to reference fields or interacting with other threads that might perform such operations.
189 mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (MonoObject *obj))
191 toggleref_callback = proccess_toggleref;