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;
108 #elif defined(__native_client__)
109 /* Do nothing, GC_stackbottom is set correctly in libgc */
113 gsize stack_bottom = (gsize)&dummy;
114 stack_bottom += 4095;
115 stack_bottom &= ~4095;
116 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
117 GC_stackbottom = (char*)stack_bottom;
121 #if !defined(PLATFORM_ANDROID)
122 /* If GC_no_dls is set to true, GC_find_limit is not called. This causes a seg fault on Android. */
126 GC_oom_fn = mono_gc_out_of_memory;
127 GC_set_warn_proc (mono_gc_warning);
128 GC_finalize_on_demand = 1;
129 GC_finalizer_notifier = mono_gc_finalize_notify;
131 #ifdef HAVE_GC_GCJ_MALLOC
132 GC_init_gcj_malloc (5, NULL);
135 if ((env = getenv ("MONO_GC_PARAMS"))) {
136 char **ptr, **opts = g_strsplit (env, ",", -1);
137 for (ptr = opts; *ptr; ++ptr) {
139 if (g_str_has_prefix (opt, "max-heap-size=")) {
142 opt = strchr (opt, '=') + 1;
143 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
144 if (max_heap < MIN_BOEHM_MAX_HEAP_SIZE) {
145 fprintf (stderr, "max-heap-size must be at least %dMb.\n", MIN_BOEHM_MAX_HEAP_SIZE_IN_MB);
148 GC_set_max_heap_size (max_heap);
150 fprintf (stderr, "max-heap-size must be an integer.\n");
155 fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
156 fprintf (stderr, " max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
163 mono_gc_enable_events ();
164 gc_initialized = TRUE;
169 * @generation: GC generation identifier
171 * Perform a garbage collection for the given generation, higher numbers
172 * mean usually older objects. Collecting a high-numbered generation
173 * implies collecting also the lower-numbered generations.
174 * The maximum value for @generation can be retrieved with a call to
175 * mono_gc_max_generation(), so this function is usually called as:
177 * mono_gc_collect (mono_gc_max_generation ());
180 mono_gc_collect (int generation)
182 MONO_PROBE_GC_BEGIN (generation);
184 mono_perfcounters->gc_induced++;
187 MONO_PROBE_GC_END (generation);
188 #if defined(ENABLE_DTRACE) && defined(__sun__)
189 /* This works around a dtrace -G problem on Solaris.
190 Limit its actual use to when the probe is enabled. */
191 if (MONO_PROBE_GC_END_ENABLED ())
197 * mono_gc_max_generation:
199 * Get the maximum generation number used by the current garbage
200 * collector. The value will be 0 for the Boehm collector, 1 or more
201 * for the generational collectors.
203 * Returns: the maximum generation number.
206 mono_gc_max_generation (void)
212 * mono_gc_get_generation:
213 * @object: a managed object
215 * Get the garbage collector's generation that @object belongs to.
216 * Use this has a hint only.
218 * Returns: a garbage collector generation number
221 mono_gc_get_generation (MonoObject *object)
227 * mono_gc_collection_count:
228 * @generation: a GC generation number
230 * Get how many times a garbage collection has been performed
231 * for the given @generation number.
233 * Returns: the number of garbage collections
236 mono_gc_collection_count (int generation)
242 * mono_gc_add_memory_pressure:
243 * @value: amount of bytes
245 * Adjust the garbage collector's view of how many bytes of memory
246 * are indirectly referenced by managed objects (for example unmanaged
247 * memory holding image or other binary data).
248 * This is a hint only to the garbage collector algorithm.
249 * Note that negative amounts of @value will decrease the memory
253 mono_gc_add_memory_pressure (gint64 value)
258 * mono_gc_get_used_size:
260 * Get the approximate amount of memory used by managed objects.
262 * Returns: the amount of memory used in bytes
265 mono_gc_get_used_size (void)
267 return GC_get_heap_size () - GC_get_free_bytes ();
271 * mono_gc_get_heap_size:
273 * Get the amount of memory used by the garbage collector.
275 * Returns: the size of the heap in bytes
278 mono_gc_get_heap_size (void)
280 return GC_get_heap_size ();
284 mono_gc_disable (void)
286 #ifdef HAVE_GC_ENABLE
289 g_assert_not_reached ();
294 mono_gc_enable (void)
296 #ifdef HAVE_GC_ENABLE
299 g_assert_not_reached ();
304 mono_gc_is_gc_thread (void)
306 #if GC_VERSION_MAJOR >= 7
308 #elif defined(USE_INCLUDED_LIBGC)
309 return GC_thread_is_registered ();
315 extern int GC_thread_register_foreign (void *base_addr);
318 mono_gc_register_thread (void *baseptr)
320 #if GC_VERSION_MAJOR >= 7
321 struct GC_stack_base sb;
324 res = GC_get_stack_base (&sb);
325 if (res != GC_SUCCESS) {
326 sb.mem_base = baseptr;
328 /* Can't determine the register stack bounds */
329 g_error ("mono_gc_register_thread failed ().\n");
332 res = GC_register_my_thread (&sb);
333 if ((res != GC_SUCCESS) && (res != GC_DUPLICATE)) {
334 g_warning ("GC_register_my_thread () failed.\n");
339 if (mono_gc_is_gc_thread())
341 #if defined(USE_INCLUDED_LIBGC) && !defined(HOST_WIN32)
342 return GC_thread_register_foreign (baseptr);
350 mono_object_is_alive (MonoObject* o)
352 #ifdef USE_INCLUDED_LIBGC
353 return GC_is_marked ((gpointer)o);
360 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
365 #ifdef USE_INCLUDED_LIBGC
367 static gint64 gc_start_time;
370 on_gc_notification (GCEventType event)
372 if (event == MONO_GC_EVENT_START) {
373 mono_perfcounters->gc_collections0++;
374 mono_stats.major_gc_count ++;
375 gc_start_time = mono_100ns_ticks ();
376 } else if (event == MONO_GC_EVENT_END) {
377 guint64 heap_size = GC_get_heap_size ();
378 guint64 used_size = heap_size - GC_get_free_bytes ();
379 mono_perfcounters->gc_total_bytes = used_size;
380 mono_perfcounters->gc_committed_bytes = heap_size;
381 mono_perfcounters->gc_reserved_bytes = heap_size;
382 mono_perfcounters->gc_gen0size = heap_size;
383 mono_stats.major_gc_time_usecs += (mono_100ns_ticks () - gc_start_time) / 10;
384 mono_trace_message (MONO_TRACE_GC, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time) / 10);
386 mono_profiler_gc_event ((MonoGCEvent) event, 0);
390 on_gc_heap_resize (size_t new_size)
392 guint64 heap_size = GC_get_heap_size ();
393 mono_perfcounters->gc_committed_bytes = heap_size;
394 mono_perfcounters->gc_reserved_bytes = heap_size;
395 mono_perfcounters->gc_gen0size = heap_size;
396 mono_profiler_gc_heap_resize (new_size);
400 mono_gc_enable_events (void)
402 GC_notify_event = on_gc_notification;
403 GC_on_heap_resize = on_gc_heap_resize;
409 mono_gc_enable_events (void)
416 mono_gc_register_root (char *start, size_t size, void *descr)
418 /* for some strange reason, they want one extra byte on the end */
419 GC_add_roots (start, start + size + 1);
425 mono_gc_deregister_root (char* addr)
428 /* FIXME: libgc doesn't define this work win32 for some reason */
429 /* FIXME: No size info */
430 GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
435 mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
437 /* libgc requires that we use HIDE_POINTER... */
438 *link_addr = (void*)HIDE_POINTER (obj);
439 GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr, obj);
443 mono_gc_weak_link_remove (void **link_addr)
445 GC_unregister_disappearing_link (link_addr);
450 reveal_link (gpointer link_addr)
452 void **link_a = link_addr;
453 return REVEAL_POINTER (*link_a);
457 mono_gc_weak_link_get (void **link_addr)
459 MonoObject *obj = GC_call_with_alloc_lock (reveal_link, link_addr);
460 if (obj == (MonoObject *) -1)
466 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
468 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
472 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
474 return mono_gc_make_descr_from_bitmap (bitmap, numbits);
478 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
480 /* libgc has no usable support for arrays... */
481 return GC_NO_DESCRIPTOR;
485 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
487 #ifdef HAVE_GC_GCJ_MALLOC
488 /* It seems there are issues when the bitmap doesn't fit: play it safe */
490 return GC_NO_DESCRIPTOR;
492 return (gpointer)GC_make_descriptor ((GC_bitmap)bitmap, numbits);
499 mono_gc_make_root_descr_all_refs (int numbits)
505 mono_gc_alloc_fixed (size_t size, void *descr)
507 /* To help track down typed allocation bugs */
511 if (count == atoi (getenv ("COUNT2")))
513 if (count > atoi (getenv ("COUNT2")))
514 return GC_MALLOC (size);
518 return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
520 return GC_MALLOC (size);
524 mono_gc_free_fixed (void* addr)
529 mono_gc_invoke_finalizers (void)
531 /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
532 * the 'mem_freed' variable is not initialized when there are no
533 * objects to finalize, which leads to strange behavior later on.
534 * The check is necessary to work around that bug.
536 if (GC_should_invoke_finalizers ())
537 return GC_invoke_finalizers ();
542 mono_gc_pending_finalizers (void)
544 return GC_should_invoke_finalizers ();
548 * LOCKING: Assumes the domain_finalizers lock is held.
551 add_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
555 if (!domain->track_resurrection_objects_hash)
556 domain->track_resurrection_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
558 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
559 refs = g_slist_prepend (refs, GUINT_TO_POINTER (gchandle));
560 g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
564 mono_gc_add_weak_track_handle (MonoObject *obj, guint32 handle)
571 domain = mono_object_get_domain (obj);
573 mono_domain_finalizers_lock (domain);
575 add_weak_track_handle_internal (domain, obj, handle);
577 g_hash_table_insert (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (handle), obj);
579 mono_domain_finalizers_unlock (domain);
583 * LOCKING: Assumes the domain_finalizers lock is held.
586 remove_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
590 if (!domain->track_resurrection_objects_hash)
593 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
594 refs = g_slist_remove (refs, GUINT_TO_POINTER (gchandle));
595 g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
599 mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle)
601 MonoDomain *domain = mono_domain_get ();
603 mono_domain_finalizers_lock (domain);
606 remove_weak_track_handle_internal (domain, old_obj, gchandle);
608 add_weak_track_handle_internal (domain, obj, gchandle);
610 mono_domain_finalizers_unlock (domain);
614 mono_gc_remove_weak_track_handle (guint32 gchandle)
616 MonoDomain *domain = mono_domain_get ();
619 /* Clean our entries in the two hashes in MonoDomain */
621 mono_domain_finalizers_lock (domain);
623 /* Get the original object this handle pointed to */
624 obj = g_hash_table_lookup (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
626 g_hash_table_remove (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
628 remove_weak_track_handle_internal (domain, obj, gchandle);
631 mono_domain_finalizers_unlock (domain);
635 mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj)
639 if (domain->track_resurrection_objects_hash) {
640 refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
644 * Since we don't run finalizers again for resurrected objects,
645 * no need to keep these around.
647 g_hash_table_remove (domain->track_resurrection_objects_hash, obj);
654 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
656 *(void**)field_ptr = value;
660 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
662 *(void**)slot_ptr = value;
666 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
668 memmove (dest_ptr, src_ptr, count * sizeof (gpointer));
672 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
674 *(void**)ptr = value;
678 mono_gc_wbarrier_generic_nostore (gpointer ptr)
683 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
685 memmove (dest, src, count * mono_class_value_size (klass, NULL));
689 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
691 /* do not copy the sync state */
692 memcpy ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
693 mono_object_class (obj)->instance_size - sizeof (MonoObject));
697 mono_gc_clear_domain (MonoDomain *domain)
702 mono_gc_get_suspend_signal (void)
704 #ifdef USE_INCLUDED_GC
705 return GC_get_suspend_signal ();
711 #if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
712 extern __thread MONO_TLS_FAST void* GC_thread_tls;
713 #include "metadata-internals.h"
719 while (!(v & (1 << i)))
726 ATYPE_FREEPTR_FOR_BOX,
734 create_allocator (int atype, int offset)
736 int index_var, bytes_var, my_fl_var, my_entry_var;
737 guint32 no_freelist_branch, not_small_enough_branch = 0;
738 guint32 size_overflow_branch = 0;
739 MonoMethodBuilder *mb;
741 MonoMethodSignature *csig;
742 AllocatorWrapperInfo *info;
744 if (atype == ATYPE_STRING) {
745 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
746 csig->ret = &mono_defaults.string_class->byval_arg;
747 csig->params [0] = &mono_defaults.int_class->byval_arg;
748 csig->params [1] = &mono_defaults.int32_class->byval_arg;
750 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
751 csig->ret = &mono_defaults.object_class->byval_arg;
752 csig->params [0] = &mono_defaults.int_class->byval_arg;
755 mb = mono_mb_new (mono_defaults.object_class, "Alloc", MONO_WRAPPER_ALLOC);
756 bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
757 if (atype == ATYPE_STRING) {
758 /* a string alloator method takes the args: (vtable, len) */
759 /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
760 mono_mb_emit_ldarg (mb, 1);
761 mono_mb_emit_icon (mb, 1);
762 mono_mb_emit_byte (mb, MONO_CEE_ADD);
763 mono_mb_emit_icon (mb, 1);
764 mono_mb_emit_byte (mb, MONO_CEE_SHL);
765 // sizeof (MonoString) might include padding
766 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, chars));
767 mono_mb_emit_byte (mb, MONO_CEE_ADD);
768 mono_mb_emit_stloc (mb, bytes_var);
770 /* bytes = vtable->klass->instance_size */
771 mono_mb_emit_ldarg (mb, 0);
772 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
773 mono_mb_emit_byte (mb, MONO_CEE_ADD);
774 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
775 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
776 mono_mb_emit_byte (mb, MONO_CEE_ADD);
777 /* FIXME: assert instance_size stays a 4 byte integer */
778 mono_mb_emit_byte (mb, MONO_CEE_LDIND_U4);
779 mono_mb_emit_stloc (mb, bytes_var);
782 /* this is needed for strings/arrays only as the other big types are never allocated with this method */
783 if (atype == ATYPE_STRING) {
785 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
786 mono_mb_emit_ldloc (mb, bytes_var);
787 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
788 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
789 /* check for overflow */
790 mono_mb_emit_ldloc (mb, bytes_var);
791 mono_mb_emit_icon (mb, sizeof (MonoString));
792 size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
795 /* int index = INDEX_FROM_BYTES(bytes); */
796 index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
798 mono_mb_emit_ldloc (mb, bytes_var);
799 mono_mb_emit_icon (mb, GRANULARITY - 1);
800 mono_mb_emit_byte (mb, MONO_CEE_ADD);
801 mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
802 mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
803 mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
804 mono_mb_emit_byte (mb, MONO_CEE_SHL);
805 /* index var is already adjusted into bytes */
806 mono_mb_emit_stloc (mb, index_var);
808 my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
809 my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
810 /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
811 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
812 mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
813 mono_mb_emit_i4 (mb, offset);
814 if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
815 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
816 else if (atype == ATYPE_NORMAL)
817 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
818 else if (atype == ATYPE_GCJ)
819 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
821 g_assert_not_reached ();
822 mono_mb_emit_byte (mb, MONO_CEE_ADD);
823 mono_mb_emit_ldloc (mb, index_var);
824 mono_mb_emit_byte (mb, MONO_CEE_ADD);
825 mono_mb_emit_stloc (mb, my_fl_var);
827 /* my_entry = *my_fl; */
828 mono_mb_emit_ldloc (mb, my_fl_var);
829 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
830 mono_mb_emit_stloc (mb, my_entry_var);
832 /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
833 mono_mb_emit_ldloc (mb, my_entry_var);
834 mono_mb_emit_icon (mb, HBLKSIZE);
835 no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
837 /* ptr_t next = obj_link(my_entry); *my_fl = next; */
838 mono_mb_emit_ldloc (mb, my_fl_var);
839 mono_mb_emit_ldloc (mb, my_entry_var);
840 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
841 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
843 /* set the vtable and clear the words in the object */
844 mono_mb_emit_ldloc (mb, my_entry_var);
845 mono_mb_emit_ldarg (mb, 0);
846 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
848 if (atype == ATYPE_FREEPTR) {
849 int start_var, end_var, start_loop;
850 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
852 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
853 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
854 mono_mb_emit_ldloc (mb, my_entry_var);
855 mono_mb_emit_ldloc (mb, bytes_var);
856 mono_mb_emit_byte (mb, MONO_CEE_ADD);
857 mono_mb_emit_stloc (mb, end_var);
858 mono_mb_emit_ldloc (mb, my_entry_var);
859 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
860 mono_mb_emit_byte (mb, MONO_CEE_ADD);
861 mono_mb_emit_stloc (mb, start_var);
865 * } while (start < end);
867 start_loop = mono_mb_get_label (mb);
868 mono_mb_emit_ldloc (mb, start_var);
869 mono_mb_emit_icon (mb, 0);
870 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
871 mono_mb_emit_ldloc (mb, start_var);
872 mono_mb_emit_icon (mb, sizeof (gpointer));
873 mono_mb_emit_byte (mb, MONO_CEE_ADD);
874 mono_mb_emit_stloc (mb, start_var);
876 mono_mb_emit_ldloc (mb, start_var);
877 mono_mb_emit_ldloc (mb, end_var);
878 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
879 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
880 } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
881 /* need to clear just the sync pointer */
882 mono_mb_emit_ldloc (mb, my_entry_var);
883 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
884 mono_mb_emit_byte (mb, MONO_CEE_ADD);
885 mono_mb_emit_icon (mb, 0);
886 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
889 if (atype == ATYPE_STRING) {
890 /* need to set length and clear the last char */
891 /* s->length = len; */
892 mono_mb_emit_ldloc (mb, my_entry_var);
893 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
894 mono_mb_emit_byte (mb, MONO_CEE_ADD);
895 mono_mb_emit_ldarg (mb, 1);
896 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
897 /* s->chars [len] = 0; */
898 mono_mb_emit_ldloc (mb, my_entry_var);
899 mono_mb_emit_ldloc (mb, bytes_var);
900 mono_mb_emit_icon (mb, 2);
901 mono_mb_emit_byte (mb, MONO_CEE_SUB);
902 mono_mb_emit_byte (mb, MONO_CEE_ADD);
903 mono_mb_emit_icon (mb, 0);
904 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
907 /* return my_entry; */
908 mono_mb_emit_ldloc (mb, my_entry_var);
909 mono_mb_emit_byte (mb, MONO_CEE_RET);
911 mono_mb_patch_short_branch (mb, no_freelist_branch);
912 if (not_small_enough_branch > 0)
913 mono_mb_patch_short_branch (mb, not_small_enough_branch);
914 if (size_overflow_branch > 0)
915 mono_mb_patch_short_branch (mb, size_overflow_branch);
916 /* the slow path: we just call back into the runtime */
917 if (atype == ATYPE_STRING) {
918 mono_mb_emit_ldarg (mb, 1);
919 mono_mb_emit_icall (mb, mono_string_alloc);
921 mono_mb_emit_ldarg (mb, 0);
922 mono_mb_emit_icall (mb, mono_object_new_specific);
925 mono_mb_emit_byte (mb, MONO_CEE_RET);
927 res = mono_mb_create_method (mb, csig, 8);
929 mono_method_get_header (res)->init_locals = FALSE;
931 info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
932 info->gc_name = "boehm";
933 info->alloc_type = atype;
934 mono_marshal_set_wrapper_info (res, info);
939 static MonoMethod* alloc_method_cache [ATYPE_NUM];
942 * If possible, generate a managed method that can quickly allocate objects in class
943 * @klass. The method will typically have an thread-local inline allocation sequence.
944 * The signature of the called method is:
945 * object allocate (MonoVTable *vtable)
946 * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
948 * The thread local alloc logic is taken from libgc/pthread_support.c.
952 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
956 MonoClass *klass = vtable->klass;
957 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
959 /*g_print ("thread tls: %d\n", offset);*/
962 if (!SMALL_ENOUGH (klass->instance_size))
964 if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
968 if (klass->byval_arg.type == MONO_TYPE_STRING) {
969 atype = ATYPE_STRING;
970 } else if (!klass->has_references) {
972 atype = ATYPE_FREEPTR_FOR_BOX;
974 atype = ATYPE_FREEPTR;
978 * disabled because we currently do a runtime choice anyway, to
979 * deal with multiple appdomains.
980 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
983 atype = ATYPE_NORMAL;
986 return mono_gc_get_managed_allocator_by_type (atype);
990 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
996 * mono_gc_get_managed_allocator_by_type:
998 * Return a managed allocator method corresponding to allocator type ATYPE.
1001 mono_gc_get_managed_allocator_by_type (int atype)
1005 MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1007 mono_loader_lock ();
1008 res = alloc_method_cache [atype];
1010 res = alloc_method_cache [atype] = create_allocator (atype, offset);
1011 mono_loader_unlock ();
1016 mono_gc_get_managed_allocator_types (void)
1022 mono_gc_get_write_barrier (void)
1024 g_assert_not_reached ();
1031 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
1037 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
1043 mono_gc_get_managed_allocator_by_type (int atype)
1049 mono_gc_get_managed_allocator_types (void)
1055 mono_gc_get_write_barrier (void)
1057 g_assert_not_reached ();
1064 mono_gc_get_gc_name (void)
1070 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
1072 return GC_call_with_alloc_lock (func, data);
1076 mono_gc_get_description (void)
1078 return g_strdup (DEFAULT_GC_NAME);
1082 mono_gc_set_desktop_mode (void)
1088 mono_gc_is_moving (void)
1094 mono_gc_is_disabled (void)
1096 if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
1103 mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
1105 g_assert_not_reached ();
1110 mono_gc_get_card_table (int *shift_bits, gpointer *card_mask)
1112 g_assert_not_reached ();
1117 mono_gc_get_nursery (int *shift_bits, size_t *size)
1123 * These will call the redefined versions in libgc.
1129 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
1131 return pthread_create (new_thread, attr, start_routine, arg);
1135 mono_gc_pthread_join (pthread_t thread, void **retval)
1137 return pthread_join (thread, retval);
1141 mono_gc_pthread_detach (pthread_t thread)
1143 return pthread_detach (thread);
1148 #endif /* no Boehm GC */