Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / loader.c
index 0cd4e07b7395ddb3c828a725489412460d6fea31..a5ef34da2d6e5901cd3c2e704b90bb3fdeeeb3b6 100644 (file)
@@ -62,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 
@@ -74,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)
@@ -118,6 +121,7 @@ void
 mono_loader_cleanup (void)
 {
        dllmap_cleanup ();
+       cached_module_cleanup ();
 
        mono_native_tls_free (loader_lock_nest_id);
 
@@ -164,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);
 
@@ -313,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);
+                       }
                }
        }
 
@@ -725,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;
        }
 
@@ -1138,6 +1150,23 @@ 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
@@ -1655,7 +1684,7 @@ 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));
        }
 
        InterlockedIncrement (&mono_stats.method_count);
@@ -2394,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);
 
@@ -2451,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 */