2 * boehm-gc.c: GC implementation using either the installed or included Boehm GC.
7 #define GC_I_HIDE_POINTERS
8 #include <mono/os/gc_wrapper.h>
9 #include <mono/metadata/mono-gc.h>
10 #include <mono/metadata/gc-internal.h>
11 #include <mono/metadata/profiler-private.h>
12 #include <mono/metadata/class-internals.h>
13 #include <mono/metadata/marshal.h>
14 #include <mono/metadata/opcodes.h>
15 #include <mono/utils/mono-logger.h>
19 #ifdef USE_INCLUDED_LIBGC
22 #define THREAD_LOCAL_ALLOC 1
23 #include "private/pthread_support.h"
27 mono_gc_warning (char *msg, GC_word arg)
29 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
33 mono_gc_base_init (void)
36 GC_oom_fn = mono_gc_out_of_memory;
37 GC_set_warn_proc (mono_gc_warning);
38 GC_finalize_on_demand = 1;
39 GC_finalizer_notifier = mono_gc_finalize_notify;
43 mono_gc_collect (int generation)
49 mono_gc_max_generation (void)
55 mono_gc_get_generation (MonoObject *object)
61 mono_gc_collection_count (int generation)
67 mono_gc_add_memory_pressure (gint64 value)
72 mono_gc_get_used_size (void)
74 return GC_get_heap_size () - GC_get_free_bytes ();
78 mono_gc_get_heap_size (void)
80 return GC_get_heap_size ();
84 mono_gc_disable (void)
89 g_assert_not_reached ();
99 g_assert_not_reached ();
104 mono_gc_is_gc_thread (void)
106 #ifdef USE_INCLUDED_LIBGC
107 return GC_thread_is_registered ();
113 extern int GC_thread_register_foreign (void *base_addr);
116 mono_gc_register_thread (void *baseptr)
118 if (mono_gc_is_gc_thread())
120 #if defined(USE_INCLUDED_LIBGC) && !defined(PLATFORM_WIN32)
121 return GC_thread_register_foreign (baseptr);
128 mono_object_is_alive (MonoObject* o)
130 #ifdef USE_INCLUDED_LIBGC
131 return GC_is_marked (o);
137 #ifdef USE_INCLUDED_LIBGC
140 on_gc_notification (GCEventType event)
142 mono_profiler_gc_event ((MonoGCEvent) event, 0);
146 on_gc_heap_resize (size_t new_size)
148 mono_profiler_gc_heap_resize (new_size);
152 mono_gc_enable_events (void)
154 GC_notify_event = on_gc_notification;
155 GC_on_heap_resize = on_gc_heap_resize;
161 mono_gc_enable_events (void)
168 mono_gc_weak_link_add (void **link_addr, MonoObject *obj)
170 /* libgc requires that we use HIDE_POINTER... */
171 *link_addr = (void*)HIDE_POINTER (obj);
172 GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr, obj);
176 mono_gc_weak_link_remove (void **link_addr)
178 GC_unregister_disappearing_link (link_addr);
183 mono_gc_weak_link_get (void **link_addr)
185 MonoObject *obj = REVEAL_POINTER (*link_addr);
186 if (obj == (MonoObject *) -1)
192 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
198 mono_gc_alloc_fixed (size_t size, void *descr)
200 return GC_MALLOC (size);
204 mono_gc_free_fixed (void* addr)
209 mono_gc_invoke_finalizers (void)
211 /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
212 * the 'mem_freed' variable is not initialized when there are no
213 * objects to finalize, which leads to strange behavior later on.
214 * The check is necessary to work around that bug.
216 if (GC_should_invoke_finalizers ())
217 return GC_invoke_finalizers ();
222 mono_gc_pending_finalizers (void)
224 return GC_should_invoke_finalizers ();
228 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
230 *(void**)field_ptr = value;
234 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
236 *(void**)slot_ptr = value;
240 mono_gc_wbarrier_arrayref_copy (MonoArray *arr, gpointer slot_ptr, int count)
242 /* no need to do anything */
246 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
248 *(void**)ptr = value;
252 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
257 mono_gc_wbarrier_object (MonoObject *object)
261 #if defined(USE_INCLUDED_LIBGC) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
262 extern __thread MONO_TLS_FAST void* GC_thread_tls;
263 #include "metadata-internals.h"
269 while (!(v & (1 << i)))
276 ATYPE_FREEPTR_FOR_BOX,
283 create_allocator (int atype, int offset)
285 int index_var, bytes_var, my_fl_var, my_entry_var;
286 guint32 no_freelist_branch, not_small_enough_branch = 0;
287 MonoMethodBuilder *mb;
289 MonoMethodSignature *csig;
291 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
292 csig->ret = &mono_defaults.object_class->byval_arg;
293 csig->params [0] = &mono_defaults.int_class->byval_arg;
295 mb = mono_mb_new (mono_defaults.object_class, "Alloc", MONO_WRAPPER_ALLOC);
296 bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
297 /* bytes = vtable->klass->instance_size */
298 mono_mb_emit_ldarg (mb, 0);
299 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
300 mono_mb_emit_byte (mb, MONO_CEE_ADD);
301 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
302 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
303 mono_mb_emit_byte (mb, MONO_CEE_ADD);
304 /* FIXME: assert instance_size stays a 4 byte integer */
305 mono_mb_emit_byte (mb, MONO_CEE_LDIND_U4);
306 mono_mb_emit_stloc (mb, bytes_var);
309 /* this is needed for strings/arrays only as the other big types are never allocated with this method */
310 if (atype != ATYPE_FREEPTR && atype != ATYPE_FREEPTR_FOR_BOX) {
312 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
313 mono_mb_emit_ldloc (mb, bytes_var);
314 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
315 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
319 /* int index = INDEX_FROM_BYTES(bytes); */
320 index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
322 mono_mb_emit_ldloc (mb, bytes_var);
323 mono_mb_emit_icon (mb, GRANULARITY - 1);
324 mono_mb_emit_byte (mb, MONO_CEE_ADD);
325 mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
326 mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
327 mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
328 mono_mb_emit_byte (mb, MONO_CEE_SHL);
329 /* index var is already adjusted into bytes */
330 mono_mb_emit_stloc (mb, index_var);
332 my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
333 my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
334 /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
335 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
336 mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
337 mono_mb_emit_i4 (mb, offset);
338 if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX)
339 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
340 else if (atype == ATYPE_NORMAL)
341 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
342 else if (atype == ATYPE_GCJ)
343 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
345 g_assert_not_reached ();
346 mono_mb_emit_byte (mb, MONO_CEE_ADD);
347 mono_mb_emit_ldloc (mb, index_var);
348 mono_mb_emit_byte (mb, MONO_CEE_ADD);
349 mono_mb_emit_stloc (mb, my_fl_var);
351 /* my_entry = *my_fl; */
352 mono_mb_emit_ldloc (mb, my_fl_var);
353 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
354 mono_mb_emit_stloc (mb, my_entry_var);
356 /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
357 mono_mb_emit_ldloc (mb, my_entry_var);
358 mono_mb_emit_icon (mb, HBLKSIZE);
359 no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
361 /* ptr_t next = obj_link(my_entry); *my_fl = next; */
362 mono_mb_emit_ldloc (mb, my_fl_var);
363 mono_mb_emit_ldloc (mb, my_entry_var);
364 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
365 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
367 /* set the vtable and clear the words in the object */
368 mono_mb_emit_ldloc (mb, my_entry_var);
369 mono_mb_emit_ldarg (mb, 0);
370 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
372 if (atype == ATYPE_FREEPTR) {
373 int start_var, end_var, start_loop;
374 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
376 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
377 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
378 mono_mb_emit_ldloc (mb, my_entry_var);
379 mono_mb_emit_ldloc (mb, bytes_var);
380 mono_mb_emit_byte (mb, MONO_CEE_ADD);
381 mono_mb_emit_stloc (mb, end_var);
382 mono_mb_emit_ldloc (mb, my_entry_var);
383 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
384 mono_mb_emit_byte (mb, MONO_CEE_ADD);
385 mono_mb_emit_stloc (mb, start_var);
389 * } while (start < end);
391 start_loop = mono_mb_get_label (mb);
392 mono_mb_emit_ldloc (mb, start_var);
393 mono_mb_emit_icon (mb, 0);
394 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
395 mono_mb_emit_ldloc (mb, start_var);
396 mono_mb_emit_icon (mb, sizeof (gpointer));
397 mono_mb_emit_byte (mb, MONO_CEE_ADD);
398 mono_mb_emit_stloc (mb, start_var);
400 mono_mb_emit_ldloc (mb, start_var);
401 mono_mb_emit_ldloc (mb, end_var);
402 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
403 // g_print ("distance: %d\n", start_loop - (mono_mb_get_label (mb) + 1));
404 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
405 } else if (atype == ATYPE_FREEPTR_FOR_BOX) {
406 /* need to clear just the sync pointer */
407 mono_mb_emit_ldloc (mb, my_entry_var);
408 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
409 mono_mb_emit_byte (mb, MONO_CEE_ADD);
410 mono_mb_emit_icon (mb, 0);
411 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
414 /* return my_entry; */
415 mono_mb_emit_ldloc (mb, my_entry_var);
416 mono_mb_emit_byte (mb, MONO_CEE_RET);
418 mono_mb_patch_short_branch (mb, no_freelist_branch);
419 if (not_small_enough_branch > 0)
420 mono_mb_patch_short_branch (mb, not_small_enough_branch);
421 /* the slow path: we just call back into the runtime */
422 mono_mb_emit_ldarg (mb, 0);
423 mono_mb_emit_icall (mb, mono_object_new_specific);
425 mono_mb_emit_byte (mb, MONO_CEE_RET);
427 res = mono_mb_create_method (mb, csig, 8);
429 mono_method_get_header (res)->init_locals = FALSE;
433 static MonoMethod* alloc_method_cache [ATYPE_NUM];
434 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
437 * If possible, generate a managed method that can quickly allocate objects in class
438 * @klass. The method will typically have an thread-local inline allocation sequence.
439 * The signature of the called method is:
440 * object allocate (MonoVTable *vtable)
441 * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
443 * The thread local alloc logic is taken from libgc/pthread_support.c.
447 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
451 MonoClass *klass = vtable->klass;
452 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
454 /*g_print ("thread tls: %d\n", offset);*/
457 if (!SMALL_ENOUGH (klass->instance_size))
459 if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
461 if (klass->rank || klass->byval_arg.type == MONO_TYPE_STRING)
463 /* now we have only the simple cases: ptrfree, gcj, non-gcj: we handle only the first as a test */
464 if (!klass->has_references) {
466 atype = ATYPE_FREEPTR_FOR_BOX;
468 atype = ATYPE_FREEPTR;
472 * disabled because we currently do a runtime choice anyway, to
473 * deal with multiple appdomains.
474 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
477 atype = ATYPE_NORMAL;
480 return mono_gc_get_managed_allocator_by_type (atype);
484 * mono_gc_get_managed_allocator_id:
486 * Return a type for the managed allocator method MANAGED_ALLOC which can later be passed
487 * to mono_gc_get_managed_allocator_by_type () to get back this allocator method. This can be
488 * used by the AOT code to encode references to managed allocator methods.
491 mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
496 for (i = 0; i < ATYPE_NUM; ++i) {
497 if (alloc_method_cache [i] == managed_alloc) {
498 mono_loader_unlock ();
502 mono_loader_unlock ();
508 * mono_gc_get_managed_allocator_by_type:
510 * Return a managed allocator method corresponding to allocator type ATYPE.
513 mono_gc_get_managed_allocator_by_type (int atype)
517 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
520 res = alloc_method_cache [atype];
522 res = alloc_method_cache [atype] = create_allocator (atype, offset);
523 mono_loader_unlock ();
530 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
536 mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
542 mono_gc_get_managed_allocator_by_type (int atype)
549 #endif /* no Boehm GC */