2001-11-07 Miguel de Icaza <miguel@ximian.com>
[mono.git] / docs / object-layout
1 Object and VTable layout
2 ========================
3
4 The first pointer inside an Object points to a MonoClass structure. Objects
5 also contains a MonoThreadsSync structure which is used by the mono Thread
6 implementation. 
7  
8 typedef struct {
9         MonoClass *class;
10         MonoThreadsSync synchronisation;
11         
12         /* object specific data goes here */
13 } MonoObject;
14
15 The MonoClass contains all Class infos, the VTable and a pointer to static
16 class data.
17
18 typedef struct {
19         /* various class infos */
20         MonoClass  *parent;
21         const char *name;
22         const char *name_space;
23
24         ...
25
26         /* interface offset table */
27         gint  *interface_offsets;
28
29         gpointer data; /* a pointer to static data */
30
31         /* the variable sized vtable is included at the end */
32         gpointer vtable [vtable_size];
33 } MonoClass;
34
35
36 Calling virtual functions:
37 ==========================
38
39 Each MonoMethod (if virtual) has an associated slot, which is an index into the
40 VTable. So we can use the following code to compute the address of a virtual
41 function: 
42  
43 method_addr = object->class->vtable [method->slot];
44
45
46 Calling interface methods:
47 ==========================
48
49 Each interface class is associated with an unique ID. The following code
50 computes the address of an interface function:
51
52 offset_into_vtable = object->class->interface_offsets [interface_id];
53 method_addr = object->class->vtable [offset_into_vtable + method->slot];
54
55
56
57
58