2001-08-09 Dietmar Maurer <dietmar@ximian.com>
[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/cli/cli.h>
13 #include <mono/cli/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_new_szarray:
70  * @image: image where the object is being referenced
71  * @etype: element type token
72  * @n: number of array elements
73  *
74  * This routine creates a new szarray with @n elements of type @token
75  */
76 MonoObject *
77 mono_new_szarray (MonoImage *image, guint32 etype, guint32 n)
78 {
79         MonoClass *c;
80         MonoObject *o;
81         MonoArrayObject *ao;
82         MonoArrayClass *ac;
83         guint32 esize;
84
85         c = mono_array_class_get (image, etype, 1);
86         g_assert (c != NULL);
87
88         o = mono_object_allocate (c->instance_size);
89         o->klass = c;
90
91         ao = (MonoArrayObject *)o;
92         ac = (MonoArrayClass *)c;
93
94         ao->bounds = g_malloc0 (sizeof (MonoArrayBounds));
95         ao->bounds [0].length = n;
96         ao->bounds [0].lower_bound = 0;
97
98         ao->vector = g_malloc0 (n * ac->esize);
99
100         return o;
101 }