merged the cli and metadata directories - everything is now in metadata
[mono.git] / mono / metadata / object.c
1 /*
2  * object.c: Object creation for the Mono runtime
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <mono/metadata/loader.h>
13 #include <mono/metadata/object.h>
14
15 /**
16  * mono_object_allocate:
17  * @size: number of bytes to allocate
18  *
19  * This is a very simplistic routine until we have our GC-aware
20  * memory allocator. 
21  *
22  * Returns: an allocated object of size @size, or NULL on failure.
23  */
24 static void *
25 mono_object_allocate (size_t size)
26 {
27         void *o = calloc (1, size);
28
29         return o;
30 }
31
32 /**
33  * mono_object_free:
34  *
35  * Frees the memory used by the object.  Debugging purposes
36  * only, as we will have our GC system.
37  */
38 void
39 mono_object_free (MonoObject *o)
40 {
41         MonoClass *c = o->klass;
42         
43         memset (o, 0, c->instance_size);
44         free (o);
45 }
46
47 /**
48  * mono_object_new:
49  * @image: Context where the type_token is hosted
50  * @type_token: a token of the type that we want to create
51  *
52  * Returns: A newly created object whose definition is
53  * looked up using @type_token in the @image image
54  */
55 MonoObject *
56 mono_object_new (MonoImage *image, guint32 type_token)
57 {
58         MonoClass *c;
59         MonoObject *o;
60
61         c = mono_class_get (image, type_token);
62         o = mono_object_allocate (c->instance_size);
63         o->klass = c;
64
65         return o;
66 }
67
68 /**
69  * mono_object_clone:
70  * @obj: the object to clone
71  *
72  * Returns: A newly created object who is a shallow copy of @obj
73  */
74 MonoObject *
75 mono_object_clone (MonoObject *obj)
76 {
77         MonoObject *o;
78         int size;
79         
80         size = obj->klass->instance_size;
81         o = mono_object_allocate (size);
82         
83         memcpy (o, obj, size);
84
85         return o;
86 }
87
88 /*
89  * mono_new_szarray:
90  * @image: image where the object is being referenced
91  * @etype: element type token
92  * @n: number of array elements
93  *
94  * This routine creates a new szarray with @n elements of type @token
95  */
96 MonoObject *
97 mono_new_szarray (MonoImage *image, guint32 etype, guint32 n)
98 {
99         MonoClass *c;
100         MonoObject *o;
101         MonoArrayObject *ao;
102         MonoArrayClass *ac;
103
104         c = mono_array_class_get (image, etype, 1);
105         g_assert (c != NULL);
106
107         o = mono_object_allocate (c->instance_size);
108         o->klass = c;
109
110         ao = (MonoArrayObject *)o;
111         ac = (MonoArrayClass *)c;
112
113         ao->bounds = g_malloc0 (sizeof (MonoArrayBounds));
114         ao->bounds [0].length = n;
115         ao->bounds [0].lower_bound = 0;
116
117         ao->vector = g_malloc0 (n * ac->esize);
118
119         return o;
120 }
121
122 MonoObject *
123 mono_new_utf16_string (const char *text, gint32 len)
124 {
125         MonoObject *s;
126         MonoArrayObject *ca;
127
128         s = mono_object_new (mono_defaults.corlib, mono_defaults.string_token);
129         g_assert (s != NULL);
130
131         ca = (MonoArrayObject *)mono_new_szarray (mono_defaults.corlib, mono_defaults.string_token, len);
132         g_assert (ca != NULL);
133         
134         ((MonoStringObject *)s)->c_str = ca;
135         ((MonoStringObject *)s)->length = len;
136
137         memcpy (ca->vector, text, len * 2);
138
139         return s;
140 }
141
142 MonoObject *
143 mono_new_string (const char *text)
144 {
145         MonoObject *o;
146         guint16 *ut;
147         int i, l;
148
149         /* fixme: use some kind of unicode library here */
150
151         l = strlen (text);
152         ut = g_malloc (l*2);
153
154         for (i = 0; i < l; i++)
155                 ut [i] = text[i];
156         
157         o = mono_new_utf16_string ((char *)ut, l);
158
159         g_free (ut);
160
161         return o;
162 }
163
164 MonoObject *
165 mono_value_box (MonoImage *image, guint32 etype, gpointer val)
166 {
167         MonoObject *res;
168         int size;
169
170         res = mono_object_new (image, etype);
171
172         g_assert (res->klass->valuetype);
173
174         size = res->klass->instance_size - sizeof (MonoObject);
175
176         memcpy ((char *)res + sizeof (MonoObject), val, size);
177
178         return res;
179 }