Merge pull request #2237 from xmcclure/container-owner
[mono.git] / mono / mini / mini-generic-sharing.c
index 6bec18405f58795a689385a065de24ab4315cfe7..a90afecd82dd6e8a2ca9cbcb925f6d6383e84220 100644 (file)
 static void
 mono_class_unregister_image_generic_subclasses (MonoImage *image, gpointer user_data);
 
-static gboolean partial_supported = FALSE;
+/* Counters */
+static int num_templates_allocted;
+static int num_templates_bytes;
+static int num_oti_allocted;
+static int num_oti_bytes;
+
+static gboolean partial_supported = TRUE;
 
 static inline gboolean
 partial_sharing_supported (void)
@@ -340,41 +346,22 @@ mono_class_unregister_image_generic_subclasses (MonoImage *image, gpointer user_
 static MonoRuntimeGenericContextTemplate*
 alloc_template (MonoClass *klass)
 {
-       static gboolean inited = FALSE;
-       static int num_allocted = 0;
-       static int num_bytes = 0;
-
        int size = sizeof (MonoRuntimeGenericContextTemplate);
 
-       if (!inited) {
-               mono_counters_register ("RGCTX template num allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_allocted);
-               mono_counters_register ("RGCTX template bytes allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_bytes);
-               inited = TRUE;
-       }
-
-       num_allocted++;
-       num_bytes += size;
+       num_templates_allocted++;
+       num_templates_bytes += size;
 
        return mono_image_alloc0 (klass->image, size);
 }
 
+/* LOCKING: Takes the loader lock */
 static MonoRuntimeGenericContextInfoTemplate*
 alloc_oti (MonoImage *image)
 {
-       static gboolean inited = FALSE;
-       static int num_allocted = 0;
-       static int num_bytes = 0;
-
        int size = sizeof (MonoRuntimeGenericContextInfoTemplate);
 
-       if (!inited) {
-               mono_counters_register ("RGCTX oti num allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_allocted);
-               mono_counters_register ("RGCTX oti bytes allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_bytes);
-               inited = TRUE;
-       }
-
-       num_allocted++;
-       num_bytes += size;
+       num_oti_allocted++;
+       num_oti_bytes += size;
 
        return mono_image_alloc0 (image, size);
 }
@@ -2624,6 +2611,11 @@ mini_type_stack_size_full (MonoType *t, guint32 *align, gboolean pinvoke)
 void
 mono_generic_sharing_init (void)
 {
+       mono_counters_register ("RGCTX template num allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_templates_allocted);
+       mono_counters_register ("RGCTX template bytes allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_templates_bytes);
+       mono_counters_register ("RGCTX oti num allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_oti_allocted);
+       mono_counters_register ("RGCTX oti bytes allocted", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_oti_bytes);
+
        mono_install_image_unload_hook (mono_class_unregister_image_generic_subclasses, NULL);
 }
 
@@ -2779,38 +2771,30 @@ mini_get_shared_gparam (MonoType *t, MonoType *constraint)
        key.param.param.gshared_constraint = constraint;
 
        g_assert (mono_generic_param_info (par));
-       /* image might not be set for sre */
-       if (par->owner && par->owner->image) {
-               image = par->owner->image;
+       image = get_image_for_generic_param(par);
 
-               /*
-                * Need a cache to ensure the newly created gparam
-                * is unique wrt T/CONSTRAINT.
-                */
-               mono_image_lock (image);
-               if (!image->gshared_types) {
-                       image->gshared_types_len = MONO_TYPE_INTERNAL;
-                       image->gshared_types = g_new0 (GHashTable*, image->gshared_types_len);
-               }
-               if (!image->gshared_types [constraint->type])
-                       image->gshared_types [constraint->type] = g_hash_table_new (shared_gparam_hash, shared_gparam_equal);
-               res = g_hash_table_lookup (image->gshared_types [constraint->type], &key);
-               mono_image_unlock (image);
-               if (res)
-                       return res;
-               copy = mono_image_alloc0 (image, sizeof (MonoGSharedGenericParam));
-               memcpy (&copy->param, par, sizeof (MonoGenericParamFull));
-               name = get_shared_gparam_name (constraint->type, ((MonoGenericParamFull*)copy)->info.name);
-               copy->param.info.name = mono_image_strdup (image, name);
-               g_free (name);
-       } else {
-               /* mono_generic_param_name () expects this to be a MonoGenericParamFull */
-               copy = g_new0 (MonoGSharedGenericParam, 1);
-               memcpy (&copy->param, par, sizeof (MonoGenericParam));
-       }
-       copy->param.param.owner = NULL;
-       // FIXME:
-       copy->param.param.image = image ? image : mono_defaults.corlib;
+       /*
+        * Need a cache to ensure the newly created gparam
+        * is unique wrt T/CONSTRAINT.
+        */
+       mono_image_lock (image);
+       if (!image->gshared_types) {
+               image->gshared_types_len = MONO_TYPE_INTERNAL;
+               image->gshared_types = g_new0 (GHashTable*, image->gshared_types_len);
+       }
+       if (!image->gshared_types [constraint->type])
+               image->gshared_types [constraint->type] = g_hash_table_new (shared_gparam_hash, shared_gparam_equal);
+       res = g_hash_table_lookup (image->gshared_types [constraint->type], &key);
+       mono_image_unlock (image);
+       if (res)
+               return res;
+       copy = mono_image_alloc0 (image, sizeof (MonoGSharedGenericParam));
+       memcpy (&copy->param, par, sizeof (MonoGenericParamFull));
+       name = get_shared_gparam_name (constraint->type, ((MonoGenericParamFull*)copy)->info.name);
+       copy->param.info.name = mono_image_strdup (image, name);
+       g_free (name);
+
+       copy->param.param.owner = par->owner;
 
        copy->param.param.gshared_constraint = constraint;
        copy->parent = par;