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 * @klass: the class of the object that we want to create
51 * Returns: A newly created object whose definition is
52 * looked up using @klass
55 mono_object_new (MonoClass *klass)
59 if (!klass->metadata_inited)
60 mono_class_metadata_init (klass);
62 o = mono_object_allocate (klass->instance_size);
65 mono_threads_synchronisation_init(&o->synchronisation);
71 * mono_object_new_from_token:
72 * @image: Context where the type_token is hosted
73 * @token: a token of the type that we want to create
75 * Returns: A newly created object whose definition is
76 * looked up using @token in the @image image
79 mono_object_new_from_token (MonoImage *image, guint32 token)
83 class = mono_class_get (image, token);
85 return mono_object_new (class);
91 * @obj: the object to clone
93 * Returns: A newly created object who is a shallow copy of @obj
96 mono_object_clone (MonoObject *obj)
101 size = obj->klass->instance_size;
102 o = mono_object_allocate (size);
104 memcpy (o, obj, size);
110 mono_array_new_full (MonoClass *array_class, guint32 *lengths, guint32 *lower_bounds)
115 MonoArrayBounds *bounds;
118 if (!array_class->metadata_inited)
119 mono_class_metadata_init (array_class);
120 byte_len = mono_array_element_size (array_class);
122 bounds = g_malloc0 (sizeof (MonoArrayBounds) * array_class->rank);
123 for (i = 0; i < array_class->rank; ++i) {
124 bounds [i].length = lengths [i];
125 byte_len *= lengths [i];
129 for (i = 0; i < array_class->rank; ++i)
130 bounds [i].lower_bound = lower_bounds [i];
132 * Following three lines almost taken from mono_object_new ():
133 * they need to be kept in sync.
135 o = mono_object_allocate (sizeof (MonoArray) + byte_len);
136 o->klass = array_class;
137 mono_threads_synchronisation_init (&o->synchronisation);
139 array = (MonoArray*)o;
141 array->bounds = bounds;
147 * @image: image where the object is being referenced
148 * @eclass: element class
149 * @n: number of array elements
151 * This routine creates a new szarray with @n elements of type @token
154 mono_array_new (MonoClass *eclass, guint32 n)
158 ac = mono_array_class_get (eclass, 1);
159 g_assert (ac != NULL);
161 return mono_array_new_full (ac, &n, NULL);
165 * mono_string_new_utf16:
166 * @text: a pointer to an utf16 string
167 * @len: the length of the string
169 * Returns: A newly created string object which contains @text.
172 mono_string_new_utf16 (const guint16 *text, gint32 len)
177 s = (MonoString*)mono_object_new (mono_defaults.string_class);
178 g_assert (s != NULL);
180 ca = (MonoArray *)mono_array_new (mono_defaults.string_class, len);
181 g_assert (ca != NULL);
186 memcpy (ca->vector, text, len * 2);
193 * @text: a pointer to an utf8 string
195 * Returns: A newly created string object which contains @text.
198 mono_string_new (const char *text)
204 /* fixme: use some kind of unicode library here */
209 for (i = 0; i < l; i++)
212 o = mono_string_new_utf16 (ut, l);
221 * @class: the class of the value
222 * @value: a pointer to the unboxed data
224 * Returns: A newly created object which contains @value.
227 mono_value_box (MonoClass *class, gpointer value)
232 g_assert (class->valuetype);
234 size = mono_class_instance_size (class);
235 res = mono_object_allocate (size);
238 size = size - sizeof (MonoObject);
240 memcpy ((char *)res + sizeof (MonoObject), value, size);
246 * mono_object_isinst:
248 * @klass: a pointer to a class
250 * Returns: #TRUE if @obj is derived from @klass
253 mono_object_isinst (MonoObject *obj, MonoClass *klass)
255 MonoClass *oklass = obj->klass;
260 oklass = oklass->parent;
265 static GHashTable *ldstr_table = NULL;
268 ldstr_hash (const char* str)
272 len = mono_metadata_decode_blob_size (str, &str);
276 * FIXME: The distribution may not be so nice with lots of
277 * null chars in the string.
279 for (str += 1; str < end; str++)
280 h = (h << 5) - h + *str;
285 ldstr_equal (const char *str1, const char *str2) {
287 if ((len=mono_metadata_decode_blob_size (str1, &str1)) !=
288 mono_metadata_decode_blob_size (str2, &str2))
290 return memcmp (str1, str2, len) == 0;
299 check_interned (gpointer key, MonoString *value, InternCheck *check)
301 if (value == check->obj)
302 check->found = value;
306 mono_string_is_interned (MonoString *o)
312 * Yes, this is slow. Our System.String implementation needs to be redone.
313 * And GLib needs foreach() methods that can be stopped halfway.
315 g_hash_table_foreach (ldstr_table, (GHFunc)check_interned, &check);
320 mono_string_intern (MonoString *str)
323 char *ins = g_malloc (4 + str->length * 2);
326 /* Encode the length */
328 mono_metadata_encode_value (str->length, p, &p);
329 memcpy (p, str->c_str->vector, str->length * 2);
331 if ((res = g_hash_table_lookup (ldstr_table, str))) {
335 g_hash_table_insert (ldstr_table, ins, str);
340 mono_ldstr (MonoImage *image, guint32 index)
342 const char *str, *sig;
347 ldstr_table = g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
349 sig = str = mono_metadata_user_string (image, index);
351 if ((o = g_hash_table_lookup (ldstr_table, str)))
354 len = mono_metadata_decode_blob_size (str, &str);
355 o = mono_string_new_utf16 ((guint16*)str, len >> 1);
356 g_hash_table_insert (ldstr_table, (gpointer)sig, o);
362 mono_string_to_utf8 (MonoString *s)
367 g_assert (s != NULL);
369 if (!s->length || !s->c_str)
370 return g_strdup ("");
372 vector = (char*)s->c_str->vector;
374 g_assert (vector != NULL);
376 as = g_malloc (s->length + 1);
378 /* fixme: replace with a real unicode/ansi conversion */
379 for (i = 0; i < s->length; i++) {
380 as [i] = vector [i*2];