Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
authorJonathan Chambers <joncham@gmail.com>
Fri, 13 Jan 2017 19:20:06 +0000 (14:20 -0500)
committerJonathan Chambers <joncham@gmail.com>
Tue, 17 Jan 2017 20:59:44 +0000 (15:59 -0500)
mono/metadata/boehm-gc.c
mono/metadata/domain.c
mono/metadata/gc-internals.h
mono/metadata/mono-hash.c
mono/metadata/object.c
mono/metadata/threads.c
mono/mini/debugger-agent.c

index 09e06b81530e70c9be9c5b306dbe4e423a5a0e08..dcfd310f6490ebdd894c863b32e8c65d6ff52f6d 100644 (file)
@@ -259,9 +259,6 @@ mono_gc_base_init (void)
        GC_set_on_collection_event (on_gc_notification);
        GC_on_heap_resize = on_gc_heap_resize;
 
-       MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_NORMAL].entries, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
-       MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_PINNED].entries, MONO_ROOT_SOURCE_GC_HANDLE, "gc handles table");
-
        gc_initialized = TRUE;
 }
 
@@ -538,6 +535,12 @@ mono_gc_register_root (char *start, size_t size, void *descr, MonoGCRootSource s
        return TRUE;
 }
 
+int
+mono_gc_register_root_wbarrier (char *start, size_t size, MonoGCDescriptor descr, MonoGCRootSource source, const char *msg)
+{
+       return mono_gc_register_root (start, size, descr, source, msg);
+}
+
 void
 mono_gc_deregister_root (char* addr)
 {
@@ -623,25 +626,13 @@ mono_gc_make_root_descr_all_refs (int numbits)
 void*
 mono_gc_alloc_fixed (size_t size, void *descr, MonoGCRootSource source, const char *msg)
 {
-       /* To help track down typed allocation bugs */
-       /*
-       static int count;
-       count ++;
-       if (count == atoi (g_getenv ("COUNT2")))
-               printf ("HIT!\n");
-       if (count > atoi (g_getenv ("COUNT2")))
-               return GC_MALLOC (size);
-       */
-
-       if (descr)
-               return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
-       else
-               return GC_MALLOC (size);
+       return GC_MALLOC_UNCOLLECTABLE (size);
 }
 
 void
 mono_gc_free_fixed (void* addr)
 {
+       GC_FREE (addr);
 }
 
 void *
index 76cfe9979db3b8498e9f195c71787832da9ed2d0..39f265d5f3d9f32b4d3a78d936618cd61087bafe 100644 (file)
@@ -383,13 +383,7 @@ mono_domain_create (void)
        mono_appdomains_unlock ();
 
 #ifdef HAVE_BOEHM_GC
-       /*
-        * Boehm doesn't like roots inside GC allocated objects, and alloc_fixed returns
-        * a GC_MALLOC-ed object, contrary to the api docs. This causes random crashes when
-        * running the corlib test suite.
-        * To solve this, we pass a NULL descriptor, and don't register roots.
-        */
-       domain = (MonoDomain *)mono_gc_alloc_fixed (sizeof (MonoDomain), NULL, MONO_ROOT_SOURCE_DOMAIN, "domain object");
+       domain = (MonoDomain *)mono_gc_alloc_fixed (sizeof (MonoDomain), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_DOMAIN, "domain object");
 #else
        domain = (MonoDomain *)mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc, MONO_ROOT_SOURCE_DOMAIN, "domain object");
        mono_gc_register_root ((char*)&(domain->MONO_DOMAIN_FIRST_GC_TRACKED), G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_LAST_GC_TRACKED) - G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_DOMAIN, "misc domain fields");
@@ -514,9 +508,6 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_reflection_init ();
        mono_runtime_init_tls ();
 
-       /* FIXME: When should we release this memory? */
-       MONO_GC_REGISTER_ROOT_FIXED (appdomains_list, MONO_ROOT_SOURCE_DOMAIN, "domains list");
-
        domain = mono_domain_create ();
        mono_root_domain = domain;
 
index 2ef55c6e6733baa729f9a37c2bbb544d82c7d03d..a11a2f5b501f476f8b3bd621c766d02088c8e89e 100644 (file)
 
 #define MONO_GC_UNREGISTER_ROOT(x) mono_gc_deregister_root ((char*)&(x))
 
-/*
- * Register a memory location as a root pointing to memory allocated using
- * mono_gc_alloc_fixed (). This includes MonoGHashTable.
- */
-/* The result of alloc_fixed () is not GC tracked memory */
-#define MONO_GC_REGISTER_ROOT_FIXED(x,src,msg) do { \
-       if (!mono_gc_is_moving ())                              \
-               MONO_GC_REGISTER_ROOT_PINNING ((x),(src),(msg)); \
-       } while (0)
-
 /*
  * Return a GC descriptor for an array containing N pointers to memory allocated
  * by mono_gc_alloc_fixed ().
@@ -131,8 +121,6 @@ gboolean mono_gc_user_markers_supported (void);
  * The memory is non-moving and it will be explicitly deallocated.
  * size bytes will be available from the returned address (ie, descr
  * must not be stored in the returned memory)
- * NOTE: Under Boehm, this returns memory allocated using GC_malloc, so the result should
- * be stored into a location registered using MONO_GC_REGISTER_ROOT_FIXED ().
  */
 void* mono_gc_alloc_fixed            (size_t size, MonoGCDescriptor descr, MonoGCRootSource source, const char *msg);
 void  mono_gc_free_fixed             (void* addr);
index 075c62f129bbd06f19442241c2ee0777aca36c8b..00f7afab0ccea8eb71d5cb11bdc7adef687aa1a4 100644 (file)
@@ -69,9 +69,6 @@ struct _MonoGHashTable {
        const char *msg;
 };
 
-static MonoGHashTable *
-mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func);
-
 #ifdef HAVE_SGEN_GC
 static MonoGCDescriptor table_hash_descr = MONO_GC_DESCRIPTOR_NULL;
 
@@ -122,7 +119,25 @@ calc_prime (int x)
 MonoGHashTable *
 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
 {
-       MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
+       MonoGHashTable *hash;
+
+       if (hash_func == NULL)
+               hash_func = g_direct_hash;
+       if (key_equal_func == NULL)
+               key_equal_func = g_direct_equal;
+
+#ifdef HAVE_SGEN_GC
+       hash = mg_new0 (MonoGHashTable, 1);
+#else
+       hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
+#endif
+
+       hash->hash_func = hash_func;
+       hash->key_equal_func = key_equal_func;
+
+       hash->table_size = g_spaced_primes_closest (1);
+       hash->table = mg_new0 (Slot *, hash->table_size);
+       hash->last_rehash = hash->table_size;
 
        hash->gc_type = type;
        hash->source = source;
@@ -144,27 +159,6 @@ mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, Mono
        return hash;
 }
 
-static MonoGHashTable *
-mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
-{
-       MonoGHashTable *hash;
-
-       if (hash_func == NULL)
-               hash_func = g_direct_hash;
-       if (key_equal_func == NULL)
-               key_equal_func = g_direct_equal;
-       hash = mg_new0 (MonoGHashTable, 1);
-
-       hash->hash_func = hash_func;
-       hash->key_equal_func = key_equal_func;
-
-       hash->table_size = g_spaced_primes_closest (1);
-       hash->table = mg_new0 (Slot *, hash->table_size);
-       hash->last_rehash = hash->table_size;
-       
-       return hash;
-}
-
 typedef struct {
        MonoGHashTable *hash;
        int new_size;
@@ -404,7 +398,11 @@ mono_g_hash_table_destroy (MonoGHashTable *hash)
                }
        }
        mg_free (hash->table);
+#ifdef HAVE_SGEN_GC
        mg_free (hash);
+#else
+       mono_gc_free_fixed (hash);
+#endif
 }
 
 static void
index 97aea9e71681d690d9de43d67897360f7e3e79d9..b1264232746cc1052014c0c5ed8aa08f11cd380c 100644 (file)
@@ -748,18 +748,11 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int
 
                        type = mono_type_get_underlying_type (field->type);
                        switch (type->type) {
+                       case MONO_TYPE_U:
                        case MONO_TYPE_I:
                        case MONO_TYPE_PTR:
                        case MONO_TYPE_FNPTR:
                                break;
-                       /* only UIntPtr is allowed to be GC-tracked and only in mscorlib */
-                       case MONO_TYPE_U:
-#ifdef HAVE_SGEN_GC
-                               break;
-#else
-                               if (klass->image != mono_defaults.corlib)
-                                       break;
-#endif
                        case MONO_TYPE_STRING:
                        case MONO_TYPE_SZARRAY:
                        case MONO_TYPE_CLASS:
index fd424bdfb90ca00d66a27b7c722c9f3dda8eee3d..82590363f6fc7b0d528c9445465b46d3cf646709 100644 (file)
@@ -538,7 +538,6 @@ mono_thread_attach_internal (MonoThread *thread, gboolean force_attach, gboolean
        }
 
        if (!threads) {
-               MONO_GC_REGISTER_ROOT_FIXED (threads, MONO_ROOT_SOURCE_THREADING, "threads table");
                threads = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_THREADING, "threads table");
        }
 
@@ -762,7 +761,6 @@ create_thread (MonoThread *thread, MonoInternalThread *internal, MonoObject *sta
                return FALSE;
        }
        if (threads_starting_up == NULL) {
-               MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up, MONO_ROOT_SOURCE_THREADING, "starting threads table");
                threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC, MONO_ROOT_SOURCE_THREADING, "starting threads table");
        }
        mono_g_hash_table_insert (threads_starting_up, thread, thread);
index 0e95ae85f204cbb403abaf29880c74fde4690c6c..98381c28f19073d5cd037a10bbff5b98279ca717 100644 (file)
@@ -993,13 +993,10 @@ mono_debugger_agent_init (void)
        mono_gc_base_init ();
 
        thread_to_tls = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "thread-to-tls table");
-       MONO_GC_REGISTER_ROOT_FIXED (thread_to_tls, MONO_ROOT_SOURCE_DEBUGGER, "thread-to-tls table");
 
        tid_to_thread = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread table");
-       MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread table");
 
        tid_to_thread_obj = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread object table");
-       MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread_obj, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread object table");
 
        pending_assembly_loads = g_ptr_array_new ();
        domains = g_hash_table_new (mono_aligned_addr_hash, NULL);
@@ -1937,7 +1934,6 @@ objrefs_init (void)
        objrefs = g_hash_table_new_full (NULL, NULL, NULL, free_objref);
        obj_to_objref = g_hash_table_new (NULL, NULL);
        suspended_objs = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "suspended objects table");
-       MONO_GC_REGISTER_ROOT_FIXED (suspended_objs, MONO_ROOT_SOURCE_DEBUGGER, "suspended objects table");
 }
 
 static void