Revert some changes which don't have proper dependencies.
[mono-project.git] / mono / sgen / sgen-descriptor.h
blobf10ebf6a5104a95122f357b887afc42c9b3a9003
1 /**
2 * \file
3 * GC descriptors describe object layout.
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
9 * Copyright (C) 2012 Xamarin Inc
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #ifndef __MONO_SGEN_DESCRIPTOR_H__
14 #define __MONO_SGEN_DESCRIPTOR_H__
16 #include <mono/sgen/sgen-conf.h>
19 * ######################################################################
20 * ######## GC descriptors
21 * ######################################################################
22 * Used to quickly get the info the GC needs about an object: size and
23 * where the references are held.
25 #define OBJECT_HEADER_WORDS (SGEN_CLIENT_OBJECT_HEADER_SIZE / sizeof(gpointer))
26 #define LOW_TYPE_BITS 3
27 #define DESC_TYPE_MASK ((1 << LOW_TYPE_BITS) - 1)
28 #define MAX_RUNLEN_OBJECT_SIZE 0xFFFF
29 #define VECTOR_INFO_SHIFT 14
30 #define VECTOR_KIND_SHIFT 13
31 #define VECTOR_ELSIZE_SHIFT 3
32 #define VECTOR_BITMAP_SHIFT 16
33 #define VECTOR_BITMAP_SIZE (GC_BITS_PER_WORD - VECTOR_BITMAP_SHIFT)
34 #define BITMAP_NUM_BITS (GC_BITS_PER_WORD - LOW_TYPE_BITS)
35 #define MAX_ELEMENT_SIZE 0x3ff
36 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
37 #define VECTOR_SUBTYPE_REFS (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
38 #define VECTOR_SUBTYPE_BITMAP (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
40 #define VECTOR_KIND_SZARRAY (DESC_TYPE_V_SZARRAY << VECTOR_KIND_SHIFT)
41 #define VECTOR_KIND_ARRAY (DESC_TYPE_V_ARRAY << VECTOR_KIND_SHIFT)
44 * Objects are aligned to 8 bytes boundaries.
46 * A descriptor is a pointer in GCVTable, so 32 or 64 bits of size.
47 * The low 3 bits define the type of the descriptor. The other bits
48 * depend on the type.
50 * It's important to be able to quickly identify two properties of classes from their
51 * descriptors: whether they are small enough to live in the regular major heap (size <=
52 * SGEN_MAX_SMALL_OBJ_SIZE), and whether they contain references.
54 * To that end we have three descriptor types that only apply to small classes: RUN_LENGTH,
55 * BITMAP, and SMALL_PTRFREE. We also have the type COMPLEX_PTRFREE, which applies to
56 * classes that are either not small or of unknown size (those being strings and arrays).
57 * The lowest two bits of the SMALL_PTRFREE and COMPLEX_PTRFREE tags are the same, so we can
58 * quickly check for references.
60 * As a general rule the 13 remaining low bits define the size, either
61 * of the whole object or of the elements in the arrays. While for objects
62 * the size is already in bytes, for arrays we need to shift, because
63 * array elements might be smaller than 8 bytes. In case of arrays, we
64 * use two bits to describe what the additional high bits represents,
65 * so the default behaviour can handle element sizes less than 2048 bytes.
66 * The high 16 bits, if 0 it means the object is pointer-free.
67 * This design should make it easy and fast to skip over ptr-free data.
68 * The first 4 types should cover >95% of the objects.
69 * Note that since the size of objects is limited to 64K, larger objects
70 * will be allocated in the large object heap.
71 * If we want 4-bytes alignment, we need to put vector and small bitmap
72 * inside complex.
74 * We don't use 0 so that 0 isn't a valid GC descriptor. No deep reason for this other than
75 * to be able to identify a non-inited descriptor for debugging.
77 enum {
78 /* Keep in sync with `descriptor_types` in sgen-debug.c! */
79 DESC_TYPE_RUN_LENGTH = 1, /* 16 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
80 DESC_TYPE_BITMAP = 2, /* | 29-61 bitmap bits */
81 DESC_TYPE_SMALL_PTRFREE = 3,
82 DESC_TYPE_MAX_SMALL_OBJ = 3,
83 DESC_TYPE_COMPLEX = 4, /* index for bitmap into complex_descriptors */
84 DESC_TYPE_VECTOR = 5, /* 10 bits element size | 1 bit kind | 2 bits desc | element desc */
85 DESC_TYPE_COMPLEX_ARR = 6, /* index for bitmap into complex_descriptors */
86 DESC_TYPE_COMPLEX_PTRFREE = 7, /* Nothing, used to encode large ptr objects and strings. */
87 DESC_TYPE_MAX = 7,
89 DESC_TYPE_PTRFREE_MASK = 3,
90 DESC_TYPE_PTRFREE_BITS = 3
93 /* values for array kind */
94 enum {
95 DESC_TYPE_V_SZARRAY = 0, /*vector with no bounds data */
96 DESC_TYPE_V_ARRAY = 1, /* array with bounds data */
99 /* subtypes for arrays and vectors */
100 enum {
101 DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value */
102 DESC_TYPE_V_REFS, /* all the array elements are refs */
103 DESC_TYPE_V_RUN_LEN, /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
104 DESC_TYPE_V_BITMAP /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
107 #define SGEN_DESC_STRING (DESC_TYPE_COMPLEX_PTRFREE | (1 << LOW_TYPE_BITS))
109 /* Root bitmap descriptors are simpler: the lower three bits describe the type
110 * and we either have 30/62 bitmap bits or nibble-based run-length,
111 * or a complex descriptor, or a user defined marker function.
113 enum {
114 ROOT_DESC_CONSERVATIVE, /* 0, so matches NULL value */
115 ROOT_DESC_BITMAP,
116 ROOT_DESC_RUN_LEN,
117 ROOT_DESC_COMPLEX,
118 ROOT_DESC_VECTOR,
119 ROOT_DESC_USER,
120 ROOT_DESC_TYPE_MASK = 0x7,
121 ROOT_DESC_TYPE_SHIFT = 3,
124 typedef void (*SgenUserMarkFunc) (GCObject **addr, void *gc_data);
125 typedef void (*SgenUserReportRootFunc) (void *addr, GCObject *obj, void *gc_data);
126 typedef void (*SgenUserRootMarkFunc) (void *addr, SgenUserMarkFunc mark_func, void *gc_data);
128 SgenDescriptor sgen_make_user_root_descriptor (SgenUserRootMarkFunc marker);
130 gsize* sgen_get_complex_descriptor (SgenDescriptor desc);
131 void* sgen_get_complex_descriptor_bitmap (SgenDescriptor desc);
132 SgenUserRootMarkFunc sgen_get_user_descriptor_func (SgenDescriptor desc);
134 void sgen_init_descriptors (void);
136 #ifdef HEAVY_STATISTICS
137 void sgen_descriptor_count_scanned_object (SgenDescriptor desc);
138 void sgen_descriptor_count_copied_object (SgenDescriptor desc);
139 #endif
141 static inline gboolean
142 sgen_gc_descr_has_references (SgenDescriptor desc)
144 /* This covers SMALL_PTRFREE and COMPLEX_PTRFREE */
145 if ((desc & DESC_TYPE_PTRFREE_MASK) == DESC_TYPE_PTRFREE_BITS)
146 return FALSE;
148 /*The array is ptr-free*/
149 if ((desc & 0xC007) == (DESC_TYPE_VECTOR | VECTOR_SUBTYPE_PTRFREE))
150 return FALSE;
152 return TRUE;
155 #define SGEN_VTABLE_HAS_REFERENCES(vt) (sgen_gc_descr_has_references (sgen_vtable_get_descriptor ((vt))))
156 #define SGEN_OBJECT_HAS_REFERENCES(o) (SGEN_VTABLE_HAS_REFERENCES (SGEN_LOAD_VTABLE ((o))))
158 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
159 #ifdef __GNUC__
160 #define PREFETCH_READ(addr) __builtin_prefetch ((addr), 0, 1)
161 #define PREFETCH_WRITE(addr) __builtin_prefetch ((addr), 1, 1)
162 #else
163 #define PREFETCH_READ(addr)
164 #define PREFETCH_WRITE(addr)
165 #endif
167 #if defined(__GNUC__) && SIZEOF_VOID_P==4
168 #define GNUC_BUILTIN_CTZ(bmap) __builtin_ctz(bmap)
169 #elif defined(__GNUC__) && SIZEOF_VOID_P==8
170 #define GNUC_BUILTIN_CTZ(bmap) __builtin_ctzl(bmap)
171 #endif
173 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
174 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj) do { \
175 if ((desc) & 0xffff0000) { \
176 /* there are pointers */ \
177 void **_objptr_end; \
178 void **_objptr = (void**)(obj); \
179 _objptr += ((desc) >> 16) & 0xff; \
180 _objptr_end = _objptr + (((desc) >> 24) & 0xff); \
181 while (_objptr < _objptr_end) { \
182 HANDLE_PTR ((GCObject**)_objptr, (obj)); \
183 _objptr++; \
184 }; \
186 } while (0)
188 /* a bitmap desc means that there are pointer references or we'd have
189 * choosen run-length, instead: add an assert to check.
191 #ifdef __GNUC__
192 #define OBJ_BITMAP_FOREACH_PTR(desc,obj) do { \
193 /* there are pointers */ \
194 void **_objptr = (void**)(obj); \
195 gsize _bmap = (desc) >> LOW_TYPE_BITS; \
196 _objptr += OBJECT_HEADER_WORDS; \
197 do { \
198 int _index = GNUC_BUILTIN_CTZ (_bmap); \
199 _objptr += _index; \
200 _bmap >>= (_index + 1); \
201 HANDLE_PTR ((GCObject**)_objptr, (obj)); \
202 ++_objptr; \
203 } while (_bmap); \
204 } while (0)
205 #else
206 #define OBJ_BITMAP_FOREACH_PTR(desc,obj) do { \
207 /* there are pointers */ \
208 void **_objptr = (void**)(obj); \
209 gsize _bmap = (desc) >> LOW_TYPE_BITS; \
210 _objptr += OBJECT_HEADER_WORDS; \
211 do { \
212 if ((_bmap & 1)) { \
213 HANDLE_PTR ((GCObject**)_objptr, (obj)); \
215 _bmap >>= 1; \
216 ++_objptr; \
217 } while (_bmap); \
218 } while (0)
219 #endif
221 #define OBJ_COMPLEX_FOREACH_PTR(desc,obj) do { \
222 /* there are pointers */ \
223 void **_objptr = (void**)(obj); \
224 gsize *bitmap_data = sgen_get_complex_descriptor ((desc)); \
225 gsize bwords = (*bitmap_data) - 1; \
226 void **start_run = _objptr; \
227 bitmap_data++; \
228 while (bwords-- > 0) { \
229 gsize _bmap = *bitmap_data++; \
230 _objptr = start_run; \
231 /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/ \
232 while (_bmap) { \
233 if ((_bmap & 1)) { \
234 HANDLE_PTR ((GCObject**)_objptr, (obj)); \
236 _bmap >>= 1; \
237 ++_objptr; \
239 start_run += GC_BITS_PER_WORD; \
241 } while (0)
243 /* this one is untested */
244 #define OBJ_COMPLEX_ARR_FOREACH_PTR(desc,obj) do { \
245 /* there are pointers */ \
246 GCVTable vt = SGEN_LOAD_VTABLE (obj); \
247 gsize *mbitmap_data = sgen_get_complex_descriptor ((desc)); \
248 gsize mbwords = (*mbitmap_data++) - 1; \
249 gsize el_size = sgen_client_array_element_size (vt); \
250 char *e_start = sgen_client_array_data_start ((GCObject*)(obj)); \
251 char *e_end = e_start + el_size * sgen_client_array_length ((GCObject*)(obj)); \
252 while (e_start < e_end) { \
253 void **_objptr = (void**)e_start; \
254 gsize *bitmap_data = mbitmap_data; \
255 gsize bwords = mbwords; \
256 while (bwords-- > 0) { \
257 gsize _bmap = *bitmap_data++; \
258 void **start_run = _objptr; \
259 /*g_print ("bitmap: 0x%x\n", _bmap);*/ \
260 while (_bmap) { \
261 if ((_bmap & 1)) { \
262 HANDLE_PTR ((GCObject**)_objptr, (obj)); \
264 _bmap >>= 1; \
265 ++_objptr; \
267 _objptr = start_run + GC_BITS_PER_WORD; \
269 e_start += el_size; \
271 } while (0)
273 #define OBJ_VECTOR_FOREACH_PTR(desc,obj) do { \
274 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */ \
275 if ((desc) & 0xffffc000) { \
276 int el_size = ((desc) >> 3) & MAX_ELEMENT_SIZE; \
277 /* there are pointers */ \
278 int etype = (desc) & 0xc000; \
279 if (etype == (DESC_TYPE_V_REFS << 14)) { \
280 void **p = (void**)sgen_client_array_data_start ((GCObject*)(obj)); \
281 void **end_refs = (void**)((char*)p + el_size * sgen_client_array_length ((GCObject*)(obj))); \
282 /* Note: this code can handle also arrays of struct with only references in them */ \
283 while (p < end_refs) { \
284 HANDLE_PTR ((GCObject**)p, (obj)); \
285 ++p; \
287 } else if (etype == DESC_TYPE_V_RUN_LEN << 14) { \
288 int offset = ((desc) >> 16) & 0xff; \
289 int num_refs = ((desc) >> 24) & 0xff; \
290 char *e_start = sgen_client_array_data_start ((GCObject*)(obj)); \
291 char *e_end = e_start + el_size * sgen_client_array_length ((GCObject*)(obj)); \
292 while (e_start < e_end) { \
293 void **p = (void**)e_start; \
294 int i; \
295 p += offset; \
296 for (i = 0; i < num_refs; ++i) { \
297 HANDLE_PTR ((GCObject**)p + i, (obj)); \
299 e_start += el_size; \
301 } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
302 char *e_start = sgen_client_array_data_start ((GCObject*)(obj)); \
303 char *e_end = e_start + el_size * sgen_client_array_length ((GCObject*)(obj)); \
304 while (e_start < e_end) { \
305 void **p = (void**)e_start; \
306 gsize _bmap = (desc) >> 16; \
307 /* Note: there is no object header here to skip */ \
308 while (_bmap) { \
309 if ((_bmap & 1)) { \
310 HANDLE_PTR ((GCObject**)p, (obj)); \
312 _bmap >>= 1; \
313 ++p; \
315 e_start += el_size; \
319 } while (0)
322 #endif