1 Handling GC allocated objects in C
3 As part of an effort to improve our GC, we need to keep track
4 precisely of where objects are stored, so we can incrementally move
5 from the current conservative GC to a more advanced precise and moving GC.
6 Previously, all global C variables were considered GC roots, but this makes
7 the GC less efficient and increases the chances false references are found
8 to GC memory, hence retaining more memory than needed.
9 We need to tell the GC that some object is supposed to be kept alive
10 as if it was referenced in a global variable.
20 Any object which is stored in `o' is considered to be alive -- it will
21 not be collected. `o' is a member of the root set for the GC.
23 However, in C code, this is not the case. If you have:
25 static MonoObject* o = NULL;
27 The object in `o' will *NOT* be scanned.
29 If you need to store an object in a C variable and prevent it from being
30 collected, you need to acquire a GC handle for it.
32 guint32 handle = mono_gchandle_new (my_object, TRUE);
34 TRUE means the object will be pinned, so it won't move in memory
35 when we'll use a moving GC. You can access the MonoObject* referenced by
38 MonoObject* obj = mono_gchandle_get_target (handle);
40 When you don't need the handle anymore you need to call:
42 mono_gchandle_free (handle);
44 Note that if you assign a new object to the C var, you need to get a new
45 handle, it's not enough to store a new object in the C var.
47 So code that looked like this:
49 static MonoObject* o = NULL;
51 o = mono_object_new (...);
54 /* when done to allow the GC to collect o */
57 should now be changed to:
59 static guint32 o_handle;
61 MonoObject *o = mono_object_new (...);
62 o_handle = mono_gchandle_new (o, TRUE);
63 /* use o or mono_gchandle_get_target (o_handle) */
65 /* when done to allow the GC to collect o */
66 mono_gchandle_free (o_handle);
69 For Mono runtime developers
70 ---------------------------
72 There are two kinds of static vars used to store pointers to GC memory
73 that we need to consider:
75 *) other memory chunks allocated with GC_MALLOC().
77 Objects should be dealt with the GC handle support as detailed above.
78 Other items should register the static pointer as an area to be considered
79 part of the root set with the following:
81 static gpointer my_gc_data = NULL;
83 MONO_GC_REGISTER_ROOT (my_gc_data);
84 my_gc_data = GC_MALLOC (...);
86 Note that this registration is not necessary for *LOCAL* variables,
87 as they are stored on the stack. It is only necessary for global variables,
88 as they are not a part of the GC's root set.
90 Once you have done the MONO_GC_REGISTER_ROOT, the variable is just like
91 a static variable in C#. To keep an object alive, you have the variable reference
92 the GC memory, to remove the reference, set the variable to NULL.
94 As we prepare the code for a precise GC, GC_MALLOC () will not be used anymore
95 in this way in most cases: we'll have a mechanism to specify exactly where
96 references to GC memory is stored.
98 [The rest of this file is useless, just kept until the switchover
99 of the internals is complete.]
103 Until now, we have been able to store gc references in
104 variables. This means we must audit the source code for where
105 this happens, and ensure the GC knows the right roots.
107 Because this is a change from previous behavior, an audit
108 must be done to check for any incorrect uses. Basically,
109 we need to check all variables that contain GC allocated objects. This
110 includes MonoObject*'s, any other pointer to a managed type,
111 and MonoGHashTable's. Any such variable must either
112 1) be added as a root -or-
113 2) only hold values that are referenced elsewhere
115 The status of the audit is below:
120 No variables which can contain GC allocated data
123 No variables which can contain GC allocated data
126 No variables which can contain GC allocated data
129 No variables which can contain GC allocated data
135 No variables which can contain GC allocated data
138 static MonoGHashTable * appdomains_list = NULL;
139 This has been added as a root
141 static MonoDomain *mono_root_domain = NULL;
142 this is added to the `appdomains_list' hashtable
143 which keeps the reference
145 No other variables contain GC allocated data.
148 No variables which can contain GC allocated data
157 No variablesNo other variables contain GC allocated data.
159 static MonoThread *gc_thread;
160 MonoThread*'s are taken care of by threads.c
161 static gpointer *gc_handles = NULL;
162 static guint8 *gc_handle_types = NULL;
163 These were added as roots
165 No other variables contain GC allocated data.
167 No variables which can contain GC allocated data
170 No variables which can contain GC allocated data
173 No variables which can contain GC allocated data
179 static MonoGHashTable *wrapper_hash;
181 static MonoString *string_dummy = NULL;
183 No other variables contain GC allocated data.
189 No variables which can contain GC allocated data
195 No variables which can contain GC allocated data
198 No variables which can contain GC allocated data
200 mono-debug-debugger.c
201 static MonoObject *last_exception = NULL;
204 No other variables contain GC allocated data.
213 static MonoThread *main_thread;
214 Taken care of by threads.c
216 No other variables contain GC allocated data.
218 No variables which can contain GC allocated data
221 No variables which canMONO_GC_REGISTER_ROOT (my_object); contain GC allocated data
224 No variables which can contain GC allocated data
227 No variables which can contain GC allocated data
233 No variables which can contain GC allocated data
236 No variables which can contain GC allocated data
242 No variables which can contain GC allocated data
251 static MonoGHashTable *ares_htable = NULL;
254 No other variables contain GC allocated data.
256 static MonoGHashTable *threads=NULL
257 Added as a root. This variable keeps a reference to
258 all threads, so it covers other files.
260 No other variables contain GC allocated data.
264 No variables which can contain GC allocated data
266 No variables which can contain GC allocated data
272 No variables which can contain GC allocated data
274 No variables which can contain GC allocated data
276 static MonoGHashNode *node_free_list = NULL;
278 No other variables contain GC allocated data.
280 No variables which can contain GC allocated data
282 No variables which can contain GC allocated data
284 No variables which can contain GC allocated data
286 No variables which can contain GC allocated data
288 No variables which can contain GC allocated data
290 No variables which can contain GC allocated data
292 No variables which can contain GC allocated data
297 static MonoGHashTable *method_pointer_hash = NULL;
299 No other variables contain GC allocated data.
301 No variables which can contain GC allocated data
303 No variables which can contain GC allocated data
305 No variables which can contain GC allocated data
311 No variables which can contain GC allocated data
313 static MonoGHashTable *aot_modules;
315 No other variables contain GC allocated data.
317 No variables which can contain GC allocated data
325 No variables which can contain GC allocated data
327 No variables which can contain GC allocated data
329 No variables which can contain GC allocated data
331 No variables which can contain GC allocated data
333 No variables which can contain GC allocated data
337 No variables which can contain GC allocated data
339 No variables which can contain GC allocated data
341 No variables which can contain GC allocated data
351 No variables which can contain GC allocated data
353 No variables which can contain GC allocated data
355 No variables which can contain GC allocated data
357 No variables which can contain G
360 No variables which can contain GC allocated data
362 No variables which can contain GC allocated data
364 No variables which can contain GC allocated data
366 No variables which can contain GC allocated data
368 No variables which can contain GC allocated data
370 No variables which can contain GC allocated data
372 No variables which can contain GC allocated data
374 No variables which can contain GC allocated data
376 No variables which can contain GC allocated data
382 No variables which can contain GC allocated data
384 No variables which can contain GC allocated data
386 No variables which can contain GC allocated data
388 No variables which can contain GC allocated data
390 No variables which can contain GC allocated data
392 No variables which can contain GC allocated data
394 No variables which can contain GC allocated data
396 No variables which can contain GC allocated data
398 No variables which can contain GC allocated data
400 No variables which can contain GC allocated data
402 No variables which can contain GC allocated data
404 No variables which can contain GC allocated data
406 No variables which can contain GC allocated data
408 No variables which can contain GC allocated data
410 No variables which can contain GC allocated data
412 No variables which can contain GC allocated data
414 No variables which can contain GC allocated data
416 No variables which can contain GC allocated data
418 No variables which can contain GC allocated data
420 static MonoGHashTable *tls_gc_hash = NULL;
422 No other variables contain GC allocated data.
425 No variables which can contain GC allocated data
427 No variables which can contain GC allocated data
429 No variables which can contain GC allocated data