Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / loader.c
index d9781cbdfdd8e849e0cc4d742fb4665eb6cbd4c9..a5ef34da2d6e5901cd3c2e704b90bb3fdeeeb3b6 100644 (file)
@@ -35,7 +35,6 @@
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/reflection.h>
-#include <mono/metadata/profiler.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/marshal.h>
@@ -63,10 +62,10 @@ static mono_mutex_t global_loader_data_mutex;
 static gboolean loader_lock_inited;
 
 /* Statistics */
-static guint32 inflated_signatures_size;
-static guint32 memberref_sig_cache_size;
-static guint32 methods_size;
-static guint32 signatures_size;
+static gint32 inflated_signatures_size;
+static gint32 memberref_sig_cache_size;
+static gint32 methods_size;
+static gint32 signatures_size;
 
 /*
  * This TLS variable holds how many times the current thread has acquired the loader 
@@ -75,7 +74,10 @@ static guint32 signatures_size;
 MonoNativeTlsKey loader_lock_nest_id;
 
 static void dllmap_cleanup (void);
+static void cached_module_cleanup(void);
 
+/* Class lazy loading functions */
+GENERATE_GET_CLASS_WITH_CACHE (appdomain_unloaded_exception, "System", "AppDomainUnloadedException")
 
 static void
 global_loader_data_lock (void)
@@ -119,6 +121,7 @@ void
 mono_loader_cleanup (void)
 {
        dllmap_cleanup ();
+       cached_module_cleanup ();
 
        mono_native_tls_free (loader_lock_nest_id);
 
@@ -165,7 +168,7 @@ cache_memberref_sig (MonoImage *image, guint32 sig_idx, gpointer sig)
        else {
                g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (sig_idx), sig);
                /* An approximation based on glib 2.18 */
-               memberref_sig_cache_size += sizeof (gpointer) * 4;
+               InterlockedAdd (&memberref_sig_cache_size, sizeof (gpointer) * 4);
        }
        mono_image_unlock (image);
 
@@ -314,9 +317,17 @@ mono_field_from_token_checked (MonoImage *image, guint32 token, MonoClass **retk
                mono_class_init (k);
                if (retklass)
                        *retklass = k;
-               field = mono_class_get_field (k, token);
-               if (!field) {
-                       mono_error_set_bad_image (error, image, "Could not resolve field token 0x%08x", token);
+               if (mono_class_has_failure (k)) {
+                       MonoError causedby_error;
+                       error_init (&causedby_error);
+                       mono_error_set_for_class_failure (&causedby_error, k);
+                       mono_error_set_bad_image (error, image, "Could not resolve field token 0x%08x, due to: %s", token, mono_error_get_message (&causedby_error));
+                       mono_error_cleanup (&causedby_error);
+               } else {
+                       field = mono_class_get_field (k, token);
+                       if (!field) {
+                               mono_error_set_bad_image (error, image, "Could not resolve field token 0x%08x", token);
+                       }
                }
        }
 
@@ -726,7 +737,7 @@ mono_method_get_signature_checked (MonoMethod *method, MonoImage *image, guint32
                if (cached != sig)
                        mono_metadata_free_inflated_signature (sig);
                else
-                       inflated_signatures_size += mono_metadata_signature_size (cached);
+                       InterlockedAdd (&inflated_signatures_size, mono_metadata_signature_size (cached));
                sig = cached;
        }
 
@@ -1139,12 +1150,29 @@ mono_loader_register_module (const char *name, MonoDl *module)
        g_hash_table_insert (global_module_map, g_strdup (name), module);
 }
 
+static void
+remove_cached_module(gpointer key, gpointer value, gpointer user_data)
+{
+       mono_dl_close((MonoDl*)value);
+}
+
+static void
+cached_module_cleanup(void)
+{
+       if (global_module_map != NULL) {
+               g_hash_table_foreach(global_module_map, remove_cached_module, NULL);
+
+               g_hash_table_destroy(global_module_map);
+               global_module_map = NULL;
+       }
+}
+
 static MonoDl *internal_module;
 
 static gboolean
 is_absolute_path (const char *path)
 {
-#ifdef PLATFORM_MACOSX
+#ifdef HOST_DARWIN
        if (!strncmp (path, "@executable_path/", 17) || !strncmp (path, "@loader_path/", 13) ||
            !strncmp (path, "@rpath/", 7))
            return TRUE;
@@ -1656,10 +1684,10 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
        } else {
                result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethod));
-               methods_size += sizeof (MonoMethod);
+               InterlockedAdd (&methods_size, sizeof (MonoMethod));
        }
 
-       mono_stats.method_count ++;
+       InterlockedIncrement (&mono_stats.method_count);
 
        result->slot = -1;
        result->klass = klass;
@@ -1888,7 +1916,7 @@ mono_get_method_constrained_checked (MonoImage *image, guint32 token, MonoClass
 {
        error_init (error);
 
-       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL, error);
+       *cil_method = mono_get_method_checked (image, token, NULL, context, error);
        if (!*cil_method)
                return NULL;
 
@@ -1901,11 +1929,10 @@ mono_get_method_constrained_checked (MonoImage *image, guint32 token, MonoClass
 void
 mono_free_method  (MonoMethod *method)
 {
-       if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
-               mono_profiler_method_free (method);
+       MONO_PROFILER_RAISE (method_free, (method));
        
        /* FIXME: This hack will go away when the profiler will support freeing methods */
-       if (mono_profiler_get_events () != MONO_PROFILE_NONE)
+       if (G_UNLIKELY (mono_profiler_installed ()))
                return;
        
        if (method->signature) {
@@ -2396,7 +2423,7 @@ mono_method_signature_checked (MonoMethod *m, MonoError *error)
                if (!mono_error_ok (error))
                        return NULL;
 
-               inflated_signatures_size += mono_metadata_signature_size (signature);
+               InterlockedAdd (&inflated_signatures_size, mono_metadata_signature_size (signature));
 
                mono_image_lock (img);
 
@@ -2453,7 +2480,7 @@ mono_method_signature_checked (MonoMethod *m, MonoError *error)
                        mono_image_unlock (img);
                }
 
-               signatures_size += mono_metadata_signature_size (signature);
+               InterlockedAdd (&signatures_size, mono_metadata_signature_size (signature));
        }
 
        /* Verify metadata consistency */