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)
9 #define GC_I_HIDE_POINTERS
10 #include <mono/metadata/gc-internal.h>
11 #include <mono/metadata/mono-gc.h>
12 #include <mono/metadata/gc-internal.h>
13 #include <mono/metadata/profiler-private.h>
14 #include <mono/metadata/class-internals.h>
15 #include <mono/metadata/method-builder.h>
16 #include <mono/metadata/opcodes.h>
17 #include <mono/metadata/domain-internals.h>
18 #include <mono/metadata/metadata-internals.h>
19 #include <mono/utils/mono-logger.h>
20 #include <mono/utils/mono-time.h>
21 #include <mono/utils/dtrace.h>
25 #ifdef USE_INCLUDED_LIBGC
28 #define THREAD_LOCAL_ALLOC 1
29 #include "private/pthread_support.h"
32 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
34 static gboolean gc_initialized
= FALSE
;
37 mono_gc_warning (char *msg
, GC_word arg
)
39 mono_trace (G_LOG_LEVEL_WARNING
, MONO_TRACE_GC
, msg
, (unsigned long)arg
);
43 mono_gc_base_init (void)
49 * Handle the case when we are called from a thread different from the main thread,
51 * FIXME: Move this to libgc where it belongs.
53 * we used to do this only when running on valgrind,
54 * but it happens also in other setups.
56 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
61 pthread_getattr_np (pthread_self (), &attr
);
62 pthread_attr_getstack (&attr
, &sstart
, &size
);
63 pthread_attr_destroy (&attr
);
64 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
67 * The calculation above doesn't seem to work on ia64, also we need to set
68 * GC_register_stackbottom as well, but don't know how.
71 /* apparently with some linuxthreads implementations sstart can be NULL,
72 * fallback to the more imprecise method (bug# 78096).
75 GC_stackbottom
= (char*)sstart
+ size
;
78 gsize stack_bottom
= (gsize
)&dummy
;
80 stack_bottom
&= ~4095;
81 GC_stackbottom
= (char*)stack_bottom
;
85 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
86 GC_stackbottom
= (char*)pthread_get_stackaddr_np (pthread_self ());
90 gsize stack_bottom
= (gsize
)&dummy
;
92 stack_bottom
&= ~4095;
93 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
94 GC_stackbottom
= (char*)stack_bottom
;
100 GC_oom_fn
= mono_gc_out_of_memory
;
101 GC_set_warn_proc (mono_gc_warning
);
102 GC_finalize_on_demand
= 1;
103 GC_finalizer_notifier
= mono_gc_finalize_notify
;
105 #ifdef HAVE_GC_GCJ_MALLOC
106 GC_init_gcj_malloc (5, NULL
);
108 mono_gc_enable_events ();
109 gc_initialized
= TRUE
;
113 mono_gc_collect (int generation
)
115 MONO_PROBE_GC_BEGIN (generation
);
117 mono_perfcounters
->gc_induced
++;
120 MONO_PROBE_GC_END (generation
);
121 #if defined(ENABLE_DTRACE) && defined(__sun__)
122 /* This works around a dtrace -G problem on Solaris.
123 Limit its actual use to when the probe is enabled. */
124 if (MONO_PROBE_GC_END_ENABLED ())
130 mono_gc_max_generation (void)
136 mono_gc_get_generation (MonoObject
*object
)
142 mono_gc_collection_count (int generation
)
148 mono_gc_add_memory_pressure (gint64 value
)
153 mono_gc_get_used_size (void)
155 return GC_get_heap_size () - GC_get_free_bytes ();
159 mono_gc_get_heap_size (void)
161 return GC_get_heap_size ();
165 mono_gc_disable (void)
167 #ifdef HAVE_GC_ENABLE
170 g_assert_not_reached ();
175 mono_gc_enable (void)
177 #ifdef HAVE_GC_ENABLE
180 g_assert_not_reached ();
185 mono_gc_is_gc_thread (void)
187 #if GC_VERSION_MAJOR >= 7
189 #elif defined(USE_INCLUDED_LIBGC)
190 return GC_thread_is_registered ();
196 extern int GC_thread_register_foreign (void *base_addr
);
199 mono_gc_register_thread (void *baseptr
)
201 #if GC_VERSION_MAJOR >= 7
202 struct GC_stack_base sb
;
205 res
= GC_get_stack_base (&sb
);
206 if (res
!= GC_SUCCESS
) {
207 sb
.mem_base
= baseptr
;
209 /* Can't determine the register stack bounds */
210 g_error ("mono_gc_register_thread failed ().\n");
213 res
= GC_register_my_thread (&sb
);
214 if ((res
!= GC_SUCCESS
) && (res
!= GC_DUPLICATE
)) {
215 g_warning ("GC_register_my_thread () failed.\n");
220 if (mono_gc_is_gc_thread())
222 #if defined(USE_INCLUDED_LIBGC) && !defined(PLATFORM_WIN32)
223 return GC_thread_register_foreign (baseptr
);
231 mono_object_is_alive (MonoObject
* o
)
233 #ifdef USE_INCLUDED_LIBGC
234 return GC_is_marked ((gpointer
)o
);
240 #ifdef USE_INCLUDED_LIBGC
242 static gint64 gc_start_time
;
245 on_gc_notification (GCEventType event
)
247 if (event
== MONO_GC_EVENT_START
) {
248 mono_perfcounters
->gc_collections0
++;
249 mono_stats
.major_gc_count
++;
250 gc_start_time
= mono_100ns_ticks ();
251 } else if (event
== MONO_GC_EVENT_END
) {
252 guint64 heap_size
= GC_get_heap_size ();
253 guint64 used_size
= heap_size
- GC_get_free_bytes ();
254 mono_perfcounters
->gc_total_bytes
= used_size
;
255 mono_perfcounters
->gc_committed_bytes
= heap_size
;
256 mono_perfcounters
->gc_reserved_bytes
= heap_size
;
257 mono_perfcounters
->gc_gen0size
= heap_size
;
258 mono_stats
.major_gc_time_usecs
+= (mono_100ns_ticks () - gc_start_time
) / 10;
259 mono_trace_message (MONO_TRACE_GC
, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time
) / 10);
261 mono_profiler_gc_event ((MonoGCEvent
) event
, 0);
265 on_gc_heap_resize (size_t new_size
)
267 guint64 heap_size
= GC_get_heap_size ();
268 mono_perfcounters
->gc_committed_bytes
= heap_size
;
269 mono_perfcounters
->gc_reserved_bytes
= heap_size
;
270 mono_perfcounters
->gc_gen0size
= heap_size
;
271 mono_profiler_gc_heap_resize (new_size
);
275 mono_gc_enable_events (void)
277 GC_notify_event
= on_gc_notification
;
278 GC_on_heap_resize
= on_gc_heap_resize
;
284 mono_gc_enable_events (void)
291 mono_gc_register_root (char *start
, size_t size
, void *descr
)
293 /* for some strange reason, they want one extra byte on the end */
294 GC_add_roots (start
, start
+ size
+ 1);
300 mono_gc_deregister_root (char* addr
)
302 #ifndef PLATFORM_WIN32
303 /* FIXME: libgc doesn't define this work win32 for some reason */
304 /* FIXME: No size info */
305 GC_remove_roots (addr
, addr
+ sizeof (gpointer
) + 1);
310 mono_gc_weak_link_add (void **link_addr
, MonoObject
*obj
, gboolean track
)
312 /* libgc requires that we use HIDE_POINTER... */
313 *link_addr
= (void*)HIDE_POINTER (obj
);
314 GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr
, obj
);
318 mono_gc_weak_link_remove (void **link_addr
)
320 GC_unregister_disappearing_link (link_addr
);
325 mono_gc_weak_link_get (void **link_addr
)
327 MonoObject
*obj
= REVEAL_POINTER (*link_addr
);
328 if (obj
== (MonoObject
*) -1)
334 mono_gc_make_descr_for_string (gsize
*bitmap
, int numbits
)
336 return mono_gc_make_descr_from_bitmap (bitmap
, numbits
);
340 mono_gc_make_descr_for_object (gsize
*bitmap
, int numbits
, size_t obj_size
)
342 return mono_gc_make_descr_from_bitmap (bitmap
, numbits
);
346 mono_gc_make_descr_for_array (int vector
, gsize
*elem_bitmap
, int numbits
, size_t elem_size
)
348 /* libgc has no usable support for arrays... */
349 return GC_NO_DESCRIPTOR
;
353 mono_gc_make_descr_from_bitmap (gsize
*bitmap
, int numbits
)
355 #ifdef HAVE_GC_GCJ_MALLOC
356 /* It seems there are issues when the bitmap doesn't fit: play it safe */
358 return GC_NO_DESCRIPTOR
;
360 return (gpointer
)GC_make_descriptor ((GC_bitmap
)bitmap
, numbits
);
367 mono_gc_alloc_fixed (size_t size
, void *descr
)
369 /* To help track down typed allocation bugs */
373 if (count == atoi (getenv ("COUNT2")))
375 if (count > atoi (getenv ("COUNT2")))
376 return GC_MALLOC (size);
380 return GC_MALLOC_EXPLICITLY_TYPED (size
, (GC_descr
)descr
);
382 return GC_MALLOC (size
);
386 mono_gc_free_fixed (void* addr
)
391 mono_gc_invoke_finalizers (void)
393 /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
394 * the 'mem_freed' variable is not initialized when there are no
395 * objects to finalize, which leads to strange behavior later on.
396 * The check is necessary to work around that bug.
398 if (GC_should_invoke_finalizers ())
399 return GC_invoke_finalizers ();
404 mono_gc_pending_finalizers (void)
406 return GC_should_invoke_finalizers ();
410 * LOCKING: Assumes the domain_finalizers lock is held.
413 add_weak_track_handle_internal (MonoDomain
*domain
, MonoObject
*obj
, guint32 gchandle
)
417 if (!domain
->track_resurrection_objects_hash
)
418 domain
->track_resurrection_objects_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
420 refs
= g_hash_table_lookup (domain
->track_resurrection_objects_hash
, obj
);
421 refs
= g_slist_prepend (refs
, GUINT_TO_POINTER (gchandle
));
422 g_hash_table_insert (domain
->track_resurrection_objects_hash
, obj
, refs
);
426 mono_gc_add_weak_track_handle (MonoObject
*obj
, guint32 handle
)
428 MonoDomain
*domain
= mono_domain_get ();
430 mono_domain_finalizers_lock (domain
);
432 add_weak_track_handle_internal (domain
, obj
, handle
);
434 g_hash_table_insert (domain
->track_resurrection_handles_hash
, GUINT_TO_POINTER (handle
), obj
);
436 mono_domain_finalizers_unlock (domain
);
440 * LOCKING: Assumes the domain_finalizers lock is held.
443 remove_weak_track_handle_internal (MonoDomain
*domain
, MonoObject
*obj
, guint32 gchandle
)
447 if (!domain
->track_resurrection_objects_hash
)
450 refs
= g_hash_table_lookup (domain
->track_resurrection_objects_hash
, obj
);
451 refs
= g_slist_remove (refs
, GUINT_TO_POINTER (gchandle
));
452 g_hash_table_insert (domain
->track_resurrection_objects_hash
, obj
, refs
);
456 mono_gc_change_weak_track_handle (MonoObject
*old_obj
, MonoObject
*obj
, guint32 gchandle
)
458 MonoDomain
*domain
= mono_domain_get ();
460 mono_domain_finalizers_lock (domain
);
463 remove_weak_track_handle_internal (domain
, old_obj
, gchandle
);
465 add_weak_track_handle_internal (domain
, obj
, gchandle
);
467 mono_domain_finalizers_unlock (domain
);
471 mono_gc_remove_weak_track_handle (guint32 gchandle
)
473 MonoDomain
*domain
= mono_domain_get ();
476 /* Clean our entries in the two hashes in MonoDomain */
478 mono_domain_finalizers_lock (domain
);
480 /* Get the original object this handle pointed to */
481 obj
= g_hash_table_lookup (domain
->track_resurrection_handles_hash
, GUINT_TO_POINTER (gchandle
));
483 g_hash_table_remove (domain
->track_resurrection_handles_hash
, GUINT_TO_POINTER (gchandle
));
485 remove_weak_track_handle_internal (domain
, obj
, gchandle
);
488 mono_domain_finalizers_unlock (domain
);
492 mono_gc_remove_weak_track_object (MonoDomain
*domain
, MonoObject
*obj
)
496 if (domain
->track_resurrection_objects_hash
) {
497 refs
= g_hash_table_lookup (domain
->track_resurrection_objects_hash
, obj
);
501 * Since we don't run finalizers again for resurrected objects,
502 * no need to keep these around.
504 g_hash_table_remove (domain
->track_resurrection_objects_hash
, obj
);
511 mono_gc_wbarrier_set_field (MonoObject
*obj
, gpointer field_ptr
, MonoObject
* value
)
513 *(void**)field_ptr
= value
;
517 mono_gc_wbarrier_set_arrayref (MonoArray
*arr
, gpointer slot_ptr
, MonoObject
* value
)
519 *(void**)slot_ptr
= value
;
523 mono_gc_wbarrier_arrayref_copy (MonoArray
*arr
, gpointer slot_ptr
, int count
)
525 /* no need to do anything */
529 mono_gc_wbarrier_generic_store (gpointer ptr
, MonoObject
* value
)
531 *(void**)ptr
= value
;
535 mono_gc_wbarrier_value_copy (gpointer dest
, gpointer src
, int count
, MonoClass
*klass
)
540 mono_gc_wbarrier_object (MonoObject
*object
)
544 #if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
545 extern __thread MONO_TLS_FAST
void* GC_thread_tls
;
546 #include "metadata-internals.h"
552 while (!(v
& (1 << i
)))
559 ATYPE_FREEPTR_FOR_BOX
,
567 create_allocator (int atype
, int offset
)
569 int index_var
, bytes_var
, my_fl_var
, my_entry_var
;
570 guint32 no_freelist_branch
, not_small_enough_branch
= 0;
571 guint32 size_overflow_branch
= 0;
572 MonoMethodBuilder
*mb
;
574 MonoMethodSignature
*csig
;
576 if (atype
== ATYPE_STRING
) {
577 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
578 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
579 csig
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
580 csig
->params
[1] = &mono_defaults
.int32_class
->byval_arg
;
582 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
583 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
584 csig
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
587 mb
= mono_mb_new (mono_defaults
.object_class
, "Alloc", MONO_WRAPPER_ALLOC
);
588 bytes_var
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
589 if (atype
== ATYPE_STRING
) {
590 /* a string alloator method takes the args: (vtable, len) */
591 /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
592 mono_mb_emit_ldarg (mb
, 1);
593 mono_mb_emit_icon (mb
, 1);
594 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
595 mono_mb_emit_icon (mb
, 1);
596 mono_mb_emit_byte (mb
, MONO_CEE_SHL
);
597 // sizeof (MonoString) might include padding
598 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoString
, chars
));
599 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
600 mono_mb_emit_stloc (mb
, bytes_var
);
602 /* bytes = vtable->klass->instance_size */
603 mono_mb_emit_ldarg (mb
, 0);
604 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
605 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
606 mono_mb_emit_byte (mb
, MONO_CEE_LDIND_I
);
607 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoClass
, instance_size
));
608 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
609 /* FIXME: assert instance_size stays a 4 byte integer */
610 mono_mb_emit_byte (mb
, MONO_CEE_LDIND_U4
);
611 mono_mb_emit_stloc (mb
, bytes_var
);
614 /* this is needed for strings/arrays only as the other big types are never allocated with this method */
615 if (atype
== ATYPE_STRING
) {
617 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
618 mono_mb_emit_ldloc (mb
, bytes_var
);
619 mono_mb_emit_icon (mb
, (NFREELISTS
-1) * GRANULARITY
);
620 not_small_enough_branch
= mono_mb_emit_short_branch (mb
, MONO_CEE_BGT_UN_S
);
621 /* check for overflow */
622 mono_mb_emit_ldloc (mb
, bytes_var
);
623 mono_mb_emit_icon (mb
, sizeof (MonoString
));
624 size_overflow_branch
= mono_mb_emit_short_branch (mb
, MONO_CEE_BLE_UN_S
);
627 /* int index = INDEX_FROM_BYTES(bytes); */
628 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
630 mono_mb_emit_ldloc (mb
, bytes_var
);
631 mono_mb_emit_icon (mb
, GRANULARITY
- 1);
632 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
633 mono_mb_emit_icon (mb
, shift_amount (GRANULARITY
));
634 mono_mb_emit_byte (mb
, MONO_CEE_SHR_UN
);
635 mono_mb_emit_icon (mb
, shift_amount (sizeof (gpointer
)));
636 mono_mb_emit_byte (mb
, MONO_CEE_SHL
);
637 /* index var is already adjusted into bytes */
638 mono_mb_emit_stloc (mb
, index_var
);
640 my_fl_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
641 my_entry_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
642 /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
643 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
644 mono_mb_emit_byte (mb
, 0x0D); /* CEE_MONO_TLS */
645 mono_mb_emit_i4 (mb
, offset
);
646 if (atype
== ATYPE_FREEPTR
|| atype
== ATYPE_FREEPTR_FOR_BOX
|| atype
== ATYPE_STRING
)
647 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (struct GC_Thread_Rep
, ptrfree_freelists
));
648 else if (atype
== ATYPE_NORMAL
)
649 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (struct GC_Thread_Rep
, normal_freelists
));
650 else if (atype
== ATYPE_GCJ
)
651 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (struct GC_Thread_Rep
, gcj_freelists
));
653 g_assert_not_reached ();
654 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
655 mono_mb_emit_ldloc (mb
, index_var
);
656 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
657 mono_mb_emit_stloc (mb
, my_fl_var
);
659 /* my_entry = *my_fl; */
660 mono_mb_emit_ldloc (mb
, my_fl_var
);
661 mono_mb_emit_byte (mb
, MONO_CEE_LDIND_I
);
662 mono_mb_emit_stloc (mb
, my_entry_var
);
664 /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
665 mono_mb_emit_ldloc (mb
, my_entry_var
);
666 mono_mb_emit_icon (mb
, HBLKSIZE
);
667 no_freelist_branch
= mono_mb_emit_short_branch (mb
, MONO_CEE_BLT_UN_S
);
669 /* ptr_t next = obj_link(my_entry); *my_fl = next; */
670 mono_mb_emit_ldloc (mb
, my_fl_var
);
671 mono_mb_emit_ldloc (mb
, my_entry_var
);
672 mono_mb_emit_byte (mb
, MONO_CEE_LDIND_I
);
673 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I
);
675 /* set the vtable and clear the words in the object */
676 mono_mb_emit_ldloc (mb
, my_entry_var
);
677 mono_mb_emit_ldarg (mb
, 0);
678 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I
);
680 if (atype
== ATYPE_FREEPTR
) {
681 int start_var
, end_var
, start_loop
;
682 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
684 start_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
685 end_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
686 mono_mb_emit_ldloc (mb
, my_entry_var
);
687 mono_mb_emit_ldloc (mb
, bytes_var
);
688 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
689 mono_mb_emit_stloc (mb
, end_var
);
690 mono_mb_emit_ldloc (mb
, my_entry_var
);
691 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoObject
, synchronisation
));
692 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
693 mono_mb_emit_stloc (mb
, start_var
);
697 * } while (start < end);
699 start_loop
= mono_mb_get_label (mb
);
700 mono_mb_emit_ldloc (mb
, start_var
);
701 mono_mb_emit_icon (mb
, 0);
702 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I
);
703 mono_mb_emit_ldloc (mb
, start_var
);
704 mono_mb_emit_icon (mb
, sizeof (gpointer
));
705 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
706 mono_mb_emit_stloc (mb
, start_var
);
708 mono_mb_emit_ldloc (mb
, start_var
);
709 mono_mb_emit_ldloc (mb
, end_var
);
710 mono_mb_emit_byte (mb
, MONO_CEE_BLT_UN_S
);
711 mono_mb_emit_byte (mb
, start_loop
- (mono_mb_get_label (mb
) + 1));
712 } else if (atype
== ATYPE_FREEPTR_FOR_BOX
|| atype
== ATYPE_STRING
) {
713 /* need to clear just the sync pointer */
714 mono_mb_emit_ldloc (mb
, my_entry_var
);
715 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoObject
, synchronisation
));
716 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
717 mono_mb_emit_icon (mb
, 0);
718 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I
);
721 if (atype
== ATYPE_STRING
) {
722 /* need to set length and clear the last char */
723 /* s->length = len; */
724 mono_mb_emit_ldloc (mb
, my_entry_var
);
725 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoString
, length
));
726 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
727 mono_mb_emit_ldarg (mb
, 1);
728 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I4
);
729 /* s->chars [len] = 0; */
730 mono_mb_emit_ldloc (mb
, my_entry_var
);
731 mono_mb_emit_ldloc (mb
, bytes_var
);
732 mono_mb_emit_icon (mb
, 2);
733 mono_mb_emit_byte (mb
, MONO_CEE_SUB
);
734 mono_mb_emit_byte (mb
, MONO_CEE_ADD
);
735 mono_mb_emit_icon (mb
, 0);
736 mono_mb_emit_byte (mb
, MONO_CEE_STIND_I2
);
739 /* return my_entry; */
740 mono_mb_emit_ldloc (mb
, my_entry_var
);
741 mono_mb_emit_byte (mb
, MONO_CEE_RET
);
743 mono_mb_patch_short_branch (mb
, no_freelist_branch
);
744 if (not_small_enough_branch
> 0)
745 mono_mb_patch_short_branch (mb
, not_small_enough_branch
);
746 if (size_overflow_branch
> 0)
747 mono_mb_patch_short_branch (mb
, size_overflow_branch
);
748 /* the slow path: we just call back into the runtime */
749 if (atype
== ATYPE_STRING
) {
750 mono_mb_emit_ldarg (mb
, 1);
751 mono_mb_emit_icall (mb
, mono_string_alloc
);
753 mono_mb_emit_ldarg (mb
, 0);
754 mono_mb_emit_icall (mb
, mono_object_new_specific
);
757 mono_mb_emit_byte (mb
, MONO_CEE_RET
);
759 res
= mono_mb_create_method (mb
, csig
, 8);
761 mono_method_get_header (res
)->init_locals
= FALSE
;
765 static MonoMethod
* alloc_method_cache
[ATYPE_NUM
];
768 * If possible, generate a managed method that can quickly allocate objects in class
769 * @klass. The method will typically have an thread-local inline allocation sequence.
770 * The signature of the called method is:
771 * object allocate (MonoVTable *vtable)
772 * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
774 * The thread local alloc logic is taken from libgc/pthread_support.c.
778 mono_gc_get_managed_allocator (MonoVTable
*vtable
, gboolean for_box
)
782 MonoClass
*klass
= vtable
->klass
;
783 MONO_THREAD_VAR_OFFSET (GC_thread_tls
, offset
);
785 /*g_print ("thread tls: %d\n", offset);*/
788 if (!SMALL_ENOUGH (klass
->instance_size
))
790 if (klass
->has_finalize
|| klass
->marshalbyref
|| (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS
))
794 if (klass
->byval_arg
.type
== MONO_TYPE_STRING
) {
795 atype
= ATYPE_STRING
;
796 } else if (!klass
->has_references
) {
798 atype
= ATYPE_FREEPTR_FOR_BOX
;
800 atype
= ATYPE_FREEPTR
;
804 * disabled because we currently do a runtime choice anyway, to
805 * deal with multiple appdomains.
806 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
809 atype = ATYPE_NORMAL;
812 return mono_gc_get_managed_allocator_by_type (atype
);
816 * mono_gc_get_managed_allocator_id:
818 * Return a type for the managed allocator method MANAGED_ALLOC which can later be passed
819 * to mono_gc_get_managed_allocator_by_type () to get back this allocator method. This can be
820 * used by the AOT code to encode references to managed allocator methods.
823 mono_gc_get_managed_allocator_type (MonoMethod
*managed_alloc
)
828 for (i
= 0; i
< ATYPE_NUM
; ++i
) {
829 if (alloc_method_cache
[i
] == managed_alloc
) {
830 mono_loader_unlock ();
834 mono_loader_unlock ();
840 * mono_gc_get_managed_allocator_by_type:
842 * Return a managed allocator method corresponding to allocator type ATYPE.
845 mono_gc_get_managed_allocator_by_type (int atype
)
849 MONO_THREAD_VAR_OFFSET (GC_thread_tls
, offset
);
852 res
= alloc_method_cache
[atype
];
854 res
= alloc_method_cache
[atype
] = create_allocator (atype
, offset
);
855 mono_loader_unlock ();
860 mono_gc_get_managed_allocator_types (void)
868 mono_gc_get_managed_allocator (MonoVTable
*vtable
, gboolean for_box
)
874 mono_gc_get_managed_allocator_type (MonoMethod
*managed_alloc
)
880 mono_gc_get_managed_allocator_by_type (int atype
)
886 mono_gc_get_managed_allocator_types (void)
893 #endif /* no Boehm GC */