Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gc-variables-in-c « docs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0146dcb4d244322f588c5405e289cbf3060cc170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
		Handling GC allocated objects in C

As part of an effort to improve our GC, we need to keep track
precisely of where objects are stored, so we can incrementally move
from the current conservative GC to a more advanced precise and moving GC.
Previously, all global C variables were considered GC roots, but this makes
the GC less efficient and increases the chances false references are found
to GC memory, hence retaining more memory than needed.
We need to tell the GC that some object is supposed to be kept alive
as if it was referenced in a global variable.

For Mono embedders
------------------

In C#, if you say:
class T {
	static object o;
}

Any object which is stored in `o' is considered to be alive -- it will
not be collected. `o' is a member of the root set for the GC.

However, in C code, this is not the case. If you have:

static MonoObject* o = NULL;

The object in `o' will *NOT* be scanned.

If you need to store an object in a C variable and prevent it from being 
collected, you need to acquire a GC handle for it.

	guint32 handle = mono_gchandle_new (my_object, TRUE);

TRUE means the object will be pinned, so it won't move in memory 
when we'll use a moving GC. You can access the MonoObject* referenced by
a handle with:

	MonoObject* obj = mono_gchandle_get_target (handle);

When you don't need the handle anymore you need to call:

	mono_gchandle_free (handle);

Note that if you assign a new object to the C var, you need to get a new 
handle, it's not enough to store a new object in the C var.

So code that looked like this:

	static MonoObject* o = NULL;
	...
	o = mono_object_new (...);
	/* use o */
	...
	/* when done to allow the GC to collect o */
	o = NULL;

should now be changed to:

	static guint32 o_handle;
	...
	MonoObject *o = mono_object_new (...);
	o_handle = mono_gchandle_new (o, TRUE);
	/* use o or mono_gchandle_get_target (o_handle) */
	...
	/* when done to allow the GC to collect o */
	mono_gchandle_free (o_handle);


For Mono runtime developers
---------------------------

There are two kinds of static vars used to store pointers to GC memory 
that we need to consider:
*) objects
*) other memory chunks allocated with GC_MALLOC().

Objects should be dealt with the GC handle support as detailed above.
Other items should register the static pointer as an area to be considered
part of the root set with the following:

	static gpointer my_gc_data = NULL;
	...
	MONO_GC_REGISTER_ROOT (my_gc_data);
	my_gc_data = GC_MALLOC (...);
	
Note that this registration is not necessary for *LOCAL* variables,
as they are stored on the stack. It is only necessary for global variables,
as they are not a part of the GC's root set.

Once you have done the MONO_GC_REGISTER_ROOT, the variable is just like
a static variable in C#. To keep an object alive, you have the variable reference
the GC memory, to remove the reference, set the variable to NULL.

As we prepare the code for a precise GC, GC_MALLOC () will not be used anymore
in this way in most cases: we'll have a mechanism to specify exactly where
references to GC memory is stored.

[The rest of this file is useless, just kept until the switchover 
of the internals is complete.]
Mono Internal Audit
-------------------

Until now, we have been able to store gc references in
variables. This means we must audit the source code for where
this happens, and ensure the GC knows the right roots.

Because this is a change from previous behavior, an audit
must be done to check for any incorrect uses. Basically,
we need to check all variables that contain GC allocated objects. This
includes MonoObject*'s, any other pointer to a managed type,
and MonoGHashTable's. Any such variable must either
	1) be added as a root -or-
	2) only hold values that are referenced elsewhere

The status of the audit is below:

metadata
--------
appdomain.c
	No variables which can contain GC allocated data

assembly.c
	No variables which can contain GC allocated data
	
class.c
	No variables which can contain GC allocated data

debug-helpers.c
	No variables which can contain GC allocated data

debug-mono-symfile.c
	No variables
	
decimal.c
	No variables which can contain GC allocated data

domain.c
	static MonoGHashTable * appdomains_list = NULL;
		This has been added as a root
		
	static MonoDomain *mono_root_domain = NULL;
		this is added to the `appdomains_list' hashtable
		which keeps the reference
	
	No other variables contain GC allocated data.

environment.c
	No variables which can contain GC allocated data

exception.c
	No variables.

file-io.c
	No variables

filewatcher.c
	No variablesNo other variables contain GC allocated data.
gc.c
	static MonoThread *gc_thread;
		MonoThread*'s are taken care of by threads.c
	static gpointer *gc_handles = NULL;
	static guint8 *gc_handle_types = NULL;
		These were added as roots
		
	No other variables contain GC allocated data.
icall.c
	No variables which can contain GC allocated data

image.c
	No variables which can contain GC allocated data

loader.c
	No variables which can contain GC allocated data
	
locales.c
	No variables
	
marshal.c
	static MonoGHashTable *wrapper_hash;
		Added as a root
	static MonoString *string_dummy = NULL;
		Added as a root
	No other variables contain GC allocated data.
	
mempool.c
	No variables
	
metadata.c
	No variables which can contain GC allocated data

monitor.c
	No variables

mono-config.c
	No variables which can contain GC allocated data

mono-debug.c
	No variables which can contain GC allocated data

mono-debug-debugger.c
	static MonoObject *last_exception = NULL;
		Added as a root
	
	No other variables contain GC allocated data.
	
mono-endian.c
	No variables

monosn.c
	Not compiled

object.c
	static MonoThread *main_thread;
		Taken care of by threads.c
		
	No other variables contain GC allocated data.
opcodes.c
	No variables which can contain GC allocated data

pedump.c
	No variables which canMONO_GC_REGISTER_ROOT (my_object); contain GC allocated data

process.c
	No variables which can contain GC allocated data
	
profiler.c
	No variables which can contain GC allocated data

rand.c
	No variables
	
rawbuffer.c
	No variables which can contain GC allocated data

reflection.c
	No variables which can contain GC allocated data
	
security.c
	No variables.
	
socket-io.c
	No variables which can contain GC allocated data
	
string-icalls.c
	No variables
	
sysmath.c
	No variables
	
threadpool.c
	static MonoGHashTable *ares_htable = NULL;
		Added as a root
	
	No other variables contain GC allocated data.
threads.c
	static MonoGHashTable *threads=NULL
		Added as a root. This variable keeps a reference to
		all threads, so it covers other files.
		
	No other variables contain GC allocated data.
typedef.c
	No variables
unicode.c
	No variables which can contain GC allocated data
verify.c
	No variables which can contain GC allocated data
	

utils/
------
monobitset.c
	No variables which can contain GC allocated data
mono-codeman.c
	No variables which can contain GC allocated data
mono-hash.c
	static MonoGHashNode *node_free_list = NULL;
		added as a root
	No other variables contain GC allocated data.
mono-logger.c
	No variables which can contain GC allocated data
mono-math.c
	No variables which can contain GC allocated data
mono-md5.c
	No variables which can contain GC allocated data
mono-sha1.c
	No variables which can contain GC allocated data
mono-uri.c
	No variables which can contain GC allocated data
strenc.c
	No variables which can contain GC allocated data
strtod.c
	No variables which can contain GC allocated data
	
interp/
--------
interp.c
	static MonoGHashTable *method_pointer_hash = NULL;
		Added as a root
	No other variables contain GC allocated data.
main.c
	No variables which can contain GC allocated data
mintops.c
	No variables which can contain GC allocated data
transform.c
	No variables which can contain GC allocated data
	
mini/
-----

abcremoval.c
	No variables which can contain GC allocated data
aot.c
	static MonoGHashTable *aot_modules;
		Added as root
	No other variables contain GC allocated data.
cfold.c
	No variables which can contain GC allocated data
cprop.c
	Not compiled
debug-mini.c
	No variables.
dominators.c
	No variables
driver.c
	No variables which can contain GC allocated data
exceptions-ppc.c
	No variables which can contain GC allocated data
exceptions-s390.c
	No variables which can contain GC allocated data
exceptions-sparc.c
	No variables which can contain GC allocated data
exceptions-x86.c
	No variables which can contain GC allocated data
genmdesc.c
	Not part of mini
graph.c
	No variables which can contain GC allocated data
helpers.c
	No variables which can contain GC allocated data
inssel.c
	No variables which can contain GC allocated data
jit-icalls.c
	No variables
linear-scan.c
	No variables
liveness.c
	No variables
main.c
	No variables
mini.c
	No variables which can contain GC allocated data
mini-exceptions.c
	No variables which can contain GC allocated data
mini-ppc.c
	No variables which can contain GC allocated data
mini-s390.c
	No variables which can contain G 
C allocated data
mini-sparc.c
	No variables which can contain GC allocated data
mini-x86.c
	No variables which can contain GC allocated data
regalloc.c
	No variables which can contain GC allocated data
ssa.c
	No variables which can contain GC allocated data
trace.c
	No variables which can contain GC allocated data
tramp-ppc.c
	No variables which can contain GC allocated data
tramp-s390.c
	No variables which can contain GC allocated data
tramp-sparc.c
	No variables which can contain GC allocated data
tramp-x86.c
	No variables which can contain GC allocated data


io-layer/
---------
atomic.c
	No variables which can contain GC allocated data
context.c
	No variables which can contain GC allocated data
critical-sections.c
	No variables which can contain GC allocated data
daemon.c
	No variables which can contain GC allocated data
daemon-messages.c
	No variables which can contain GC allocated data
error.c
	No variables which can contain GC allocated data
events.c
	No variables which can contain GC allocated data
handles.c
	No variables which can contain GC allocated data
io.c
	No variables which can contain GC allocated data
io-layer-dummy.c
	No variables which can contain GC allocated data
misc.c
	No variables which can contain GC allocated data
mono-mutex.c
	No variables which can contain GC allocated data
mutexes.c
	No variables which can contain GC allocated data
processes.c
	No variables which can contain GC allocated data
security.c
	No variables which can contain GC allocated data
semaphores.c
	No variables which can contain GC allocated data
shared.c
	No variables which can contain GC allocated data
sockets.c
	No variables which can contain GC allocated data
system.c
	No variables which can contain GC allocated data
threads.c
	static MonoGHashTable *tls_gc_hash = NULL;
		added as a root
	No other variables contain GC allocated data.
	
timed-thread.c
	No variables which can contain GC allocated data
timefuncs.c
	No variables which can contain GC allocated data
wait.c
	No variables which can contain GC allocated data