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-2009 Novell, Inc (http://www.novell.com)
12 #define GC_I_HIDE_POINTERS
13 #include <mono/metadata/gc-internal.h>
14 #include <mono/metadata/mono-gc.h>
15 #include <mono/metadata/gc-internal.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/method-builder.h>
19 #include <mono/metadata/opcodes.h>
20 #include <mono/metadata/domain-internals.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/marshal.h>
23 #include <mono/utils/mono-logger-internal.h>
24 #include <mono/utils/mono-time.h>
25 #include <mono/utils/dtrace.h>
26 #include <mono/utils/gc_wrapper.h>
30 #ifdef USE_INCLUDED_LIBGC
33 #define THREAD_LOCAL_ALLOC 1
34 #include "private/pthread_support.h"
37 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
38 /*Boehm max heap cannot be smaller than 16MB*/
39 #define MIN_BOEHM_MAX_HEAP_SIZE_IN_MB 16
40 #define MIN_BOEHM_MAX_HEAP_SIZE (MIN_BOEHM_MAX_HEAP_SIZE_IN_MB << 20)
42 static gboolean gc_initialized = FALSE;
45 mono_gc_warning (char *msg, GC_word arg)
47 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
51 mono_gc_base_init (void)
59 * Handle the case when we are called from a thread different from the main thread,
61 * FIXME: Move this to libgc where it belongs.
63 * we used to do this only when running on valgrind,
64 * but it happens also in other setups.
66 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
71 pthread_getattr_np (pthread_self (), &attr);
72 pthread_attr_getstack (&attr, &sstart, &size);
73 pthread_attr_destroy (&attr);
74 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
77 * The calculation above doesn't seem to work on ia64, also we need to set
78 * GC_register_stackbottom as well, but don't know how.
81 /* apparently with some linuxthreads implementations sstart can be NULL,
82 * fallback to the more imprecise method (bug# 78096).
85 GC_stackbottom = (char*)sstart + size;
88 gsize stack_bottom = (gsize)&dummy;
90 stack_bottom &= ~4095;
91 GC_stackbottom = (char*)stack_bottom;
95 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
96 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
97 #elif defined(__OpenBSD__)
98 # include <pthread_np.h>
103 rslt = pthread_stackseg_np(pthread_self(), &ss);
104 g_assert (rslt == 0);
106 GC_stackbottom = (char*)ss.ss_sp;
111 gsize stack_bottom = (gsize)&dummy;
112 stack_bottom += 4095;
113 stack_bottom &= ~4095;
114 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
115 GC_stackbottom = (char*)stack_bottom;
119 #if !defined(PLATFORM_ANDROID)
120 /* If GC_no_dls is set to true, GC_find_limit is not called. This causes a seg fault on Android. */
124 GC_oom_fn = mono_gc_out_of_memory;
125 GC_set_warn_proc (mono_gc_warning);
126 GC_finalize_on_demand = 1;
127 GC_finalizer_notifier = mono_gc_finalize_notify;
129 #ifdef HAVE_GC_GCJ_MALLOC
130 GC_init_gcj_malloc (5, NULL);
133 if ((env = getenv ("MONO_GC_PARAMS"))) {
134 char **ptr, **opts = g_strsplit (env, ",", -1);
135 for (ptr = opts; *ptr; ++ptr) {
137 if (g_str_has_prefix (opt, "max-heap-size=")) {
140 opt = strchr (opt, '=') + 1;
141 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
142 if (max_heap < MIN_BOEHM_MAX_HEAP_SIZE) {
143 fprintf (stderr, "max-heap-size must be at least %dMb.\n", MIN_BOEHM_MAX_HEAP_SIZE_IN_MB);
146 GC_set_max_heap_size (max_heap);
148 fprintf (stderr, "max-heap-size must be an integer.\n");
153 fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
154 fprintf (stderr, " max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
161 mono_gc_enable_events ();
162 gc_initialized = TRUE;
167 * @generation: GC generation identifier
169 * Perform a garbage collection for the given generation, higher numbers
170 * mean usually older objects. Collecting a high-numbered generation
171 * implies collecting also the lower-numbered generations.
172 * The maximum value for @generation can be retrieved with a call to
173 * mono_gc_max_generation(), so this function is usually called as:
175 * mono_gc_collect (mono_gc_max_generation ());
178 mono_gc_collect (int generation)
180 MONO_PROBE_GC_BEGIN (generation);
182 mono_perfcounters->gc_induced++;
185 MONO_PROBE_GC_END (generation);
186 #if defined(ENABLE_DTRACE) && defined(__sun__)
187 /* This works around a dtrace -G problem on Solaris.
188 Limit its actual use to when the probe is enabled. */
189 if (MONO_PROBE_GC_END_ENABLED ())
195 * mono_gc_max_generation:
197 * Get the maximum generation number used by the current garbage
198 * collector. The value will be 0 for the Boehm collector, 1 or more
199 * for the generational collectors.
201 * Returns: the maximum generation number.
204 mono_gc_max_generation (void)
210 * mono_gc_get_generation:
211 * @object: a managed object
213 * Get the garbage collector's generation that @object belongs to.
214 * Use this has a hint only.
216 * Returns: a garbage collector generation number
219 mono_gc_get_generation (MonoObject *object)
225 * mono_gc_collection_count:
226 * @generation: a GC generation number
228 * Get how many times a garbage collection has been performed
229 * for the given @generation number.
231 * Returns: the number of garbage collections
234 mono_gc_collection_count (int generation)
240 * mono_gc_add_memory_pressure:
241 * @value: amount of bytes
243 * Adjust the garbage collector's view of how many bytes of memory
244 * are indirectly referenced by managed objects (for example unmanaged
245 * memory holding image or other binary data).
246 * This is a hint only to the garbage collector algorithm.
247 * Note that negative amounts of @value will decrease the memory
251 mono_gc_add_memory_pressure (gint64 value)
256 * mono_gc_get_used_size:
258 * Get the approximate amount of memory used by managed objects.
260 * Returns: the amount of memory used in bytes
263 mono_gc_get_used_size (void)
265 return GC_get_heap_size () - GC_get_free_bytes ();
269 * mono_gc_get_heap_size:
271 * Get the amount of memory used by the garbage collector.
273 * Returns: the size of the heap in bytes
276 mono_gc_get_heap_size (void)
278 return GC_get_heap_size ();
282 mono_gc_disable (void)
284 #ifdef HAVE_GC_ENABLE
287 g_assert_not_reached ();
292 mono_gc_enable (void)
294 #ifdef HAVE_GC_ENABLE
297 g_assert_not_reached ();
302 mono_gc_is_gc_thread (void)
304 #if GC_VERSION_MAJOR >= 7
306 #elif defined(USE_INCLUDED_LIBGC)
307 return GC_thread_is_registered ();
313 extern int GC_thread_register_foreign (void *base_addr);
316 mono_gc_register_thread (void *baseptr)
318 #if GC_VERSION_MAJOR >= 7
319 struct GC_stack_base sb;
322 res = GC_get_stack_base (&sb);
323 if (res != GC_SUCCESS) {
324 sb.mem_base = baseptr;
326 /* Can't determine the register stack bounds */
327 g_error ("mono_gc_register_thread failed ().\n");
330 res = GC_register_my_thread (&sb);
331 if ((res != GC_SUCCESS) && (res != GC_DUPLICATE)) {
332 g_warning ("GC_register_my_thread () failed.\n");
337 if (mono_gc_is_gc_thread())
339 #if defined(USE_INCLUDED_LIBGC) && !defined(HOST_WIN32)
340 return GC_thread_register_foreign (baseptr);
348 mono_object_is_alive (MonoObject* o)
350 #ifdef USE_INCLUDED_LIBGC
351 return GC_is_marked ((gpointer)o);
358 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
363 #ifdef USE_INCLUDED_LIBGC
365 static gint64 gc_start_time;
368 on_gc_notification (GCEventType event)
370 if (event == MONO_GC_EVENT_START) {
371 mono_perfcounters->gc_collections0++;
372 mono_stats.major_gc_count ++;
373 gc_start_time = mono_100ns_ticks ();
374 } else if (event == MONO_GC_EVENT_END) {
375 guint64 heap_size = GC_get_heap_size ();
376 guint64 used_size = heap_size - GC_get_free_bytes ();
377 mono_perfcounters->gc_total_bytes = used_size;
378 mono_perfcounters->gc_committed_bytes = heap_size;
379 mono_perfcounters->gc_reserved_bytes = heap_size;
380 mono_perfcounters->gc_gen0size = heap_size;
381 mono_stats.major_gc_time_usecs += (mono_100ns_ticks () - gc_start_time) / 10;
382 mono_trace_message (MONO_TRACE_GC, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time) / 10);
384 mono_profiler_gc_event ((MonoGCEvent) event, 0);
388 on_gc_heap_resize (size_t new_size)
390 guint64 heap_size = GC_get_heap_size ();
391 mono_perfcounters->gc_committed_bytes = heap_size;
392 mono_perfcounters->gc_reserved_bytes = heap_size;
393 mono_perfcounters->gc_gen0size = heap_size;
394 mono_profiler_gc_heap_resize (new_size);
398 mono_gc_enable_events (void)
400 GC_notify_event = on_gc_notification;
401 GC_on_heap_resize = on_gc_heap_resize;
407 mono_gc_enable_events (void)
414 mono_gc_register_root (char *start, size_t size, void *descr)
416 /* for some strange reason, they want one extra byte on the end */
417 GC_add_roots (start, start + size + 1);
423 mono_gc_deregister_root (char* addr)
426 /* FIXME: libgc doesn't define this work win32 for some reason */
427 /* FIXME: No size info */
428 GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
433 mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
435 /* libgc requires that we use HIDE_POINTER... */
436 *link_addr = (void*)HIDE_POINTER (obj);
437 GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr, obj);
441 mono_gc_weak_link_remove (void **link_addr)
443 GC_unregister_disappearing_link (link_addr);
448 reveal_link (gpointer link_addr)
450 void **link_a = link_addr;
451 return REVEAL_POINTER (*link_a);
455 mono_gc_weak_link_get (void **link_addr)
457 MonoObject *obj = GC_call_with_alloc_lock (reveal_link, link_addr);
458 if (obj == (MonoObject *) -1)
464 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
466 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
470 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
472 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
476 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
478 /* libgc has no usable support for arrays... */
479 return GC_NO_DESCRIPTOR;
483 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
485 #ifdef HAVE_GC_GCJ_MALLOC
486 /* It seems there are issues when the bitmap doesn't fit: play it safe */
488 return GC_NO_DESCRIPTOR;
490 return (gpointer)GC_make_descriptor ((GC_bitmap)bitmap, numbits);
497 mono_gc_make_root_descr_all_refs (int numbits)
503 mono_gc_alloc_fixed (size_t size, void *descr)
505 /* To help track down typed allocation bugs */
509 if (count == atoi (getenv ("COUNT2")))
511 if (count > atoi (getenv ("COUNT2")))
512 return GC_MALLOC (size);
516 return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
518 return GC_MALLOC (size);
522 mono_gc_free_fixed (void* addr)
527 mono_gc_invoke_finalizers (void)
529 /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
530 * the 'mem_freed' variable is not initialized when there are no
531 * objects to finalize, which leads to strange behavior later on.
532 * The check is necessary to work around that bug.
534 if (GC_should_invoke_finalizers ())
535 return GC_invoke_finalizers ();
540 mono_gc_pending_finalizers (void)
542 return GC_should_invoke_finalizers ();
546 * LOCKING: Assumes the domain_finalizers lock is held.
549 add_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
553 if (!domain->track_resurrection_objects_hash)
554 domain->track_resurrection_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
556 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
557 refs = g_slist_prepend (refs, GUINT_TO_POINTER (gchandle));
558 g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
562 mono_gc_add_weak_track_handle (MonoObject *obj, guint32 handle)
569 domain = mono_object_get_domain (obj);
571 mono_domain_finalizers_lock (domain);
573 add_weak_track_handle_internal (domain, obj, handle);
575 g_hash_table_insert (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (handle), obj);
577 mono_domain_finalizers_unlock (domain);
581 * LOCKING: Assumes the domain_finalizers lock is held.
584 remove_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
588 if (!domain->track_resurrection_objects_hash)
591 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
592 refs = g_slist_remove (refs, GUINT_TO_POINTER (gchandle));
593 g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
597 mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle)
599 MonoDomain *domain = mono_domain_get ();
601 mono_domain_finalizers_lock (domain);
604 remove_weak_track_handle_internal (domain, old_obj, gchandle);
606 add_weak_track_handle_internal (domain, obj, gchandle);
608 mono_domain_finalizers_unlock (domain);
612 mono_gc_remove_weak_track_handle (guint32 gchandle)
614 MonoDomain *domain = mono_domain_get ();
617 /* Clean our entries in the two hashes in MonoDomain */
619 mono_domain_finalizers_lock (domain);
621 /* Get the original object this handle pointed to */
622 obj = g_hash_table_lookup (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
624 g_hash_table_remove (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
626 remove_weak_track_handle_internal (domain, obj, gchandle);
629 mono_domain_finalizers_unlock (domain);
633 mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj)
637 if (domain->track_resurrection_objects_hash) {
638 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
642 * Since we don't run finalizers again for resurrected objects,
643 * no need to keep these around.
645 g_hash_table_remove (domain->track_resurrection_objects_hash, obj);
652 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
654 *(void**)field_ptr = value;
658 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
660 *(void**)slot_ptr = value;
664 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
666 memmove (dest_ptr, src_ptr, count * sizeof (gpointer));
670 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
672 *(void**)ptr = value;
676 mono_gc_wbarrier_generic_nostore (gpointer ptr)
681 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
683 memmove (dest, src, count * mono_class_value_size (klass, NULL));
687 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
689 /* do not copy the sync state */
690 memcpy ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
691 mono_object_class (obj)->instance_size - sizeof (MonoObject));
695 mono_gc_clear_domain (MonoDomain *domain)
700 mono_gc_get_suspend_signal (void)
702 #ifdef USE_INCLUDED_GC
703 return GC_get_suspend_signal ();
709 #if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
710 extern __thread MONO_TLS_FAST void* GC_thread_tls;
711 #include "metadata-internals.h"
717 while (!(v & (1 << i)))
724 ATYPE_FREEPTR_FOR_BOX,
732 create_allocator (int atype, int offset)
734 int index_var, bytes_var, my_fl_var, my_entry_var;
735 guint32 no_freelist_branch, not_small_enough_branch = 0;
736 guint32 size_overflow_branch = 0;
737 MonoMethodBuilder *mb;
739 MonoMethodSignature *csig;
740 AllocatorWrapperInfo *info;
742 if (atype == ATYPE_STRING) {
743 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
744 csig->ret = &mono_defaults.string_class->byval_arg;
745 csig->params [0] = &mono_defaults.int_class->byval_arg;
746 csig->params [1] = &mono_defaults.int32_class->byval_arg;
748 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
749 csig->ret = &mono_defaults.object_class->byval_arg;
750 csig->params [0] = &mono_defaults.int_class->byval_arg;
753 mb = mono_mb_new (mono_defaults.object_class, "Alloc", MONO_WRAPPER_ALLOC);
754 bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
755 if (atype == ATYPE_STRING) {
756 /* a string alloator method takes the args: (vtable, len) */
757 /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
758 mono_mb_emit_ldarg (mb, 1);
759 mono_mb_emit_icon (mb, 1);
760 mono_mb_emit_byte (mb, MONO_CEE_ADD);
761 mono_mb_emit_icon (mb, 1);
762 mono_mb_emit_byte (mb, MONO_CEE_SHL);
763 // sizeof (MonoString) might include padding
764 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, chars));
765 mono_mb_emit_byte (mb, MONO_CEE_ADD);
766 mono_mb_emit_stloc (mb, bytes_var);
768 /* bytes = vtable->klass->instance_size */
769 mono_mb_emit_ldarg (mb, 0);
770 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
771 mono_mb_emit_byte (mb, MONO_CEE_ADD);
772 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
773 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
774 mono_mb_emit_byte (mb, MONO_CEE_ADD);
775 /* FIXME: assert instance_size stays a 4 byte integer */
776 mono_mb_emit_byte (mb, MONO_CEE_LDIND_U4);
777 mono_mb_emit_stloc (mb, bytes_var);
780 /* this is needed for strings/arrays only as the other big types are never allocated with this method */
781 if (atype == ATYPE_STRING) {
783 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
784 mono_mb_emit_ldloc (mb, bytes_var);
785 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
786 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
787 /* check for overflow */
788 mono_mb_emit_ldloc (mb, bytes_var);
789 mono_mb_emit_icon (mb, sizeof (MonoString));
790 size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
793 /* int index = INDEX_FROM_BYTES(bytes); */
794 index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
796 mono_mb_emit_ldloc (mb, bytes_var);
797 mono_mb_emit_icon (mb, GRANULARITY - 1);
798 mono_mb_emit_byte (mb, MONO_CEE_ADD);
799 mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
800 mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
801 mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
802 mono_mb_emit_byte (mb, MONO_CEE_SHL);
803 /* index var is already adjusted into bytes */
804 mono_mb_emit_stloc (mb, index_var);
806 my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
807 my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
808 /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
809 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
810 mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
811 mono_mb_emit_i4 (mb, offset);
812 if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
813 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
814 else if (atype == ATYPE_NORMAL)
815 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
816 else if (atype == ATYPE_GCJ)
817 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
819 g_assert_not_reached ();
820 mono_mb_emit_byte (mb, MONO_CEE_ADD);
821 mono_mb_emit_ldloc (mb, index_var);
822 mono_mb_emit_byte (mb, MONO_CEE_ADD);
823 mono_mb_emit_stloc (mb, my_fl_var);
825 /* my_entry = *my_fl; */
826 mono_mb_emit_ldloc (mb, my_fl_var);
827 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
828 mono_mb_emit_stloc (mb, my_entry_var);
830 /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
831 mono_mb_emit_ldloc (mb, my_entry_var);
832 mono_mb_emit_icon (mb, HBLKSIZE);
833 no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
835 /* ptr_t next = obj_link(my_entry); *my_fl = next; */
836 mono_mb_emit_ldloc (mb, my_fl_var);
837 mono_mb_emit_ldloc (mb, my_entry_var);
838 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
839 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
841 /* set the vtable and clear the words in the object */
842 mono_mb_emit_ldloc (mb, my_entry_var);
843 mono_mb_emit_ldarg (mb, 0);
844 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
846 if (atype == ATYPE_FREEPTR) {
847 int start_var, end_var, start_loop;
848 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
850 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
851 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
852 mono_mb_emit_ldloc (mb, my_entry_var);
853 mono_mb_emit_ldloc (mb, bytes_var);
854 mono_mb_emit_byte (mb, MONO_CEE_ADD);
855 mono_mb_emit_stloc (mb, end_var);
856 mono_mb_emit_ldloc (mb, my_entry_var);
857 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
858 mono_mb_emit_byte (mb, MONO_CEE_ADD);
859 mono_mb_emit_stloc (mb, start_var);
863 * } while (start < end);
865 start_loop = mono_mb_get_label (mb);
866 mono_mb_emit_ldloc (mb, start_var);
867 mono_mb_emit_icon (mb, 0);
868 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
869 mono_mb_emit_ldloc (mb, start_var);
870 mono_mb_emit_icon (mb, sizeof (gpointer));
871 mono_mb_emit_byte (mb, MONO_CEE_ADD);
872 mono_mb_emit_stloc (mb, start_var);
874 mono_mb_emit_ldloc (mb, start_var);
875 mono_mb_emit_ldloc (mb, end_var);
876 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
877 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
878 } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
879 /* need to clear just the sync pointer */
880 mono_mb_emit_ldloc (mb, my_entry_var);
881 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
882 mono_mb_emit_byte (mb, MONO_CEE_ADD);
883 mono_mb_emit_icon (mb, 0);
884 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
887 if (atype == ATYPE_STRING) {
888 /* need to set length and clear the last char */
889 /* s->length = len; */
890 mono_mb_emit_ldloc (mb, my_entry_var);
891 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
892 mono_mb_emit_byte (mb, MONO_CEE_ADD);
893 mono_mb_emit_ldarg (mb, 1);
894 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
895 /* s->chars [len] = 0; */
896 mono_mb_emit_ldloc (mb, my_entry_var);
897 mono_mb_emit_ldloc (mb, bytes_var);
898 mono_mb_emit_icon (mb, 2);
899 mono_mb_emit_byte (mb, MONO_CEE_SUB);
900 mono_mb_emit_byte (mb, MONO_CEE_ADD);
901 mono_mb_emit_icon (mb, 0);
902 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
905 /* return my_entry; */
906 mono_mb_emit_ldloc (mb, my_entry_var);
907 mono_mb_emit_byte (mb, MONO_CEE_RET);
909 mono_mb_patch_short_branch (mb, no_freelist_branch);
910 if (not_small_enough_branch > 0)
911 mono_mb_patch_short_branch (mb, not_small_enough_branch);
912 if (size_overflow_branch > 0)
913 mono_mb_patch_short_branch (mb, size_overflow_branch);
914 /* the slow path: we just call back into the runtime */
915 if (atype == ATYPE_STRING) {
916 mono_mb_emit_ldarg (mb, 1);
917 mono_mb_emit_icall (mb, mono_string_alloc);
919 mono_mb_emit_ldarg (mb, 0);
920 mono_mb_emit_icall (mb, mono_object_new_specific);
923 mono_mb_emit_byte (mb, MONO_CEE_RET);
925 res = mono_mb_create_method (mb, csig, 8);
927 mono_method_get_header (res)->init_locals = FALSE;
929 info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
930 info->gc_name = "boehm";
931 info->alloc_type = atype;
932 mono_marshal_set_wrapper_info (res, info);
937 static MonoMethod* alloc_method_cache [ATYPE_NUM];
940 * If possible, generate a managed method that can quickly allocate objects in class
941 * @klass. The method will typically have an thread-local inline allocation sequence.
942 * The signature of the called method is:
943 * object allocate (MonoVTable *vtable)
944 * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
946 * The thread local alloc logic is taken from libgc/pthread_support.c.
950 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
954 MonoClass *klass = vtable->klass;
955 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
957 /*g_print ("thread tls: %d\n", offset);*/
960 if (!SMALL_ENOUGH (klass->instance_size))
962 if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
966 if (klass->byval_arg.type == MONO_TYPE_STRING) {
967 atype = ATYPE_STRING;
968 } else if (!klass->has_references) {
970 atype = ATYPE_FREEPTR_FOR_BOX;
972 atype = ATYPE_FREEPTR;
976 * disabled because we currently do a runtime choice anyway, to
977 * deal with multiple appdomains.
978 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
981 atype = ATYPE_NORMAL;
984 return mono_gc_get_managed_allocator_by_type (atype);
988 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
994 * mono_gc_get_managed_allocator_by_type:
996 * Return a managed allocator method corresponding to allocator type ATYPE.
999 mono_gc_get_managed_allocator_by_type (int atype)
1003 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1005 mono_loader_lock ();
1006 res = alloc_method_cache [atype];
1008 res = alloc_method_cache [atype] = create_allocator (atype, offset);
1009 mono_loader_unlock ();
1014 mono_gc_get_managed_allocator_types (void)
1020 mono_gc_get_write_barrier (void)
1022 g_assert_not_reached ();
1029 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
1035 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
1041 mono_gc_get_managed_allocator_by_type (int atype)
1047 mono_gc_get_managed_allocator_types (void)
1053 mono_gc_get_write_barrier (void)
1055 g_assert_not_reached ();
1062 mono_gc_get_gc_name (void)
1068 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
1070 return GC_call_with_alloc_lock (func, data);
1074 mono_gc_get_description (void)
1076 return g_strdup (DEFAULT_GC_NAME);
1080 mono_gc_set_desktop_mode (void)
1086 mono_gc_is_moving (void)
1092 mono_gc_is_disabled (void)
1094 if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
1101 mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
1103 g_assert_not_reached ();
1108 mono_gc_get_card_table (int *shift_bits, gpointer *card_mask)
1110 g_assert_not_reached ();
1115 mono_gc_get_nursery (int *shift_bits, size_t *size)
1121 * These will call the redefined versions in libgc.
1127 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
1129 return pthread_create (new_thread, attr, start_routine, arg);
1133 mono_gc_pthread_join (pthread_t thread, void **retval)
1135 return pthread_join (thread, retval);
1139 mono_gc_pthread_detach (pthread_t thread)
1141 return pthread_detach (thread);
1146 #endif /* no Boehm GC */