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/loader.h>
14 #include <mono/metadata/object.h>
15 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
17 #include <limits.h> /* for PAGESIZE */
24 * mono_object_allocate:
25 * @size: number of bytes to allocate
27 * This is a very simplistic routine until we have our GC-aware
30 * Returns: an allocated object of size @size, or NULL on failure.
33 mono_object_allocate (size_t size)
35 void *o = calloc (1, size);
43 * Frees the memory used by the object. Debugging purposes
44 * only, as we will have our GC system.
47 mono_object_free (MonoObject *o)
49 MonoClass *c = o->klass;
51 memset (o, 0, c->instance_size);
57 * @klass: the class of the object that we want to create
59 * Returns: A newly created object whose definition is
60 * looked up using @klass
63 mono_object_new (MonoClass *klass)
67 if (!klass->metadata_inited)
68 mono_class_metadata_init (klass);
70 o = mono_object_allocate (klass->instance_size);
77 * mono_object_new_from_token:
78 * @image: Context where the type_token is hosted
79 * @token: a token of the type that we want to create
81 * Returns: A newly created object whose definition is
82 * looked up using @token in the @image image
85 mono_object_new_from_token (MonoImage *image, guint32 token)
89 class = mono_class_get (image, token);
91 return mono_object_new (class);
97 * @obj: the object to clone
99 * Returns: A newly created object who is a shallow copy of @obj
102 mono_object_clone (MonoObject *obj)
107 size = obj->klass->instance_size;
108 o = mono_object_allocate (size);
109 /* FIXME: handle arrays... */
111 memcpy (o, obj, size);
117 mono_array_new_full (MonoClass *array_class, guint32 *lengths, guint32 *lower_bounds)
122 MonoArrayBounds *bounds;
125 if (!array_class->metadata_inited)
126 mono_class_metadata_init (array_class);
127 byte_len = mono_array_element_size (array_class);
129 bounds = g_malloc0 (sizeof (MonoArrayBounds) * array_class->rank);
130 for (i = 0; i < array_class->rank; ++i) {
131 bounds [i].length = lengths [i];
132 byte_len *= lengths [i];
136 for (i = 0; i < array_class->rank; ++i)
137 bounds [i].lower_bound = lower_bounds [i];
139 * Following three lines almost taken from mono_object_new ():
140 * they need to be kept in sync.
142 o = mono_object_allocate (sizeof (MonoArray) + byte_len);
145 o->klass = array_class;
147 array = (MonoArray*)o;
149 array->bounds = bounds;
155 * @image: image where the object is being referenced
156 * @eclass: element class
157 * @n: number of array elements
159 * This routine creates a new szarray with @n elements of type @token
162 mono_array_new (MonoClass *eclass, guint32 n)
166 ac = mono_array_class_get (eclass, 1);
167 g_assert (ac != NULL);
169 return mono_array_new_full (ac, &n, NULL);
173 * mono_string_new_utf16:
174 * @text: a pointer to an utf16 string
175 * @len: the length of the string
177 * Returns: A newly created string object which contains @text.
180 mono_string_new_utf16 (const guint16 *text, gint32 len)
185 s = (MonoString*)mono_object_new (mono_defaults.string_class);
186 g_assert (s != NULL);
188 ca = (MonoArray *)mono_array_new (mono_defaults.string_class, len);
189 g_assert (ca != NULL);
194 memcpy (ca->vector, text, len * 2);
201 * @text: a pointer to an utf8 string
203 * Returns: A newly created string object which contains @text.
206 mono_string_new (const char *text)
212 /* fixme: use some kind of unicode library here */
217 for (i = 0; i < l; i++)
220 o = mono_string_new_utf16 (ut, l);
229 * @class: the class of the value
230 * @value: a pointer to the unboxed data
232 * Returns: A newly created object which contains @value.
235 mono_value_box (MonoClass *class, gpointer value)
240 g_assert (class->valuetype);
242 size = mono_class_instance_size (class);
243 res = mono_object_allocate (size);
246 size = size - sizeof (MonoObject);
248 memcpy ((char *)res + sizeof (MonoObject), value, size);
254 * mono_object_isinst:
256 * @klass: a pointer to a class
258 * Returns: #TRUE if @obj is derived from @klass
261 mono_object_isinst (MonoObject *obj, MonoClass *klass)
263 MonoClass *oklass = obj->klass;
268 oklass = oklass->parent;
273 static GHashTable *ldstr_table = NULL;
276 ldstr_hash (const char* str)
280 len = mono_metadata_decode_blob_size (str, &str);
284 * FIXME: The distribution may not be so nice with lots of
285 * null chars in the string.
287 for (str += 1; str < end; str++)
288 h = (h << 5) - h + *str;
293 ldstr_equal (const char *str1, const char *str2) {
295 if ((len=mono_metadata_decode_blob_size (str1, &str1)) !=
296 mono_metadata_decode_blob_size (str2, &str2))
298 return memcmp (str1, str2, len) == 0;
307 check_interned (gpointer key, MonoString *value, InternCheck *check)
309 if (value == check->obj)
310 check->found = value;
314 mono_string_is_interned (MonoString *o)
320 * Yes, this is slow. Our System.String implementation needs to be redone.
321 * And GLib needs foreach() methods that can be stopped halfway.
323 g_hash_table_foreach (ldstr_table, (GHFunc)check_interned, &check);
328 mono_string_intern (MonoString *str)
331 char *ins = g_malloc (4 + str->length * 2);
334 /* Encode the length */
336 mono_metadata_encode_value (str->length, p, &p);
337 memcpy (p, str->c_str->vector, str->length * 2);
339 if ((res = g_hash_table_lookup (ldstr_table, str))) {
343 g_hash_table_insert (ldstr_table, ins, str);
348 mono_ldstr (MonoImage *image, guint32 index)
350 const char *str, *sig;
355 ldstr_table = g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
357 sig = str = mono_metadata_user_string (image, index);
359 if ((o = g_hash_table_lookup (ldstr_table, str)))
362 len2 = mono_metadata_decode_blob_size (str, &str);
363 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
364 #define SWAP16(x) (x) = GUINT16_FROM_LE ((x))
369 /* FIXME: it will be better to just add WRITE and after get it to previous state */
370 mprotect ((void *) ((int) str & ~(PAGESIZE - 1)), len2 + ((int) str & (PAGESIZE - 1)),
371 PROT_READ | PROT_WRITE | PROT_EXEC);
373 for (i = 0, s = (guint16 *) str; i < len2; i++, s++) {
374 *s = ((*s & 0xff) << 8) | (*s >> 8);
382 o = mono_string_new_utf16 ((guint16*)str, len2);
383 g_hash_table_insert (ldstr_table, (gpointer)sig, o);
389 mono_string_to_utf8 (MonoString *s)
394 g_assert (s != NULL);
396 if (!s->length || !s->c_str)
397 return g_strdup ("");
399 vector = (char*)s->c_str->vector;
401 g_assert (vector != NULL);
403 as = g_malloc (s->length + 1);
405 /* fixme: replace with a real unicode/ansi conversion */
406 for (i = 0; i < s->length; i++) {
407 as [i] = vector [i*2];
416 default_ex_handler (MonoException *ex)
418 MonoObject *o = (MonoObject*)ex;
419 g_error ("Exception %s.%s raised in C code", o->klass->name_space, o->klass->name);
422 static MonoExceptionFunc ex_handler = default_ex_handler;
425 mono_install_handler (MonoExceptionFunc func)
427 ex_handler = func? func: default_ex_handler;
431 mono_raise_exception (MonoException *ex)