3 * toggleref support for sgen
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2011 Xamarin, Inc.
9 * Copyright (C) 2012 Xamarin Inc
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #include "sgen/sgen-gc.h"
19 #include "sgen-toggleref.h"
20 #include "sgen/sgen-client.h"
23 /*only one of the two can be non null at a given time*/
29 static MonoToggleRefStatus (*toggleref_callback
) (MonoObject
*obj
);
30 static MonoGCToggleRef
*toggleref_array
;
31 static int toggleref_array_size
;
32 static int toggleref_array_capacity
;
35 sgen_process_togglerefs (void)
38 int toggle_ref_counts
[3] = { 0, 0, 0 };
40 SGEN_LOG (4, "Proccessing ToggleRefs %d", toggleref_array_size
);
42 for (i
= w
= 0; i
< toggleref_array_size
; ++i
) {
44 MonoGCToggleRef r
= toggleref_array
[i
];
55 res
= toggleref_callback (obj
);
56 ++toggle_ref_counts
[res
];
58 case MONO_TOGGLE_REF_DROP
:
60 case MONO_TOGGLE_REF_STRONG
:
61 toggleref_array
[w
].strong_ref
= obj
;
62 toggleref_array
[w
].weak_ref
= NULL
;
65 case MONO_TOGGLE_REF_WEAK
:
66 toggleref_array
[w
].strong_ref
= NULL
;
67 toggleref_array
[w
].weak_ref
= obj
;
71 g_assert_not_reached ();
75 toggleref_array_size
= w
;
77 SGEN_LOG (4, "Done Proccessing ToggleRefs dropped %d strong %d weak %d final size %d",
78 toggle_ref_counts
[MONO_TOGGLE_REF_DROP
],
79 toggle_ref_counts
[MONO_TOGGLE_REF_STRONG
],
80 toggle_ref_counts
[MONO_TOGGLE_REF_WEAK
],
84 void sgen_client_mark_togglerefs (char *start
, char *end
, ScanCopyContext ctx
)
86 CopyOrMarkObjectFunc copy_func
= ctx
.ops
->copy_or_mark_object
;
87 SgenGrayQueue
*queue
= ctx
.queue
;
90 SGEN_LOG (4, "Marking ToggleRefs %d", toggleref_array_size
);
92 for (i
= 0; i
< toggleref_array_size
; ++i
) {
93 if (toggleref_array
[i
].strong_ref
) {
94 GCObject
*object
= toggleref_array
[i
].strong_ref
;
95 if ((char*)object
>= start
&& (char*)object
< end
) {
96 SGEN_LOG (6, "\tcopying strong slot %d", i
);
97 copy_func (&toggleref_array
[i
].strong_ref
, queue
);
101 sgen_drain_gray_stack (ctx
);
105 sgen_foreach_toggleref_root (void (*callback
)(MonoObject
*, gpointer
), gpointer data
)
108 for (i
= 0; i
< toggleref_array_size
; ++i
) {
109 if (toggleref_array
[i
].strong_ref
)
110 callback (toggleref_array
[i
].strong_ref
, data
);
114 void sgen_client_clear_togglerefs (char *start
, char *end
, ScanCopyContext ctx
)
116 CopyOrMarkObjectFunc copy_func
= ctx
.ops
->copy_or_mark_object
;
117 SgenGrayQueue
*queue
= ctx
.queue
;
120 SGEN_LOG (4, "Clearing ToggleRefs %d", toggleref_array_size
);
122 for (i
= 0; i
< toggleref_array_size
; ++i
) {
123 if (toggleref_array
[i
].weak_ref
) {
124 GCObject
*object
= toggleref_array
[i
].weak_ref
;
126 if ((char*)object
>= start
&& (char*)object
< end
) {
127 if (sgen_gc_is_object_ready_for_finalization (object
)) {
128 SGEN_LOG (6, "\tcleaning weak slot %d", i
);
129 toggleref_array
[i
].weak_ref
= NULL
; /* We defer compaction to only happen on the callback step. */
131 SGEN_LOG (6, "\tkeeping weak slot %d", i
);
132 copy_func (&toggleref_array
[i
].weak_ref
, queue
);
137 sgen_drain_gray_stack (ctx
);
141 ensure_toggleref_capacity (int capacity
)
143 if (!toggleref_array
) {
144 toggleref_array_capacity
= 32;
145 toggleref_array
= (MonoGCToggleRef
*)sgen_alloc_internal_dynamic (
146 toggleref_array_capacity
* sizeof (MonoGCToggleRef
),
147 INTERNAL_MEM_TOGGLEREF_DATA
,
150 if (toggleref_array_size
+ capacity
>= toggleref_array_capacity
) {
151 MonoGCToggleRef
*tmp
;
152 int old_capacity
= toggleref_array_capacity
;
153 while (toggleref_array_capacity
< toggleref_array_size
+ capacity
)
154 toggleref_array_capacity
*= 2;
156 tmp
= (MonoGCToggleRef
*)sgen_alloc_internal_dynamic (
157 toggleref_array_capacity
* sizeof (MonoGCToggleRef
),
158 INTERNAL_MEM_TOGGLEREF_DATA
,
161 memcpy (tmp
, toggleref_array
, toggleref_array_size
* sizeof (MonoGCToggleRef
));
163 sgen_free_internal_dynamic (toggleref_array
, old_capacity
* sizeof (MonoGCToggleRef
), INTERNAL_MEM_TOGGLEREF_DATA
);
164 toggleref_array
= tmp
;
169 * mono_gc_toggleref_add:
170 * @object object to register for toggleref processing
171 * @strong_ref if true the object is registered with a strong ref, a weak one otherwise
173 * Register a given object for toggleref processing. It will be stored internally and the toggleref callback will be called
174 * on it until it returns MONO_TOGGLE_REF_DROP or is collected.
177 mono_gc_toggleref_add (MonoObject
*object
, mono_bool strong_ref
)
179 if (!toggleref_callback
)
182 MONO_ENTER_GC_UNSAFE
;
184 SGEN_LOG (4, "Adding toggleref %p %d", object
, strong_ref
);
188 ensure_toggleref_capacity (1);
189 toggleref_array
[toggleref_array_size
].strong_ref
= strong_ref
? object
: NULL
;
190 toggleref_array
[toggleref_array_size
].weak_ref
= strong_ref
? NULL
: object
;
191 ++toggleref_array_size
;
199 * mono_gc_toggleref_register_callback:
200 * \param callback callback used to determine the new state of the given object.
202 * The callback must decide the status of a given object. It must return one of the values in the \c MONO_TOGGLE_REF_ enum.
203 * This function is called with the world running but with the GC locked. This means that you can do everything that doesn't
204 * require GC interaction. This includes, but not limited to, allocating objects, (de)registering for finalization, manipulating
205 * gchandles, storing to reference fields or interacting with other threads that might perform such operations.
208 mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref
) (MonoObject
*obj
))
210 toggleref_callback
= proccess_toggleref
;
213 static MonoToggleRefStatus
214 test_toggleref_callback (MonoObject
*obj
)
216 MonoToggleRefStatus status
= MONO_TOGGLE_REF_DROP
;
218 MONO_STATIC_POINTER_INIT (MonoClassField
, mono_toggleref_test_field
)
220 mono_toggleref_test_field
= mono_class_get_field_from_name_full (mono_object_class (obj
), "__test", NULL
);
221 g_assert (mono_toggleref_test_field
);
223 MONO_STATIC_POINTER_INIT_END (MonoClassField
, mono_toggleref_test_field
)
225 /* In coop mode, important to not call a helper that will pin obj! */
226 mono_field_get_value_internal (obj
, mono_toggleref_test_field
, &status
);
227 printf ("toggleref-cb obj %d\n", status
);
232 sgen_register_test_toggleref_callback (void)
234 toggleref_callback
= test_toggleref_callback
;