2010-03-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / sgen-scan-object.h
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  *
25  * Scans one object, using the OBJ_XXX macros.  The start of the
26  * object must be given in the variable "char* start".  Afterwards,
27  * "start" will point to the start of the next object.
28  *
29  * Modifiers (automatically undefined):
30  *
31  * SCAN_OBJECT_NOSCAN - if defined, don't actually scan the object,
32  * i.e. don't invoke the OBJ_XXX macros.
33  *
34  * SCAN_OBJECT_ACTION - is invoked after an object has been scanned.
35  * The object's start is "start", its length in bytes (including
36  * padding at the end) is "skip_size".  "desc" is the object's GC
37  * descriptor.  The action can use the macro
38  * "SCAN" to scan the object.
39  */
40
41 #ifndef SCAN_OBJECT_ACTION
42 #define SCAN_OBJECT_ACTION
43 #endif
44
45 {
46         GCVTable *vt;
47         size_t skip_size;
48         mword desc;
49
50         vt = (GCVTable*)LOAD_VTABLE (start);
51         //type = vt->desc & 0x7;
52
53         /* gcc should be smart enough to remove the bounds check, but it isn't:( */
54         desc = vt->desc;
55         switch (desc & 0x7) {
56         case DESC_TYPE_STRING:
57                 STRING_SIZE (skip_size, start);
58 #define SCAN
59                 SCAN_OBJECT_ACTION;
60 #undef SCAN
61                 start += skip_size;
62                 break;
63         case DESC_TYPE_RUN_LENGTH:
64                 OBJ_RUN_LEN_SIZE (skip_size, desc, start);
65                 g_assert (skip_size);
66 #define SCAN OBJ_RUN_LEN_FOREACH_PTR (desc, start)
67 #ifndef SCAN_OBJECT_NOSCAN
68                 SCAN;
69 #endif
70                 SCAN_OBJECT_ACTION;
71 #undef SCAN
72                 start += skip_size;
73                 break;
74         case DESC_TYPE_ARRAY:
75         case DESC_TYPE_VECTOR:
76                 skip_size = safe_object_get_size ((MonoObject*)start);
77                 skip_size += (ALLOC_ALIGN - 1);
78                 skip_size &= ~(ALLOC_ALIGN - 1);
79 #define SCAN OBJ_VECTOR_FOREACH_PTR (vt, start)
80 #ifndef SCAN_OBJECT_NOSCAN
81                 SCAN;
82 #endif
83                 SCAN_OBJECT_ACTION;
84 #undef SCAN
85                 start += skip_size;
86                 break;
87         case DESC_TYPE_SMALL_BITMAP:
88                 OBJ_BITMAP_SIZE (skip_size, desc, start);
89                 g_assert (skip_size);
90 #define SCAN OBJ_BITMAP_FOREACH_PTR (desc, start)
91 #ifndef SCAN_OBJECT_NOSCAN
92                 SCAN;
93 #endif
94                 SCAN_OBJECT_ACTION;
95 #undef SCAN
96                 start += skip_size;
97                 break;
98         case DESC_TYPE_LARGE_BITMAP:
99                 skip_size = safe_object_get_size ((MonoObject*)start);
100                 skip_size += (ALLOC_ALIGN - 1);
101                 skip_size &= ~(ALLOC_ALIGN - 1);
102 #define SCAN OBJ_LARGE_BITMAP_FOREACH_PTR (vt,start)
103 #ifndef SCAN_OBJECT_NOSCAN
104                 SCAN;
105 #endif
106                 SCAN_OBJECT_ACTION;
107 #undef SCAN
108                 start += skip_size;
109                 break;
110         case DESC_TYPE_COMPLEX:
111                 /* this is a complex object */
112                 skip_size = safe_object_get_size ((MonoObject*)start);
113                 skip_size += (ALLOC_ALIGN - 1);
114                 skip_size &= ~(ALLOC_ALIGN - 1);
115 #define SCAN OBJ_COMPLEX_FOREACH_PTR (vt, start)
116 #ifndef SCAN_OBJECT_NOSCAN
117                 SCAN;
118 #endif
119                 SCAN_OBJECT_ACTION;
120 #undef SCAN
121                 start += skip_size;
122                 break;
123         case DESC_TYPE_COMPLEX_ARR:
124                 /* this is an array of complex structs */
125                 skip_size = safe_object_get_size ((MonoObject*)start);
126                 skip_size += (ALLOC_ALIGN - 1);
127                 skip_size &= ~(ALLOC_ALIGN - 1);
128 #define SCAN OBJ_COMPLEX_ARR_FOREACH_PTR (vt, start)
129 #ifndef SCAN_OBJECT_NOSCAN
130                 SCAN;
131 #endif
132                 SCAN_OBJECT_ACTION;
133 #undef SCAN
134                 start += skip_size;
135                 break;
136         default:
137                 g_assert_not_reached ();
138         }
139 }
140
141 #undef SCAN_OBJECT_NOSCAN
142 #undef SCAN_OBJECT_ACTION