Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mono / metadata / sgen-descriptor.h
1 /*
2  * sgen-descriptor.h: GC descriptors describe object layout.
3
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
7  *
8  * Copyright (C) 2012 Xamarin Inc
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License 2.0 as published by the Free Software Foundation;
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License 2.0 along with this library; if not, write to the Free
21  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 #ifndef __MONO_SGEN_DESCRIPTOR_H__
24 #define __MONO_SGEN_DESCRIPTOR_H__
25
26 #include <mono/metadata/gc-internal.h>
27 #include <mono/metadata/sgen-conf.h>
28
29
30 /*
31  * ######################################################################
32  * ########  GC descriptors
33  * ######################################################################
34  * Used to quickly get the info the GC needs about an object: size and
35  * where the references are held.
36  */
37 #define OBJECT_HEADER_WORDS (sizeof(MonoObject)/sizeof(gpointer))
38 #define LOW_TYPE_BITS 3
39 #define MAX_RUNLEN_OBJECT_SIZE 0xFFFF
40 #define SMALL_BITMAP_SHIFT 16
41 #define SMALL_BITMAP_SIZE (GC_BITS_PER_WORD - SMALL_BITMAP_SHIFT)
42 #define VECTOR_INFO_SHIFT 14
43 #define VECTOR_KIND_SHIFT 13
44 #define VECTOR_ELSIZE_SHIFT 3
45 #define LARGE_BITMAP_SIZE (GC_BITS_PER_WORD - LOW_TYPE_BITS)
46 #define MAX_ELEMENT_SIZE 0x3ff
47 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
48 #define VECTOR_SUBTYPE_REFS    (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
49 #define VECTOR_SUBTYPE_RUN_LEN (DESC_TYPE_V_RUN_LEN << VECTOR_INFO_SHIFT)
50 #define VECTOR_SUBTYPE_BITMAP  (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
51
52 #define VECTOR_KIND_SZARRAY  (DESC_TYPE_V_SZARRAY << VECTOR_KIND_SHIFT)
53 #define VECTOR_KIND_ARRAY  (DESC_TYPE_V_ARRAY << VECTOR_KIND_SHIFT)
54
55 /* objects are aligned to 8 bytes boundaries
56  * A descriptor is a pointer in MonoVTable, so 32 or 64 bits of size.
57  * The low 3 bits define the type of the descriptor. The other bits
58  * depend on the type.
59  * As a general rule the 13 remaining low bits define the size, either
60  * of the whole object or of the elements in the arrays. While for objects
61  * the size is already in bytes, for arrays we need to shift, because
62  * array elements might be smaller than 8 bytes. In case of arrays, we
63  * use two bits to describe what the additional high bits represents,
64  * so the default behaviour can handle element sizes less than 2048 bytes.
65  * The high 16 bits, if 0 it means the object is pointer-free.
66  * This design should make it easy and fast to skip over ptr-free data.
67  * The first 4 types should cover >95% of the objects.
68  * Note that since the size of objects is limited to 64K, larger objects
69  * will be allocated in the large object heap.
70  * If we want 4-bytes alignment, we need to put vector and small bitmap
71  * inside complex.
72  */
73 enum {
74         /*
75          * We don't use 0 so that 0 isn't a valid GC descriptor.  No
76          * deep reason for this other than to be able to identify a
77          * non-inited descriptor for debugging.
78          *
79          * If an object contains no references, its GC descriptor is
80          * always DESC_TYPE_RUN_LENGTH, without a size, no exceptions.
81          * This is so that we can quickly check for that in
82          * copy_object_no_checks(), without having to fetch the
83          * object's class.
84          */
85         DESC_TYPE_RUN_LENGTH = 1, /* 16 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
86         DESC_TYPE_SMALL_BITMAP,   /* 16 bits aligned byte size | 16-48 bit bitmap */
87         DESC_TYPE_COMPLEX,      /* index for bitmap into complex_descriptors */
88         DESC_TYPE_VECTOR,       /* 10 bits element size | 1 bit kind | 2 bits desc | element desc */
89         DESC_TYPE_LARGE_BITMAP, /* | 29-61 bitmap bits */
90         DESC_TYPE_COMPLEX_ARR,  /* index for bitmap into complex_descriptors */
91         DESC_TYPE_COMPLEX_PTRFREE, /*Nothing, used to encode large ptr objects. */
92         /* values for array kind */
93         DESC_TYPE_V_SZARRAY = 0, /*vector with no bounds data */
94         DESC_TYPE_V_ARRAY = 1, /* array with bounds data */
95         /* subtypes for arrays and vectors */
96         DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value  */
97         DESC_TYPE_V_REFS,       /* all the array elements are refs */
98         DESC_TYPE_V_RUN_LEN,    /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
99         DESC_TYPE_V_BITMAP      /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
100 };
101
102 /* Root bitmap descriptors are simpler: the lower three bits describe the type
103  * and we either have 30/62 bitmap bits or nibble-based run-length,
104  * or a complex descriptor, or a user defined marker function.
105  */
106 enum {
107         ROOT_DESC_CONSERVATIVE, /* 0, so matches NULL value */
108         ROOT_DESC_BITMAP,
109         ROOT_DESC_RUN_LEN, 
110         ROOT_DESC_COMPLEX,
111         ROOT_DESC_USER,
112         ROOT_DESC_TYPE_MASK = 0x7,
113         ROOT_DESC_TYPE_SHIFT = 3,
114 };
115
116 gsize* sgen_get_complex_descriptor (mword desc) MONO_INTERNAL;
117 void* sgen_get_complex_descriptor_bitmap (mword desc) MONO_INTERNAL;
118 MonoGCRootMarkFunc sgen_get_user_descriptor_func (mword desc) MONO_INTERNAL;
119
120
121 static inline gboolean
122 sgen_gc_descr_has_references (mword desc)
123 {
124         /*Both string and fixed size objects are encoded using a zero run RUN_LEN*/
125         if ((desc & 0xffff0007) == DESC_TYPE_RUN_LENGTH)
126                 return FALSE;
127
128         /*The array is ptr-free*/
129         if ((desc & 0xC007) == (DESC_TYPE_VECTOR | VECTOR_SUBTYPE_PTRFREE))
130                 return FALSE;
131
132         if ((desc & 0x7) == DESC_TYPE_COMPLEX_PTRFREE)
133                 return FALSE;
134
135         return TRUE;
136 }
137
138 #define SGEN_VTABLE_HAS_REFERENCES(vt)  (sgen_gc_descr_has_references ((mword)((MonoVTable*)(vt))->gc_descr))
139 #define SGEN_CLASS_HAS_REFERENCES(c)    (sgen_gc_descr_has_references ((mword)(c)->gc_descr))
140
141 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
142 #define OBJ_RUN_LEN_SIZE(size,desc,obj) do { \
143                 (size) = ((desc) & 0xfff8);     \
144     } while (0)
145
146 #define OBJ_BITMAP_SIZE(size,desc,obj) do { \
147                 (size) = ((desc) & 0xfff8);     \
148     } while (0)
149
150 #ifdef __GNUC__
151 #define PREFETCH(addr)  __builtin_prefetch ((addr))
152 #else
153 #define PREFETCH(addr)
154 #endif
155
156 #if defined(__GNUC__) && SIZEOF_VOID_P==4
157 #define GNUC_BUILTIN_CTZ(bmap)  __builtin_ctz(bmap)
158 #elif defined(__GNUC__) && SIZEOF_VOID_P==8
159 #define GNUC_BUILTIN_CTZ(bmap)  __builtin_ctzl(bmap)
160 #endif
161
162 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
163 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj)       do {    \
164                 if ((desc) & 0xffff0000) {      \
165                         /* there are pointers */        \
166                         void **_objptr_end;     \
167                         void **_objptr = (void**)(obj); \
168                         _objptr += ((desc) >> 16) & 0xff;       \
169                         _objptr_end = _objptr + (((desc) >> 24) & 0xff);        \
170                         HANDLE_PTR (_objptr, (obj)); \
171                         _objptr ++; \
172                         while (_objptr < _objptr_end) { \
173                                 HANDLE_PTR (_objptr, (obj));    \
174                                 _objptr++;      \
175                         }       \
176                 }       \
177         } while (0)
178
179 #if defined(__GNUC__)
180 #define OBJ_BITMAP_FOREACH_PTR(desc,obj)       do {    \
181                 /* there are pointers */        \
182                 void **_objptr = (void**)(obj); \
183                 gsize _bmap = (desc) >> 16;     \
184                 _objptr += OBJECT_HEADER_WORDS; \
185                 { \
186                         int _index = GNUC_BUILTIN_CTZ (_bmap);          \
187                         _objptr += _index; \
188                         _bmap >>= (_index + 1);                         \
189                         HANDLE_PTR (_objptr, (obj));            \
190                         _objptr ++;                                                     \
191                         } \
192                 while (_bmap) { \
193                         int _index = GNUC_BUILTIN_CTZ (_bmap);          \
194                         _objptr += _index; \
195                         _bmap >>= (_index + 1);                         \
196                         HANDLE_PTR (_objptr, (obj));            \
197                         _objptr ++;                                                     \
198                 }                                                                               \
199         } while (0)
200 #else
201 #define OBJ_BITMAP_FOREACH_PTR(desc,obj)       do {    \
202                 /* there are pointers */        \
203                 void **_objptr = (void**)(obj); \
204                 gsize _bmap = (desc) >> 16;     \
205                 _objptr += OBJECT_HEADER_WORDS; \
206                 while (_bmap) {                                            \
207                         if ((_bmap & 1)) {                                                                 \
208                                 HANDLE_PTR (_objptr, (obj));                               \
209                         }                                                                                                  \
210                         _bmap >>= 1;                                                                       \
211                         ++_objptr;                                                                                 \
212                 }                                                                                                          \
213         } while (0)
214 #endif
215
216 /* a bitmap desc means that there are pointer references or we'd have
217  * choosen run-length, instead: add an assert to check.
218  */
219 #define OBJ_LARGE_BITMAP_FOREACH_PTR(desc,obj)  do {    \
220                 /* there are pointers */        \
221                 void **_objptr = (void**)(obj); \
222                 gsize _bmap = (desc) >> LOW_TYPE_BITS;  \
223                 _objptr += OBJECT_HEADER_WORDS; \
224                 while (_bmap) { \
225                         if ((_bmap & 1)) {      \
226                                 HANDLE_PTR (_objptr, (obj));    \
227                         }       \
228                         _bmap >>= 1;    \
229                         ++_objptr;      \
230                 }       \
231         } while (0)
232
233 #define OBJ_COMPLEX_FOREACH_PTR(vt,obj) do {    \
234                 /* there are pointers */        \
235                 void **_objptr = (void**)(obj); \
236                 gsize *bitmap_data = sgen_get_complex_descriptor ((desc)); \
237                 int bwords = (*bitmap_data) - 1;        \
238                 void **start_run = _objptr;     \
239                 bitmap_data++;  \
240                 if (0) {        \
241                         MonoObject *myobj = (MonoObject*)obj;   \
242                         g_print ("found %d at %p (0x%zx): %s.%s\n", bwords, (obj), (desc), myobj->vtable->klass->name_space, myobj->vtable->klass->name); \
243                 }       \
244                 while (bwords-- > 0) {  \
245                         gsize _bmap = *bitmap_data++;   \
246                         _objptr = start_run;    \
247                         /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/        \
248                         while (_bmap) { \
249                                 if ((_bmap & 1)) {      \
250                                         HANDLE_PTR (_objptr, (obj));    \
251                                 }       \
252                                 _bmap >>= 1;    \
253                                 ++_objptr;      \
254                         }       \
255                         start_run += GC_BITS_PER_WORD;  \
256                 }       \
257         } while (0)
258
259 /* this one is untested */
260 #define OBJ_COMPLEX_ARR_FOREACH_PTR(vt,obj)     do {    \
261                 /* there are pointers */        \
262                 gsize *mbitmap_data = sgen_get_complex_descriptor ((vt)->desc); \
263                 int mbwords = (*mbitmap_data++) - 1;    \
264                 int el_size = mono_array_element_size (vt->klass);      \
265                 char *e_start = (char*)(obj) +  G_STRUCT_OFFSET (MonoArray, vector);    \
266                 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj));   \
267                 if (0)                                                  \
268                         g_print ("found %d at %p (0x%zx): %s.%s\n", mbwords, (obj), (vt)->desc, vt->klass->name_space, vt->klass->name); \
269                 while (e_start < e_end) {       \
270                         void **_objptr = (void**)e_start;       \
271                         gsize *bitmap_data = mbitmap_data;      \
272                         unsigned int bwords = mbwords;  \
273                         while (bwords-- > 0) {  \
274                                 gsize _bmap = *bitmap_data++;   \
275                                 void **start_run = _objptr;     \
276                                 /*g_print ("bitmap: 0x%x\n", _bmap);*/  \
277                                 while (_bmap) { \
278                                         if ((_bmap & 1)) {      \
279                                                 HANDLE_PTR (_objptr, (obj));    \
280                                         }       \
281                                         _bmap >>= 1;    \
282                                         ++_objptr;      \
283                                 }       \
284                                 _objptr = start_run + GC_BITS_PER_WORD; \
285                         }       \
286                         e_start += el_size;     \
287                 }       \
288         } while (0)
289
290 #define OBJ_VECTOR_FOREACH_PTR(desc,obj)        do {    \
291                 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */     \
292                 if ((desc) & 0xffffc000) {                              \
293                         int el_size = ((desc) >> 3) & MAX_ELEMENT_SIZE; \
294                         /* there are pointers */        \
295                         int etype = (desc) & 0xc000;                    \
296                         if (etype == (DESC_TYPE_V_REFS << 14)) {        \
297                                 void **p = (void**)((char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector));        \
298                                 void **end_refs = (void**)((char*)p + el_size * mono_array_length_fast ((MonoArray*)(obj)));    \
299                                 /* Note: this code can handle also arrays of struct with only references in them */     \
300                                 while (p < end_refs) {  \
301                                         HANDLE_PTR (p, (obj));  \
302                                         ++p;    \
303                                 }       \
304                         } else if (etype == DESC_TYPE_V_RUN_LEN << 14) {        \
305                                 int offset = ((desc) >> 16) & 0xff;     \
306                                 int num_refs = ((desc) >> 24) & 0xff;   \
307                                 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector);     \
308                                 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj));   \
309                                 while (e_start < e_end) {       \
310                                         void **p = (void**)e_start;     \
311                                         int i;  \
312                                         p += offset;    \
313                                         for (i = 0; i < num_refs; ++i) {        \
314                                                 HANDLE_PTR (p + i, (obj));      \
315                                         }       \
316                                         e_start += el_size;     \
317                                 }       \
318                         } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
319                                 char *e_start = (char*)(obj) +  G_STRUCT_OFFSET (MonoArray, vector);    \
320                                 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj));   \
321                                 while (e_start < e_end) {       \
322                                         void **p = (void**)e_start;     \
323                                         gsize _bmap = (desc) >> 16;     \
324                                         /* Note: there is no object header here to skip */      \
325                                         while (_bmap) { \
326                                                 if ((_bmap & 1)) {      \
327                                                         HANDLE_PTR (p, (obj));  \
328                                                 }       \
329                                                 _bmap >>= 1;    \
330                                                 ++p;    \
331                                         }       \
332                                         e_start += el_size;     \
333                                 }       \
334                         }       \
335                 }       \
336         } while (0)
337
338
339 #endif