* 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>
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 *
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);
* 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;
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;
return TRUE;
}
-static gboolean
-mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
+gboolean
+mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
{
const MonoGenericInst *a = (const MonoGenericInst *) ka;
const MonoGenericInst *b = (const MonoGenericInst *) kb;
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)
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;
}
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,
{
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;
}
return mono_metadata_signature_dup_full (NULL, sig);
}
+/*
+ * 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 *);
+}
+
/*
* mono_metadata_parse_method_signature:
* @m: metadata context
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;
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));
+ (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
MonoImage *image = data;
MonoInflatedMethodSignature *sig = key;
- return /* signature_in_image (sig->sig, image) || */
+ 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));
}
for (i = 0; i < ginst->type_argc; ++i)
mono_metadata_free_type (ginst->type_argv [i]);
- g_free (ginst->type_argv);
g_free (ginst);
}
/* 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);
for (i = 0; i < dgclass->count_fields; ++i) {
MonoClassField *field = dgclass->fields + i;
mono_metadata_free_type (field->type);
- if (field->generic_info) {
- mono_metadata_free_type (field->generic_info->generic_type);
- g_free (field->generic_info);
- }
g_free ((char*)field->name);
}
for (i = 0; i < dgclass->count_properties; ++i) {
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);
}
mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
{
MonoGenericInst *ginst;
- MonoGenericInst helper;
+ gboolean is_open;
int i;
-
- helper.type_argc = type_argc;
- helper.type_argv = type_argv;
- helper.id = 0;
+ 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;
- helper.is_open = (i < type_argc);
+ 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 (&helper);*/
mono_loader_lock ();
- ginst = g_hash_table_lookup (generic_inst_cache, &helper);
- if (ginst) {
- mono_loader_unlock ();
- /*g_print (" found cached\n");*/
- return ginst;
- }
- ginst = g_new0 (MonoGenericInst, 1);
- ginst->type_argc = type_argc;
- ginst->type_argv = g_new (MonoType*, type_argc);
- ginst->id = ++next_generic_inst_id;
- ginst->is_open = helper.is_open;
+ 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]);
+ 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);
+ g_hash_table_insert (generic_inst_cache, ginst, ginst);
+ }
mono_loader_unlock ();
- /*g_print (" inserted\n");*/
return ginst;
}
* @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,
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 &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
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 {
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_internal (t->data.klass->enum_basetype, align, allow_open);
+ 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;
}
if (container_class->valuetype) {
if (container_class->enumtype)
- return mono_type_stack_size_internal (container_class->enum_basetype, align, allow_open);
+ 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:
/* 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;
/*
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);
}
}
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;
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){
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: Assumes the loader lock is held.
+ * LOCKING: Acquires the loader lock
*
*/
MonoGenericContainer *
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);
+ 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 = 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;
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;
+}
+