The bridge callback now is per-class and not per-object. Introduce versioning to...
[mono-project.git] / mono / metadata / sgen-gc.h
blob674a13143ec0944ef66a94e8c8ea650ab9a9a4a3
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
24 #ifndef __MONO_SGENGC_H__
25 #define __MONO_SGENGC_H__
27 /* pthread impl */
28 #include "config.h"
30 #ifdef HAVE_SGEN_GC
32 #include <glib.h>
33 #include <pthread.h>
34 #include <signal.h>
35 #include <mono/utils/mono-compiler.h>
36 #include <mono/metadata/class-internals.h>
37 #include <mono/metadata/object-internals.h>
38 #include <mono/metadata/sgen-archdep.h>
39 #if defined(__MACH__)
40 #include <mach/mach_port.h>
41 #endif
44 * Turning on heavy statistics will turn off the managed allocator and
45 * the managed write barrier.
47 //#define HEAVY_STATISTICS
50 * If this is set, the nursery is aligned to an address aligned to its size, ie.
51 * a 1MB nursery will be aligned to an address divisible by 1MB. This allows us to
52 * speed up ptr_in_nursery () checks which are very frequent. This requires the
53 * nursery size to be a compile time constant.
55 #define SGEN_ALIGN_NURSERY 1
57 //#define SGEN_BINARY_PROTOCOL
59 #define SGEN_MAX_DEBUG_LEVEL 2
61 #define THREAD_HASH_SIZE 11
63 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
65 #define ARCH_THREAD_TYPE pthread_t
66 #define ARCH_GET_THREAD pthread_self
67 #define ARCH_THREAD_EQUALS(a,b) pthread_equal (a, b)
69 #if SIZEOF_VOID_P == 4
70 typedef guint32 mword;
71 #else
72 typedef guint64 mword;
73 #endif
75 #define SGEN_TV_DECLARE(name) gint64 name
76 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
77 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
78 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
80 /* for use with write barriers */
81 typedef struct _RememberedSet RememberedSet;
82 struct _RememberedSet {
83 mword *store_next;
84 mword *end_set;
85 RememberedSet *next;
86 mword data [MONO_ZERO_LEN_ARRAY];
89 /* eventually share with MonoThread? */
90 typedef struct _SgenThreadInfo SgenThreadInfo;
92 struct _SgenThreadInfo {
93 SgenThreadInfo *next;
94 ARCH_THREAD_TYPE id;
95 #if defined(__MACH__)
96 thread_port_t mach_port;
97 #endif
99 #if defined(PLATFORM_ANDROID)
100 void *android_tid;
101 #endif
103 unsigned int stop_count; /* to catch duplicate signals */
104 int signal;
105 int skip;
106 volatile int in_critical_region;
107 gboolean gc_disabled;
108 void *stack_end;
109 void *stack_start;
110 void *stack_start_limit;
111 char **tlab_next_addr;
112 char **tlab_start_addr;
113 char **tlab_temp_end_addr;
114 char **tlab_real_end_addr;
115 gpointer **store_remset_buffer_addr;
116 long *store_remset_buffer_index_addr;
117 RememberedSet *remset;
118 gpointer runtime_data;
119 gpointer stopped_ip; /* only valid if the thread is stopped */
120 MonoDomain *stopped_domain; /* ditto */
122 #if defined(__MACH__)
123 gpointer regs[ARCH_NUM_REGS]; /* ditto */
124 #endif
126 gpointer *stopped_regs; /* ditto */
127 #ifndef HAVE_KW_THREAD
128 char *tlab_start;
129 char *tlab_next;
130 char *tlab_temp_end;
131 char *tlab_real_end;
132 gpointer *store_remset_buffer;
133 long store_remset_buffer_index;
134 #endif
137 enum {
138 MEMORY_ROLE_GEN0,
139 MEMORY_ROLE_GEN1,
140 MEMORY_ROLE_PINNED,
141 MEMORY_ROLE_INTERNAL
144 typedef struct _SgenBlock SgenBlock;
145 struct _SgenBlock {
146 void *next;
147 unsigned char role;
151 * The nursery section and the major copying collector's sections use
152 * this struct.
154 typedef struct _GCMemSection GCMemSection;
155 struct _GCMemSection {
156 SgenBlock block;
157 char *data;
158 mword size;
159 /* pointer where more data could be allocated if it fits */
160 char *next_data;
161 char *end_data;
163 * scan starts is an array of pointers to objects equally spaced in the allocation area
164 * They let use quickly find pinned objects from pinning pointers.
166 char **scan_starts;
167 /* in major collections indexes in the pin_queue for objects that pin this section */
168 void **pin_queue_start;
169 int pin_queue_num_entries;
170 unsigned short num_scan_start;
171 gboolean is_to_space;
174 #define SGEN_SIZEOF_GC_MEM_SECTION ((sizeof (GCMemSection) + 7) & ~7)
177 * to quickly find the head of an object pinned by a conservative
178 * address we keep track of the objects allocated for each
179 * SGEN_SCAN_START_SIZE memory chunk in the nursery or other memory
180 * sections. Larger values have less memory overhead and bigger
181 * runtime cost. 4-8 KB are reasonable values.
183 #define SGEN_SCAN_START_SIZE (4096*2)
186 * Objects bigger then this go into the large object space. This size
187 * has a few constraints. It must fit into the major heap, which in
188 * the case of the copying collector means that it must fit into a
189 * pinned chunk. It must also play well with the GC descriptors, some
190 * of which (DESC_TYPE_RUN_LENGTH, DESC_TYPE_SMALL_BITMAP) encode the
191 * object size.
193 #define SGEN_MAX_SMALL_OBJ_SIZE 8000
195 /* This is also the MAJOR_SECTION_SIZE for the copying major
196 collector */
197 #define SGEN_PINNED_CHUNK_SIZE (128 * 1024)
199 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
201 typedef struct _SgenPinnedChunk SgenPinnedChunk;
203 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
204 const static int suspend_signal_num = SIGXFSZ;
205 #else
206 const static int suspend_signal_num = SIGPWR;
207 #endif
208 const static int restart_signal_num = SIGXCPU;
211 * Recursion is not allowed for the thread lock.
213 #define LOCK_DECLARE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
214 /* if changing LOCK_INIT to something that isn't idempotent, look at
215 its use in mono_gc_base_init in sgen-gc.c */
216 #define LOCK_INIT(name)
217 #define LOCK_GC pthread_mutex_lock (&gc_mutex)
218 #define UNLOCK_GC pthread_mutex_unlock (&gc_mutex)
219 #define LOCK_INTERRUPTION pthread_mutex_lock (&interruption_mutex)
220 #define UNLOCK_INTERRUPTION pthread_mutex_unlock (&interruption_mutex)
222 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
223 #define SGEN_ATOMIC_ADD(x,i) do { \
224 int __old_x; \
225 do { \
226 __old_x = (x); \
227 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
228 } while (0)
230 /* non-pthread will need to provide their own version of start/stop */
231 #define USE_SIGNAL_BASED_START_STOP_WORLD 1
232 /* we intercept pthread_create calls to know which threads exist */
233 #define USE_PTHREAD_INTERCEPT 1
235 #ifdef HEAVY_STATISTICS
236 #define HEAVY_STAT(x) x
238 extern long long stat_objects_alloced_degraded;
239 extern long long stat_bytes_alloced_degraded;
240 extern long long stat_copy_object_called_major;
241 extern long long stat_objects_copied_major;
242 #else
243 #define HEAVY_STAT(x)
244 #endif
246 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) a;} while (0)
248 extern int gc_debug_level;
249 extern FILE* gc_debug_file;
251 extern int current_collection_generation;
253 extern gboolean conservative_stack_mark;
255 #define SGEN_ALLOC_ALIGN 8
256 #define SGEN_ALLOC_ALIGN_BITS 3
258 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
260 #ifdef SGEN_ALIGN_NURSERY
261 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
262 #else
263 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
264 #endif
266 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
267 * no cast from a pointer to an integer
269 typedef struct {
270 MonoClass *klass;
271 mword desc;
272 } GCVTable;
274 /* these bits are set in the object vtable: we could merge them since an object can be
275 * either pinned or forwarded but not both.
276 * We store them in the vtable slot because the bits are used in the sync block for
277 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
278 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
279 * would be an invalid combination for the monitor and hash code).
280 * The values are already shifted.
281 * The forwarding address is stored in the sync block.
283 #define SGEN_FORWARDED_BIT 1
284 #define SGEN_PINNED_BIT 2
285 #define SGEN_VTABLE_BITS_MASK 0x3
287 /* returns NULL if not forwarded, or the forwarded address */
288 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
289 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
291 /* set the forwarded address fw_addr for object obj */
292 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
293 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
294 } while (0)
295 #define SGEN_PIN_OBJECT(obj) do { \
296 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
297 } while (0)
298 #define SGEN_UNPIN_OBJECT(obj) do { \
299 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
300 } while (0)
303 * Since we set bits in the vtable, use the macro to load it from the pointer to
304 * an object that is potentially pinned.
306 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
309 * ######################################################################
310 * ######## GC descriptors
311 * ######################################################################
312 * Used to quickly get the info the GC needs about an object: size and
313 * where the references are held.
315 #define OBJECT_HEADER_WORDS (sizeof(MonoObject)/sizeof(gpointer))
316 #define LOW_TYPE_BITS 3
317 #define SMALL_BITMAP_SHIFT 16
318 #define SMALL_BITMAP_SIZE (GC_BITS_PER_WORD - SMALL_BITMAP_SHIFT)
319 #define VECTOR_INFO_SHIFT 14
320 #define VECTOR_ELSIZE_SHIFT 3
321 #define LARGE_BITMAP_SIZE (GC_BITS_PER_WORD - LOW_TYPE_BITS)
322 #define MAX_ELEMENT_SIZE 0x3ff
323 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
324 #define VECTOR_SUBTYPE_REFS (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
325 #define VECTOR_SUBTYPE_RUN_LEN (DESC_TYPE_V_RUN_LEN << VECTOR_INFO_SHIFT)
326 #define VECTOR_SUBTYPE_BITMAP (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
328 /* objects are aligned to 8 bytes boundaries
329 * A descriptor is a pointer in MonoVTable, so 32 or 64 bits of size.
330 * The low 3 bits define the type of the descriptor. The other bits
331 * depend on the type.
332 * As a general rule the 13 remaining low bits define the size, either
333 * of the whole object or of the elements in the arrays. While for objects
334 * the size is already in bytes, for arrays we need to shift, because
335 * array elements might be smaller than 8 bytes. In case of arrays, we
336 * use two bits to describe what the additional high bits represents,
337 * so the default behaviour can handle element sizes less than 2048 bytes.
338 * The high 16 bits, if 0 it means the object is pointer-free.
339 * This design should make it easy and fast to skip over ptr-free data.
340 * The first 4 types should cover >95% of the objects.
341 * Note that since the size of objects is limited to 64K, larger objects
342 * will be allocated in the large object heap.
343 * If we want 4-bytes alignment, we need to put vector and small bitmap
344 * inside complex.
346 enum {
348 * We don't use 0 so that 0 isn't a valid GC descriptor. No
349 * deep reason for this other than to be able to identify a
350 * non-inited descriptor for debugging.
352 * If an object contains no references, its GC descriptor is
353 * always DESC_TYPE_RUN_LENGTH, without a size, no exceptions.
354 * This is so that we can quickly check for that in
355 * copy_object_no_checks(), without having to fetch the
356 * object's class.
358 DESC_TYPE_RUN_LENGTH = 1, /* 15 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
359 DESC_TYPE_SMALL_BITMAP, /* 15 bits aligned byte size | 16-48 bit bitmap */
360 DESC_TYPE_COMPLEX, /* index for bitmap into complex_descriptors */
361 DESC_TYPE_VECTOR, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
362 DESC_TYPE_ARRAY, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
363 DESC_TYPE_LARGE_BITMAP, /* | 29-61 bitmap bits */
364 DESC_TYPE_COMPLEX_ARR, /* index for bitmap into complex_descriptors */
365 /* subtypes for arrays and vectors */
366 DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value */
367 DESC_TYPE_V_REFS, /* all the array elements are refs */
368 DESC_TYPE_V_RUN_LEN, /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
369 DESC_TYPE_V_BITMAP /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
372 #define SGEN_VTABLE_HAS_REFERENCES(vt) (((MonoVTable*)(vt))->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
373 #define SGEN_CLASS_HAS_REFERENCES(c) ((c)->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
375 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
376 #define OBJ_RUN_LEN_SIZE(size,desc,obj) do { \
377 (size) = ((desc) & 0xfff8) >> 1; \
378 } while (0)
380 #define OBJ_BITMAP_SIZE(size,desc,obj) do { \
381 (size) = ((desc) & 0xfff8) >> 1; \
382 } while (0)
384 //#define PREFETCH(addr) __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(addr)))
385 #define PREFETCH(addr)
387 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
388 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj) do { \
389 if ((desc) & 0xffff0000) { \
390 /* there are pointers */ \
391 void **_objptr_end; \
392 void **_objptr = (void**)(obj); \
393 _objptr += ((desc) >> 16) & 0xff; \
394 _objptr_end = _objptr + (((desc) >> 24) & 0xff); \
395 while (_objptr < _objptr_end) { \
396 HANDLE_PTR (_objptr, (obj)); \
397 _objptr++; \
400 } while (0)
402 /* a bitmap desc means that there are pointer references or we'd have
403 * choosen run-length, instead: add an assert to check.
405 #define OBJ_BITMAP_FOREACH_PTR(desc,obj) do { \
406 /* there are pointers */ \
407 void **_objptr = (void**)(obj); \
408 gsize _bmap = (desc) >> 16; \
409 _objptr += OBJECT_HEADER_WORDS; \
410 while (_bmap) { \
411 if ((_bmap & 1)) { \
412 HANDLE_PTR (_objptr, (obj)); \
414 _bmap >>= 1; \
415 ++_objptr; \
417 } while (0)
419 #define OBJ_LARGE_BITMAP_FOREACH_PTR(desc,obj) do { \
420 /* there are pointers */ \
421 void **_objptr = (void**)(obj); \
422 gsize _bmap = (desc) >> LOW_TYPE_BITS; \
423 _objptr += OBJECT_HEADER_WORDS; \
424 while (_bmap) { \
425 if ((_bmap & 1)) { \
426 HANDLE_PTR (_objptr, (obj)); \
428 _bmap >>= 1; \
429 ++_objptr; \
431 } while (0)
433 gsize* mono_sgen_get_complex_descriptor (mword desc) MONO_INTERNAL;
435 #define OBJ_COMPLEX_FOREACH_PTR(desc,obj) do { \
436 /* there are pointers */ \
437 void **_objptr = (void**)(obj); \
438 gsize *bitmap_data = mono_sgen_get_complex_descriptor (desc); \
439 int bwords = (*bitmap_data) - 1; \
440 void **start_run = _objptr; \
441 bitmap_data++; \
442 if (0) { \
443 MonoObject *myobj = (MonoObject*)obj; \
444 g_print ("found %d at %p (0x%zx): %s.%s\n", bwords, (obj), desc, myobj->vtable->klass->name_space, myobj->vtable->klass->name); \
446 while (bwords-- > 0) { \
447 gsize _bmap = *bitmap_data++; \
448 _objptr = start_run; \
449 /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/ \
450 while (_bmap) { \
451 if ((_bmap & 1)) { \
452 HANDLE_PTR (_objptr, (obj)); \
454 _bmap >>= 1; \
455 ++_objptr; \
457 start_run += GC_BITS_PER_WORD; \
459 } while (0)
461 /* this one is untested */
462 #define OBJ_COMPLEX_ARR_FOREACH_PTR(vt,obj) do { \
463 /* there are pointers */ \
464 gsize *mbitmap_data = mono_sgen_get_complex_descriptor ((vt)->desc); \
465 int mbwords = (*mbitmap_data++) - 1; \
466 int el_size = mono_array_element_size (vt->klass); \
467 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
468 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
469 if (0) \
470 g_print ("found %d at %p (0x%zx): %s.%s\n", mbwords, (obj), (vt)->desc, vt->klass->name_space, vt->klass->name); \
471 while (e_start < e_end) { \
472 void **_objptr = (void**)e_start; \
473 gsize *bitmap_data = mbitmap_data; \
474 unsigned int bwords = mbwords; \
475 while (bwords-- > 0) { \
476 gsize _bmap = *bitmap_data++; \
477 void **start_run = _objptr; \
478 /*g_print ("bitmap: 0x%x\n", _bmap);*/ \
479 while (_bmap) { \
480 if ((_bmap & 1)) { \
481 HANDLE_PTR (_objptr, (obj)); \
483 _bmap >>= 1; \
484 ++_objptr; \
486 _objptr = start_run + GC_BITS_PER_WORD; \
488 e_start += el_size; \
490 } while (0)
492 #define OBJ_VECTOR_FOREACH_PTR(vt,obj) do { \
493 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */ \
494 if ((vt)->desc & 0xffffc000) { \
495 int el_size = ((vt)->desc >> 3) & MAX_ELEMENT_SIZE; \
496 /* there are pointers */ \
497 int etype = (vt)->desc & 0xc000; \
498 if (etype == (DESC_TYPE_V_REFS << 14)) { \
499 void **p = (void**)((char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector)); \
500 void **end_refs = (void**)((char*)p + el_size * mono_array_length_fast ((MonoArray*)(obj))); \
501 /* Note: this code can handle also arrays of struct with only references in them */ \
502 while (p < end_refs) { \
503 HANDLE_PTR (p, (obj)); \
504 ++p; \
506 } else if (etype == DESC_TYPE_V_RUN_LEN << 14) { \
507 int offset = ((vt)->desc >> 16) & 0xff; \
508 int num_refs = ((vt)->desc >> 24) & 0xff; \
509 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
510 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
511 while (e_start < e_end) { \
512 void **p = (void**)e_start; \
513 int i; \
514 p += offset; \
515 for (i = 0; i < num_refs; ++i) { \
516 HANDLE_PTR (p + i, (obj)); \
518 e_start += el_size; \
520 } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
521 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
522 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
523 while (e_start < e_end) { \
524 void **p = (void**)e_start; \
525 gsize _bmap = (vt)->desc >> 16; \
526 /* Note: there is no object header here to skip */ \
527 while (_bmap) { \
528 if ((_bmap & 1)) { \
529 HANDLE_PTR (p, (obj)); \
531 _bmap >>= 1; \
532 ++p; \
534 e_start += el_size; \
538 } while (0)
540 typedef struct _SgenInternalAllocator SgenInternalAllocator;
542 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - 3)
545 * This is a stack now instead of a queue, so the most recently added items are removed
546 * first, improving cache locality, and keeping the stack size manageable.
548 typedef struct _GrayQueueSection GrayQueueSection;
549 struct _GrayQueueSection {
550 int end;
551 GrayQueueSection *next;
552 char *objects [SGEN_GRAY_QUEUE_SECTION_SIZE];
555 typedef struct _SgenGrayQueue SgenGrayQueue;
557 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
559 struct _SgenGrayQueue {
560 SgenInternalAllocator *allocator;
561 GrayQueueSection *first;
562 GrayQueueSection *free_list;
563 int balance;
564 GrayQueueAllocPrepareFunc alloc_prepare_func;
565 void *alloc_prepare_data;
568 #if SGEN_MAX_DEBUG_LEVEL >= 9
569 #define GRAY_OBJECT_ENQUEUE gray_object_enqueue
570 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = gray_object_dequeue ((queue)))
571 #else
572 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
573 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
574 mono_sgen_gray_object_enqueue ((queue), (o)); \
575 else \
576 (queue)->first->objects [(queue)->first->end++] = (o); \
577 } while (0)
578 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
579 if (!(queue)->first) \
580 (o) = NULL; \
581 else if (G_UNLIKELY ((queue)->first->end == 1)) \
582 (o) = mono_sgen_gray_object_dequeue ((queue)); \
583 else \
584 (o) = (queue)->first->objects [--(queue)->first->end]; \
585 } while (0)
586 #endif
588 void mono_sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj) MONO_INTERNAL;
589 char* mono_sgen_gray_object_dequeue (SgenGrayQueue *queue) MONO_INTERNAL;
591 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
593 void* mono_sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
594 void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
595 void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
597 int mono_sgen_thread_handshake (int signum) MONO_INTERNAL;
598 SgenThreadInfo* mono_sgen_thread_info_lookup (ARCH_THREAD_TYPE id) MONO_INTERNAL;
599 SgenThreadInfo** mono_sgen_get_thread_table (void) MONO_INTERNAL;
600 gboolean mono_sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
601 int mono_sgen_pthread_kill (SgenThreadInfo *info, int signum) MONO_INTERNAL;
604 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
606 gboolean mono_sgen_is_worker_thread (pthread_t thread) MONO_INTERNAL;
608 void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
610 void mono_sgen_register_major_sections_alloced (int num_sections) MONO_INTERNAL;
611 mword mono_sgen_get_minor_collection_allowance (void) MONO_INTERNAL;
613 void mono_sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
614 void mono_sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
616 /* Keep in sync with mono_sgen_dump_internal_mem_usage() in dump_heap()! */
617 enum {
618 INTERNAL_MEM_MANAGED,
619 INTERNAL_MEM_PIN_QUEUE,
620 INTERNAL_MEM_FRAGMENT,
621 INTERNAL_MEM_SECTION,
622 INTERNAL_MEM_SCAN_STARTS,
623 INTERNAL_MEM_FIN_TABLE,
624 INTERNAL_MEM_FINALIZE_ENTRY,
625 INTERNAL_MEM_DISLINK_TABLE,
626 INTERNAL_MEM_DISLINK,
627 INTERNAL_MEM_ROOTS_TABLE,
628 INTERNAL_MEM_ROOT_RECORD,
629 INTERNAL_MEM_STATISTICS,
630 INTERNAL_MEM_REMSET,
631 INTERNAL_MEM_GRAY_QUEUE,
632 INTERNAL_MEM_STORE_REMSET,
633 INTERNAL_MEM_MS_TABLES,
634 INTERNAL_MEM_MS_BLOCK_INFO,
635 INTERNAL_MEM_EPHEMERON_LINK,
636 INTERNAL_MEM_WORKER_DATA,
637 INTERNAL_MEM_BRIDGE_DATA,
638 INTERNAL_MEM_TOGGLEREF_DATA,
639 INTERNAL_MEM_MAX
642 #define SGEN_INTERNAL_FREELIST_NUM_SLOTS 30
644 struct _SgenInternalAllocator {
645 SgenPinnedChunk *chunk_list;
646 SgenPinnedChunk *free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
647 void *delayed_free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
648 long small_internal_mem_bytes [INTERNAL_MEM_MAX];
651 enum {
652 GENERATION_NURSERY,
653 GENERATION_OLD,
654 GENERATION_MAX
657 void mono_sgen_init_internal_allocator (void) MONO_INTERNAL;
659 SgenInternalAllocator* mono_sgen_get_unmanaged_allocator (void) MONO_INTERNAL;
661 const char* mono_sgen_internal_mem_type_name (int type) MONO_INTERNAL;
662 void mono_sgen_report_internal_mem_usage (void) MONO_INTERNAL;
663 void mono_sgen_report_internal_mem_usage_full (SgenInternalAllocator *alc) MONO_INTERNAL;
664 void mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
665 void mono_sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
666 void mono_sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
668 void mono_sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
670 void mono_sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
672 void* mono_sgen_alloc_internal (int type) MONO_INTERNAL;
673 void mono_sgen_free_internal (void *addr, int type) MONO_INTERNAL;
675 void* mono_sgen_alloc_internal_dynamic (size_t size, int type) MONO_INTERNAL;
676 void mono_sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
678 void* mono_sgen_alloc_internal_fixed (SgenInternalAllocator *allocator, int type) MONO_INTERNAL;
679 void mono_sgen_free_internal_fixed (SgenInternalAllocator *allocator, void *addr, int type) MONO_INTERNAL;
681 void* mono_sgen_alloc_internal_full (SgenInternalAllocator *allocator, size_t size, int type) MONO_INTERNAL;
682 void mono_sgen_free_internal_full (SgenInternalAllocator *allocator, void *addr, size_t size, int type) MONO_INTERNAL;
684 void mono_sgen_free_internal_delayed (void *addr, int type, SgenInternalAllocator *thread_allocator) MONO_INTERNAL;
686 void mono_sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
688 gboolean mono_sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
690 void mono_sgen_internal_scan_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
691 void mono_sgen_internal_scan_pinned_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
693 void mono_sgen_internal_update_heap_boundaries (SgenInternalAllocator *alc) MONO_INTERNAL;
695 void** mono_sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
696 void mono_sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
697 void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
699 void mono_sgen_pin_stats_register_object (char *obj, size_t size);
701 void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
703 int mono_sgen_get_current_collection_generation (void) MONO_INTERNAL;
705 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
707 typedef struct _SgenMajorCollector SgenMajorCollector;
708 struct _SgenMajorCollector {
709 size_t section_size;
710 gboolean is_parallel;
711 gboolean supports_cardtable;
714 * This is set to TRUE if the sweep for the last major
715 * collection has been completed.
717 gboolean *have_swept;
719 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
720 gboolean (*is_object_live) (char *obj);
721 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
722 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
723 void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
724 void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
725 char* (*minor_scan_vtype) (char *start, MonoClass *klass, char* from_start, char* from_end, SgenGrayQueue *queue);
726 void (*major_scan_object) (char *start, SgenGrayQueue *queue);
727 void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
728 void* (*alloc_object) (int size, gboolean has_references);
729 void (*free_pinned_object) (char *obj, size_t size);
730 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
731 void (*free_non_pinned_object) (char *obj, size_t size);
732 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
733 void (*pin_objects) (SgenGrayQueue *queue);
734 void (*scan_card_table) (SgenGrayQueue *queue);
735 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
736 void (*init_to_space) (void);
737 void (*sweep) (void);
738 void (*check_scan_starts) (void);
739 void (*dump_heap) (FILE *heap_dump_file);
740 gint64 (*get_used_size) (void);
741 void (*start_nursery_collection) (void);
742 void (*finish_nursery_collection) (void);
743 void (*start_major_collection) (void);
744 void (*finish_major_collection) (void);
745 void (*have_computed_minor_collection_allowance) (void);
746 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
747 gboolean (*obj_is_from_pinned_alloc) (char *obj);
748 void (*report_pinned_memory_usage) (void);
749 int (*get_num_major_sections) (void);
750 gboolean (*handle_gc_param) (const char *opt);
751 void (*print_gc_param_usage) (void);
752 gboolean (*is_worker_thread) (pthread_t thread);
755 void mono_sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
756 void mono_sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
757 void mono_sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
758 void mono_sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
759 void mono_sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
762 * This function can be called on an object whose first word, the
763 * vtable field, is not intact. This is necessary for the parallel
764 * collector.
766 static inline guint
767 mono_sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
769 MonoClass *klass = vtable->klass;
771 * We depend on mono_string_length_fast and
772 * mono_array_length_fast not using the object's vtable.
774 if (klass == mono_defaults.string_class) {
775 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
776 } else if (klass->rank) {
777 MonoArray *array = (MonoArray*)o;
778 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
779 if (G_UNLIKELY (array->bounds)) {
780 size += sizeof (mono_array_size_t) - 1;
781 size &= ~(sizeof (mono_array_size_t) - 1);
782 size += sizeof (MonoArrayBounds) * klass->rank;
784 return size;
785 } else {
786 /* from a created object: the class must be inited already */
787 return klass->instance_size;
791 static inline guint
792 mono_sgen_safe_object_get_size (MonoObject *obj)
794 char *forwarded;
796 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
797 obj = (MonoObject*)forwarded;
799 return mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
802 const char* mono_sgen_safe_name (void* obj) MONO_INTERNAL;
804 gboolean mono_sgen_object_is_live (void *obj) MONO_INTERNAL;
806 gboolean mono_sgen_need_bridge_processing (void) MONO_INTERNAL;
807 void mono_sgen_bridge_processing_register_objects (int num_objs, MonoObject **objs) MONO_INTERNAL;
808 void mono_sgen_bridge_reset_data (void) MONO_INTERNAL;
809 void mono_sgen_bridge_processing_stw_step (void) MONO_INTERNAL;
810 void mono_sgen_bridge_processing_finish (void) MONO_INTERNAL;
811 void mono_sgen_register_test_bridge_callbacks (void) MONO_INTERNAL;
812 gboolean mono_sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
813 gboolean mono_sgen_is_bridge_class (MonoClass *class) MONO_INTERNAL;
814 void mono_sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
816 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
817 void mono_sgen_scan_togglerefs (CopyOrMarkObjectFunc copy_func, char *start, char *end, SgenGrayQueue *queue) MONO_INTERNAL;
818 void mono_sgen_process_togglerefs (void) MONO_INTERNAL;
821 gboolean mono_sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
822 void mono_sgen_gc_lock (void) MONO_INTERNAL;
823 void mono_sgen_gc_unlock (void) MONO_INTERNAL;
825 enum {
826 SPACE_MAJOR,
827 SPACE_LOS
830 gboolean mono_sgen_try_alloc_space (mword size, int space) MONO_INTERNAL;
831 void mono_sgen_release_space (mword size, int space) MONO_INTERNAL;
832 void mono_sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
833 void sgen_collect_major_no_lock (const char *reason) MONO_INTERNAL;
834 gboolean mono_sgen_need_major_collection (mword space_needed) MONO_INTERNAL;
836 /* LOS */
838 typedef struct _LOSObject LOSObject;
839 struct _LOSObject {
840 LOSObject *next;
841 mword size; /* this is the object size */
842 guint16 huge_object;
843 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
844 char data [MONO_ZERO_LEN_ARRAY];
847 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
849 extern LOSObject *los_object_list;
850 extern mword los_memory_usage;
852 void mono_sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
853 void* mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
854 void mono_sgen_los_sweep (void) MONO_INTERNAL;
855 gboolean mono_sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
856 void mono_sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
857 void mono_sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
858 void mono_sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
859 FILE *mono_sgen_get_logfile (void) MONO_INTERNAL;
861 #endif /* HAVE_SGEN_GC */
863 #endif /* __MONO_SGENGC_H__ */