X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmetadata%2Fmetadata.c;h=62fbe4b6587f5c7eabdeb1fa722c1446375e64af;hb=0cfb605201a9670ce2bc9320202c66e05f82149c;hp=7dd623ec8c533918ddce847d4f80a4af3b7e4a92;hpb=2116599e86acdc5a44d4339ceb5aba2749bbc650;p=mono.git diff --git a/mono/metadata/metadata.c b/mono/metadata/metadata.c index 7dd623ec8c5..62fbe4b6587 100644 --- a/mono/metadata/metadata.c +++ b/mono/metadata/metadata.c @@ -441,10 +441,49 @@ typedef struct { 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 * @@ -538,8 +577,10 @@ mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bit 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); @@ -596,9 +637,11 @@ mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bit 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); @@ -954,10 +997,13 @@ mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res { 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); @@ -989,10 +1035,12 @@ mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col) { 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) { @@ -1244,16 +1292,17 @@ mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *p * Returns: a #MonoArrayType structure describing the array type * and dimensions. Memory is allocated from the image mempool. * - * LOCKING: Assumes the loader lock is held. + * 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 = mono_mempool_alloc0 (m->mempool, sizeof (MonoArrayType)); + 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; @@ -1392,8 +1441,8 @@ mono_type_equal (gconstpointer ka, gconstpointer kb) 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; @@ -1408,21 +1457,35 @@ mono_generic_inst_hash (gconstpointer data) } 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->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) { @@ -1430,6 +1493,7 @@ 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; @@ -1456,7 +1520,7 @@ mono_metadata_init (void) int i; type_cache = g_hash_table_new (mono_type_hash, mono_type_equal); - generic_inst_cache = g_hash_table_new_full (mono_generic_inst_hash, mono_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst); + 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) @@ -1523,7 +1587,6 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo 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: @@ -1556,7 +1619,7 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo } 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"); @@ -1593,7 +1656,6 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo type->pinned = pinned ? 1 : 0; if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) { - mono_loader_unlock (); return NULL; } @@ -1624,13 +1686,11 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo 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; } } @@ -1638,15 +1698,14 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo /* 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, @@ -1754,15 +1813,33 @@ mono_metadata_signature_alloc (MonoImage *m, guint32 nparams) { 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 @@ -1775,10 +1852,7 @@ mono_metadata_signature_alloc (MonoImage *m, guint32 nparams) MonoMethodSignature* mono_metadata_signature_dup (MonoMethodSignature *sig) { - int sigsize; - - sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *); - return g_memdup (sig, sigsize); + return mono_metadata_signature_dup_full (NULL, sig); } /* @@ -1930,8 +2004,7 @@ mono_metadata_free_inflated_signature (MonoMethodSignature *sig) if (sig->params [i]) mono_metadata_free_type (sig->params [i]); } - /* FIXME: The signature is allocated from a mempool */ - //g_free (sig); + g_free (sig); } static gboolean @@ -1941,6 +2014,8 @@ inflated_method_equal (gconstpointer a, gconstpointer b) 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); } @@ -1948,7 +2023,7 @@ 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); + return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring)) + ma->is_mb_open; } static gboolean @@ -2043,6 +2118,23 @@ retry: 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); + 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; @@ -2127,6 +2219,7 @@ mono_metadata_clean_for_image (MonoImage *image) 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 (); } @@ -2173,23 +2266,42 @@ free_generic_class (MonoGenericClass *gclass) int i; /* FIXME: The dynamic case */ - if (gclass->cached_class && !gclass->cached_class->image->dynamic) { + 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_class_setup_fields () */ - if (class->fields) { - for (i = 0; i < class->field.count; ++i) { - g_free (class->fields [i].generic_info); - mono_metadata_free_type (class->fields [i].type); - } - } /* 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); } @@ -2299,6 +2411,16 @@ mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv) 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: * @@ -2309,12 +2431,14 @@ MonoGenericClass * 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 (); @@ -2337,9 +2461,12 @@ mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst 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 && !is_tb_open) + gclass->cached_class = container_class; g_hash_table_insert (generic_class_cache, gclass, gclass); @@ -2468,7 +2595,7 @@ select_container (MonoGenericContainer *gc, MonoTypeEnum type) * @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: Assumes the loader lock is held. + * LOCKING: Acquires the loader lock */ static MonoGenericParam * mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container, @@ -2481,10 +2608,13 @@ mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_c generic_container = select_container (generic_container, type); if (!generic_container) { /* Create dummy MonoGenericParam */ - MonoGenericParam *param = mono_mempool_alloc0 (m->mempool, sizeof (MonoGenericParam)); - param->name = mono_mempool_alloc0 (m->mempool, 8); + 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; } @@ -2493,6 +2623,38 @@ mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_c 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 @@ -2682,7 +2844,7 @@ parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr 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; @@ -2731,7 +2893,7 @@ parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr * 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. */ @@ -2748,19 +2910,17 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons 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; @@ -2771,7 +2931,6 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons */ mh->code_size = flags >> 2; mh->code = (unsigned char*)ptr; - mono_loader_unlock (); return mh; case METHOD_HEADER_FAT_FORMAT: fat_flags = read16 (ptr); @@ -2800,7 +2959,6 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons ptr = (char*)code + code_size; break; default: - mono_loader_unlock (); return NULL; } @@ -2817,18 +2975,17 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons 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; @@ -2836,7 +2993,6 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons 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; } @@ -3289,9 +3445,7 @@ mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, Mono ++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) { @@ -3624,6 +3778,12 @@ mono_type_size (MonoType *t, int *align) */ 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; @@ -3657,6 +3817,11 @@ mono_type_stack_size (MonoType *t, int *align) case MONO_TYPE_ARRAY: *align = __alignof__(gpointer); return sizeof (gpointer); + case MONO_TYPE_VAR: + case MONO_TYPE_MVAR: + g_assert (allow_open); + *align = __alignof__(gpointer); + return sizeof (gpointer); case MONO_TYPE_TYPEDBYREF: *align = __alignof__(gpointer); return sizeof (gpointer) * 3; @@ -3674,7 +3839,7 @@ mono_type_stack_size (MonoType *t, int *align) guint32 size; if (t->data.klass->enumtype) - return mono_type_stack_size (t->data.klass->enum_basetype, align); + return mono_type_stack_size_internal (t->data.klass->enum_basetype, align, allow_open); else { size = mono_class_value_size (t->data.klass, (guint32*)align); @@ -3691,11 +3856,12 @@ mono_type_stack_size (MonoType *t, int *align) 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 (container_class->enum_basetype, align, allow_open); else { guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align); @@ -3734,19 +3900,29 @@ mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass) 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) + 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 @@ -3755,9 +3931,9 @@ mono_metadata_generic_context_hash (const MonoGenericContext *context) /* 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; } @@ -3821,6 +3997,10 @@ mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only 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); @@ -3933,6 +4113,23 @@ mono_metadata_type_equal (MonoType *t1, MonoType *t2) 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 @@ -4002,13 +4199,10 @@ mono_metadata_type_dup (MonoMemPool *mp, const MonoType *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) { - /* FIXME: should mono_dup_array_type() use mempools? */ - g_assert (!mp); - r->data.array = mono_dup_array_type (o->data.array); + r->data.array = mono_dup_array_type (mp, o->data.array); } else if (o->type == MONO_TYPE_FNPTR) { - /* FIXME: should mono_metadata_signature_deep_dup() use mempools? */ - g_assert (!mp); - r->data.method = mono_metadata_signature_deep_dup (o->data.method); + /*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; } @@ -4075,6 +4269,13 @@ mono_metadata_encode_value (guint32 value, char *buf, char **endbuf) void mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec) +{ + return 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; @@ -4114,7 +4315,7 @@ mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint 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); } } @@ -4402,7 +4603,7 @@ mono_type_create_from_typespec (MonoImage *image, guint32 type_spec) ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]); len = mono_metadata_decode_value (ptr, &ptr); - type = mono_mempool_alloc0 (image->mempool, sizeof (MonoType)); + type = mono_image_alloc0 (image, sizeof (MonoType)); if (*ptr == MONO_TYPE_BYREF) { type->byref = 1; @@ -4410,7 +4611,6 @@ mono_type_create_from_typespec (MonoImage *image, guint32 type_spec) } if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) { - g_free (type); mono_loader_unlock (); return NULL; } @@ -4429,8 +4629,27 @@ mono_type_create_from_typespec (MonoImage *image, guint32 type_spec) 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; @@ -4438,7 +4657,10 @@ mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr) /* 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++; @@ -4487,11 +4709,11 @@ mono_metadata_parse_marshal_spec (MonoImage *image, const char *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) { @@ -4878,8 +5100,7 @@ guint32 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) @@ -4895,13 +5116,18 @@ mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *o } *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 @@ -4943,7 +5169,7 @@ mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token, * * Returns: NULL if @token is not a generic type or method definition or the new generic container. * - * LOCKING: Assumes the loader lock is held. + * LOCKING: Acquires the loader lock * */ MonoGenericContainer * @@ -4961,7 +5187,7 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE); params = NULL; n = 0; - container = mono_mempool_alloc0 (image->mempool, sizeof (MonoGenericContainer)); + container = mono_image_alloc0_lock (image, sizeof (MonoGenericContainer)); do { n++; params = g_realloc (params, sizeof (MonoGenericParam) * n); @@ -4977,7 +5203,7 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC } while (cols [MONO_GENERICPARAM_OWNER] == owner); container->type_argc = n; - container->type_params = mono_mempool_alloc0 (image->mempool, sizeof (MonoGenericParam) * n); + 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; @@ -5141,3 +5367,57 @@ mono_aligned_addr_hash (gconstpointer ptr) 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; +} +