Merge pull request #2609 from directhex/xambug-30902
[mono.git] / mono / metadata / sgen-mono.c
index f6ae431afde262f5a525b75c5a8505e8ef23090d..8f86228dce17bece01892b1e24934f67aa7af264 100644 (file)
@@ -34,6 +34,7 @@
 #include "metadata/runtime.h"
 #include "metadata/sgen-bridge-internals.h"
 #include "metadata/gc-internals.h"
+#include "metadata/handle.h"
 #include "utils/mono-memory-model.h"
 #include "utils/mono-logger-internals.h"
 
@@ -935,8 +936,10 @@ mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
 {
        MonoObject *obj = sgen_alloc_obj (vtable, size);
 
-       if (G_UNLIKELY (alloc_events))
-               mono_profiler_allocation (obj);
+       if (G_UNLIKELY (alloc_events)) {
+               if (obj)
+                       mono_profiler_allocation (obj);
+       }
 
        return obj;
 }
@@ -946,22 +949,23 @@ mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
 {
        MonoObject *obj = sgen_alloc_obj_pinned (vtable, size);
 
-       if (G_UNLIKELY (alloc_events))
-               mono_profiler_allocation (obj);
+       if (G_UNLIKELY (alloc_events)) {
+               if (obj)
+                       mono_profiler_allocation (obj);
+       }
 
        return obj;
 }
 
 void*
-mono_gc_alloc_mature (MonoVTable *vtable)
+mono_gc_alloc_mature (MonoVTable *vtable, size_t size)
 {
-       MonoObject *obj = sgen_alloc_obj_mature (vtable, vtable->klass->instance_size);
-
-       if (obj && G_UNLIKELY (obj->vtable->klass->has_finalize))
-               mono_object_register_finalizer (obj);
+       MonoObject *obj = sgen_alloc_obj_mature (vtable, size);
 
-       if (G_UNLIKELY (alloc_events))
-               mono_profiler_allocation (obj);
+       if (G_UNLIKELY (alloc_events)) {
+               if (obj)
+                       mono_profiler_allocation (obj);
+       }
 
        return obj;
 }
@@ -1104,16 +1108,16 @@ create_allocator (int atype, gboolean slowpath)
                case ATYPE_NORMAL:
                case ATYPE_SMALL:
                        mono_mb_emit_ldarg (mb, 0);
-                       mono_mb_emit_icall (mb, mono_object_new_specific);
+                       mono_mb_emit_icall (mb, ves_icall_object_new_specific);
                        break;
                case ATYPE_VECTOR:
                        mono_mb_emit_ldarg (mb, 0);
                        mono_mb_emit_ldarg (mb, 1);
-                       mono_mb_emit_icall (mb, mono_array_new_specific);
+                       mono_mb_emit_icall (mb, ves_icall_array_new_specific);
                        break;
                case ATYPE_STRING:
                        mono_mb_emit_ldarg (mb, 1);
-                       mono_mb_emit_icall (mb, mono_string_alloc);
+                       mono_mb_emit_icall (mb, ves_icall_string_alloc);
                        break;
                default:
                        g_assert_not_reached ();
@@ -1400,10 +1404,7 @@ create_allocator (int atype, gboolean slowpath)
 int
 mono_gc_get_aligned_size_for_allocator (int size)
 {
-       int aligned_size = size;
-       aligned_size += SGEN_ALLOC_ALIGN - 1;
-       aligned_size &= ~(SGEN_ALLOC_ALIGN - 1);
-       return aligned_size;
+       return SGEN_ALIGN_UP (size);
 }
 
 /*
@@ -1735,7 +1736,7 @@ mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
        arr = (MonoArray*)sgen_alloc_obj_nolock (vtable, size);
        if (G_UNLIKELY (!arr)) {
                UNLOCK_GC;
-               return mono_gc_out_of_memory (size);
+               return NULL;
        }
 
        arr->max_length = (mono_array_size_t)max_length;
@@ -1780,7 +1781,7 @@ mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uint
        arr = (MonoArray*)sgen_alloc_obj_nolock (vtable, size);
        if (G_UNLIKELY (!arr)) {
                UNLOCK_GC;
-               return mono_gc_out_of_memory (size);
+               return NULL;
        }
 
        arr->max_length = (mono_array_size_t)max_length;
@@ -1824,7 +1825,7 @@ mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
        str = (MonoString*)sgen_alloc_obj_nolock (vtable, size);
        if (G_UNLIKELY (!str)) {
                UNLOCK_GC;
-               return mono_gc_out_of_memory (size);
+               return NULL;
        }
 
        str->length = len;
@@ -2276,6 +2277,8 @@ thread_in_critical_region (SgenThreadInfo *info)
 static void
 sgen_thread_attach (SgenThreadInfo *info)
 {
+       mono_handle_arena_init ((MonoHandleArena**) &info->client_info.info.handle_arena);
+
        if (mono_gc_get_gc_callbacks ()->thread_attach_func && !info->client_info.runtime_data)
                info->client_info.runtime_data = mono_gc_get_gc_callbacks ()->thread_attach_func ();
 }
@@ -2291,6 +2294,8 @@ sgen_thread_detach (SgenThreadInfo *p)
         */
        if (mono_domain_get ())
                mono_thread_detach_internal (mono_thread_internal_current ());
+
+       mono_handle_arena_cleanup ((MonoHandleArena**) &p->client_info.info.handle_arena);
 }
 
 gboolean
@@ -2377,7 +2382,7 @@ sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean p
 
                if (!precise) {
 #ifdef USE_MONO_CTX
-                       sgen_conservatively_pin_objects_from ((void**)&info->client_info.ctx, (void**)&info->client_info.ctx + ARCH_NUM_REGS,
+                       sgen_conservatively_pin_objects_from ((void**)&info->client_info.ctx, (void**)(&info->client_info.ctx + 1),
                                start_nursery, end_nursery, PIN_TYPE_STACK);
 #else
                        sgen_conservatively_pin_objects_from ((void**)&info->client_info.regs, (void**)&info->client_info.regs + ARCH_NUM_REGS,
@@ -2600,7 +2605,7 @@ sgen_client_metadata_for_object (GCObject *obj)
  * @gchandle: a GCHandle's handle.
  * @domain: An application domain.
  *
- * Returns: true if the object wrapped by the @gchandle belongs to the specific @domain.
+ * Returns: TRUE if the object wrapped by the @gchandle belongs to the specific @domain.
  */
 gboolean
 mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
@@ -2704,12 +2709,6 @@ mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack,
        // FIXME:
 }
 
-void
-sgen_client_out_of_memory (size_t size)
-{
-       mono_gc_out_of_memory (size);
-}
-
 guint8*
 mono_gc_get_card_table (int *shift_bits, gpointer *mask)
 {
@@ -2739,7 +2738,7 @@ sgen_client_degraded_allocation (size_t size)
        static int last_major_gc_warned = -1;
        static int num_degraded = 0;
 
-       if (last_major_gc_warned < gc_stats.major_gc_count) {
+       if (last_major_gc_warned < (int)gc_stats.major_gc_count) {
                ++num_degraded;
                if (num_degraded == 1 || num_degraded == 3)
                        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "Warning: Degraded allocation.  Consider increasing nursery-size if the warning persists.");
@@ -2980,6 +2979,12 @@ mono_gc_base_init (void)
 
        if (nursery_canaries_enabled ())
                sgen_set_use_managed_allocator (FALSE);
+
+#if defined(HAVE_KW_THREAD)
+       /* This can happen with using libmonosgen.so */
+       if (mono_tls_key_get_offset (TLS_KEY_SGEN_TLAB_NEXT_ADDR) == -1)
+               sgen_set_use_managed_allocator (FALSE);
+#endif
 }
 
 void