* Author:
* Miguel de Icaza (miguel@ximian.com)
*
- * (C) 2001 Ximian, Inc.
+ * (C) 2001-2006 Novell, Inc.
*
- * Possible Optimizations:
- * in mono_class_create, do not allocate the class right away,
- * but wait until you know the size of the FieldMap, so that
- * the class embeds directly the FieldMap after the vtable.
- *
- *
*/
#include <config.h>
#include <glib.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/security-manager.h>
#include <mono/os/gc_wrapper.h>
+#include <mono/utils/mono-counters.h>
MonoStats mono_stats;
void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
+/*
+ * mono_class_from_typeref:
+ * @image: a MonoImage
+ * @type_token: a TypeRef token
+ *
+ * Creates the MonoClass* structure representing the type defined by
+ * the typeref token valid inside @image.
+ * Returns: the MonoClass* representing the typeref token, NULL ifcould
+ * not be loaded.
+ */
MonoClass *
mono_class_from_typeref (MonoImage *image, guint32 type_token)
{
/* a typedef in disguise */
return mono_class_from_name (image, nspace, name);
case MONO_RESOLTION_SCOPE_MODULEREF:
- return mono_class_from_name (image->modules [idx - 1], nspace, name);
+ if (image->modules [idx-1])
+ return mono_class_from_name (image->modules [idx - 1], nspace, name);
+ else {
+ char *msg = g_strdup_printf ("%s%s%s", nspace, nspace [0] ? "." : "", name);
+ char *human_name;
+
+ human_name = mono_stringify_assembly_name (&image->assembly->aname);
+ mono_loader_set_error_type_load (msg, human_name);
+ g_free (msg);
+ g_free (human_name);
+
+ return NULL;
+ }
case MONO_RESOLTION_SCOPE_TYPEREF: {
MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
GList *tmp;
/* If this assert fails, it probably means that you haven't installed an assembly load/search hook */
g_assert (references == image->references);
g_assert (references [idx - 1]);
- if (references [idx - 1] == (gpointer)-1)
+
+ /* If the assembly did not load, register this as a type load exception */
+ if (references [idx - 1] == REFERENCE_MISSING){
+ MonoAssemblyName aname;
+ char *human_name;
+
+ mono_assembly_get_assemblyref (image, idx - 1, &aname);
+ human_name = mono_stringify_assembly_name (&aname);
+ mono_loader_set_error_assembly_load (human_name, image->assembly->ref_only);
+ g_free (human_name);
+
return NULL;
+ }
return mono_class_from_name (references [idx - 1]->image, nspace, name);
}
return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
}
+/**
+ * mono_type_get_name:
+ * @type: a type
+ *
+ * Returns: the string representation for type as it would be represented in IL code.
+ */
char*
mono_type_get_name (MonoType *type)
{
return type;
}
+/*
+ * mono_class_is_open_constructed_type:
+ * @type: a type
+ *
+ * Returns TRUE if type represents a generics open constructed type
+ * (not all the type parameters required for the instantiation have
+ * been provided).
+ */
gboolean
mono_class_is_open_constructed_type (MonoType *t)
{
return (MonoInflatedGenericClass *) gclass;
}
+/*
+ * mono_class_inflate_generic_type:
+ * @type: a type
+ * @context: a generics context
+ *
+ * Instantiate the generic type @type, using the generics context @context.
+ *
+ * Returns: the instantiated type
+ */
MonoType*
mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
{
return res;
}
+/*
+ * mono_class_inflate_generic_method:
+ * @method: a generic method
+ * @context: a generics context
+ *
+ * Instantiate the generic method @method using the generics context @context.
+ *
+ * Returns: the new instantiated method
+ */
MonoMethod *
mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
{
* For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
* method, it just "prepares" it for that. If you really need to fully instantiate the method
* (including its signature and header), call this method.
+ * FIXME: Martin? this description looks completely wrong.
*/
MonoMethod *
mono_get_inflated_method (MonoMethod *method)
* @class: The class to initialize
*
* Initializes the class->fields.
- * Assumes the loader lock is held.
+ * LOCKING: Assumes the loader lock is held.
*/
static void
mono_class_setup_fields (MonoClass *class)
{
MonoImage *m = class->image;
- const int top = class->field.count;
+ int top = class->field.count;
guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
- int i, blittable = TRUE, real_size = 0;
+ int i, blittable = TRUE;
+ guint32 real_size = 0;
guint32 packing_size = 0;
gboolean explicit_size;
MonoClassField *field;
if (class->size_inited)
return;
- if (class->inited)
- mono_class_init (class);
+ if (class->generic_class) {
+ MonoClass *gklass = class->generic_class->container_class;
+ mono_class_setup_fields (gklass);
+ top = gklass->field.count;
+ }
class->instance_size = 0;
class->class_size = 0;
if (class->parent) {
+ /* For generic instances, class->parent might not have been initialized */
+ mono_class_init (class->parent);
if (!class->parent->size_inited)
mono_class_setup_fields (class->parent);
class->instance_size += class->parent->instance_size;
field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
if (!field->type) {
mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
- continue;
+ break;
}
if (mono_field_is_deleted (field))
continue;
if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
- mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
+ guint32 offset;
+ mono_metadata_field_info (m, idx, &offset, NULL, NULL);
+ field->offset = offset;
if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
g_warning ("%s not initialized correctly (missing field layout info for %s)",
class->name, field->name);
class->instance_size = MAX (real_size, class->instance_size);
}
+ if (class->exception_type)
+ return;
mono_class_layout_fields (class);
}
* mono_class_setup_fields_locking:
* @class: The class to initialize
*
- * Initializes the class->fields.
+ * Initializes the class->fields array of fields.
* Aquires the loader lock.
*/
static void
}
/* useful until we keep track of gc-references in corlib etc. */
+#ifdef HAVE_SGEN_GC
+#define IS_GC_REFERENCE(t) FALSE
+#else
#define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
+#endif
/*
+ * mono_class_layout_fields:
+ * @class: a class
+ *
+ * Compute the placement of fields inside an object or struct, according to
+ * the layout rules and set the following fields in @class:
+ * - has_references (if the class contains instance references firled or structs that contain references)
+ * - has_static_refs (same, but for static fields)
+ * - instance_size (size of the object in memory)
+ * - class_size (size needed for the static fields)
+ * - size_inited (flag set when the instance_size is set)
+ *
* LOCKING: this is supposed to be called with the loader lock held.
*/
void
/*
* mono_class_setup_methods:
+ * @class: a class
*
* Initializes the 'methods' array in the klass.
* Calling this method should be avoided if possible since it allocates a lot
* of long-living MonoMethod structures.
+ * Methods belonging to an interface are assigned a sequential slot starting
+ * from 0.
*/
void
mono_class_setup_methods (MonoClass *class)
mono_loader_unlock ();
}
+/*
+ * LOCKING: assumes the loader lock is held.
+ */
static void
inflate_event (MonoClass *class, MonoEvent *event, MonoInflatedGenericClass *gclass)
{
}
if (event->other) {
- MonoMethod *inflated = mono_class_inflate_generic_method_full (
- event->other, class, gclass->generic_class.context);
+ MonoMethod **om = event->other;
+ int count = 0;
+ while (*om) {
+ count++;
+ om++;
+ }
+ om = event->other;
+ event->other = g_new0 (MonoMethod*, count + 1);
+ count = 0;
+ while (*om) {
+ MonoMethod *inflated = mono_class_inflate_generic_method_full (
+ *om, class, gclass->generic_class.context);
- event->other = mono_get_inflated_method (inflated);
+ event->other [count++] = mono_get_inflated_method (inflated);
+ om++;
+ }
}
}
int n = 0;
if (event->other == NULL) {
- event->other = g_new0 (MonoMethod*, 1);
+ event->other = g_new0 (MonoMethod*, 2);
} else {
while (event->other [n])
n++;
- event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
+ event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
}
event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
+ /* NULL terminated */
+ event->other [n + 1] = NULL;
break;
}
default:
mono_loader_unlock ();
}
+/*
+ * Global pool of interface IDs, represented as a bitset.
+ * LOCKING: this is supposed to be accessed with the loader lock held.
+ */
+static MonoBitSet *global_interface_bitset = NULL;
+
+/*
+ * mono_unload_interface_ids:
+ * @bitset: bit set of interface IDs
+ *
+ * When an image is unloaded, the interface IDs associated with
+ * the image are put back in the global pool of IDs so the numbers
+ * can be reused.
+ */
+void
+mono_unload_interface_ids (MonoBitSet *bitset)
+{
+ mono_loader_lock ();
+ mono_bitset_sub (global_interface_bitset, bitset);
+ mono_loader_unlock ();
+}
+
+/*
+ * mono_get_unique_iid:
+ * @class: interface
+ *
+ * Assign a unique integer ID to the interface represented by @class.
+ * The ID will positive and as small as possible.
+ * LOCKING: this is supposed to be called with the loader lock held.
+ * Returns: the new ID.
+ */
static guint
mono_get_unique_iid (MonoClass *class)
{
- static GHashTable *iid_hash = NULL;
- static guint iid = 0;
- char *str, *type_name;
- gpointer value;
- int generic_id;
+ int iid;
g_assert (MONO_CLASS_IS_INTERFACE (class));
- mono_loader_lock ();
-
- if (!iid_hash)
- iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
-
- if (class->generic_class && !class->generic_class->inst->is_open) {
- generic_id = class->generic_class->inst->id;
- g_assert (generic_id != 0);
- } else
- generic_id = 0;
-
- type_name = mono_type_full_name (&class->byval_arg);
- str = g_strdup_printf ("%s|%s|%d", class->image->name, type_name, generic_id);
- g_free (type_name);
-
- if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
- mono_loader_unlock ();
- if (mono_print_vtable)
- printf ("Interface: reusing id %d for %s\n", iid, str);
- g_free (str);
- return GPOINTER_TO_INT (value);
+ if (!global_interface_bitset) {
+ global_interface_bitset = mono_bitset_new (128, 0);
+ }
+
+ iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
+ if (iid < 0) {
+ int old_size = mono_bitset_size (global_interface_bitset);
+ MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
+ mono_bitset_free (global_interface_bitset);
+ global_interface_bitset = new_set;
+ iid = old_size;
+ }
+ mono_bitset_set (global_interface_bitset, iid);
+ /* set the bit also in the per-image set */
+ if (class->image->interface_bitset) {
+ if (iid >= mono_bitset_size (class->image->interface_bitset)) {
+ MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
+ mono_bitset_free (class->image->interface_bitset);
+ class->image->interface_bitset = new_set;
+ }
} else {
- if (mono_print_vtable)
- printf ("Interface: assigned id %d to %s\n", iid, str);
- g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
- ++iid;
+ class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
}
+ mono_bitset_set (class->image->interface_bitset, iid);
- mono_loader_unlock ();
+ if (mono_print_vtable) {
+ int generic_id;
+ char *type_name = mono_type_full_name (&class->byval_arg);
+ if (class->generic_class && !class->generic_class->inst->is_open) {
+ generic_id = class->generic_class->inst->id;
+ g_assert (generic_id != 0);
+ } else {
+ generic_id = 0;
+ }
+ printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
+ g_free (type_name);
+ }
g_assert (iid <= 65535);
- return iid - 1;
+ return iid;
}
static void
return cached;
}
+/*
+ * LOCKING: this is supposed to be called with the loader lock held.
+ */
static int
setup_interface_offsets (MonoClass *class, int cur_slot)
{
}
class->max_interface_id = max_iid;
/* compute vtable offset for interfaces */
- class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
+ class->interface_offsets = g_malloc (sizeof (int) * (max_iid + 1));
for (i = 0; i <= max_iid; i++)
class->interface_offsets [i] = -1;
return cur_slot;
}
-static void
-setup_generic_vtable (MonoClass *class)
+/*
+ * Setup interface offsets for interfaces. Used by Ref.Emit.
+ */
+void
+mono_class_setup_interface_offsets (MonoClass *class)
{
- MonoClass *gklass;
- int i;
-
- gklass = class->generic_class->container_class;
-
- mono_class_init (gklass);
- class->vtable_size = gklass->vtable_size;
-
- class->vtable = g_new0 (MonoMethod*, class->vtable_size);
- memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
-
- for (i = 0; i < class->vtable_size; i++) {
- MonoMethod *m = class->vtable [i];
-
- if (!m)
- continue;
+ mono_loader_lock ();
- m = mono_class_inflate_generic_method_full (m, class, class->generic_class->context);
- class->vtable [i] = m;
- }
+ setup_interface_offsets (class, 0);
- class->max_interface_id = gklass->max_interface_id;
- class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
- memcpy (class->interface_offsets, gklass->interface_offsets,
- sizeof (gint) * (gklass->max_interface_id + 1));
+ mono_loader_unlock ();
}
void
MonoGenericContext *context;
guint32 type_token;
int onum = 0;
+ gboolean ok = TRUE;
if (class->vtable)
return;
}
if (class->generic_class) {
- if (class->generic_class->inst->is_open) {
- setup_generic_vtable (class);
- mono_loader_unlock ();
- return;
- }
-
context = class->generic_class->context;
type_token = class->generic_class->container_class->type_token;
} else {
if (class->image->dynamic)
mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
- else
- mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
+ else {
+ /* The following call fails if there are missing methods in the type */
+ ok = mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
+ }
- mono_class_setup_vtable_general (class, overrides, onum);
+ if (ok)
+ mono_class_setup_vtable_general (class, overrides, onum);
+
g_free (overrides);
mono_loader_unlock ();
+
+ return;
}
/*
if (class->vtable)
return;
- if (class->generic_class && class->generic_class->inst->is_open) {
- setup_generic_vtable (class);
- return;
- }
-
ifaces = mono_class_get_implemented_interfaces (class);
if (ifaces) {
for (i = 0; i < ifaces->len; i++) {
int j;
for (j = 0; j < k->method.count; ++j) {
MonoMethod *m1 = k->methods [j];
+ MonoMethodSignature *cmsig, *m1sig;
+
if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
continue;
+
+ cmsig = mono_method_signature (cm);
+ m1sig = mono_method_signature (m1);
+
+ if (!cmsig || !m1sig) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+ return;
+ }
+
if (!strcmp(cm->name, m1->name) &&
- mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
+ mono_metadata_signature_equal (cmsig, m1sig)) {
/* CAS - SecurityAction.InheritanceDemand */
if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
} else
class->vtable_size = cur_slot;
- class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
+ class->vtable = mono_mempool_alloc0 (class->image->mempool, sizeof (gpointer) * class->vtable_size);
memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
if (mono_print_vtable) {
}
}
+static MonoMethod *default_ghc = NULL;
+static MonoMethod *default_finalize = NULL;
+static int finalize_slot = -1;
+static int ghc_slot = -1;
+
+static void
+initialize_object_slots (MonoClass *class)
+{
+ int i;
+ if (default_ghc)
+ return;
+ if (class == mono_defaults.object_class) {
+ mono_class_setup_vtable (class);
+ for (i = 0; i < class->vtable_size; ++i) {
+ MonoMethod *cm = class->vtable [i];
+
+ if (!strcmp (cm->name, "GetHashCode"))
+ ghc_slot = i;
+ else if (!strcmp (cm->name, "Finalize"))
+ finalize_slot = i;
+ }
+
+ g_assert (ghc_slot > 0);
+ default_ghc = class->vtable [ghc_slot];
+
+ g_assert (finalize_slot > 0);
+ default_finalize = class->vtable [finalize_slot];
+ }
+}
+
+static GList*
+g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
+{
+ GList* n = mono_mempool_alloc (mp, sizeof (GList));
+ n->next = l;
+ n->prev = NULL;
+ n->data = datum;
+ return n;
+}
+
/**
* mono_class_init:
* @class: the class to initialize
* compute the instance_size, class_size and other infos that cannot be
* computed at mono_class_get() time. Also compute a generic vtable and
* the method slot numbers. We use this infos later to create a domain
- * specific vtable.
+ * specific vtable.
+ *
+ * Returns TRUE on success or FALSE if there was a problem in loading
+ * the type (incorrect assemblies, missing assemblies, methods, etc).
*/
-void
+gboolean
mono_class_init (MonoClass *class)
{
int i;
- static MonoMethod *default_ghc = NULL;
- static MonoMethod *default_finalize = NULL;
- static int finalize_slot = -1;
- static int ghc_slot = -1;
MonoCachedClassInfo cached_info;
gboolean has_cached_info;
-
+ int class_init_ok = TRUE;
+
g_assert (class);
if (class->inited)
- return;
+ return TRUE;
/*g_print ("Init class %s\n", class->name);*/
if (class->inited) {
mono_loader_unlock ();
/* Somebody might have gotten in before us */
- return;
+ return TRUE;
}
if (class->init_pending) {
guint32 cols [MONO_NESTED_CLASS_SIZE];
mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
- class->nested_classes = g_list_prepend (class->nested_classes, nclass);
+ class->nested_classes = g_list_prepend_mempool (class->nested_classes, class->image->mempool, nclass);
i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
}
class->has_static_refs = cached_info.has_static_refs;
}
else
- if (!class->size_inited)
+ if (!class->size_inited){
mono_class_setup_fields (class);
+ if (class->exception_type || mono_loader_get_last_error ()){
+ class_init_ok = FALSE;
+ goto leave;
+ }
+ }
+
/* initialize method pointers */
if (class->rank) {
for (i = 0; i < class->rank; ++i)
sig->params [i] = &mono_defaults.int32_class->byval_arg;
- ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
+ ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
ctor->klass = class;
ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
ctor->signature = sig;
ctor->name = ".ctor";
ctor->slot = -1;
- class->methods = g_new (MonoMethod*, class->method.count);
+ class->methods = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
class->methods [0] = ctor;
if (class->rank > 1) {
sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
for (i = 0; i < class->rank * 2; ++i)
sig->params [i] = &mono_defaults.int32_class->byval_arg;
- ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
+ ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
ctor->klass = class;
ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
mono_class_setup_supertypes (class);
- if (!default_ghc) {
- if (class == mono_defaults.object_class) {
- mono_class_setup_vtable (class);
- for (i = 0; i < class->vtable_size; ++i) {
- MonoMethod *cm = class->vtable [i];
-
- if (!strcmp (cm->name, "GetHashCode")) {
- ghc_slot = i;
- break;
- }
- }
-
- g_assert (ghc_slot > 0);
-
- default_ghc = class->vtable [ghc_slot];
- }
- }
-
- if (!default_finalize) {
- if (class == mono_defaults.object_class) {
- mono_class_setup_vtable (class);
- for (i = 0; i < class->vtable_size; ++i) {
- MonoMethod *cm = class->vtable [i];
-
- if (!strcmp (cm->name, "Finalize")) {
- finalize_slot = i;
- break;
- }
- }
-
- g_assert (finalize_slot > 0);
-
- default_finalize = class->vtable [finalize_slot];
- }
- }
+ if (!default_ghc)
+ initialize_object_slots (class);
/*
* If possible, avoid the creation of the generic vtable by requesting
}
else {
mono_class_setup_vtable (class);
-
+
+ if (class->exception_type || mono_loader_get_last_error ()){
+ class_init_ok = FALSE;
+ goto leave;
+ }
+
class->ghcimpl = 1;
if (class->parent) {
MonoMethod *cmethod = class->vtable [ghc_slot];
}
}
- class->inited = 1;
- class->init_pending = 0;
-
- mono_loader_unlock ();
-
if (MONO_CLASS_IS_INTERFACE (class)) {
/*
* class->interface_offsets is needed for the castclass/isinst code, so
setup_interface_offsets (class, 0);
}
+ leave:
+ class->inited = 1;
+ class->init_pending = 0;
+
+ mono_loader_unlock ();
+
if (mono_debugger_class_init_func)
mono_debugger_class_init_func (class);
+
+ return class_init_ok;
}
/*
}
/*
+ * mono_class_setup_supertypes:
+ * @class: a class
+ *
+ * Build the data structure needed to make fast type checks work.
+ * This currently sets two fields in @class:
+ * - idepth: distance between @class and System.Object in the type
+ * hierarchy + 1
+ * - supertypes: array of classes: each element has a class in the hierarchy
+ * starting from @class up to System.Object
+ *
* LOCKING: this assumes the loader lock is held
*/
void
class->idepth = 1;
ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
- class->supertypes = g_new0 (MonoClass *, ms);
+ class->supertypes = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClass *) * ms);
if (class->parent) {
class->supertypes [class->idepth - 1] = class;
}
/**
- * @image: context where the image is created
+ * mono_class_create_from_typedef:
+ * @image: image where the token is valid
* @type_token: typedef token
+ *
+ * Create the MonoClass* representing the specified type token.
+ * @type_token must be a TypeDef token.
*/
static MonoClass *
mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
- class = g_malloc0 (sizeof (MonoClass));
+ class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
class->name = name;
class->name_space = nspace;
if (cols [MONO_TYPEDEF_EXTENDS]) {
parent = mono_class_get_full (
image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
+ if (parent == NULL){
+ g_hash_table_remove (image->class_cache, GUINT_TO_POINTER (type_token));
+ mono_loader_unlock ();
+ return NULL;
+ }
}
/* do this early so it's available for interfaces in setup_mono_type () */
mono_class_setup_parent (class, parent);
+ /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
mono_class_setup_mono_type (class);
if (!class->enumtype) {
if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
class->unicode = 1;
- /* fixme: maybe we must set this on windows
+
+#if PLATFORM_WIN32
if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
class->unicode = 1;
- */
+#endif
class->cast_class = class->element_class = class;
MonoClass *klass, *gklass;
int i;
- if (gclass->is_initialized)
+ mono_loader_lock ();
+ if (gclass->is_initialized) {
+ mono_loader_unlock ();
return;
- gclass->is_initialized = TRUE;
+ }
if (!gclass->klass)
gclass->klass = g_malloc0 (sizeof (MonoClass));
gklass = gclass->generic_class.container_class;
- klass->nested_in = gklass->nested_in;
+ if (gklass->nested_in) {
+ /*
+ * FIXME: the nested type context should include everything the
+ * nesting context should have, but it may also have additional
+ * generic parameters...
+ */
+ MonoType *inflated = mono_class_inflate_generic_type (
+ &gklass->nested_in->byval_arg, gclass->generic_class.context);
+ klass->nested_in = mono_class_from_mono_type (inflated);
+ }
klass->name = gklass->name;
klass->name_space = gklass->name_space;
i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
}
- if (gclass->generic_class.is_dynamic) {
- MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
-
- if (dgclass->parent)
- klass->parent = mono_class_from_mono_type (dgclass->parent);
- } else if (gklass->parent) {
+ if (gklass->parent) {
MonoType *inflated = mono_class_inflate_generic_type (
&gklass->parent->byval_arg, gclass->generic_class.context);
if (MONO_CLASS_IS_INTERFACE (klass))
setup_interface_offsets (klass, 0);
+ gclass->is_initialized = TRUE;
+ mono_loader_unlock ();
}
MonoClass *
{
MonoClass *result;
MonoClass *el_class;
- static GHashTable *ptr_hash = NULL;
+ MonoImage *image;
+ char *name;
+
+ el_class = mono_class_from_mono_type (type);
+ image = el_class->image;
mono_loader_lock ();
- if (!ptr_hash)
- ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
- el_class = mono_class_from_mono_type (type);
- if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
+ if (!image->ptr_cache)
+ image->ptr_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
+
+ if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
mono_loader_unlock ();
return result;
}
- result = g_new0 (MonoClass, 1);
+ result = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
result->parent = NULL; /* no parent for PTR types */
result->name_space = el_class->name_space;
- result->name = g_strdup_printf ("%s*", el_class->name);
+ name = g_strdup_printf ("%s*", el_class->name);
+ result->name = mono_mempool_strdup (image->mempool, name);
+ g_free (name);
result->image = el_class->image;
result->inited = TRUE;
result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
mono_class_setup_supertypes (result);
- g_hash_table_insert (ptr_hash, el_class, result);
+ g_hash_table_insert (image->ptr_cache, el_class, result);
mono_loader_unlock ();
MonoClass *result;
static GHashTable *ptr_hash = NULL;
+ /* FIXME: These should be allocate from a mempool as well, but which one ? */
+
mono_loader_lock ();
if (!ptr_hash)
result->parent = NULL; /* no parent for PTR types */
result->name_space = "System";
result->name = "MonoFNPtrFakeClass";
- result->image = NULL; /* need to fix... */
+ result->image = mono_defaults.corlib; /* need to fix... */
result->inited = TRUE;
result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
/* Can pointers get boxed? */
mono_loader_lock ();
+ if (!image->array_cache)
+ image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
+
if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
for (; list; list = list->next) {
class = list->data;
mono_class_init (parent);
}
- class = g_malloc0 (sizeof (MonoClass));
+ class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
class->image = image;
class->name_space = eclass->name_space;
memset (name + nsize + 1, ',', rank - 1);
name [nsize + rank] = ']';
name [nsize + rank + 1] = 0;
- class->name = name;
+ class->name = mono_mempool_strdup (image->mempool, name);
+ g_free (name);
class->type_token = 0;
/* all arrays are marked serializable and sealed, bug #42779 */
class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
class->element_class = eclass;
if ((rank > 1) || bounded) {
- MonoArrayType *at = g_new0 (MonoArrayType, 1);
+ MonoArrayType *at = mono_mempool_alloc0 (image->mempool, sizeof (MonoArrayType));
class->byval_arg.type = MONO_TYPE_ARRAY;
class->byval_arg.data.array = at;
at->eklass = eclass;
return 0;
}
-void *
-mono_vtable_get_static_field_data (MonoVTable *vt)
-{
- return vt->data;
-}
-
MonoProperty*
mono_class_get_property_from_name (MonoClass *klass, const char *name)
{
mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
{
MonoClass *class = mono_class_get (image, type_token);
+
+ if (!image->dynamic && ((type_token & 0xff000000) == MONO_TOKEN_TYPE_DEF))
+ return class;
+
if (class && context && (context->gclass || context->gmethod)) {
MonoType *inflated = inflate_generic_type (&class->byval_arg, context);
if (inflated)
if (nested)
return return_nested_in (class, nested);
return class;
+ } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
+ MonoAssembly **references = image->references;
+ if (!references [idx - 1])
+ mono_assembly_load_reference (image, idx - 1);
+ g_assert (references == image->references);
+ g_assert (references [idx - 1]);
+ if (references [idx - 1] == (gpointer)-1)
+ return NULL;
+ else
+ /* FIXME: Cycle detection */
+ return mono_class_from_name (references [idx - 1]->image, name_space, name);
} else {
g_error ("not yet implemented");
}
mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
gboolean check_interfaces)
{
- again:
g_assert (klassc->idepth > 0);
if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
if ((klassc->interface_id <= klass->max_interface_id) &&
if (klassc == mono_defaults.object_class)
return TRUE;
- if (klass->generic_class && klass->generic_class->is_dynamic) {
- MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
-
- if (!dgclass->parent)
- return FALSE;
-
- if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
- return TRUE;
- klass = mono_class_from_mono_type (dgclass->parent);
- goto again;
- }
-
return FALSE;
}
mono_class_get_finalizer (MonoClass *klass)
{
MonoCachedClassInfo cached_info;
- static int finalize_slot = -1;
-
- if (finalize_slot < 0) {
- int i;
- MonoClass* obj_class = mono_get_object_class ();
- mono_class_setup_vtable (obj_class);
- for (i = 0; i < obj_class->vtable_size; ++i) {
- MonoMethod *cm = obj_class->vtable [i];
-
- if (!strcmp (mono_method_get_name (cm), "Finalize")) {
- finalize_slot = i;
- break;
- }
- }
- }
+ if (!klass->inited)
+ mono_class_init (klass);
if (!klass->has_finalize)
return NULL;
return TRUE;
}
+/**
+ * mono_classes_init:
+ *
+ * Initialize the resources used by this module.
+ */
+void
+mono_classes_init (void)
+{
+}
+
+/**
+ * mono_classes_cleanup:
+ *
+ * Free the resources used by this module.
+ */
+void
+mono_classes_cleanup (void)
+{
+ IOffsetInfo *cached_info, *next;
+
+ if (global_interface_bitset)
+ mono_bitset_free (global_interface_bitset);
+
+ for (cached_info = cached_offset_info; cached_info;) {
+ next = cached_info->next;
+
+ g_free (cached_info);
+ cached_info = next;
+ }
+}
+
/**
* mono_class_get_exception_for_failure:
* @klass: class in which the failure was detected
}
case MONO_EXCEPTION_TYPE_LOAD:
return mono_exception_from_name (mono_defaults.corlib, "System", "TypeLoadException");
- /* TODO - handle other class related failures */
- default:
+
+ default: {
+ MonoLoaderError *error;
+ MonoException *ex;
+
+ error = mono_loader_get_last_error ();
+ if (error != NULL){
+ ex = mono_loader_error_prepare_exception (error);
+ return ex;
+ }
+
+ /* TODO - handle other class related failures */
return NULL;
}
+ }
}