* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
- * (C) 2001-2002 Ximian, Inc.
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#include <config.h>
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
gboolean signature_only);
+static void free_generic_inst (MonoGenericInst *ginst);
+static void free_generic_class (MonoGenericClass *ginst);
+static void free_inflated_method (MonoMethodInflated *method);
/*
* This enumeration is used to describe the data types in the metadata
#endif
+/* Auxiliary structure used for caching inflated signatures */
+typedef struct {
+ MonoMethodSignature *sig;
+ MonoGenericContext context;
+} MonoInflatedMethodSignature;
+
+/**
+ * mono_image_alloc_lock:
+ *
+ * Same as mono_image_alloc, but do the locking as well.
+ * LOCKING: Acquires the loader lock.
+ */
+static gpointer
+mono_image_alloc_lock (MonoImage *image, guint size)
+{
+ gpointer res;
+
+ mono_loader_lock ();
+ res = mono_image_alloc (image, size);
+ mono_loader_unlock ();
+
+ return res;
+}
+
+/**
+ * mono_image_alloc0_lock:
+ *
+ * Same as mono_image_alloc, but do the locking as well.
+ * LOCKING: Acquires the loader lock.
+ */
+static gpointer
+mono_image_alloc0_lock (MonoImage *image, guint size)
+{
+ gpointer res;
+
+ mono_loader_lock ();
+ res = mono_image_alloc0 (image, size);
+ mono_loader_unlock ();
+
+ return res;
+}
+
/**
* mono_meta_table_name:
* @table: table index
*
+ * Returns the name of the given ECMA metadata logical format table
+ * as described in ECMA 335, Partition II, Section 22.
+ *
* Returns: the name for the @table index
*/
const char *
break;
case MONO_TABLE_EVENT:
g_assert (i == 2);
- field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
- field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
+ n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows, meta->tables [MONO_TABLE_TYPEREF].rows);
+ n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
+ /*This is a coded token for 3 tables, so takes 2 bits */
+ field_size = rtsize (n, 16 - MONO_TYPEDEFORREF_BITS);
break;
case MONO_TABLE_EVENT_POINTER:
g_assert (i == 0);
break;
case MONO_TABLE_GENERICPARAM:
g_assert (i == 2 || i == 4 || i == 5);
- if (i == 2)
- field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
- else if (i == 4)
+ if (i == 2) {
+ n = MAX (meta->tables [MONO_TABLE_METHOD].rows, meta->tables [MONO_TABLE_TYPEDEF].rows);
+ /*This is a coded token for 2 tables, so takes 1 bit */
+ field_size = rtsize (n, 16 - MONO_TYPEORMETHOD_BITS);
+ } else if (i == 4)
field_size = idx_size (MONO_TABLE_TYPEDEF);
else if (i == 5)
field_size = idx_size (MONO_TABLE_TYPEDEF);
{
guint32 bitfield = t->size_bitfield;
int i, count = mono_metadata_table_count (bitfield);
- const char *data = t->base + idx * t->row_size;
+ const char *data;
+
+ g_assert (idx < t->rows);
+ data = t->base + idx * t->row_size;
g_assert (res_size == count);
-
+
for (i = 0; i < count; i++) {
int n = mono_metadata_table_size (bitfield, i);
{
guint32 bitfield = t->size_bitfield;
int i;
- register const char *data = t->base + idx * t->row_size;
+ register const char *data;
register int n;
+ g_assert (idx < t->rows);
g_assert (col < mono_metadata_table_count (bitfield));
+ data = t->base + idx * t->row_size;
n = mono_metadata_table_size (bitfield, 0);
for (i = 0; i < col; ++i) {
}
/*
- * mono_metadata_parse_array:
+ * mono_metadata_parse_array_full:
* @m: a metadata context.
* @ptr: a pointer to an encoded array description.
* @rptr: pointer updated to match the end of the decoded stream
* Decodes the compressed array description found in the metadata @m at @ptr.
*
* Returns: a #MonoArrayType structure describing the array type
- * and dimensions.
+ * and dimensions. Memory is allocated from the image mempool.
+ *
+ * LOCKING: Acquires the loader lock
*/
MonoArrayType *
mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
const char *ptr, const char **rptr)
{
int i;
- MonoArrayType *array = g_new0 (MonoArrayType, 1);
+ MonoArrayType *array;
MonoType *etype;
+ array = mono_image_alloc0_lock (m, sizeof (MonoArrayType));
etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
if (!etype)
return NULL;
static GHashTable *generic_class_cache = NULL;
static int next_generic_inst_id = 0;
+/*
+ * Protected by the loader lock.
+ * It has a MonoMethodInflated* as key and value.
+ * The key lookup will just access the declaring and context fields
+ */
+static GHashTable *generic_method_cache = NULL;
+
+/*
+ * Protected by the loader lock.
+ * It has a MonoInflatedMethodSignature* as key and value.
+ */
+static GHashTable *generic_signature_cache = NULL;
+
static guint mono_generic_class_hash (gconstpointer data);
/*
return 1;
}
-static guint
-mono_generic_inst_hash (gconstpointer data)
+guint
+mono_metadata_generic_inst_hash (gconstpointer data)
{
const MonoGenericInst *ginst = (const MonoGenericInst *) data;
guint hash = 0;
}
static gboolean
-mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
+mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
{
- const MonoGenericInst *a = (const MonoGenericInst *) ka;
- const MonoGenericInst *b = (const MonoGenericInst *) kb;
int i;
- if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
+ if (a->id && b->id) {
+ if (a->id == b->id)
+ return TRUE;
+ if (!signature_only)
+ return FALSE;
+ }
+
+ if (a->is_open != b->is_open || a->type_argc != b->type_argc)
return FALSE;
for (i = 0; i < a->type_argc; ++i) {
- if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
+ if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
return FALSE;
}
return TRUE;
}
+gboolean
+mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
+{
+ const MonoGenericInst *a = (const MonoGenericInst *) ka;
+ const MonoGenericInst *b = (const MonoGenericInst *) kb;
+
+ return mono_generic_inst_equal_full (a, b, FALSE);
+}
+
static guint
mono_generic_class_hash (gconstpointer data)
{
guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
hash *= 13;
+ hash += gclass->is_tb_open;
hash += mono_metadata_generic_context_hash (&gclass->context);
return hash;
int i;
type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
- generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
- generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
+ generic_inst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
+ generic_class_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
for (i = 0; i < NBUILTIN_TYPES (); ++i)
g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
g_hash_table_destroy (type_cache);
g_hash_table_destroy (generic_inst_cache);
g_hash_table_destroy (generic_class_cache);
+ if (generic_method_cache)
+ g_hash_table_destroy (generic_method_cache);
+ if (generic_signature_cache)
+ g_hash_table_destroy (generic_signature_cache);
type_cache = NULL;
generic_inst_cache = NULL;
generic_class_cache = NULL;
+ generic_method_cache = NULL;
+ generic_signature_cache = NULL;
}
/**
* this MonoGenericContainer.
* This is a Mono runtime internal function.
*
- * LOCKING: Assumes the loader lock is held.
+ * LOCKING: Acquires the loader lock.
*
* Returns: a #MonoType structure representing the decoded type.
*/
int count = 0;
gboolean found;
- mono_loader_lock ();
/*
* According to the spec, custom modifiers should come before the byref
* flag, but the IL produced by ilasm from the following signature:
}
if (count) {
- type = mono_mempool_alloc0 (m->mempool, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
+ type = mono_image_alloc0_lock (m, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
type->num_mods = count;
if (count > 64)
g_warning ("got more than 64 modifiers in type");
type->pinned = pinned ? 1 : 0;
if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
- mono_loader_unlock ();
return NULL;
}
if (rptr)
*rptr = ptr;
- if (!type->num_mods) {
+ if (!type->num_mods) {
/* no need to free type here, because it is on the stack */
if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
of a MonoClass which currently holds the loader lock. 'type' is local.
*/
if (ret->data.klass == type->data.klass) {
- mono_loader_unlock ();
return ret;
}
}
/* No need to use locking since nobody is modifying the hash table */
if ((cached = g_hash_table_lookup (type_cache, type))) {
- mono_loader_unlock ();
return cached;
}
}
/* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
if (type == &stype) {
- type = mono_mempool_alloc (m->mempool, sizeof (MonoType));
+ type = mono_image_alloc_lock (m, sizeof (MonoType));
memcpy (type, &stype, sizeof (MonoType));
}
- mono_loader_unlock ();
return type;
}
/*
- * LOCKING: Assumes the loader lock is held.
+ * LOCKING: Acquires the loader lock.
*/
MonoType*
mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
const char *ptr;
if (image->dynamic)
- return mono_lookup_dynamic_token (image, token);
+ return mono_lookup_dynamic_token (image, token, NULL);
g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
{
MonoMethodSignature *sig;
- mono_loader_lock ();
- sig = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+ sig = mono_image_alloc0_lock (m, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
sig->param_count = nparams;
sig->sentinelpos = -1;
- mono_loader_unlock ();
return sig;
}
+MonoMethodSignature*
+mono_metadata_signature_dup_full (MonoMemPool *mp, MonoMethodSignature *sig)
+{
+ int sigsize;
+
+ sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
+
+ if (mp) {
+ MonoMethodSignature *ret;
+ mono_loader_lock ();
+ ret = mono_mempool_alloc (mp, sigsize);
+ mono_loader_unlock ();
+
+ memcpy (ret, sig, sigsize);
+ return ret;
+ } else {
+ return g_memdup (sig, sigsize);
+ }
+}
+
/*
* mono_metadata_signature_dup:
* @sig: method signature
MonoMethodSignature*
mono_metadata_signature_dup (MonoMethodSignature *sig)
{
- int sigsize;
+ return mono_metadata_signature_dup_full (NULL, sig);
+}
- sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
- return g_memdup (sig, sigsize);
+/*
+ * mono_metadata_signature_size:
+ *
+ * Return the amount of memory allocated to SIG.
+ */
+guint32
+mono_metadata_signature_size (MonoMethodSignature *sig)
+{
+ return sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
}
/*
void
mono_metadata_free_method_signature (MonoMethodSignature *sig)
{
+ /* Everything is allocated from mempools */
+ /*
int i;
if (sig->ret)
mono_metadata_free_type (sig->ret);
if (sig->params [i])
mono_metadata_free_type (sig->params [i]);
}
+ */
+}
+
+void
+mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
+{
+ int i;
+
+ /* Allocated in inflate_generic_signature () */
+ if (sig->ret)
+ mono_metadata_free_type (sig->ret);
+ for (i = 0; i < sig->param_count; ++i) {
+ if (sig->params [i])
+ mono_metadata_free_type (sig->params [i]);
+ }
+ g_free (sig);
+}
+
+static gboolean
+inflated_method_equal (gconstpointer a, gconstpointer b)
+{
+ const MonoMethodInflated *ma = a;
+ const MonoMethodInflated *mb = b;
+ if (ma->declaring != mb->declaring)
+ return FALSE;
+ if (ma->is_mb_open != mb->is_mb_open)
+ return FALSE;
+ return mono_metadata_generic_context_equal (&ma->context, &mb->context);
+}
+
+static guint
+inflated_method_hash (gconstpointer a)
+{
+ const MonoMethodInflated *ma = a;
+ return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring)) + ma->is_mb_open;
+}
+
+static gboolean
+inflated_signature_equal (gconstpointer a, gconstpointer b)
+{
+ const MonoInflatedMethodSignature *sig1 = a;
+ const MonoInflatedMethodSignature *sig2 = b;
+
+ /* sig->sig is assumed to be canonized */
+ if (sig1->sig != sig2->sig)
+ return FALSE;
+ /* The generic instances are canonized */
+ return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
+}
+
+static guint
+inflated_signature_hash (gconstpointer a)
+{
+ const MonoInflatedMethodSignature *sig = a;
+
+ /* sig->sig is assumed to be canonized */
+ return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
}
/*static void
g_print (">");
}*/
-static gboolean gclass_in_image (gpointer key, gpointer value, gpointer data);
+static gboolean type_in_image (MonoType *type, MonoImage *image);
static gboolean
-ginst_in_image (gpointer key, gpointer value, gpointer data)
+signature_in_image (MonoMethodSignature *sig, MonoImage *image)
+{
+ gpointer iter = NULL;
+ MonoType *p;
+
+ while ((p = mono_signature_get_params (sig, &iter)) != NULL)
+ if (type_in_image (p, image))
+ return TRUE;
+
+ return type_in_image (mono_signature_get_return_type (sig), image);
+}
+
+static gboolean
+ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
{
- MonoImage *image = data;
- MonoGenericInst *ginst = key;
- MonoClass *klass;
int i;
+
for (i = 0; i < ginst->type_argc; ++i) {
- MonoType *type = ginst->type_argv [i];
+ if (type_in_image (ginst->type_argv [i], image))
+ return TRUE;
+ }
- /* FIXME: Avoid a possible mono_class_inst inside mono_class_from_mono_type */
- if (type->type == MONO_TYPE_GENERICINST) {
- if (gclass_in_image (type->data.generic_class, NULL, image))
- return TRUE;
- continue;
- }
+ return FALSE;
+}
- klass = mono_class_from_mono_type (ginst->type_argv [i]);
- if (klass->image == image) {
- /*dump_ginst (ginst);
- g_print (" removed\n");*/
- return TRUE;
+static gboolean
+gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
+{
+ return gclass->container_class->image == image ||
+ ginst_in_image (gclass->context.class_inst, image);
+}
+
+static gboolean
+type_in_image (MonoType *type, MonoImage *image)
+{
+retry:
+ switch (type->type) {
+ case MONO_TYPE_GENERICINST:
+ return gclass_in_image (type->data.generic_class, image);
+ case MONO_TYPE_PTR:
+ type = type->data.type;
+ goto retry;
+ case MONO_TYPE_SZARRAY:
+ type = &type->data.klass->byval_arg;
+ goto retry;
+ case MONO_TYPE_ARRAY:
+ type = &type->data.array->eklass->byval_arg;
+ goto retry;
+ case MONO_TYPE_FNPTR:
+ return signature_in_image (type->data.method, image);
+ case MONO_TYPE_VAR:
+ if (type->data.generic_param->owner) {
+ g_assert (!type->data.generic_param->owner->is_method);
+ /*
+ * FIXME: The following check is here solely
+ * for monodis, which uses the internal
+ * function
+ * mono_metadata_load_generic_params(). The
+ * caller of that function needs to fill in
+ * owner->klass or owner->method of the
+ * returned struct, but monodis doesn't do
+ * that. The image unloading depends on that,
+ * however, so a crash results without this
+ * check.
+ */
+ if (!type->data.generic_param->owner->owner.klass)
+ return FALSE;
+ return type->data.generic_param->owner->owner.klass->image == image;
+ } else {
+ return type->data.generic_param->image == image;
+ }
+ case MONO_TYPE_MVAR:
+ if (type->data.generic_param->owner) {
+ g_assert (type->data.generic_param->owner->is_method);
+ if (!type->data.generic_param->owner->owner.method)
+ /* RefEmit created generic param whose method is not finished */
+ return FALSE;
+ return type->data.generic_param->owner->owner.method->klass->image == image;
+ } else {
+ return type->data.generic_param->image == image;
}
+ default:
+ /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
+ return image == mono_class_from_mono_type (type)->image;
}
- return FALSE;
}
+typedef struct {
+ MonoImage *image;
+ GSList *list;
+} CleanForImageUserData;
+
static gboolean
-gclass_in_image (gpointer key, gpointer value, gpointer data)
+steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
{
- MonoImage *image = data;
MonoGenericClass *gclass = key;
+ CleanForImageUserData *user_data = data;
- if (ginst_in_image (gclass->context.class_inst, NULL, image))
- return TRUE;
- if (gclass->container_class->image == image)
- return TRUE;
- return FALSE;
+ if (!gclass_in_image (gclass, user_data->image))
+ return FALSE;
+
+ user_data->list = g_slist_prepend (user_data->list, gclass);
+ return TRUE;
}
+static gboolean
+steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
+{
+ MonoGenericInst *ginst = key;
+ CleanForImageUserData *user_data = data;
+
+ if (!ginst_in_image (ginst, user_data->image))
+ return FALSE;
+
+ user_data->list = g_slist_prepend (user_data->list, ginst);
+ return TRUE;
+}
+
+static gboolean
+inflated_method_in_image (gpointer key, gpointer value, gpointer data)
+{
+ MonoImage *image = data;
+ MonoMethodInflated *method = key;
+
+ // FIXME:
+ // https://bugzilla.novell.com/show_bug.cgi?id=458168
+ return method->declaring->klass->image == image ||
+ (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
+ (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image));
+}
+
+static gboolean
+inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
+{
+ MonoImage *image = data;
+ MonoInflatedMethodSignature *sig = key;
+
+ return signature_in_image (sig->sig, image) ||
+ (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
+ (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
+}
+
void
mono_metadata_clean_for_image (MonoImage *image)
{
- mono_loader_lock ();
- g_hash_table_foreach_remove (generic_inst_cache, ginst_in_image, image);
- g_hash_table_foreach_remove (generic_class_cache, gclass_in_image, image);
+ CleanForImageUserData ginst_data, gclass_data;
+ GSList *l;
+
+ /* The data structures could reference each other so we delete them in two phases */
+ ginst_data.image = gclass_data.image = image;
+ ginst_data.list = gclass_data.list = NULL;
+
+ mono_loader_lock ();
+ /* Collect the items to delete and remove them from the hash table */
+ g_hash_table_foreach_steal (generic_inst_cache, steal_ginst_in_image, &ginst_data);
+ g_hash_table_foreach_steal (generic_class_cache, steal_gclass_in_image, &gclass_data);
+ if (generic_method_cache)
+ g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
+ if (generic_signature_cache)
+ g_hash_table_foreach_remove (generic_signature_cache, inflated_signature_in_image, image);
+ /* Delete the removed items */
+ for (l = ginst_data.list; l; l = l->next)
+ free_generic_inst (l->data);
+ for (l = gclass_data.list; l; l = l->next)
+ free_generic_class (l->data);
+ g_slist_free (ginst_data.list);
+ g_slist_free (gclass_data.list);
+ mono_class_unregister_image_generic_subclasses (image);
mono_loader_unlock ();
}
+static void
+free_inflated_method (MonoMethodInflated *imethod)
+{
+ int i;
+ MonoMethod *method = (MonoMethod*)imethod;
+
+ if (method->signature)
+ mono_metadata_free_inflated_signature (method->signature);
+
+ if (!((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))) {
+ MonoMethodNormal* mn = (MonoMethodNormal*) method;
+ MonoMethodHeader *header = mn->header;
+
+ if (header) {
+ /* Allocated in inflate_generic_header () */
+ for (i = 0; i < header->num_locals; ++i)
+ mono_metadata_free_type (header->locals [i]);
+ g_free (header->clauses);
+ g_free (header);
+ }
+ }
+
+ g_free (method);
+}
+
+static void
+free_generic_inst (MonoGenericInst *ginst)
+{
+ int i;
+
+ for (i = 0; i < ginst->type_argc; ++i)
+ mono_metadata_free_type (ginst->type_argv [i]);
+ g_free (ginst);
+}
+
+
+static void
+free_generic_class (MonoGenericClass *gclass)
+{
+ int i;
+
+ /* FIXME: The dynamic case */
+ if (gclass->cached_class && !gclass->cached_class->image->dynamic && !mono_generic_class_is_generic_type_definition (gclass)) {
+ MonoClass *class = gclass->cached_class;
+
+ /* Allocated in mono_class_init () */
+ g_free (class->methods);
+ g_free (class->properties);
+ /* Allocated in mono_generic_class_get_class () */
+ g_free (class->interfaces);
+ g_free (class);
+ } else if (gclass->is_dynamic) {
+ MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *)gclass;
+
+ for (i = 0; i < dgclass->count_fields; ++i) {
+ MonoClassField *field = dgclass->fields + i;
+ mono_metadata_free_type (field->type);
+ g_free ((char*)field->name);
+ }
+ for (i = 0; i < dgclass->count_properties; ++i) {
+ MonoProperty *property = dgclass->properties + i;
+ g_free ((char*)property->name);
+ }
+ for (i = 0; i < dgclass->count_events; ++i) {
+ MonoEvent *event = dgclass->events + i;
+ g_free ((char*)event->name);
+ }
+
+ g_free (dgclass->methods);
+ g_free (dgclass->ctors);
+ g_free (dgclass->fields);
+ g_free (dgclass->properties);
+ g_free (dgclass->events);
+ g_free (dgclass->field_objects);
+ g_free (dgclass->field_generic_types);
+ if (!mono_generic_class_is_generic_type_definition (gclass))
+ g_free (gclass->cached_class);
+ }
+ g_free (gclass);
+}
+
+static void
+free_inflated_signature (MonoInflatedMethodSignature *sig)
+{
+ mono_metadata_free_inflated_signature (sig->sig);
+ g_free (sig);
+}
+
+/*
+ * LOCKING: assumes the loader lock is held.
+ */
+MonoMethodInflated*
+mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
+{
+ if (cache) {
+ if (!generic_method_cache)
+ generic_method_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
+ g_hash_table_insert (generic_method_cache, method, method);
+ return method;
+ } else {
+ if (generic_method_cache)
+ return g_hash_table_lookup (generic_method_cache, method);
+ return NULL;
+ }
+}
+
/*
- * mono_metadata_lookup_generic_inst:
+ * mono_metadata_get_inflated_signature:
*
- * Check whether the newly created generic instantiation @ginst already exists
- * in the cache and return the cached value in this case. Otherwise insert
- * it into the cache.
+ * Given an inflated signature and a generic context, return a canonical copy of the
+ * signature. The returned signature might be equal to SIG or it might be a cached copy.
+ */
+MonoMethodSignature *
+mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
+{
+ MonoInflatedMethodSignature helper;
+ MonoInflatedMethodSignature *res;
+
+ mono_loader_lock ();
+ if (!generic_signature_cache)
+ generic_signature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
+
+ helper.sig = sig;
+ helper.context.class_inst = context->class_inst;
+ helper.context.method_inst = context->method_inst;
+ res = g_hash_table_lookup (generic_signature_cache, &helper);
+ if (!res) {
+ res = g_new0 (MonoInflatedMethodSignature, 1);
+ res->sig = sig;
+ res->context.class_inst = context->class_inst;
+ res->context.method_inst = context->method_inst;
+ g_hash_table_insert (generic_signature_cache, res, res);
+ }
+
+ mono_loader_unlock ();
+ return res->sig;
+}
+
+/*
+ * mono_metadata_get_generic_inst:
*
- * Use this method each time you create a new `MonoGenericInst' to ensure
- * proper caching. Only use the returned value as the argument passed to this
- * method may be freed.
+ * Given a list of types, return a MonoGenericInst that represents that list.
+ * The returned MonoGenericInst has its own copy of the list of types. The list
+ * passed in the argument can be freed, modified or disposed of.
*
*/
MonoGenericInst *
-mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
+mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
{
- MonoGenericInst *cached;
+ MonoGenericInst *ginst;
+ gboolean is_open;
int i;
+ int size = sizeof (MonoGenericInst) + (type_argc - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
+
+ for (i = 0; i < type_argc; ++i)
+ if (mono_class_is_open_constructed_type (type_argv [i]))
+ break;
+ is_open = (i < type_argc);
+
+ ginst = alloca (size);
+ ginst->id = 0;
+ ginst->is_open = is_open;
+ ginst->type_argc = type_argc;
+ memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
- /*dump_ginst (ginst);*/
mono_loader_lock ();
- cached = g_hash_table_lookup (generic_inst_cache, ginst);
- if (cached) {
- for (i = 0; i < ginst->type_argc; i++)
- mono_metadata_free_type (ginst->type_argv [i]);
- g_free (ginst->type_argv);
- g_free (ginst);
- mono_loader_unlock ();
- /*g_print (" found cached\n");*/
- return cached;
- }
- ginst->id = ++next_generic_inst_id;
- g_hash_table_insert (generic_inst_cache, ginst, ginst);
+ ginst = g_hash_table_lookup (generic_inst_cache, ginst);
+ if (!ginst) {
+ ginst = g_malloc (size);
+ ginst->id = ++next_generic_inst_id;
+ ginst->is_open = is_open;
+ ginst->type_argc = type_argc;
+
+ for (i = 0; i < type_argc; ++i)
+ ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
+
+ g_hash_table_insert (generic_inst_cache, ginst, ginst);
+ }
mono_loader_unlock ();
- /*g_print (" inserted\n");*/
return ginst;
}
+static gboolean
+mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
+{
+ MonoGenericContainer *container = container_class->generic_container;
+
+ if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
+ return FALSE;
+ return inst == container->context.class_inst;
+}
+
/*
* mono_metadata_lookup_generic_class:
*
mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
{
MonoGenericClass *gclass;
-
MonoGenericClass helper;
+ gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
+
helper.container_class = container_class;
helper.context.class_inst = inst;
helper.context.method_inst = NULL;
helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
+ helper.is_tb_open = is_tb_open;
helper.cached_class = NULL;
mono_loader_lock ();
gclass = g_new0 (MonoGenericClass, 1);
}
+ gclass->is_tb_open = is_tb_open;
gclass->container_class = container_class;
gclass->context.class_inst = inst;
gclass->context.method_inst = NULL;
-
- if (inst == container_class->generic_container->context.class_inst)
+ if (inst == container_class->generic_container->context.class_inst && !is_tb_open)
gclass->cached_class = container_class;
g_hash_table_insert (generic_class_cache, gclass, gclass);
MonoGenericInst *
mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
{
+ MonoType **type_argv;
MonoGenericInst *nginst;
int i;
if (!ginst->is_open)
return ginst;
- nginst = g_new0 (MonoGenericInst, 1);
- nginst->type_argc = ginst->type_argc;
- nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
- nginst->is_reference = 1;
+ type_argv = g_new0 (MonoType*, ginst->type_argc);
- for (i = 0; i < nginst->type_argc; i++) {
- MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
+ for (i = 0; i < ginst->type_argc; i++)
+ type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
- if (!nginst->is_open)
- nginst->is_open = mono_class_is_open_constructed_type (t);
- if (nginst->is_reference)
- nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
+ nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
- nginst->type_argv [i] = t;
- }
+ for (i = 0; i < ginst->type_argc; i++)
+ mono_metadata_free_type (type_argv [i]);
+ g_free (type_argv);
- return mono_metadata_lookup_generic_inst (nginst);
+ return nginst;
}
MonoGenericInst *
mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
int count, const char *ptr, const char **rptr)
{
+ MonoType **type_argv;
MonoGenericInst *ginst;
int i;
- ginst = g_new0 (MonoGenericInst, 1);
- ginst->type_argc = count;
- ginst->type_argv = g_new0 (MonoType*, count);
- ginst->is_reference = 1;
+ type_argv = g_new0 (MonoType*, count);
- for (i = 0; i < ginst->type_argc; i++) {
+ for (i = 0; i < count; i++) {
MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
-
if (!t) {
- g_free (ginst->type_argv);
- g_free (ginst);
+ g_free (type_argv);
return NULL;
}
- ginst->type_argv [i] = t;
- if (!ginst->is_open)
- ginst->is_open = mono_class_is_open_constructed_type (t);
- if (ginst->is_reference)
- ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
+ type_argv [i] = t;
}
if (rptr)
*rptr = ptr;
- return mono_metadata_lookup_generic_inst (ginst);
+ ginst = mono_metadata_get_generic_inst (count, type_argv);
+
+ g_free (type_argv);
+
+ return ginst;
}
static gboolean
do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
const char *ptr, const char **rptr)
{
- MonoGenericClass *gclass, *cached;
+ MonoGenericInst *inst;
MonoClass *gklass;
MonoType *gtype;
int count;
- gclass = g_new0 (MonoGenericClass, 1);
-
- type->data.generic_class = gclass;
-
gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
if (gtype == NULL)
return FALSE;
- gclass->container_class = gklass = mono_class_from_mono_type (gtype);
- g_assert (gklass->generic_container);
+ gklass = mono_class_from_mono_type (gtype);
+ if (!gklass->generic_container)
+ return FALSE;
count = mono_metadata_decode_value (ptr, &ptr);
-
- gclass->context.class_inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
- gclass->context.method_inst = NULL;
+ inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
+ if (inst == NULL)
+ return FALSE;
if (rptr)
*rptr = ptr;
- /* If we failed to parse, return, the error has been flagged. */
- if (gclass->context.class_inst == NULL)
- return FALSE;
-
- /*
- * We may be called multiple times on different metadata to create the same
- * instantiated type. This happens for instance if we're part of a method or
- * local variable signature.
- *
- * It's important to return the same MonoGenericClass * for each particualar
- * instantiation of a generic type (ie "Stack<Int32>") to make static fields
- * work.
- *
- * According to the spec ($26.1.5), a static variable in a generic class
- * declaration is shared amongst all instances of the same closed constructed
- * type.
- */
-
- cached = g_hash_table_lookup (generic_class_cache, gclass);
- if (cached) {
- g_free (gclass);
-
- type->data.generic_class = cached;
- return TRUE;
- } else {
- g_hash_table_insert (generic_class_cache, gclass, gclass);
-
- mono_stats.generic_instance_count++;
- mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
- gclass->context.class_inst->type_argc * sizeof (MonoType);
- }
+ type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
return TRUE;
}
* @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
* see mono_metadata_parse_type_full() for details.
* Internal routine to parse a generic type parameter.
+ * LOCKING: Acquires the loader lock
*/
static MonoGenericParam *
mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
generic_container = select_container (generic_container, type);
if (!generic_container) {
/* Create dummy MonoGenericParam */
- MonoGenericParam *param = g_new0 (MonoGenericParam, 1);
- param->name = g_strdup_printf ("%d", index);
+ MonoGenericParam *param;
+
+ param = mono_image_alloc0_lock (m, sizeof (MonoGenericParam));
+ param->name = mono_image_alloc0_lock (m, 8);
+ sprintf ((char*)param->name, "%d", index);
param->num = index;
+ param->image = m;
return param;
}
return &generic_container->type_params [index];
}
+/*
+ * mono_metadata_get_shared_type:
+ *
+ * Return a shared instance of TYPE, if available, NULL otherwise.
+ * Shared MonoType instances help save memory. Their contents should not be modified
+ * by the caller. They do not need to be freed as their lifetime is bound by either
+ * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
+ * instance they are embedded in. If they are freed, they should be freed using
+ * mono_metadata_free_type () instead of g_free ().
+ */
+MonoType*
+mono_metadata_get_shared_type (MonoType *type)
+{
+ MonoType *cached;
+
+ /* No need to use locking since nobody is modifying the hash table */
+ if ((cached = g_hash_table_lookup (type_cache, type)))
+ return cached;
+
+ switch (type->type){
+ case MONO_TYPE_CLASS:
+ case MONO_TYPE_VALUETYPE:
+ if (type == &type->data.klass->byval_arg)
+ return type;
+ if (type == &type->data.klass->this_arg)
+ return type;
+ break;
+ }
+
+ return NULL;
+}
+
/*
* do_mono_metadata_parse_type:
* @type: MonoType to be filled in with the return value
if (!etype)
return FALSE;
type->data.klass = mono_class_from_mono_type (etype);
- mono_metadata_free_type (etype);
break;
}
case MONO_TYPE_PTR:
* mono_metadata_free_type:
* @type: type to free
*
- * Free the memory allocated for type @type which is assumed to be created by
- * mono_metadata_parse_type ().
+ * Free the memory allocated for type @type which is allocated on the heap.
*/
void
mono_metadata_free_type (MonoType *type)
break;
}
- /* Allocated from a mempool, no need to free it */
+ g_free (type);
}
#if 0
int i;
mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
/* we could just store a pointer if we don't need to byteswap */
- mh->clauses = mono_mempool_alloc0 (m->mempool, sizeof (MonoExceptionClause) * mh->num_clauses);
+ mh->clauses = mono_image_alloc0_lock (m, sizeof (MonoExceptionClause) * mh->num_clauses);
for (i = 0; i < mh->num_clauses; ++i) {
MonoExceptionClause *ec = &mh->clauses [i];
guint32 tof_value;
* info about local variables and optional exception tables.
* This is a Mono runtime internal function.
*
- * LOCKING: Assumes the loader lock is held.
+ * LOCKING: Acquires the loader lock.
*
* Returns: a MonoMethodHeader allocated from the image mempool.
*/
g_return_val_if_fail (ptr != NULL, NULL);
- mono_loader_lock ();
switch (format) {
case METHOD_HEADER_TINY_FORMAT:
- mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
+ mh = mono_image_alloc0_lock (m, sizeof (MonoMethodHeader));
ptr++;
mh->max_stack = 8;
local_var_sig_tok = 0;
mh->code_size = flags >> 2;
mh->code = (unsigned char*)ptr;
- mono_loader_unlock ();
return mh;
case METHOD_HEADER_TINY_FORMAT1:
- mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
+ mh = mono_image_alloc0_lock (m, sizeof (MonoMethodHeader));
ptr++;
mh->max_stack = 8;
local_var_sig_tok = 0;
*/
mh->code_size = flags >> 2;
mh->code = (unsigned char*)ptr;
- mono_loader_unlock ();
return mh;
case METHOD_HEADER_FAT_FORMAT:
fat_flags = read16 (ptr);
ptr = (char*)code + code_size;
break;
default:
- mono_loader_unlock ();
return NULL;
}
g_warning ("wrong signature for locals blob");
locals_ptr++;
len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
- mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+ mh = mono_image_alloc0_lock (m, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
mh->num_locals = len;
for (i = 0; i < len; ++i) {
mh->locals [i] = mono_metadata_parse_type_full (
m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
if (!mh->locals [i]) {
- mono_loader_unlock ();
return NULL;
}
}
} else {
- mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
+ mh = mono_image_alloc0_lock (m, sizeof (MonoMethodHeader));
}
mh->code = code;
mh->code_size = code_size;
mh->init_locals = init_locals;
if (fat_flags & METHOD_HEADER_MORE_SECTS)
parse_section_data (m, mh, (const unsigned char*)ptr);
- mono_loader_unlock ();
return mh;
}
++pos;
}
- mono_loader_lock ();
- result = mono_mempool_alloc0 (meta->mempool, sizeof (MonoClass*) * (pos - start));
- mono_loader_unlock ();
+ result = mono_image_alloc0_lock (meta, sizeof (MonoClass*) * (pos - start));
pos = start;
while (pos < tdef->rows) {
return sizeof (gpointer);
case MONO_TYPE_VALUETYPE: {
if (t->data.klass->enumtype)
- return mono_type_size (t->data.klass->enum_basetype, align);
+ return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
else
return mono_class_value_size (t->data.klass, (guint32*)align);
}
if (container_class->valuetype) {
if (container_class->enumtype)
- return mono_type_size (container_class->enum_basetype, align);
+ return mono_type_size (mono_class_enum_basetype (container_class), align);
else
return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
} else {
*/
int
mono_type_stack_size (MonoType *t, int *align)
+{
+ return mono_type_stack_size_internal (t, align, FALSE);
+}
+
+int
+mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
{
int tmp;
+#if SIZEOF_VOID_P == SIZEOF_REGISTER
+ int stack_slot_size = sizeof (gpointer);
+ int stack_slot_align = __alignof__ (gpointer);
+#elif SIZEOF_VOID_P < SIZEOF_REGISTER
+ int stack_slot_size = SIZEOF_REGISTER;
+ int stack_slot_align = SIZEOF_REGISTER;
+#endif
g_assert (t != NULL);
align = &tmp;
if (t->byref) {
- *align = __alignof__(gpointer);
- return sizeof (gpointer);
+ *align = stack_slot_align;
+ return stack_slot_size;
}
switch (t->type){
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_ARRAY:
- *align = __alignof__(gpointer);
- return sizeof (gpointer);
+ *align = stack_slot_align;
+ return stack_slot_size;
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
+ g_assert (allow_open);
+ *align = stack_slot_align;
+ return stack_slot_size;
case MONO_TYPE_TYPEDBYREF:
- *align = __alignof__(gpointer);
- return sizeof (gpointer) * 3;
+ *align = stack_slot_align;
+ return stack_slot_size * 3;
case MONO_TYPE_R4:
*align = __alignof__(float);
return sizeof (float);
guint32 size;
if (t->data.klass->enumtype)
- return mono_type_stack_size (t->data.klass->enum_basetype, align);
+ return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
else {
size = mono_class_value_size (t->data.klass, (guint32*)align);
- *align = *align + __alignof__(gpointer) - 1;
- *align &= ~(__alignof__(gpointer) - 1);
+ *align = *align + stack_slot_align - 1;
+ *align &= ~(stack_slot_align - 1);
- size += sizeof (gpointer) - 1;
- size &= ~(sizeof (gpointer) - 1);
+ size += stack_slot_size - 1;
+ size &= ~(stack_slot_size - 1);
return size;
}
MonoGenericClass *gclass = t->data.generic_class;
MonoClass *container_class = gclass->container_class;
- g_assert (!gclass->context.class_inst->is_open);
+ if (!allow_open)
+ g_assert (!gclass->context.class_inst->is_open);
if (container_class->valuetype) {
if (container_class->enumtype)
- return mono_type_stack_size (container_class->enum_basetype, align);
+ return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
else {
guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
- *align = *align + __alignof__(gpointer) - 1;
- *align &= ~(__alignof__(gpointer) - 1);
+ *align = *align + stack_slot_align - 1;
+ *align &= ~(stack_slot_align - 1);
- size += sizeof (gpointer) - 1;
- size &= ~(sizeof (gpointer) - 1);
+ size += stack_slot_size - 1;
+ size &= ~(stack_slot_size - 1);
return size;
}
} else {
- *align = __alignof__(gpointer);
- return sizeof (gpointer);
+ *align = stack_slot_align;
+ return stack_slot_size;
}
}
default:
static gboolean
_mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
{
- int i;
MonoGenericInst *i1 = g1->context.class_inst;
MonoGenericInst *i2 = g2->context.class_inst;
- if (i1->type_argc != i2->type_argc || g1->is_dynamic != g2->is_dynamic || i1->is_reference != i2->is_reference)
+ if (g1->is_dynamic != g2->is_dynamic)
return FALSE;
if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
return FALSE;
- for (i = 0; i < i1->type_argc; ++i) {
- if (!do_mono_metadata_type_equal (i1->type_argv [i], i2->type_argv [i], signature_only))
- return FALSE;
- }
- return TRUE;
+ if (!mono_generic_inst_equal_full (i1, i2, signature_only))
+ return FALSE;
+ return g1->is_tb_open == g2->is_tb_open;
+}
+
+static gboolean
+_mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
+{
+ MonoGenericInst *i1 = g1->context.class_inst;
+ MonoGenericInst *i2 = c2->generic_container->context.class_inst;
+
+ if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
+ return FALSE;
+ if (!mono_generic_inst_equal_full (i1, i2, signature_only))
+ return FALSE;
+ return !g1->is_tb_open;
}
guint
/* FIXME: check if this seed is good enough */
guint hash = 0xc01dfee7;
if (context->class_inst)
- hash = ((hash << 5) - hash) ^ context->class_inst->id;
+ hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
if (context->method_inst)
- hash = ((hash << 5) - hash) ^ context->method_inst->id;
+ hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
return hash;
}
if (p1->num != p2->num)
return FALSE;
- if (p1->owner == p2->owner)
+ /*
+ * We have to compare the image as well because if we didn't,
+ * the generic_inst_cache lookup wouldn't care about the image
+ * of generic params, so what could happen is that a generic
+ * inst with params from image A is put into the cache, then
+ * image B gets that generic inst from the cache, image A is
+ * unloaded, so the inst is deleted, but image B still retains
+ * a pointer to it.
+ *
+ * The AOT runtime doesn't set the image when it's decoding
+ * types, so we only compare it when the owner is NULL.
+ */
+ if (p1->owner == p2->owner && (p1->owner || p1->image == p2->image))
return TRUE;
/*
return TRUE;
if (c1->generic_class && c2->generic_class)
return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
+ if (c1->generic_class && c2->generic_container)
+ return _mono_metadata_generic_class_container_equal (c1->generic_class, c2, signature_only);
+ if (c1->generic_container && c2->generic_class)
+ return _mono_metadata_generic_class_container_equal (c2->generic_class, c1, signature_only);
if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
return mono_metadata_generic_param_equal (
c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
return do_mono_metadata_type_equal (t1, t2, FALSE);
}
+/**
+ * mono_metadata_type_equal_full:
+ * @t1: a type
+ * @t2: another type
+ * @signature_only: if signature only comparison should be made
+ *
+ * Determine if @t1 and @t2 are signature compatible if @signature_only is #TRUE, otherwise
+ * behaves the same way as mono_metadata_type_equal.
+ * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
+ * Returns: #TRUE if @t1 and @t2 are equal taking @signature_only into account.
+ */
+gboolean
+mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
+{
+ return do_mono_metadata_type_equal (t1, t2, signature_only);
+}
+
/**
* mono_metadata_signature_equal:
* @sig1: a signature
if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
return FALSE;
+ if (sig1->generic_param_count != sig2->generic_param_count)
+ return FALSE;
+
/*
* We're just comparing the signatures of two methods here:
*
}
/**
- * mono_metadata_type_dup_mp:
- * @image: image type is defined in
+ * mono_metadata_type_dup:
+ * @mp: mempool to use
* @original: type to duplicate
*
- * Returns: copy of type allocated from mempool.
+ * Returns: copy of type allocated from mempool (or from the heap, if @mp is null).
*/
MonoType *
-mono_metadata_type_dup_mp (MonoImage *image, const MonoType *original)
+mono_metadata_type_dup (MonoMemPool *mp, const MonoType *o)
{
MonoType *r = NULL;
+ int sizeof_o = sizeof (MonoType);
+ if (o->num_mods)
+ sizeof_o += (o->num_mods - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod);
+
mono_loader_lock ();
- r = mono_mempool_alloc0 (image->mempool, sizeof(MonoType));
+ r = mp ? mono_mempool_alloc0 (mp, sizeof_o) : g_malloc (sizeof_o);
mono_loader_unlock ();
- *r = *original;
- /* FIXME: we don't handle these yet because they need to duplicate memory
- * but the current routines used are not using the mempools
- */
- if (original->type == MONO_TYPE_PTR ||
- original->type == MONO_TYPE_ARRAY ||
- original->type == MONO_TYPE_FNPTR)
- g_assert_not_reached ();
+
+ memcpy (r, o, sizeof_o);
+
+ if (o->type == MONO_TYPE_PTR) {
+ r->data.type = mono_metadata_type_dup (mp, o->data.type);
+ } else if (o->type == MONO_TYPE_ARRAY) {
+ r->data.array = mono_dup_array_type (mp, o->data.array);
+ } else if (o->type == MONO_TYPE_FNPTR) {
+ /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
+ r->data.method = mono_metadata_signature_deep_dup (mp, o->data.method);
+ }
return r;
}
void
mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
MonoMarshalSpec **marshal_spec)
+{
+ mono_metadata_field_info_with_mempool (NULL, meta, index, offset, rva, marshal_spec);
+}
+
+void
+mono_metadata_field_info_with_mempool (MonoMemPool *mempool, MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
+ MonoMarshalSpec **marshal_spec)
{
MonoTableInfo *tdef;
locator_t loc;
const char *p;
if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
- *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
+ *marshal_spec = mono_metadata_parse_marshal_spec_with_mempool (mempool, p);
}
}
guint32 cols [MONO_TYPESPEC_SIZE];
const char *ptr;
guint32 len;
- MonoType *type;
+ MonoType *type, *type2;
mono_loader_lock ();
ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
len = mono_metadata_decode_value (ptr, &ptr);
- type = g_new0 (MonoType, 1);
-
- g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
+ type = mono_image_alloc0 (image, sizeof (MonoType));
if (*ptr == MONO_TYPE_BYREF) {
type->byref = 1;
}
if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
- g_hash_table_remove (image->typespec_cache, GUINT_TO_POINTER (type_spec));
- g_free (type);
mono_loader_unlock ();
return NULL;
}
+ type2 = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
+
+ if (type2) {
+ mono_loader_unlock ();
+ return type2;
+ }
+
+ g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
+
mono_loader_unlock ();
return type;
}
+
+static char*
+mono_mempool_strndup (MonoMemPool *mp, const char *data, guint len)
+{
+ char *res;
+ if (!mp)
+ return g_strndup (data, len);
+ res = mono_mempool_alloc (mp, len + 1);
+ memcpy (res, data, len);
+ res [len] = 0;
+ return res;
+}
+
MonoMarshalSpec *
mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
+{
+ return mono_metadata_parse_marshal_spec_with_mempool (NULL, ptr);
+}
+
+MonoMarshalSpec *
+mono_metadata_parse_marshal_spec_with_mempool (MonoMemPool *mempool, const char *ptr)
{
MonoMarshalSpec *res;
int len;
/* fixme: this is incomplete, but I cant find more infos in the specs */
- res = g_new0 (MonoMarshalSpec, 1);
+ if (mempool)
+ res = mono_mempool_alloc0 (mempool, sizeof (MonoMarshalSpec));
+ else
+ res = g_new0 (MonoMarshalSpec, 1);
len = mono_metadata_decode_value (ptr, &ptr);
res->native = *ptr++;
ptr += len;
/* read custom marshaler type name */
len = mono_metadata_decode_value (ptr, &ptr);
- res->data.custom_data.custom_name = g_strndup (ptr, len);
+ res->data.custom_data.custom_name = mono_mempool_strndup (mempool, ptr, len);
ptr += len;
/* read cookie string */
len = mono_metadata_decode_value (ptr, &ptr);
- res->data.custom_data.cookie = g_strndup (ptr, len);
+ res->data.custom_data.cookie = mono_mempool_strndup (mempool, ptr, len);
}
if (res->native == MONO_NATIVE_SAFEARRAY) {
case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
case MONO_TYPE_VALUETYPE: /*FIXME*/
if (type->data.klass->enumtype) {
- t = type->data.klass->enum_basetype->type;
+ t = mono_class_enum_basetype (type->data.klass)->type;
goto handle_enum;
}
if (type->data.klass == mono_defaults.handleref_class){
switch (mspec->native) {
case MONO_NATIVE_STRUCT:
return MONO_NATIVE_STRUCT;
+ case MONO_NATIVE_CUSTOM:
+ return MONO_NATIVE_CUSTOM;
case MONO_NATIVE_INTERFACE:
*conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
return MONO_NATIVE_INTERFACE;
mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
{
MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
- guint32 cols [MONO_GENERICPARAM_SIZE];
- guint32 i;
+ locator_t loc;
g_assert (owner);
if (!tdef->base)
}
*owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
- for (i = 0; i < tdef->rows; ++i) {
- mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
- if (cols [MONO_GENERICPARAM_OWNER] == *owner)
- return i + 1;
- }
+ loc.idx = *owner;
+ loc.col_idx = MONO_GENERICPARAM_OWNER;
+ loc.t = tdef;
- return 0;
+ if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+ return 0;
+
+ /* Find the first entry by searching backwards */
+ while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
+ loc.result --;
+
+ return loc.result + 1;
}
gboolean
*
* Returns: NULL if @token is not a generic type or method definition or the new generic container.
*
+ * LOCKING: Acquires the loader lock
+ *
*/
MonoGenericContainer *
mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
params = NULL;
n = 0;
- container = g_new0 (MonoGenericContainer, 1);
+ container = mono_image_alloc0_lock (image, sizeof (MonoGenericContainer));
do {
n++;
params = g_realloc (params, sizeof (MonoGenericParam) * n);
+ memset (¶ms [n - 1], 0, sizeof (MonoGenericParam));
params [n - 1].owner = container;
- params [n - 1].pklass = NULL;
params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
- params [n - 1].constraints = NULL;
if (++i > tdef->rows)
break;
mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
} while (cols [MONO_GENERICPARAM_OWNER] == owner);
container->type_argc = n;
- container->type_params = params;
+ container->type_params = mono_image_alloc0_lock (image, sizeof (MonoGenericParam) * n);
+ memcpy (container->type_params, params, sizeof (MonoGenericParam) * n);
+ g_free (params);
container->parent = parent_container;
if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
MonoGenericInst *
mono_get_shared_generic_inst (MonoGenericContainer *container)
{
+ MonoType **type_argv;
+ MonoType *helper;
MonoGenericInst *nginst;
int i;
- nginst = g_new0 (MonoGenericInst, 1);
- nginst->type_argc = container->type_argc;
- nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
- nginst->is_reference = 1;
- nginst->is_open = 1;
+ type_argv = g_new0 (MonoType *, container->type_argc);
+ helper = g_new0 (MonoType, container->type_argc);
- for (i = 0; i < nginst->type_argc; i++) {
- MonoType *t = g_new0 (MonoType, 1);
+ for (i = 0; i < container->type_argc; i++) {
+ MonoType *t = &helper [i];
t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
t->data.generic_param = &container->type_params [i];
- nginst->type_argv [i] = t;
+ type_argv [i] = t;
}
- return mono_metadata_lookup_generic_inst (nginst);
+ nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
+
+ g_free (type_argv);
+ g_free (helper);
+
+ return nginst;
}
gboolean
return GPOINTER_TO_UINT (ptr) >> 3;
}
+/*
+ * If @field belongs to an inflated generic class, return the corresponding field of the
+ * generic type definition class.
+ */
+MonoClassField*
+mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
+{
+ MonoClass *gtd;
+ int offset;
+
+ if (!field->parent->generic_class)
+ return field;
+
+ gtd = field->parent->generic_class->container_class;
+ offset = field - field->parent->fields;
+ return gtd->fields + offset;
+}
+
+/*
+ * If @event belongs to an inflated generic class, return the corresponding event of the
+ * generic type definition class.
+ */
+MonoEvent*
+mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
+{
+ MonoClass *gtd;
+ int offset;
+
+ if (!event->parent->generic_class)
+ return event;
+
+ gtd = event->parent->generic_class->container_class;
+ offset = event - event->parent->events;
+ return gtd->events + offset;
+}
+
+/*
+ * If @property belongs to an inflated generic class, return the corresponding property of the
+ * generic type definition class.
+ */
+MonoProperty*
+mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
+{
+ MonoClass *gtd;
+ int offset;
+
+ if (!property->parent->generic_class)
+ return property;
+
+ gtd = property->parent->generic_class->container_class;
+ offset = property - property->parent->properties;
+ return gtd->properties + offset;
+}
+