2 * object.c: Object creation for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
12 #include <mono/metadata/loader.h>
13 #include <mono/metadata/object.h>
16 * mono_object_allocate:
17 * @size: number of bytes to allocate
19 * This is a very simplistic routine until we have our GC-aware
22 * Returns: an allocated object of size @size, or NULL on failure.
25 mono_object_allocate (size_t size)
27 void *o = calloc (1, size);
35 * Frees the memory used by the object. Debugging purposes
36 * only, as we will have our GC system.
39 mono_object_free (MonoObject *o)
41 MonoClass *c = o->klass;
43 memset (o, 0, c->instance_size);
49 * @image: Context where the type_token is hosted
50 * @type_token: a token of the type that we want to create
52 * Returns: A newly created object whose definition is
53 * looked up using @type_token in the @image image
56 mono_object_new (MonoImage *image, guint32 type_token)
61 c = mono_class_get (image, type_token);
62 o = mono_object_allocate (c->instance_size);
70 * @obj: the object to clone
72 * Returns: A newly created object who is a shallow copy of @obj
75 mono_object_clone (MonoObject *obj)
80 size = obj->klass->instance_size;
81 o = mono_object_allocate (size);
83 memcpy (o, obj, size);
90 * @image: image where the object is being referenced
91 * @etype: element type token
92 * @n: number of array elements
94 * This routine creates a new szarray with @n elements of type @token
97 mono_new_szarray (MonoImage *image, guint32 etype, guint32 n)
104 c = mono_array_class_get (image, etype, 1);
105 g_assert (c != NULL);
107 o = mono_object_allocate (c->instance_size);
110 ao = (MonoArrayObject *)o;
111 ac = (MonoArrayClass *)c;
113 ao->bounds = g_malloc0 (sizeof (MonoArrayBounds));
114 ao->bounds [0].length = n;
115 ao->bounds [0].lower_bound = 0;
117 ao->vector = g_malloc0 (n * ac->esize);
123 mono_new_utf16_string (const char *text, gint32 len)
128 s = mono_object_new (mono_defaults.corlib, mono_defaults.string_token);
129 g_assert (s != NULL);
131 ca = (MonoArrayObject *)mono_new_szarray (mono_defaults.corlib, mono_defaults.string_token, len);
132 g_assert (ca != NULL);
134 ((MonoStringObject *)s)->c_str = ca;
135 ((MonoStringObject *)s)->length = len;
137 memcpy (ca->vector, text, len * 2);
143 mono_new_string (const char *text)
149 /* fixme: use some kind of unicode library here */
154 for (i = 0; i < l; i++)
157 o = mono_new_utf16_string ((char *)ut, l);
165 mono_value_box (MonoImage *image, guint32 etype, gpointer val)
170 res = mono_object_new (image, etype);
172 g_assert (res->klass->valuetype);
174 size = res->klass->instance_size - sizeof (MonoObject);
176 memcpy ((char *)res + sizeof (MonoObject), val, size);