* roottypes.cs: Rename from tree.cs.
[mono.git] / mono / metadata / domain.c
index dd5f95841c0e29b38dc2ff595c915337acf54ea5..59098db4706daabf1952e0f5685c4ac0aa6ebe9c 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * domain.c: MonoDomain functions
  *
@@ -17,6 +16,7 @@
 #include <mono/os/gc_wrapper.h>
 
 #include <mono/utils/mono-compiler.h>
+#include <mono/utils/mono-logger.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/domain-internals.h>
@@ -28,6 +28,7 @@
 #include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/mono-debug-debugger.h>
 #include <metadata/threads.h>
 
 /* #define DEBUG_DOMAIN_UNLOAD */
@@ -55,7 +56,7 @@ static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
 #endif
 
 #define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
-#define SET_APPCONTEXT(x) mono_thread_current ()->current_appcontext = (x)
+#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
 
 static guint16 appdomain_list_size = 0;
 static guint16 appdomain_next = 0;
@@ -198,7 +199,7 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
 
        if (ji == NULL) {
                /* Maybe its an AOT module */
-               MonoImage *image = mono_jit_info_find_aot_module (addr);
+               MonoImage *image = mono_jit_info_find_aot_module ((guint8*)addr);
                if (image)
                        ji = jit_info_find_in_aot_func (domain, image, addr);
        }
@@ -439,12 +440,26 @@ domain_id_alloc (MonoDomain *domain)
        return id;
 }
 
+static guint32 domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
+static gpointer domain_gc_desc = NULL;
+
 MonoDomain *
 mono_domain_create (void)
 {
        MonoDomain *domain;
 
-       domain = mono_gc_alloc_fixed (sizeof (MonoDomain), NULL);
+       mono_appdomains_lock ();
+       if (!domain_gc_desc) {
+               unsigned int i, bit = 0;
+               for (i = G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_OBJECT); i < G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED); i += sizeof (gpointer)) {
+                       bit = i / sizeof (gpointer);
+                       domain_gc_bitmap [bit / 32] |= 1 << (bit % 32);
+               }
+               domain_gc_desc = mono_gc_make_descr_from_bitmap (domain_gc_bitmap, bit + 1);
+       }
+       mono_appdomains_unlock ();
+
+       domain = mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc);
        domain->domain = NULL;
        domain->setup = NULL;
        domain->friendly_name = NULL;
@@ -452,11 +467,11 @@ mono_domain_create (void)
 
        domain->mp = mono_mempool_new ();
        domain->code_mp = mono_code_manager_new ();
-       domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
+       domain->env = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC);
        domain->domain_assemblies = NULL;
        domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
-       domain->static_data_hash = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
+       domain->proxy_vtable_hash = g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
+       domain->static_data_array = NULL;
        domain->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
        domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
        domain->jit_info_table = mono_jit_info_table_new ();
@@ -511,6 +526,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_raw_buffer_init ();
        mono_images_init ();
        mono_assemblies_init ();
+       mono_classes_init ();
        mono_loader_init ();
 
        /* FIXME: When should we release this memory? */
@@ -676,6 +692,10 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
        g_assert (mono_defaults.fieldhandle_class != 0);
 
+       mono_defaults.systemtype_class = mono_class_from_name (
+                mono_defaults.corlib, "System", "Type");
+       g_assert (mono_defaults.systemtype_class != 0);
+
        mono_defaults.monotype_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "MonoType");
        g_assert (mono_defaults.monotype_class != 0);
@@ -774,6 +794,15 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_defaults.executioncontext_class = mono_class_from_name (
                mono_defaults.corlib, "System.Threading", "ExecutionContext");
 
+       mono_defaults.internals_visible_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
+
+       mono_defaults.variant_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "Variant");
+
+       mono_defaults.com_object_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "__ComObject");
+
        /*
         * Note that mono_defaults.generic_*_class is only non-NULL if we're
         * using the 2.0 corlib.
@@ -839,6 +868,30 @@ mono_init_version (const char *domain_name, const char *version)
        return mono_init_internal (domain_name, NULL, version);
 }
 
+/**
+ * mono_cleanup:
+ *
+ * Cleans up all metadata modules. 
+ */
+void
+mono_cleanup (void)
+{
+       mono_loader_cleanup ();
+       mono_classes_cleanup ();
+       mono_assemblies_cleanup ();
+       mono_images_cleanup ();
+       mono_raw_buffer_cleanup ();
+       mono_metadata_cleanup ();
+
+       TlsFree (appdomain_thread_id);
+       DeleteCriticalSection (&appdomains_mutex);
+}
+
+/**
+ * mono_get_root_domain:
+ *
+ * Returns: the root appdomain.
+ */
 MonoDomain*
 mono_get_root_domain (void)
 {
@@ -962,24 +1015,26 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->null_reference_ex = NULL;
        domain->stack_overflow_ex = NULL;
        domain->entry_assembly = NULL;
-       g_free (domain->friendly_name);
-       domain->friendly_name = NULL;
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
                MonoAssembly *ass = tmp->data;
-               /*g_print ("Unloading domain %p, assembly %s, refcount: %d\n", domain, ass->aname.name, ass->ref_count);*/
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading domain %s %p, assembly %s %p, refcount=%d\n", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
                mono_assembly_close (ass);
        }
        g_slist_free (domain->domain_assemblies);
        domain->domain_assemblies = NULL;
 
+       g_free (domain->friendly_name);
+       domain->friendly_name = NULL;
        mono_g_hash_table_destroy (domain->env);
        domain->env = NULL;
        g_hash_table_destroy (domain->class_vtable_hash);
        domain->class_vtable_hash = NULL;
-       mono_g_hash_table_destroy (domain->proxy_vtable_hash);
+       g_hash_table_destroy (domain->proxy_vtable_hash);
        domain->proxy_vtable_hash = NULL;
-       mono_g_hash_table_destroy (domain->static_data_hash);
-       domain->static_data_hash = NULL;
+       if (domain->static_data_array) {
+               mono_gc_free_fixed (domain->static_data_array);
+               domain->static_data_array = NULL;
+       }
        g_hash_table_destroy (domain->jit_code_hash);
        domain->jit_code_hash = NULL;
        if (domain->dynamic_code_hash) {
@@ -1077,6 +1132,37 @@ mono_context_get (void)
        return GET_APPCONTEXT ();
 }
 
+/* LOCKING: the caller holds the lock for this domain */
+void
+mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap)
+{
+       /* The first entry in the array is the index of the next free slot
+        * and the total size of the array
+        */
+       int next;
+       if (domain->static_data_array) {
+               int size = GPOINTER_TO_INT (domain->static_data_array [1]);
+               next = GPOINTER_TO_INT (domain->static_data_array [0]);
+               if (next >= size) {
+                       gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), NULL);
+                       memcpy (new_array, domain->static_data_array, sizeof (gpointer) * size);
+                       size *= 2;
+                       new_array [1] = GINT_TO_POINTER (size);
+                       mono_gc_free_fixed (domain->static_data_array);
+                       domain->static_data_array = new_array;
+               }
+       } else {
+               int size = 32;
+               gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, NULL);
+               next = 2;
+               new_array [0] = GINT_TO_POINTER (next);
+               new_array [1] = GINT_TO_POINTER (size);
+               domain->static_data_array = new_array;
+       }
+       domain->static_data_array [next++] = data;
+       domain->static_data_array [0] = GINT_TO_POINTER (next);
+}
+
 MonoImage*
 mono_get_corlib (void)
 {
@@ -1383,6 +1469,12 @@ get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
                return;
        }
 
+       /* 
+        * FIXME: This would cause us to unload the image, and it will be loaded again later.
+        * Disabling it will mean the initial exe will not be unloaded on shutdown.
+        */
+       //mono_image_close (image);
+
        runtimes [0] = get_runtime_by_version (image->version);
        runtimes [1] = NULL;
 }