2 * boehm-gc.c: GC implementation using either the installed or included Boehm GC.
4 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
5 * Copyright 2004-2011 Novell, Inc (http://www.novell.com)
6 * Copyright 2011-2012 Xamarin, Inc (http://www.xamarin.com)
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #define GC_I_HIDE_POINTERS
15 #include <mono/metadata/gc-internals.h>
16 #include <mono/metadata/mono-gc.h>
17 #include <mono/metadata/profiler-private.h>
18 #include <mono/metadata/class-internals.h>
19 #include <mono/metadata/method-builder.h>
20 #include <mono/metadata/opcodes.h>
21 #include <mono/metadata/domain-internals.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/marshal.h>
24 #include <mono/metadata/runtime.h>
25 #include <mono/metadata/handle.h>
26 #include <mono/metadata/sgen-toggleref.h>
27 #include <mono/metadata/w32handle.h>
28 #include <mono/utils/atomic.h>
29 #include <mono/utils/mono-logger-internals.h>
30 #include <mono/utils/mono-memory-model.h>
31 #include <mono/utils/mono-time.h>
32 #include <mono/utils/mono-threads.h>
33 #include <mono/utils/dtrace.h>
34 #include <mono/utils/gc_wrapper.h>
35 #include <mono/utils/mono-os-mutex.h>
36 #include <mono/utils/mono-counters.h>
37 #include <mono/utils/mono-compiler.h>
43 #define THREAD_LOCAL_ALLOC 1
44 #include "private/pthread_support.h"
46 #if defined(PLATFORM_MACOSX) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
47 void *pthread_get_stackaddr_np(pthread_t);
50 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
51 /*Boehm max heap cannot be smaller than 16MB*/
52 #define MIN_BOEHM_MAX_HEAP_SIZE_IN_MB 16
53 #define MIN_BOEHM_MAX_HEAP_SIZE (MIN_BOEHM_MAX_HEAP_SIZE_IN_MB << 20)
55 static gboolean gc_initialized = FALSE;
56 static mono_mutex_t mono_gc_lock;
59 boehm_thread_register (MonoThreadInfo* info, void *baseptr);
61 boehm_thread_unregister (MonoThreadInfo *p);
63 boehm_thread_detach (MonoThreadInfo *p);
65 register_test_toggleref_callback (void);
67 #define BOEHM_GC_BIT_FINALIZER_AWARE 1
68 static MonoGCFinalizerCallbacks fin_callbacks;
72 static mono_mutex_t handle_section;
73 #define lock_handles(handles) mono_os_mutex_lock (&handle_section)
74 #define unlock_handles(handles) mono_os_mutex_unlock (&handle_section)
81 guint slot_hint : 24; /* starting slot for search in bitmap */
82 /* 2^16 appdomains should be enough for everyone (though I know I'll regret this in 20 years) */
83 /* we alloc this only for weak refs, since we can get the domain directly in the other cases */
87 #define EMPTY_HANDLE_DATA(type) {NULL, NULL, 0, (type), 0, NULL}
89 /* weak and weak-track arrays will be allocated in malloc memory
91 static HandleData gc_handles [] = {
92 EMPTY_HANDLE_DATA (HANDLE_WEAK),
93 EMPTY_HANDLE_DATA (HANDLE_WEAK_TRACK),
94 EMPTY_HANDLE_DATA (HANDLE_NORMAL),
95 EMPTY_HANDLE_DATA (HANDLE_PINNED)
99 mono_gc_warning (char *msg, GC_word arg)
101 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
104 static void on_gc_notification (GC_EventType event);
105 static void on_gc_heap_resize (size_t new_size);
108 mono_gc_base_init (void)
110 MonoThreadInfoCallbacks cb;
117 mono_counters_init ();
120 mono_w32handle_init ();
124 * Handle the case when we are called from a thread different from the main thread,
126 * FIXME: Move this to libgc where it belongs.
128 * we used to do this only when running on valgrind,
129 * but it happens also in other setups.
131 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK) && !defined(__native_client__)
136 pthread_getattr_np (pthread_self (), &attr);
137 pthread_attr_getstack (&attr, &sstart, &size);
138 pthread_attr_destroy (&attr);
139 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
142 * The calculation above doesn't seem to work on ia64, also we need to set
143 * GC_register_stackbottom as well, but don't know how.
146 /* apparently with some linuxthreads implementations sstart can be NULL,
147 * fallback to the more imprecise method (bug# 78096).
150 GC_stackbottom = (char*)sstart + size;
153 gsize stack_bottom = (gsize)&dummy;
154 stack_bottom += 4095;
155 stack_bottom &= ~4095;
156 GC_stackbottom = (char*)stack_bottom;
160 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
161 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
162 #elif defined(__OpenBSD__)
163 # include <pthread_np.h>
168 rslt = pthread_stackseg_np(pthread_self(), &ss);
169 g_assert (rslt == 0);
171 GC_stackbottom = (char*)ss.ss_sp;
173 #elif defined(__native_client__)
174 /* Do nothing, GC_stackbottom is set correctly in libgc */
178 gsize stack_bottom = (gsize)&dummy;
179 stack_bottom += 4095;
180 stack_bottom &= ~4095;
181 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
182 GC_stackbottom = (char*)stack_bottom;
186 #if !defined(PLATFORM_ANDROID)
187 /* If GC_no_dls is set to true, GC_find_limit is not called. This causes a seg fault on Android. */
191 if ((env = g_getenv ("MONO_GC_DEBUG"))) {
192 char **opts = g_strsplit (env, ",", -1);
193 for (char **ptr = opts; ptr && *ptr; ptr ++) {
195 if (!strcmp (opt, "do-not-finalize")) {
196 mono_do_not_finalize = 1;
197 } else if (!strcmp (opt, "log-finalizers")) {
206 GC_set_warn_proc (mono_gc_warning);
207 GC_finalize_on_demand = 1;
208 GC_finalizer_notifier = mono_gc_finalize_notify;
210 GC_init_gcj_malloc (5, NULL);
211 GC_allow_register_threads ();
213 if ((env = g_getenv ("MONO_GC_PARAMS"))) {
214 char **ptr, **opts = g_strsplit (env, ",", -1);
215 for (ptr = opts; *ptr; ++ptr) {
217 if (g_str_has_prefix (opt, "max-heap-size=")) {
220 opt = strchr (opt, '=') + 1;
221 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
222 if (max_heap < MIN_BOEHM_MAX_HEAP_SIZE) {
223 fprintf (stderr, "max-heap-size must be at least %dMb.\n", MIN_BOEHM_MAX_HEAP_SIZE_IN_MB);
226 GC_set_max_heap_size (max_heap);
228 fprintf (stderr, "max-heap-size must be an integer.\n");
232 } else if (g_str_has_prefix (opt, "toggleref-test")) {
233 register_test_toggleref_callback ();
236 /* Could be a parameter for sgen */
238 fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
239 fprintf (stderr, " max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
247 memset (&cb, 0, sizeof (cb));
248 cb.thread_register = boehm_thread_register;
249 cb.thread_unregister = boehm_thread_unregister;
250 cb.thread_detach = boehm_thread_detach;
251 cb.mono_method_is_critical = (gboolean (*)(void *))mono_runtime_is_critical_method;
253 mono_threads_init (&cb, sizeof (MonoThreadInfo));
254 mono_os_mutex_init (&mono_gc_lock);
255 mono_os_mutex_init_recursive (&handle_section);
257 mono_thread_info_attach (&dummy);
259 GC_set_on_collection_event (on_gc_notification);
260 GC_on_heap_resize = on_gc_heap_resize;
262 MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_NORMAL].entries, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
263 MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_PINNED].entries, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
265 gc_initialized = TRUE;
269 mono_gc_base_cleanup (void)
271 GC_finalizer_notifier = NULL;
276 * @generation: GC generation identifier
278 * Perform a garbage collection for the given generation, higher numbers
279 * mean usually older objects. Collecting a high-numbered generation
280 * implies collecting also the lower-numbered generations.
281 * The maximum value for @generation can be retrieved with a call to
282 * mono_gc_max_generation(), so this function is usually called as:
284 * mono_gc_collect (mono_gc_max_generation ());
287 mono_gc_collect (int generation)
289 #ifndef DISABLE_PERFCOUNTERS
290 mono_perfcounters->gc_induced++;
296 * mono_gc_max_generation:
298 * Get the maximum generation number used by the current garbage
299 * collector. The value will be 0 for the Boehm collector, 1 or more
300 * for the generational collectors.
302 * Returns: the maximum generation number.
305 mono_gc_max_generation (void)
311 * mono_gc_get_generation:
312 * @object: a managed object
314 * Get the garbage collector's generation that @object belongs to.
315 * Use this has a hint only.
317 * Returns: a garbage collector generation number
320 mono_gc_get_generation (MonoObject *object)
326 * mono_gc_collection_count:
327 * @generation: a GC generation number
329 * Get how many times a garbage collection has been performed
330 * for the given @generation number.
332 * Returns: the number of garbage collections
335 mono_gc_collection_count (int generation)
341 * mono_gc_add_memory_pressure:
342 * @value: amount of bytes
344 * Adjust the garbage collector's view of how many bytes of memory
345 * are indirectly referenced by managed objects (for example unmanaged
346 * memory holding image or other binary data).
347 * This is a hint only to the garbage collector algorithm.
348 * Note that negative amounts of @value will decrease the memory
352 mono_gc_add_memory_pressure (gint64 value)
357 * mono_gc_get_used_size:
359 * Get the approximate amount of memory used by managed objects.
361 * Returns: the amount of memory used in bytes
364 mono_gc_get_used_size (void)
366 return GC_get_heap_size () - GC_get_free_bytes ();
370 * mono_gc_get_heap_size:
372 * Get the amount of memory used by the garbage collector.
374 * Returns: the size of the heap in bytes
377 mono_gc_get_heap_size (void)
379 return GC_get_heap_size ();
383 mono_gc_is_gc_thread (void)
385 return GC_thread_is_registered ();
389 mono_gc_register_thread (void *baseptr)
391 return mono_thread_info_attach (baseptr) != NULL;
395 boehm_thread_register (MonoThreadInfo* info, void *baseptr)
397 struct GC_stack_base sb;
400 /* TODO: use GC_get_stack_base instead of baseptr. */
401 sb.mem_base = baseptr;
402 res = GC_register_my_thread (&sb);
403 if (res == GC_UNIMPLEMENTED)
404 return NULL; /* Cannot happen with GC v7+. */
406 info->handle_stack = mono_handle_stack_alloc ();
412 boehm_thread_unregister (MonoThreadInfo *p)
414 MonoNativeThreadId tid;
416 tid = mono_thread_info_get_tid (p);
418 if (p->runtime_thread)
419 mono_threads_add_joinable_thread ((gpointer)tid);
423 boehm_thread_detach (MonoThreadInfo *p)
425 if (mono_thread_internal_current_is_attached ())
426 mono_thread_detach_internal (mono_thread_internal_current ());
430 mono_object_is_alive (MonoObject* o)
432 return GC_is_marked ((ptr_t)o);
436 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
441 static gint64 gc_start_time;
444 on_gc_notification (GC_EventType event)
446 MonoGCEvent e = (MonoGCEvent)event;
449 case MONO_GC_EVENT_PRE_STOP_WORLD:
450 MONO_GC_WORLD_STOP_BEGIN ();
453 case MONO_GC_EVENT_POST_STOP_WORLD:
454 MONO_GC_WORLD_STOP_END ();
457 case MONO_GC_EVENT_PRE_START_WORLD:
458 MONO_GC_WORLD_RESTART_BEGIN (1);
461 case MONO_GC_EVENT_POST_START_WORLD:
462 MONO_GC_WORLD_RESTART_END (1);
465 case MONO_GC_EVENT_START:
467 #ifndef DISABLE_PERFCOUNTERS
468 if (mono_perfcounters)
469 mono_perfcounters->gc_collections0++;
471 gc_stats.major_gc_count ++;
472 gc_start_time = mono_100ns_ticks ();
475 case MONO_GC_EVENT_END:
477 #if defined(ENABLE_DTRACE) && defined(__sun__)
478 /* This works around a dtrace -G problem on Solaris.
479 Limit its actual use to when the probe is enabled. */
480 if (MONO_GC_END_ENABLED ())
484 #ifndef DISABLE_PERFCOUNTERS
485 if (mono_perfcounters) {
486 guint64 heap_size = GC_get_heap_size ();
487 guint64 used_size = heap_size - GC_get_free_bytes ();
488 mono_perfcounters->gc_total_bytes = used_size;
489 mono_perfcounters->gc_committed_bytes = heap_size;
490 mono_perfcounters->gc_reserved_bytes = heap_size;
491 mono_perfcounters->gc_gen0size = heap_size;
494 gc_stats.major_gc_time += mono_100ns_ticks () - gc_start_time;
495 mono_trace_message (MONO_TRACE_GC, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time) / 10);
501 mono_profiler_gc_event (e, 0);
504 case MONO_GC_EVENT_PRE_STOP_WORLD:
505 mono_thread_info_suspend_lock ();
506 mono_profiler_gc_event (MONO_GC_EVENT_PRE_STOP_WORLD_LOCKED, 0);
508 case MONO_GC_EVENT_POST_START_WORLD:
509 mono_thread_info_suspend_unlock ();
510 mono_profiler_gc_event (MONO_GC_EVENT_POST_START_WORLD_UNLOCKED, 0);
519 on_gc_heap_resize (size_t new_size)
521 guint64 heap_size = GC_get_heap_size ();
522 #ifndef DISABLE_PERFCOUNTERS
523 if (mono_perfcounters) {
524 mono_perfcounters->gc_committed_bytes = heap_size;
525 mono_perfcounters->gc_reserved_bytes = heap_size;
526 mono_perfcounters->gc_gen0size = heap_size;
529 mono_profiler_gc_heap_resize (new_size);
533 mono_gc_register_root (char *start, size_t size, void *descr, MonoGCRootSource source, const char *msg)
535 /* for some strange reason, they want one extra byte on the end */
536 GC_add_roots (start, start + size + 1);
542 mono_gc_deregister_root (char* addr)
545 /* FIXME: libgc doesn't define this work win32 for some reason */
546 /* FIXME: No size info */
547 GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
552 mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
554 /* libgc requires that we use HIDE_POINTER... */
555 *link_addr = (void*)HIDE_POINTER (obj);
557 GC_REGISTER_LONG_LINK (link_addr, obj);
559 GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr, obj);
563 mono_gc_weak_link_remove (void **link_addr, gboolean track)
566 GC_unregister_long_link (link_addr);
568 GC_unregister_disappearing_link (link_addr);
573 reveal_link (gpointer link_addr)
575 void **link_a = (void **)link_addr;
576 return REVEAL_POINTER (*link_a);
580 mono_gc_weak_link_get (void **link_addr)
582 MonoObject *obj = (MonoObject *)GC_call_with_alloc_lock (reveal_link, link_addr);
583 if (obj == (MonoObject *) -1)
589 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
591 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
595 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
597 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
601 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
603 /* libgc has no usable support for arrays... */
604 return GC_NO_DESCRIPTOR;
608 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
610 /* It seems there are issues when the bitmap doesn't fit: play it safe */
612 return GC_NO_DESCRIPTOR;
614 return (gpointer)GC_make_descriptor ((GC_bitmap)bitmap, numbits);
618 mono_gc_make_root_descr_all_refs (int numbits)
624 mono_gc_alloc_fixed (size_t size, void *descr, MonoGCRootSource source, const char *msg)
626 /* To help track down typed allocation bugs */
630 if (count == atoi (g_getenv ("COUNT2")))
632 if (count > atoi (g_getenv ("COUNT2")))
633 return GC_MALLOC (size);
637 return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
639 return GC_MALLOC (size);
643 mono_gc_free_fixed (void* addr)
648 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
652 if (!vtable->klass->has_references) {
653 obj = (MonoObject *)GC_MALLOC_ATOMIC (size);
654 if (G_UNLIKELY (!obj))
657 obj->vtable = vtable;
658 obj->synchronisation = NULL;
660 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
661 } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
662 obj = (MonoObject *)GC_GCJ_MALLOC (size, vtable);
663 if (G_UNLIKELY (!obj))
666 obj = (MonoObject *)GC_MALLOC (size);
667 if (G_UNLIKELY (!obj))
670 obj->vtable = vtable;
673 if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_ALLOCATIONS))
674 mono_profiler_allocation (obj);
680 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
684 if (!vtable->klass->has_references) {
685 obj = (MonoArray *)GC_MALLOC_ATOMIC (size);
686 if (G_UNLIKELY (!obj))
689 obj->obj.vtable = vtable;
690 obj->obj.synchronisation = NULL;
692 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
693 } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
694 obj = (MonoArray *)GC_GCJ_MALLOC (size, vtable);
695 if (G_UNLIKELY (!obj))
698 obj = (MonoArray *)GC_MALLOC (size);
699 if (G_UNLIKELY (!obj))
702 obj->obj.vtable = vtable;
705 obj->max_length = max_length;
707 if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_ALLOCATIONS))
708 mono_profiler_allocation (&obj->obj);
714 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
718 if (!vtable->klass->has_references) {
719 obj = (MonoArray *)GC_MALLOC_ATOMIC (size);
720 if (G_UNLIKELY (!obj))
723 obj->obj.vtable = vtable;
724 obj->obj.synchronisation = NULL;
726 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
727 } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
728 obj = (MonoArray *)GC_GCJ_MALLOC (size, vtable);
729 if (G_UNLIKELY (!obj))
732 obj = (MonoArray *)GC_MALLOC (size);
733 if (G_UNLIKELY (!obj))
736 obj->obj.vtable = vtable;
739 obj->max_length = max_length;
742 obj->bounds = (MonoArrayBounds *) ((char *) obj + size - bounds_size);
744 if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_ALLOCATIONS))
745 mono_profiler_allocation (&obj->obj);
751 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
753 MonoString *obj = (MonoString *)GC_MALLOC_ATOMIC (size);
754 if (G_UNLIKELY (!obj))
757 obj->object.vtable = vtable;
758 obj->object.synchronisation = NULL;
760 obj->chars [len] = 0;
762 if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_ALLOCATIONS))
763 mono_profiler_allocation (&obj->object);
769 mono_gc_alloc_mature (MonoVTable *vtable, size_t size)
771 return mono_gc_alloc_obj (vtable, size);
775 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
777 return mono_gc_alloc_obj (vtable, size);
781 mono_gc_invoke_finalizers (void)
783 /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
784 * the 'mem_freed' variable is not initialized when there are no
785 * objects to finalize, which leads to strange behavior later on.
786 * The check is necessary to work around that bug.
788 if (GC_should_invoke_finalizers ())
789 return GC_invoke_finalizers ();
794 mono_gc_pending_finalizers (void)
796 return GC_should_invoke_finalizers ();
800 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
802 *(void**)field_ptr = value;
806 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
808 *(void**)slot_ptr = value;
812 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
814 mono_gc_memmove_aligned (dest_ptr, src_ptr, count * sizeof (gpointer));
818 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
820 *(void**)ptr = value;
824 mono_gc_wbarrier_generic_store_atomic (gpointer ptr, MonoObject *value)
826 InterlockedWritePointer ((volatile gpointer *)ptr, value);
830 mono_gc_wbarrier_generic_nostore (gpointer ptr)
835 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
837 mono_gc_memmove_atomic (dest, src, count * mono_class_value_size (klass, NULL));
841 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
843 /* do not copy the sync state */
844 mono_gc_memmove_aligned ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
845 mono_object_class (obj)->instance_size - sizeof (MonoObject));
849 mono_gc_clear_domain (MonoDomain *domain)
854 mono_gc_suspend_finalizers (void)
859 mono_gc_get_suspend_signal (void)
861 return GC_get_suspend_signal ();
865 mono_gc_get_restart_signal (void)
867 return GC_get_thr_restart_signal ();
870 #if defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
871 extern __thread MONO_TLS_FAST void* GC_thread_tls;
872 #include "metadata-internals.h"
878 while (!(v & (1 << i)))
885 ATYPE_FREEPTR_FOR_BOX,
893 create_allocator (int atype, int tls_key, gboolean slowpath)
895 int index_var, bytes_var, my_fl_var, my_entry_var;
896 guint32 no_freelist_branch, not_small_enough_branch = 0;
897 guint32 size_overflow_branch = 0;
898 MonoMethodBuilder *mb;
900 MonoMethodSignature *csig;
901 const char *name = NULL;
904 if (atype == ATYPE_FREEPTR) {
905 name = slowpath ? "SlowAllocPtrfree" : "AllocPtrfree";
906 } else if (atype == ATYPE_FREEPTR_FOR_BOX) {
907 name = slowpath ? "SlowAllocPtrfreeBox" : "AllocPtrfreeBox";
908 } else if (atype == ATYPE_NORMAL) {
909 name = slowpath ? "SlowAlloc" : "Alloc";
910 } else if (atype == ATYPE_GCJ) {
911 name = slowpath ? "SlowAllocGcj" : "AllocGcj";
912 } else if (atype == ATYPE_STRING) {
913 name = slowpath ? "SlowAllocString" : "AllocString";
915 g_assert_not_reached ();
918 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
920 if (atype == ATYPE_STRING) {
921 csig->ret = &mono_defaults.string_class->byval_arg;
922 csig->params [0] = &mono_defaults.int_class->byval_arg;
923 csig->params [1] = &mono_defaults.int32_class->byval_arg;
925 csig->ret = &mono_defaults.object_class->byval_arg;
926 csig->params [0] = &mono_defaults.int_class->byval_arg;
927 csig->params [1] = &mono_defaults.int32_class->byval_arg;
930 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
933 goto always_slowpath;
935 bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
936 if (atype == ATYPE_STRING) {
937 /* a string alloator method takes the args: (vtable, len) */
938 /* bytes = (offsetof (MonoString, chars) + ((len + 1) * 2)); */
939 mono_mb_emit_ldarg (mb, 1);
940 mono_mb_emit_icon (mb, 1);
941 mono_mb_emit_byte (mb, MONO_CEE_ADD);
942 mono_mb_emit_icon (mb, 1);
943 mono_mb_emit_byte (mb, MONO_CEE_SHL);
944 // sizeof (MonoString) might include padding
945 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, chars));
946 mono_mb_emit_byte (mb, MONO_CEE_ADD);
947 mono_mb_emit_stloc (mb, bytes_var);
949 mono_mb_emit_ldarg (mb, 1);
950 mono_mb_emit_stloc (mb, bytes_var);
953 /* this is needed for strings/arrays only as the other big types are never allocated with this method */
954 if (atype == ATYPE_STRING) {
956 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
957 mono_mb_emit_ldloc (mb, bytes_var);
958 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
959 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
960 /* check for overflow */
961 mono_mb_emit_ldloc (mb, bytes_var);
962 mono_mb_emit_icon (mb, sizeof (MonoString));
963 size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
966 /* int index = INDEX_FROM_BYTES(bytes); */
967 index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
969 mono_mb_emit_ldloc (mb, bytes_var);
970 mono_mb_emit_icon (mb, GRANULARITY - 1);
971 mono_mb_emit_byte (mb, MONO_CEE_ADD);
972 mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
973 mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
974 mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
975 mono_mb_emit_byte (mb, MONO_CEE_SHL);
976 /* index var is already adjusted into bytes */
977 mono_mb_emit_stloc (mb, index_var);
979 my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
980 my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
981 /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
982 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
983 mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
984 mono_mb_emit_i4 (mb, tls_key);
985 if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
986 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
987 + G_STRUCT_OFFSET (struct thread_local_freelists,
989 else if (atype == ATYPE_NORMAL)
990 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
991 + G_STRUCT_OFFSET (struct thread_local_freelists,
993 else if (atype == ATYPE_GCJ)
994 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
995 + G_STRUCT_OFFSET (struct thread_local_freelists,
998 g_assert_not_reached ();
999 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1000 mono_mb_emit_ldloc (mb, index_var);
1001 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1002 mono_mb_emit_stloc (mb, my_fl_var);
1004 /* my_entry = *my_fl; */
1005 mono_mb_emit_ldloc (mb, my_fl_var);
1006 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
1007 mono_mb_emit_stloc (mb, my_entry_var);
1009 /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
1010 mono_mb_emit_ldloc (mb, my_entry_var);
1011 mono_mb_emit_icon (mb, HBLKSIZE);
1012 no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
1014 /* ptr_t next = obj_link(my_entry); *my_fl = next; */
1015 mono_mb_emit_ldloc (mb, my_fl_var);
1016 mono_mb_emit_ldloc (mb, my_entry_var);
1017 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
1018 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
1020 /* set the vtable and clear the words in the object */
1021 mono_mb_emit_ldloc (mb, my_entry_var);
1022 mono_mb_emit_ldarg (mb, 0);
1023 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
1025 if (atype == ATYPE_FREEPTR) {
1026 int start_var, end_var, start_loop;
1027 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
1029 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
1030 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
1031 mono_mb_emit_ldloc (mb, my_entry_var);
1032 mono_mb_emit_ldloc (mb, bytes_var);
1033 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1034 mono_mb_emit_stloc (mb, end_var);
1035 mono_mb_emit_ldloc (mb, my_entry_var);
1036 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
1037 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1038 mono_mb_emit_stloc (mb, start_var);
1042 * } while (start < end);
1044 start_loop = mono_mb_get_label (mb);
1045 mono_mb_emit_ldloc (mb, start_var);
1046 mono_mb_emit_icon (mb, 0);
1047 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
1048 mono_mb_emit_ldloc (mb, start_var);
1049 mono_mb_emit_icon (mb, sizeof (gpointer));
1050 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1051 mono_mb_emit_stloc (mb, start_var);
1053 mono_mb_emit_ldloc (mb, start_var);
1054 mono_mb_emit_ldloc (mb, end_var);
1055 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
1056 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
1057 } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
1058 /* need to clear just the sync pointer */
1059 mono_mb_emit_ldloc (mb, my_entry_var);
1060 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
1061 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1062 mono_mb_emit_icon (mb, 0);
1063 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
1066 if (atype == ATYPE_STRING) {
1067 /* need to set length and clear the last char */
1068 /* s->length = len; */
1069 mono_mb_emit_ldloc (mb, my_entry_var);
1070 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
1071 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1072 mono_mb_emit_ldarg (mb, 1);
1073 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
1074 /* s->chars [len] = 0; */
1075 mono_mb_emit_ldloc (mb, my_entry_var);
1076 mono_mb_emit_ldloc (mb, bytes_var);
1077 mono_mb_emit_icon (mb, 2);
1078 mono_mb_emit_byte (mb, MONO_CEE_SUB);
1079 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1080 mono_mb_emit_icon (mb, 0);
1081 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
1084 /* return my_entry; */
1085 mono_mb_emit_ldloc (mb, my_entry_var);
1086 mono_mb_emit_byte (mb, MONO_CEE_RET);
1088 mono_mb_patch_short_branch (mb, no_freelist_branch);
1089 if (not_small_enough_branch > 0)
1090 mono_mb_patch_short_branch (mb, not_small_enough_branch);
1091 if (size_overflow_branch > 0)
1092 mono_mb_patch_short_branch (mb, size_overflow_branch);
1094 /* the slow path: we just call back into the runtime */
1096 if (atype == ATYPE_STRING) {
1097 mono_mb_emit_ldarg (mb, 1);
1098 mono_mb_emit_icall (mb, ves_icall_string_alloc);
1100 mono_mb_emit_ldarg (mb, 0);
1101 mono_mb_emit_icall (mb, ves_icall_object_new_specific);
1104 mono_mb_emit_byte (mb, MONO_CEE_RET);
1106 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1107 info->d.alloc.gc_name = "boehm";
1108 info->d.alloc.alloc_type = atype;
1109 mb->init_locals = FALSE;
1111 res = mono_mb_create (mb, csig, 8, info);
1117 static MonoMethod* alloc_method_cache [ATYPE_NUM];
1118 static MonoMethod* slowpath_alloc_method_cache [ATYPE_NUM];
1121 mono_gc_is_critical_method (MonoMethod *method)
1125 for (i = 0; i < ATYPE_NUM; ++i)
1126 if (method == alloc_method_cache [i] || method == slowpath_alloc_method_cache [i])
1133 * If possible, generate a managed method that can quickly allocate objects in class
1134 * @klass. The method will typically have an thread-local inline allocation sequence.
1135 * The signature of the called method is:
1136 * object allocate (MonoVTable *vtable)
1137 * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
1139 * The thread local alloc logic is taken from libgc/pthread_support.c.
1143 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box, gboolean known_instance_size)
1147 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1149 /*g_print ("thread tls: %d\n", offset);*/
1152 if (!SMALL_ENOUGH (klass->instance_size))
1154 if (mono_class_has_finalizer (klass) || mono_class_is_marshalbyref (klass))
1156 if (mono_profiler_get_events () & (MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_STATISTICAL))
1160 if (mono_class_is_open_constructed_type (&klass->byval_arg))
1162 if (klass->byval_arg.type == MONO_TYPE_STRING) {
1163 atype = ATYPE_STRING;
1164 } else if (!known_instance_size) {
1166 } else if (!klass->has_references) {
1168 atype = ATYPE_FREEPTR_FOR_BOX;
1170 atype = ATYPE_FREEPTR;
1174 * disabled because we currently do a runtime choice anyway, to
1175 * deal with multiple appdomains.
1176 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
1179 atype = ATYPE_NORMAL;
1182 return mono_gc_get_managed_allocator_by_type (atype, MANAGED_ALLOCATOR_REGULAR);
1186 mono_gc_get_managed_array_allocator (MonoClass *klass)
1192 * mono_gc_get_managed_allocator_by_type:
1194 * Return a managed allocator method corresponding to allocator type ATYPE.
1197 mono_gc_get_managed_allocator_by_type (int atype, ManagedAllocatorVariant variant)
1201 gboolean slowpath = variant != MANAGED_ALLOCATOR_REGULAR;
1202 MonoMethod **cache = slowpath ? slowpath_alloc_method_cache : alloc_method_cache;
1203 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1205 mono_tls_key_set_offset (TLS_KEY_BOEHM_GC_THREAD, offset);
1207 res = cache [atype];
1211 res = create_allocator (atype, TLS_KEY_BOEHM_GC_THREAD, slowpath);
1212 mono_os_mutex_lock (&mono_gc_lock);
1213 if (cache [atype]) {
1214 mono_free_method (res);
1215 res = cache [atype];
1217 mono_memory_barrier ();
1218 cache [atype] = res;
1220 mono_os_mutex_unlock (&mono_gc_lock);
1225 mono_gc_get_managed_allocator_types (void)
1231 mono_gc_get_write_barrier (void)
1233 g_assert_not_reached ();
1240 mono_gc_is_critical_method (MonoMethod *method)
1246 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box, gboolean known_instance_size)
1252 mono_gc_get_managed_array_allocator (MonoClass *klass)
1258 mono_gc_get_managed_allocator_by_type (int atype, ManagedAllocatorVariant variant)
1264 mono_gc_get_managed_allocator_types (void)
1270 mono_gc_get_write_barrier (void)
1272 g_assert_not_reached ();
1279 mono_gc_get_specific_write_barrier (gboolean is_concurrent)
1281 g_assert_not_reached ();
1286 mono_gc_get_aligned_size_for_allocator (int size)
1292 mono_gc_get_gc_name (void)
1298 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
1300 return GC_call_with_alloc_lock (func, data);
1304 mono_gc_get_description (void)
1306 return g_strdup (DEFAULT_GC_NAME);
1310 mono_gc_set_desktop_mode (void)
1316 mono_gc_is_moving (void)
1322 mono_gc_is_disabled (void)
1324 if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
1331 mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
1333 g_assert_not_reached ();
1338 mono_gc_get_card_table (int *shift_bits, gpointer *card_mask)
1340 g_assert_not_reached ();
1345 mono_gc_card_table_nursery_check (void)
1347 g_assert_not_reached ();
1352 mono_gc_get_nursery (int *shift_bits, size_t *size)
1358 mono_gc_precise_stack_mark_enabled (void)
1364 mono_gc_get_logfile (void)
1370 mono_gc_params_set (const char* options)
1375 mono_gc_debug_set (const char* options)
1380 mono_gc_conservatively_scan_area (void *start, void *end)
1382 g_assert_not_reached ();
1386 mono_gc_scan_object (void *obj, void *gc_data)
1388 g_assert_not_reached ();
1393 mono_gc_get_bitmap_for_descr (void *descr, int *numbits)
1395 g_assert_not_reached ();
1400 mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks)
1405 mono_gc_set_stack_end (void *stack_end)
1409 void mono_gc_set_skip_thread (gboolean value)
1414 mono_gc_register_for_finalization (MonoObject *obj, void *user_data)
1419 /* This assertion is not valid when GC_DEBUG is defined */
1420 g_assert (GC_base (obj) == (char*)obj - offset);
1423 GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, (GC_finalization_proc)user_data, GUINT_TO_POINTER (offset), NULL, NULL);
1428 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
1430 /* it is being replaced by GC_pthread_create on some
1431 * platforms, see libgc/include/gc_pthread_redirects.h */
1432 return pthread_create (new_thread, attr, start_routine, arg);
1437 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved)
1439 return GC_DllMain (module_handle, reason, reserved);
1444 mono_gc_get_vtable_bits (MonoClass *klass)
1446 if (fin_callbacks.is_class_finalization_aware) {
1447 if (fin_callbacks.is_class_finalization_aware (klass))
1448 return BOEHM_GC_BIT_FINALIZER_AWARE;
1454 * mono_gc_register_altstack:
1456 * Register the dimensions of the normal stack and altstack with the collector.
1457 * Currently, STACK/STACK_SIZE is only used when the thread is suspended while it is on an altstack.
1460 mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size)
1462 GC_register_altstack (stack, stack_size, altstack, altstack_size);
1466 mono_gc_get_los_limit (void)
1472 mono_gc_set_string_length (MonoString *str, gint32 new_length)
1474 mono_unichar2 *new_end = str->chars + new_length;
1476 /* zero the discarded string. This null-delimits the string and allows
1477 * the space to be reclaimed by SGen. */
1479 memset (new_end, 0, (str->length - new_length + 1) * sizeof (mono_unichar2));
1480 str->length = new_length;
1484 mono_gc_user_markers_supported (void)
1490 mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker)
1492 g_assert_not_reached ();
1496 /* Toggleref support */
1499 mono_gc_toggleref_add (MonoObject *object, mono_bool strong_ref)
1501 if (GC_toggleref_add ((GC_PTR)object, (int)strong_ref) != GC_SUCCESS)
1502 g_error ("GC_toggleref_add failed\n");
1506 mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (MonoObject *obj))
1508 GC_set_toggleref_func ((GC_ToggleRefStatus (*) (GC_PTR obj)) proccess_toggleref);
1511 /* Test support code */
1513 static MonoToggleRefStatus
1514 test_toggleref_callback (MonoObject *obj)
1516 static MonoClassField *mono_toggleref_test_field;
1517 MonoToggleRefStatus status = MONO_TOGGLE_REF_DROP;
1519 if (!mono_toggleref_test_field) {
1520 mono_toggleref_test_field = mono_class_get_field_from_name (mono_object_get_class (obj), "__test");
1521 g_assert (mono_toggleref_test_field);
1524 mono_field_get_value (obj, mono_toggleref_test_field, &status);
1525 printf ("toggleref-cb obj %d\n", status);
1530 register_test_toggleref_callback (void)
1532 mono_gc_toggleref_register_callback (test_toggleref_callback);
1536 is_finalization_aware (MonoObject *obj)
1538 MonoVTable *vt = obj->vtable;
1539 return (vt->gc_bits & BOEHM_GC_BIT_FINALIZER_AWARE) == BOEHM_GC_BIT_FINALIZER_AWARE;
1543 fin_notifier (MonoObject *obj)
1545 if (is_finalization_aware (obj))
1546 fin_callbacks.object_queued_for_finalization (obj);
1550 mono_gc_register_finalizer_callbacks (MonoGCFinalizerCallbacks *callbacks)
1552 if (callbacks->version != MONO_GC_FINALIZER_EXTENSION_VERSION)
1553 g_error ("Invalid finalizer callback version. Expected %d but got %d\n", MONO_GC_FINALIZER_EXTENSION_VERSION, callbacks->version);
1555 fin_callbacks = *callbacks;
1557 GC_set_await_finalize_proc ((void (*) (GC_PTR))fin_notifier);
1560 #define BITMAP_SIZE (sizeof (*((HandleData *)NULL)->bitmap) * CHAR_BIT)
1562 static inline gboolean
1563 slot_occupied (HandleData *handles, guint slot) {
1564 return handles->bitmap [slot / BITMAP_SIZE] & (1 << (slot % BITMAP_SIZE));
1568 vacate_slot (HandleData *handles, guint slot) {
1569 handles->bitmap [slot / BITMAP_SIZE] &= ~(1 << (slot % BITMAP_SIZE));
1573 occupy_slot (HandleData *handles, guint slot) {
1574 handles->bitmap [slot / BITMAP_SIZE] |= 1 << (slot % BITMAP_SIZE);
1578 find_first_unset (guint32 bitmap)
1581 for (i = 0; i < 32; ++i) {
1582 if (!(bitmap & (1 << i)))
1589 handle_data_alloc_entries (HandleData *handles)
1592 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1593 handles->entries = (void **)g_malloc0 (sizeof (*handles->entries) * handles->size);
1594 handles->domain_ids = (guint16 *)g_malloc0 (sizeof (*handles->domain_ids) * handles->size);
1596 handles->entries = (void **)mono_gc_alloc_fixed (sizeof (*handles->entries) * handles->size, NULL, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
1598 handles->bitmap = (guint32 *)g_malloc0 (handles->size / CHAR_BIT);
1602 handle_data_next_unset (HandleData *handles)
1605 for (slot = handles->slot_hint; slot < handles->size / BITMAP_SIZE; ++slot) {
1606 if (handles->bitmap [slot] == 0xffffffff)
1608 handles->slot_hint = slot;
1609 return find_first_unset (handles->bitmap [slot]);
1615 handle_data_first_unset (HandleData *handles)
1618 for (slot = 0; slot < handles->slot_hint; ++slot) {
1619 if (handles->bitmap [slot] == 0xffffffff)
1621 handles->slot_hint = slot;
1622 return find_first_unset (handles->bitmap [slot]);
1627 /* Returns the index of the current slot in the bitmap. */
1629 handle_data_grow (HandleData *handles, gboolean track)
1631 guint32 *new_bitmap;
1632 guint32 new_size = handles->size * 2; /* always double: we memset to 0 based on this below */
1634 /* resize and copy the bitmap */
1635 new_bitmap = (guint32 *)g_malloc0 (new_size / CHAR_BIT);
1636 memcpy (new_bitmap, handles->bitmap, handles->size / CHAR_BIT);
1637 g_free (handles->bitmap);
1638 handles->bitmap = new_bitmap;
1640 /* resize and copy the entries */
1641 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1643 guint16 *domain_ids;
1645 domain_ids = (guint16 *)g_malloc0 (sizeof (*handles->domain_ids) * new_size);
1646 entries = (void **)g_malloc0 (sizeof (*handles->entries) * new_size);
1647 memcpy (domain_ids, handles->domain_ids, sizeof (*handles->domain_ids) * handles->size);
1648 for (i = 0; i < handles->size; ++i) {
1649 MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
1651 mono_gc_weak_link_add (&(entries [i]), obj, track);
1652 mono_gc_weak_link_remove (&(handles->entries [i]), track);
1654 g_assert (!handles->entries [i]);
1657 g_free (handles->entries);
1658 g_free (handles->domain_ids);
1659 handles->entries = entries;
1660 handles->domain_ids = domain_ids;
1663 entries = (void **)mono_gc_alloc_fixed (sizeof (*handles->entries) * new_size, NULL, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
1664 mono_gc_memmove_aligned (entries, handles->entries, sizeof (*handles->entries) * handles->size);
1665 mono_gc_free_fixed (handles->entries);
1666 handles->entries = entries;
1668 handles->slot_hint = handles->size / BITMAP_SIZE;
1669 handles->size = new_size;
1673 alloc_handle (HandleData *handles, MonoObject *obj, gboolean track)
1677 lock_handles (handles);
1679 handle_data_alloc_entries (handles);
1680 i = handle_data_next_unset (handles);
1681 if (i == -1 && handles->slot_hint != 0)
1682 i = handle_data_first_unset (handles);
1684 handle_data_grow (handles, track);
1687 slot = handles->slot_hint * BITMAP_SIZE + i;
1688 occupy_slot (handles, slot);
1689 handles->entries [slot] = NULL;
1690 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1691 /*FIXME, what to use when obj == null?*/
1692 handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
1694 mono_gc_weak_link_add (&(handles->entries [slot]), obj, track);
1696 handles->entries [slot] = obj;
1699 #ifndef DISABLE_PERFCOUNTERS
1700 mono_perfcounters->gc_num_handles++;
1702 unlock_handles (handles);
1703 res = MONO_GC_HANDLE (slot, handles->type);
1704 mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_CREATED, handles->type, res, obj);
1709 * mono_gchandle_new:
1710 * @obj: managed object to get a handle for
1711 * @pinned: whether the object should be pinned
1713 * This returns a handle that wraps the object, this is used to keep a
1714 * reference to a managed object from the unmanaged world and preventing the
1715 * object from being disposed.
1717 * If @pinned is false the address of the object can not be obtained, if it is
1718 * true the address of the object can be obtained. This will also pin the
1719 * object so it will not be possible by a moving garbage collector to move the
1722 * Returns: a handle that can be used to access the object from
1726 mono_gchandle_new (MonoObject *obj, gboolean pinned)
1728 return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj, FALSE);
1732 * mono_gchandle_new_weakref:
1733 * @obj: managed object to get a handle for
1734 * @track_resurrection: Determines how long to track the object, if this is set to TRUE, the object is tracked after finalization, if FALSE, the object is only tracked up until the point of finalization.
1736 * This returns a weak handle that wraps the object, this is used to
1737 * keep a reference to a managed object from the unmanaged world.
1738 * Unlike the mono_gchandle_new the object can be reclaimed by the
1739 * garbage collector. In this case the value of the GCHandle will be
1742 * If @track_resurrection is TRUE the object will be tracked through
1743 * finalization and if the object is resurrected during the execution
1744 * of the finalizer, then the returned weakref will continue to hold
1745 * a reference to the object. If @track_resurrection is FALSE, then
1746 * the weak reference's target will become NULL as soon as the object
1747 * is passed on to the finalizer.
1749 * Returns: a handle that can be used to access the object from
1753 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
1755 return alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj, track_resurrection);
1759 * mono_gchandle_get_target:
1760 * @gchandle: a GCHandle's handle.
1762 * The handle was previously created by calling `mono_gchandle_new` or
1763 * `mono_gchandle_new_weakref`.
1765 * Returns: A pointer to the `MonoObject*` represented by the handle or
1766 * NULL for a collected object if using a weakref handle.
1769 mono_gchandle_get_target (guint32 gchandle)
1771 guint slot = MONO_GC_HANDLE_SLOT (gchandle);
1772 guint type = MONO_GC_HANDLE_TYPE (gchandle);
1773 HandleData *handles = &gc_handles [type];
1774 MonoObject *obj = NULL;
1775 if (type >= HANDLE_TYPE_MAX)
1778 lock_handles (handles);
1779 if (slot < handles->size && slot_occupied (handles, slot)) {
1780 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1781 obj = mono_gc_weak_link_get (&handles->entries [slot]);
1783 obj = (MonoObject *)handles->entries [slot];
1786 /* print a warning? */
1788 unlock_handles (handles);
1789 /*g_print ("get target of entry %d of type %d: %p\n", slot, handles->type, obj);*/
1794 mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
1796 guint slot = MONO_GC_HANDLE_SLOT (gchandle);
1797 guint type = MONO_GC_HANDLE_TYPE (gchandle);
1798 HandleData *handles = &gc_handles [type];
1799 MonoObject *old_obj = NULL;
1801 g_assert (type < HANDLE_TYPE_MAX);
1802 lock_handles (handles);
1803 if (slot < handles->size && slot_occupied (handles, slot)) {
1804 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1805 old_obj = (MonoObject *)handles->entries [slot];
1806 if (handles->entries [slot])
1807 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
1809 mono_gc_weak_link_add (&handles->entries [slot], obj, handles->type == HANDLE_WEAK_TRACK);
1810 /*FIXME, what to use when obj == null?*/
1811 handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
1813 handles->entries [slot] = obj;
1816 /* print a warning? */
1818 /*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
1819 unlock_handles (handles);
1823 mono_gc_is_null (void)
1829 * mono_gchandle_is_in_domain:
1830 * @gchandle: a GCHandle's handle.
1831 * @domain: An application domain.
1833 * Use this function to determine if the @gchandle points to an
1834 * object allocated in the specified @domain.
1836 * Returns: TRUE if the object wrapped by the @gchandle belongs to the specific @domain.
1839 mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
1841 guint slot = MONO_GC_HANDLE_SLOT (gchandle);
1842 guint type = MONO_GC_HANDLE_TYPE (gchandle);
1843 HandleData *handles = &gc_handles [type];
1844 gboolean result = FALSE;
1846 if (type >= HANDLE_TYPE_MAX)
1849 lock_handles (handles);
1850 if (slot < handles->size && slot_occupied (handles, slot)) {
1851 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1852 result = domain->domain_id == handles->domain_ids [slot];
1855 obj = (MonoObject *)handles->entries [slot];
1859 result = domain == mono_object_domain (obj);
1862 /* print a warning? */
1864 unlock_handles (handles);
1869 * mono_gchandle_free:
1870 * @gchandle: a GCHandle's handle.
1872 * Frees the @gchandle handle. If there are no outstanding
1873 * references, the garbage collector can reclaim the memory of the
1877 mono_gchandle_free (guint32 gchandle)
1879 guint slot = MONO_GC_HANDLE_SLOT (gchandle);
1880 guint type = MONO_GC_HANDLE_TYPE (gchandle);
1881 HandleData *handles = &gc_handles [type];
1882 if (type >= HANDLE_TYPE_MAX)
1885 lock_handles (handles);
1886 if (slot < handles->size && slot_occupied (handles, slot)) {
1887 if (MONO_GC_HANDLE_TYPE_IS_WEAK (handles->type)) {
1888 if (handles->entries [slot])
1889 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
1891 handles->entries [slot] = NULL;
1893 vacate_slot (handles, slot);
1895 /* print a warning? */
1897 #ifndef DISABLE_PERFCOUNTERS
1898 mono_perfcounters->gc_num_handles--;
1900 /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
1901 unlock_handles (handles);
1902 mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_DESTROYED, handles->type, gchandle, NULL);
1906 * mono_gchandle_free_domain:
1907 * @domain: domain that is unloading
1909 * Function used internally to cleanup any GC handle for objects belonging
1910 * to the specified domain during appdomain unload.
1913 mono_gchandle_free_domain (MonoDomain *domain)
1917 for (type = HANDLE_TYPE_MIN; type < HANDLE_PINNED; ++type) {
1919 HandleData *handles = &gc_handles [type];
1920 lock_handles (handles);
1921 for (slot = 0; slot < handles->size; ++slot) {
1922 if (!slot_occupied (handles, slot))
1924 if (MONO_GC_HANDLE_TYPE_IS_WEAK (type)) {
1925 if (domain->domain_id == handles->domain_ids [slot]) {
1926 vacate_slot (handles, slot);
1927 if (handles->entries [slot])
1928 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
1931 if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
1932 vacate_slot (handles, slot);
1933 handles->entries [slot] = NULL;
1937 unlock_handles (handles);
1943 MONO_EMPTY_SOURCE_FILE (boehm_gc);
1944 #endif /* no Boehm GC */