2 * object.c: Object creation for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/tabledefs.h>
14 #include <mono/metadata/loader.h>
15 #include <mono/metadata/object.h>
16 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
18 #include <limits.h> /* for PAGESIZE */
25 default_runtime_object_init (MonoObject *o)
30 MonoRuntimeObjectInit mono_runtime_object_init = default_runtime_object_init;
33 mono_install_runtime_object_init (MonoRuntimeObjectInit func)
35 mono_runtime_object_init = func? func: default_runtime_object_init;
39 * mono_object_allocate:
40 * @size: number of bytes to allocate
42 * This is a very simplistic routine until we have our GC-aware
45 * Returns: an allocated object of size @size, or NULL on failure.
48 mono_object_allocate (size_t size)
50 void *o = calloc (1, size);
58 * Frees the memory used by the object. Debugging purposes
59 * only, as we will have our GC system.
62 mono_object_free (MonoObject *o)
64 MonoClass *c = o->klass;
66 memset (o, 0, c->instance_size);
72 * @klass: the class of the object that we want to create
74 * Returns: A newly created object whose definition is
75 * looked up using @klass
78 mono_object_new (MonoClass *klass)
83 mono_class_init (klass);
85 o = mono_object_allocate (klass->instance_size);
92 * mono_object_new_from_token:
93 * @image: Context where the type_token is hosted
94 * @token: a token of the type that we want to create
96 * Returns: A newly created object whose definition is
97 * looked up using @token in the @image image
100 mono_object_new_from_token (MonoImage *image, guint32 token)
104 class = mono_class_get (image, token);
106 return mono_object_new (class);
112 * @obj: the object to clone
114 * Returns: A newly created object who is a shallow copy of @obj
117 mono_object_clone (MonoObject *obj)
122 size = obj->klass->instance_size;
123 o = mono_object_allocate (size);
124 /* FIXME: handle arrays... */
126 memcpy (o, obj, size);
132 mono_array_new_full (MonoClass *array_class, guint32 *lengths, guint32 *lower_bounds)
137 MonoArrayBounds *bounds;
140 if (!array_class->inited)
141 mono_class_init (array_class);
142 byte_len = mono_array_element_size (array_class);
144 bounds = g_malloc0 (sizeof (MonoArrayBounds) * array_class->rank);
145 for (i = 0; i < array_class->rank; ++i) {
146 bounds [i].length = lengths [i];
147 byte_len *= lengths [i];
151 for (i = 0; i < array_class->rank; ++i)
152 bounds [i].lower_bound = lower_bounds [i];
154 * Following three lines almost taken from mono_object_new ():
155 * they need to be kept in sync.
157 o = mono_object_allocate (sizeof (MonoArray) + byte_len);
160 o->klass = array_class;
162 array = (MonoArray*)o;
164 array->bounds = bounds;
170 * @image: image where the object is being referenced
171 * @eclass: element class
172 * @n: number of array elements
174 * This routine creates a new szarray with @n elements of type @token
177 mono_array_new (MonoClass *eclass, guint32 n)
181 ac = mono_array_class_get (eclass, 1);
182 g_assert (ac != NULL);
184 return mono_array_new_full (ac, &n, NULL);
188 * mono_string_new_utf16:
189 * @text: a pointer to an utf16 string
190 * @len: the length of the string
192 * Returns: A newly created string object which contains @text.
195 mono_string_new_utf16 (const guint16 *text, gint32 len)
200 s = (MonoString*)mono_object_new (mono_defaults.string_class);
201 g_assert (s != NULL);
203 ca = (MonoArray *)mono_array_new (mono_defaults.string_class, len);
204 g_assert (ca != NULL);
209 memcpy (ca->vector, text, len * 2);
216 * @text: a pointer to an utf8 string
218 * Returns: A newly created string object which contains @text.
221 mono_string_new (const char *text)
223 GError *error = NULL;
224 MonoString *o = NULL;
231 ut = g_utf8_to_utf16 (text, l, NULL, &items_written, &error);
234 o = mono_string_new_utf16 (ut, items_written);
236 g_error_free (error);
245 * @class: the class of the value
246 * @value: a pointer to the unboxed data
248 * Returns: A newly created object which contains @value.
251 mono_value_box (MonoClass *class, gpointer value)
256 g_assert (class->valuetype);
258 size = mono_class_instance_size (class);
259 res = mono_object_allocate (size);
262 size = size - sizeof (MonoObject);
264 memcpy ((char *)res + sizeof (MonoObject), value, size);
270 * mono_object_isinst:
272 * @klass: a pointer to a class
274 * Returns: @obj if @obj is derived from @klass
277 mono_object_isinst (MonoObject *obj, MonoClass *klass)
287 mono_class_init (klass);
289 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
290 if ((klass->interface_id <= oklass->max_interface_id) &&
291 oklass->interface_offsets [klass->interface_id])
294 if ((oklass->baseval - klass->baseval) <= klass->diffval)
301 static GHashTable *ldstr_table = NULL;
304 ldstr_hash (const char* str)
308 len = mono_metadata_decode_blob_size (str, &str);
312 * FIXME: The distribution may not be so nice with lots of
313 * null chars in the string.
315 for (str += 1; str < end; str++)
316 h = (h << 5) - h + *str;
321 ldstr_equal (const char *str1, const char *str2) {
323 if ((len=mono_metadata_decode_blob_size (str1, &str1)) !=
324 mono_metadata_decode_blob_size (str2, &str2))
326 return memcmp (str1, str2, len) == 0;
335 check_interned (gpointer key, MonoString *value, InternCheck *check)
337 if (value == check->obj)
338 check->found = value;
342 mono_string_is_interned (MonoString *o)
348 * Yes, this is slow. Our System.String implementation needs to be redone.
349 * And GLib needs foreach() methods that can be stopped halfway.
351 g_hash_table_foreach (ldstr_table, (GHFunc)check_interned, &check);
356 mono_string_intern (MonoString *str)
359 char *ins = g_malloc (4 + str->length * 2);
362 /* Encode the length */
364 mono_metadata_encode_value (str->length, p, &p);
365 memcpy (p, str->c_str->vector, str->length * 2);
367 if ((res = g_hash_table_lookup (ldstr_table, str))) {
371 g_hash_table_insert (ldstr_table, ins, str);
376 mono_ldstr (MonoImage *image, guint32 index)
378 const char *str, *sig;
383 ldstr_table = g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
385 sig = str = mono_metadata_user_string (image, index);
387 if ((o = g_hash_table_lookup (ldstr_table, str)))
390 len2 = mono_metadata_decode_blob_size (str, &str);
391 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
392 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
397 /* FIXME: it will be better to just add WRITE and after get it to previous state */
398 mprotect ((void *) ((int) str & ~(PAGESIZE - 1)), len2 + ((int) str & (PAGESIZE - 1)),
399 PROT_READ | PROT_WRITE | PROT_EXEC);
401 for (i = 0, s = (guint16 *) str; i < len2; i++, s++) {
402 *s = ((*s & 0xff) << 8) | (*s >> 8);
410 o = mono_string_new_utf16 ((guint16*)str, len2);
411 g_hash_table_insert (ldstr_table, (gpointer)sig, o);
417 mono_string_to_utf8 (MonoString *s)
420 GError *error = NULL;
422 g_assert (s != NULL);
424 if (!s->length || !s->c_str)
425 return g_strdup ("");
427 vector = (char*)s->c_str->vector;
429 g_assert (vector != NULL);
431 as = g_utf16_to_utf8 (vector, s->length, NULL, NULL, &error);
439 mono_string_to_utf16 (MonoString *s)
443 g_assert (s != NULL);
445 as = g_malloc ((s->length * 2) + 2);
446 as [(s->length * 2)] = '\0';
447 as [(s->length * 2) + 1] = '\0';
449 if (!s->length || !s->c_str) {
453 memcpy (as, mono_string_chars(s), s->length * 2);
459 default_ex_handler (MonoException *ex)
461 MonoObject *o = (MonoObject*)ex;
462 g_error ("Exception %s.%s raised in C code", o->klass->name_space, o->klass->name);
465 static MonoExceptionFunc ex_handler = default_ex_handler;
468 mono_install_handler (MonoExceptionFunc func)
470 ex_handler = func? func: default_ex_handler;
474 mono_raise_exception (MonoException *ex)