2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
4 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #ifndef __MONO_SGENGC_H__
26 #define __MONO_SGENGC_H__
33 typedef struct _SgenThreadInfo SgenThreadInfo;
34 #define THREAD_INFO_TYPE SgenThreadInfo
41 #include <mono/utils/mono-compiler.h>
42 #include <mono/utils/mono-threads.h>
43 #include <mono/io-layer/mono-mutex.h>
44 #include <mono/metadata/class-internals.h>
45 #include <mono/metadata/object-internals.h>
46 #include <mono/metadata/sgen-archdep.h>
48 #include <mach/mach_port.h>
52 * Turning on heavy statistics will turn off the managed allocator and
53 * the managed write barrier.
55 //#define HEAVY_STATISTICS
58 * If this is set, the nursery is aligned to an address aligned to its size, ie.
59 * a 1MB nursery will be aligned to an address divisible by 1MB. This allows us to
60 * speed up ptr_in_nursery () checks which are very frequent. This requires the
61 * nursery size to be a compile time constant.
63 #define SGEN_ALIGN_NURSERY 1
65 //#define SGEN_BINARY_PROTOCOL
67 #define SGEN_MAX_DEBUG_LEVEL 2
69 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
71 /* The method used to clear the nursery */
72 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
73 * Clearing at TLAB creation is much faster, but more complex and it might expose hard
78 CLEAR_AT_TLAB_CREATION
81 NurseryClearPolicy mono_sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
84 #if SIZEOF_VOID_P == 4
85 typedef guint32 mword;
87 typedef guint64 mword;
90 #define SGEN_TV_DECLARE(name) gint64 name
91 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
92 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
93 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
95 /* for use with write barriers */
96 typedef struct _RememberedSet RememberedSet;
97 struct _RememberedSet {
101 mword data [MONO_ZERO_LEN_ARRAY];
104 /* eventually share with MonoThread? */
105 struct _SgenThreadInfo {
107 #if defined(__MACH__)
108 thread_port_t mach_port;
111 unsigned int stop_count; /* to catch duplicate signals */
114 volatile int in_critical_region;
115 gboolean doing_handshake;
116 gboolean thread_is_dying;
119 void *stack_start_limit;
120 char **tlab_next_addr;
121 char **tlab_start_addr;
122 char **tlab_temp_end_addr;
123 char **tlab_real_end_addr;
124 gpointer **store_remset_buffer_addr;
125 long *store_remset_buffer_index_addr;
126 RememberedSet *remset;
127 gpointer runtime_data;
128 gpointer stopped_ip; /* only valid if the thread is stopped */
129 MonoDomain *stopped_domain; /* ditto */
133 MonoContext ctx; /* ditto */
135 MonoContext *monoctx; /* ditto */
139 #if defined(__MACH__) || defined(HOST_WIN32)
140 gpointer regs[ARCH_NUM_REGS]; /* ditto */
142 gpointer *stopped_regs; /* ditto */
145 #ifndef HAVE_KW_THREAD
150 gpointer *store_remset_buffer;
151 long store_remset_buffer_index;
161 typedef struct _SgenBlock SgenBlock;
168 * The nursery section and the major copying collector's sections use
171 typedef struct _GCMemSection GCMemSection;
172 struct _GCMemSection {
176 /* pointer where more data could be allocated if it fits */
180 * scan starts is an array of pointers to objects equally spaced in the allocation area
181 * They let use quickly find pinned objects from pinning pointers.
184 /* in major collections indexes in the pin_queue for objects that pin this section */
185 void **pin_queue_start;
186 int pin_queue_num_entries;
187 unsigned short num_scan_start;
188 gboolean is_to_space;
191 #define SGEN_SIZEOF_GC_MEM_SECTION ((sizeof (GCMemSection) + 7) & ~7)
194 * to quickly find the head of an object pinned by a conservative
195 * address we keep track of the objects allocated for each
196 * SGEN_SCAN_START_SIZE memory chunk in the nursery or other memory
197 * sections. Larger values have less memory overhead and bigger
198 * runtime cost. 4-8 KB are reasonable values.
200 #define SGEN_SCAN_START_SIZE (4096*2)
203 * Objects bigger then this go into the large object space. This size
204 * has a few constraints. It must fit into the major heap, which in
205 * the case of the copying collector means that it must fit into a
206 * pinned chunk. It must also play well with the GC descriptors, some
207 * of which (DESC_TYPE_RUN_LENGTH, DESC_TYPE_SMALL_BITMAP) encode the
210 #define SGEN_MAX_SMALL_OBJ_SIZE 8000
213 * This is the maximum ammount of memory we're willing to waste in order to speed up allocation.
214 * Wastage comes in thre forms:
216 * -when building the nursery fragment list, small regions are discarded;
217 * -when allocating memory from a fragment if it ends up below the threshold, we remove it from the fragment list; and
218 * -when allocating a new tlab, we discard the remaining space of the old one
220 * Increasing this value speeds up allocation but will cause more frequent nursery collections as less space will be used.
221 * Descreasing this value will cause allocation to be slower since we'll have to cycle thru more fragments.
222 * 512 annedoctally keeps wastage under control and doesn't impact allocation performance too much.
224 #define SGEN_MAX_NURSERY_WASTE 512
227 /* This is also the MAJOR_SECTION_SIZE for the copying major
229 #define SGEN_PINNED_CHUNK_SIZE (128 * 1024)
231 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
233 typedef struct _SgenPinnedChunk SgenPinnedChunk;
236 * Recursion is not allowed for the thread lock.
238 #define LOCK_DECLARE(name) mono_mutex_t name
239 /* if changing LOCK_INIT to something that isn't idempotent, look at
240 its use in mono_gc_base_init in sgen-gc.c */
241 #define LOCK_INIT(name) mono_mutex_init (&(name), NULL)
242 #define LOCK_GC mono_mutex_lock (&gc_mutex)
243 #define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
244 #define UNLOCK_GC mono_mutex_unlock (&gc_mutex)
245 #define LOCK_INTERRUPTION mono_mutex_lock (&interruption_mutex)
246 #define UNLOCK_INTERRUPTION mono_mutex_unlock (&interruption_mutex)
248 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
249 #define SGEN_ATOMIC_ADD(x,i) do { \
253 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
257 /* we intercept pthread_create calls to know which threads exist */
258 #define USE_PTHREAD_INTERCEPT 1
261 #ifdef HEAVY_STATISTICS
262 #define HEAVY_STAT(x) x
264 extern long long stat_objects_alloced_degraded;
265 extern long long stat_bytes_alloced_degraded;
266 extern long long stat_copy_object_called_major;
267 extern long long stat_objects_copied_major;
269 #define HEAVY_STAT(x)
272 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) { a; fflush (gc_debug_file); } } while (0)
274 extern int gc_debug_level;
275 extern FILE* gc_debug_file;
277 extern int current_collection_generation;
279 extern unsigned int mono_sgen_global_stop_count;
281 #define SGEN_ALLOC_ALIGN 8
282 #define SGEN_ALLOC_ALIGN_BITS 3
284 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
286 #ifdef SGEN_ALIGN_NURSERY
287 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
289 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
292 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
293 * no cast from a pointer to an integer
300 /* these bits are set in the object vtable: we could merge them since an object can be
301 * either pinned or forwarded but not both.
302 * We store them in the vtable slot because the bits are used in the sync block for
303 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
304 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
305 * would be an invalid combination for the monitor and hash code).
306 * The values are already shifted.
307 * The forwarding address is stored in the sync block.
309 #define SGEN_FORWARDED_BIT 1
310 #define SGEN_PINNED_BIT 2
311 #define SGEN_VTABLE_BITS_MASK 0x3
313 /* returns NULL if not forwarded, or the forwarded address */
314 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
315 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
317 /* set the forwarded address fw_addr for object obj */
318 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
319 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
321 #define SGEN_PIN_OBJECT(obj) do { \
322 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
324 #define SGEN_UNPIN_OBJECT(obj) do { \
325 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
329 * Since we set bits in the vtable, use the macro to load it from the pointer to
330 * an object that is potentially pinned.
332 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
335 * ######################################################################
336 * ######## GC descriptors
337 * ######################################################################
338 * Used to quickly get the info the GC needs about an object: size and
339 * where the references are held.
341 #define OBJECT_HEADER_WORDS (sizeof(MonoObject)/sizeof(gpointer))
342 #define LOW_TYPE_BITS 3
343 #define SMALL_BITMAP_SHIFT 16
344 #define SMALL_BITMAP_SIZE (GC_BITS_PER_WORD - SMALL_BITMAP_SHIFT)
345 #define VECTOR_INFO_SHIFT 14
346 #define VECTOR_ELSIZE_SHIFT 3
347 #define LARGE_BITMAP_SIZE (GC_BITS_PER_WORD - LOW_TYPE_BITS)
348 #define MAX_ELEMENT_SIZE 0x3ff
349 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
350 #define VECTOR_SUBTYPE_REFS (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
351 #define VECTOR_SUBTYPE_RUN_LEN (DESC_TYPE_V_RUN_LEN << VECTOR_INFO_SHIFT)
352 #define VECTOR_SUBTYPE_BITMAP (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
354 /* objects are aligned to 8 bytes boundaries
355 * A descriptor is a pointer in MonoVTable, so 32 or 64 bits of size.
356 * The low 3 bits define the type of the descriptor. The other bits
357 * depend on the type.
358 * As a general rule the 13 remaining low bits define the size, either
359 * of the whole object or of the elements in the arrays. While for objects
360 * the size is already in bytes, for arrays we need to shift, because
361 * array elements might be smaller than 8 bytes. In case of arrays, we
362 * use two bits to describe what the additional high bits represents,
363 * so the default behaviour can handle element sizes less than 2048 bytes.
364 * The high 16 bits, if 0 it means the object is pointer-free.
365 * This design should make it easy and fast to skip over ptr-free data.
366 * The first 4 types should cover >95% of the objects.
367 * Note that since the size of objects is limited to 64K, larger objects
368 * will be allocated in the large object heap.
369 * If we want 4-bytes alignment, we need to put vector and small bitmap
374 * We don't use 0 so that 0 isn't a valid GC descriptor. No
375 * deep reason for this other than to be able to identify a
376 * non-inited descriptor for debugging.
378 * If an object contains no references, its GC descriptor is
379 * always DESC_TYPE_RUN_LENGTH, without a size, no exceptions.
380 * This is so that we can quickly check for that in
381 * copy_object_no_checks(), without having to fetch the
384 DESC_TYPE_RUN_LENGTH = 1, /* 15 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
385 DESC_TYPE_COMPLEX, /* index for bitmap into complex_descriptors */
386 DESC_TYPE_VECTOR, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
387 DESC_TYPE_ARRAY, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
388 DESC_TYPE_LARGE_BITMAP, /* | 29-61 bitmap bits */
389 DESC_TYPE_COMPLEX_ARR, /* index for bitmap into complex_descriptors */
390 /* subtypes for arrays and vectors */
391 DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value */
392 DESC_TYPE_V_REFS, /* all the array elements are refs */
393 DESC_TYPE_V_RUN_LEN, /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
394 DESC_TYPE_V_BITMAP /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
397 #define SGEN_VTABLE_HAS_REFERENCES(vt) (((MonoVTable*)(vt))->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
398 #define SGEN_CLASS_HAS_REFERENCES(c) ((c)->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
400 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
401 #define OBJ_RUN_LEN_SIZE(size,desc,obj) do { \
402 (size) = ((desc) & 0xfff8) >> 1; \
405 #define OBJ_BITMAP_SIZE(size,desc,obj) do { \
406 (size) = ((desc) & 0xfff8) >> 1; \
410 #define PREFETCH(addr) __builtin_prefetch ((addr))
412 #define PREFETCH(addr)
415 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
416 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj) do { \
417 if ((desc) & 0xffff0000) { \
418 /* there are pointers */ \
419 void **_objptr_end; \
420 void **_objptr = (void**)(obj); \
421 _objptr += ((desc) >> 16) & 0xff; \
422 _objptr_end = _objptr + (((desc) >> 24) & 0xff); \
423 while (_objptr < _objptr_end) { \
424 HANDLE_PTR (_objptr, (obj)); \
430 /* a bitmap desc means that there are pointer references or we'd have
431 * choosen run-length, instead: add an assert to check.
433 #define OBJ_LARGE_BITMAP_FOREACH_PTR(desc,obj) do { \
434 /* there are pointers */ \
435 void **_objptr = (void**)(obj); \
436 gsize _bmap = (desc) >> LOW_TYPE_BITS; \
437 _objptr += OBJECT_HEADER_WORDS; \
440 HANDLE_PTR (_objptr, (obj)); \
447 gsize* mono_sgen_get_complex_descriptor (mword desc) MONO_INTERNAL;
449 #define OBJ_COMPLEX_FOREACH_PTR(vt,obj) do { \
450 /* there are pointers */ \
451 void **_objptr = (void**)(obj); \
452 gsize *bitmap_data = mono_sgen_get_complex_descriptor ((desc)); \
453 int bwords = (*bitmap_data) - 1; \
454 void **start_run = _objptr; \
457 MonoObject *myobj = (MonoObject*)obj; \
458 g_print ("found %d at %p (0x%zx): %s.%s\n", bwords, (obj), (desc), myobj->vtable->klass->name_space, myobj->vtable->klass->name); \
460 while (bwords-- > 0) { \
461 gsize _bmap = *bitmap_data++; \
462 _objptr = start_run; \
463 /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/ \
466 HANDLE_PTR (_objptr, (obj)); \
471 start_run += GC_BITS_PER_WORD; \
475 /* this one is untested */
476 #define OBJ_COMPLEX_ARR_FOREACH_PTR(vt,obj) do { \
477 /* there are pointers */ \
478 gsize *mbitmap_data = mono_sgen_get_complex_descriptor ((vt)->desc); \
479 int mbwords = (*mbitmap_data++) - 1; \
480 int el_size = mono_array_element_size (vt->klass); \
481 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
482 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
484 g_print ("found %d at %p (0x%zx): %s.%s\n", mbwords, (obj), (vt)->desc, vt->klass->name_space, vt->klass->name); \
485 while (e_start < e_end) { \
486 void **_objptr = (void**)e_start; \
487 gsize *bitmap_data = mbitmap_data; \
488 unsigned int bwords = mbwords; \
489 while (bwords-- > 0) { \
490 gsize _bmap = *bitmap_data++; \
491 void **start_run = _objptr; \
492 /*g_print ("bitmap: 0x%x\n", _bmap);*/ \
495 HANDLE_PTR (_objptr, (obj)); \
500 _objptr = start_run + GC_BITS_PER_WORD; \
502 e_start += el_size; \
506 #define OBJ_VECTOR_FOREACH_PTR(desc,obj) do { \
507 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */ \
508 if ((desc) & 0xffffc000) { \
509 int el_size = ((desc) >> 3) & MAX_ELEMENT_SIZE; \
510 /* there are pointers */ \
511 int etype = (desc) & 0xc000; \
512 if (etype == (DESC_TYPE_V_REFS << 14)) { \
513 void **p = (void**)((char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector)); \
514 void **end_refs = (void**)((char*)p + el_size * mono_array_length_fast ((MonoArray*)(obj))); \
515 /* Note: this code can handle also arrays of struct with only references in them */ \
516 while (p < end_refs) { \
517 HANDLE_PTR (p, (obj)); \
520 } else if (etype == DESC_TYPE_V_RUN_LEN << 14) { \
521 int offset = ((desc) >> 16) & 0xff; \
522 int num_refs = ((desc) >> 24) & 0xff; \
523 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
524 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
525 while (e_start < e_end) { \
526 void **p = (void**)e_start; \
529 for (i = 0; i < num_refs; ++i) { \
530 HANDLE_PTR (p + i, (obj)); \
532 e_start += el_size; \
534 } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
535 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
536 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
537 while (e_start < e_end) { \
538 void **p = (void**)e_start; \
539 gsize _bmap = (desc) >> 16; \
540 /* Note: there is no object header here to skip */ \
543 HANDLE_PTR (p, (obj)); \
548 e_start += el_size; \
554 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - 3)
557 * This is a stack now instead of a queue, so the most recently added items are removed
558 * first, improving cache locality, and keeping the stack size manageable.
560 typedef struct _GrayQueueSection GrayQueueSection;
561 struct _GrayQueueSection {
563 GrayQueueSection *next;
564 char *objects [SGEN_GRAY_QUEUE_SECTION_SIZE];
567 typedef struct _SgenGrayQueue SgenGrayQueue;
569 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
571 struct _SgenGrayQueue {
572 GrayQueueSection *first;
573 GrayQueueSection *free_list;
575 GrayQueueAllocPrepareFunc alloc_prepare_func;
576 void *alloc_prepare_data;
579 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
580 typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
581 typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue*);
583 #if SGEN_MAX_DEBUG_LEVEL >= 9
584 #define GRAY_OBJECT_ENQUEUE mono_sgen_gray_object_enqueue
585 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = mono_sgen_gray_object_dequeue ((queue)))
587 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
588 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
589 mono_sgen_gray_object_enqueue ((queue), (o)); \
591 (queue)->first->objects [(queue)->first->end++] = (o); \
594 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
595 if (!(queue)->first) \
597 else if (G_UNLIKELY ((queue)->first->end == 1)) \
598 (o) = mono_sgen_gray_object_dequeue ((queue)); \
600 (o) = (queue)->first->objects [--(queue)->first->end]; \
604 void mono_sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj) MONO_INTERNAL;
605 char* mono_sgen_gray_object_dequeue (SgenGrayQueue *queue) MONO_INTERNAL;
607 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
609 void* mono_sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
610 void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
611 void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
613 int mono_sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
614 gboolean mono_sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
615 gboolean mono_sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
616 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
617 gboolean mono_sgen_park_current_thread_if_doing_handshake (SgenThreadInfo *p) MONO_INTERNAL;
618 void mono_sgen_os_init (void) MONO_INTERNAL;
620 void mono_sgen_fill_thread_info_for_suspend (SgenThreadInfo *info) MONO_INTERNAL;
622 gboolean mono_sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
624 void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
626 void mono_sgen_register_major_sections_alloced (int num_sections) MONO_INTERNAL;
627 mword mono_sgen_get_minor_collection_allowance (void) MONO_INTERNAL;
629 void mono_sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
630 void mono_sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
632 /* Keep in sync with mono_sgen_dump_internal_mem_usage() in dump_heap()! */
634 INTERNAL_MEM_PIN_QUEUE,
635 INTERNAL_MEM_FRAGMENT,
636 INTERNAL_MEM_SECTION,
637 INTERNAL_MEM_SCAN_STARTS,
638 INTERNAL_MEM_FIN_TABLE,
639 INTERNAL_MEM_FINALIZE_ENTRY,
640 INTERNAL_MEM_FINALIZE_READY_ENTRY,
641 INTERNAL_MEM_DISLINK_TABLE,
642 INTERNAL_MEM_DISLINK,
643 INTERNAL_MEM_ROOTS_TABLE,
644 INTERNAL_MEM_ROOT_RECORD,
645 INTERNAL_MEM_STATISTICS,
646 INTERNAL_MEM_STAT_PINNED_CLASS,
647 INTERNAL_MEM_STAT_REMSET_CLASS,
649 INTERNAL_MEM_GRAY_QUEUE,
650 INTERNAL_MEM_STORE_REMSET,
651 INTERNAL_MEM_MS_TABLES,
652 INTERNAL_MEM_MS_BLOCK_INFO,
653 INTERNAL_MEM_EPHEMERON_LINK,
654 INTERNAL_MEM_WORKER_DATA,
655 INTERNAL_MEM_BRIDGE_DATA,
656 INTERNAL_MEM_JOB_QUEUE_ENTRY,
657 INTERNAL_MEM_TOGGLEREF_DATA,
661 #define SGEN_PINNED_FREELIST_NUM_SLOTS 30
664 SgenPinnedChunk *chunk_list;
665 SgenPinnedChunk *free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
666 void *delayed_free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
667 } SgenPinnedAllocator;
675 void mono_sgen_init_internal_allocator (void) MONO_INTERNAL;
676 void mono_sgen_init_pinned_allocator (void) MONO_INTERNAL;
678 void mono_sgen_report_internal_mem_usage (void) MONO_INTERNAL;
679 void mono_sgen_report_pinned_mem_usage (SgenPinnedAllocator *alc) MONO_INTERNAL;
680 void mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
681 void mono_sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
682 void mono_sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
684 void mono_sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
686 void mono_sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
688 void* mono_sgen_alloc_internal (int type) MONO_INTERNAL;
689 void mono_sgen_free_internal (void *addr, int type) MONO_INTERNAL;
691 void* mono_sgen_alloc_internal_dynamic (size_t size, int type) MONO_INTERNAL;
692 void mono_sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
694 void* mono_sgen_alloc_pinned (SgenPinnedAllocator *allocator, size_t size) MONO_INTERNAL;
695 void mono_sgen_free_pinned (SgenPinnedAllocator *allocator, void *addr, size_t size) MONO_INTERNAL;
698 void mono_sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
700 gboolean mono_sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
702 void mono_sgen_pinned_scan_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
703 void mono_sgen_pinned_scan_pinned_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
705 void mono_sgen_pinned_update_heap_boundaries (SgenPinnedAllocator *alc) MONO_INTERNAL;
707 void** mono_sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
708 void mono_sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
709 void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
711 void mono_sgen_pin_stats_register_object (char *obj, size_t size);
712 void mono_sgen_pin_stats_register_global_remset (char *obj);
713 void mono_sgen_pin_stats_print_class_stats (void);
715 void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
717 int mono_sgen_get_current_collection_generation (void) MONO_INTERNAL;
718 gboolean mono_sgen_nursery_collection_is_parallel (void) MONO_INTERNAL;
719 CopyOrMarkObjectFunc mono_sgen_get_copy_object (void) MONO_INTERNAL;
720 ScanObjectFunc mono_sgen_get_minor_scan_object (void) MONO_INTERNAL;
721 ScanVTypeFunc mono_sgen_get_minor_scan_vtype (void) MONO_INTERNAL;
723 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
725 typedef struct _SgenMajorCollector SgenMajorCollector;
726 struct _SgenMajorCollector {
728 gboolean is_parallel;
729 gboolean supports_cardtable;
732 * This is set to TRUE if the sweep for the last major
733 * collection has been completed.
735 gboolean *have_swept;
737 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
738 gboolean (*is_object_live) (char *obj);
739 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
740 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
741 void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
742 void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
743 void (*nopar_minor_scan_object) (char *start, SgenGrayQueue *queue);
744 void (*minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
745 void (*nopar_minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
746 void (*major_scan_object) (char *start, SgenGrayQueue *queue);
747 void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
748 void (*nopar_copy_object) (void **obj_slot, SgenGrayQueue *queue);
749 void* (*alloc_object) (int size, gboolean has_references);
750 void (*free_pinned_object) (char *obj, size_t size);
751 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
752 void (*free_non_pinned_object) (char *obj, size_t size);
753 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
754 void (*pin_objects) (SgenGrayQueue *queue);
755 void (*scan_card_table) (SgenGrayQueue *queue);
756 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
757 void (*init_to_space) (void);
758 void (*sweep) (void);
759 void (*check_scan_starts) (void);
760 void (*dump_heap) (FILE *heap_dump_file);
761 gint64 (*get_used_size) (void);
762 void (*start_nursery_collection) (void);
763 void (*finish_nursery_collection) (void);
764 void (*start_major_collection) (void);
765 void (*finish_major_collection) (void);
766 void (*have_computed_minor_collection_allowance) (void);
767 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
768 gboolean (*obj_is_from_pinned_alloc) (char *obj);
769 void (*report_pinned_memory_usage) (void);
770 int (*get_num_major_sections) (void);
771 gboolean (*handle_gc_param) (const char *opt);
772 void (*print_gc_param_usage) (void);
773 gboolean (*is_worker_thread) (MonoNativeThreadId thread);
774 void (*post_param_init) (void);
775 void* (*alloc_worker_data) (void);
776 void (*init_worker_thread) (void *data);
777 void (*reset_worker_data) (void *data);
780 void mono_sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
781 void mono_sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
782 void mono_sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
783 void mono_sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
784 void mono_sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
787 * This function can be called on an object whose first word, the
788 * vtable field, is not intact. This is necessary for the parallel
792 mono_sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
794 MonoClass *klass = vtable->klass;
796 * We depend on mono_string_length_fast and
797 * mono_array_length_fast not using the object's vtable.
799 if (klass == mono_defaults.string_class) {
800 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
801 } else if (klass->rank) {
802 MonoArray *array = (MonoArray*)o;
803 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
804 if (G_UNLIKELY (array->bounds)) {
805 size += sizeof (mono_array_size_t) - 1;
806 size &= ~(sizeof (mono_array_size_t) - 1);
807 size += sizeof (MonoArrayBounds) * klass->rank;
811 /* from a created object: the class must be inited already */
812 return klass->instance_size;
817 mono_sgen_safe_object_get_size (MonoObject *obj)
821 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
822 obj = (MonoObject*)forwarded;
824 return mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
827 const char* mono_sgen_safe_name (void* obj) MONO_INTERNAL;
829 gboolean mono_sgen_object_is_live (void *obj) MONO_INTERNAL;
831 gboolean mono_sgen_need_bridge_processing (void) MONO_INTERNAL;
832 void mono_sgen_bridge_processing_start (int num_objs, MonoObject **objs) MONO_INTERNAL;
833 void mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs) MONO_INTERNAL;
834 void mono_sgen_register_test_bridge_callbacks (void) MONO_INTERNAL;
835 gboolean mono_sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
836 void mono_sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
838 void mono_sgen_scan_togglerefs (CopyOrMarkObjectFunc copy_func, char *start, char *end, SgenGrayQueue *queue) MONO_INTERNAL;
839 void mono_sgen_process_togglerefs (void) MONO_INTERNAL;
842 gboolean mono_sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
843 void mono_sgen_gc_lock (void) MONO_INTERNAL;
844 void mono_sgen_gc_unlock (void) MONO_INTERNAL;
851 gboolean mono_sgen_try_alloc_space (mword size, int space) MONO_INTERNAL;
852 void mono_sgen_release_space (mword size, int space) MONO_INTERNAL;
853 void mono_sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
854 void sgen_collect_major_no_lock (const char *reason) MONO_INTERNAL;
855 gboolean mono_sgen_need_major_collection (mword space_needed) MONO_INTERNAL;
856 void mono_sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
860 typedef struct _LOSObject LOSObject;
863 mword size; /* this is the object size */
865 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
866 char data [MONO_ZERO_LEN_ARRAY];
869 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
871 extern LOSObject *los_object_list;
872 extern mword los_memory_usage;
874 void mono_sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
875 void* mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
876 void mono_sgen_los_sweep (void) MONO_INTERNAL;
877 gboolean mono_sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
878 void mono_sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
879 void mono_sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
880 void mono_sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
881 FILE *mono_sgen_get_logfile (void) MONO_INTERNAL;
883 /* nursery allocator */
885 void mono_sgen_clear_nursery_fragments (void) MONO_INTERNAL;
886 void mono_sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
887 void mono_sgen_clear_current_nursery_fragment (void) MONO_INTERNAL;
888 void mono_sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
889 mword mono_sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries) MONO_INTERNAL;
890 void mono_sgen_init_nursery_allocator (void) MONO_INTERNAL;
891 void mono_sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
892 char* mono_sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
893 void* mono_sgen_nursery_alloc (size_t size) MONO_INTERNAL;
894 void* mono_sgen_nursery_alloc_range (size_t size, size_t min_size, int *out_alloc_size) MONO_INTERNAL;
895 MonoVTable* mono_sgen_get_array_fill_vtable (void) MONO_INTERNAL;
896 gboolean mono_sgen_can_alloc_size (size_t size) MONO_INTERNAL;
897 void mono_sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
901 typedef struct _SgenHashTableEntry SgenHashTableEntry;
902 struct _SgenHashTableEntry {
903 SgenHashTableEntry *next;
905 char data [MONO_ZERO_LEN_ARRAY]; /* data is pointer-aligned */
913 GEqualFunc equal_func;
914 SgenHashTableEntry **table;
919 #define SGEN_HASH_TABLE_INIT(table_type,entry_type,data_size,hash_func,equal_func) { (table_type), (entry_type), (data_size), (hash_func), (equal_func), NULL, 0, 0 }
920 #define SGEN_HASH_TABLE_ENTRY_SIZE(data_size) ((data_size) + sizeof (SgenHashTableEntry*) + sizeof (gpointer))
922 gpointer mono_sgen_hash_table_lookup (SgenHashTable *table, gpointer key) MONO_INTERNAL;
923 gboolean mono_sgen_hash_table_replace (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
924 gboolean mono_sgen_hash_table_set_value (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
925 gboolean mono_sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key) MONO_INTERNAL;
926 gboolean mono_sgen_hash_table_remove (SgenHashTable *table, gpointer key, gpointer data_return) MONO_INTERNAL;
928 void mono_sgen_hash_table_clean (SgenHashTable *table) MONO_INTERNAL;
930 #define mono_sgen_hash_table_num_entries(h) ((h)->num_entries)
932 #define SGEN_HASH_TABLE_FOREACH(h,k,v) do { \
933 SgenHashTable *__hash_table = (h); \
934 SgenHashTableEntry **__table = __hash_table->table; \
935 SgenHashTableEntry *__entry, *__prev; \
937 for (__i = 0; __i < (h)->size; ++__i) { \
939 for (__entry = __table [__i]; __entry; ) { \
940 (k) = __entry->key; \
941 (v) = (gpointer)__entry->data;
943 /* The loop must be continue'd after using this! */
944 #define SGEN_HASH_TABLE_FOREACH_REMOVE(free) do { \
945 SgenHashTableEntry *__next = __entry->next; \
947 __prev->next = __next; \
949 __table [__i] = __next; \
951 mono_sgen_free_internal (__entry, __hash_table->entry_mem_type); \
953 --__hash_table->num_entries; \
956 #define SGEN_HASH_TABLE_FOREACH_SET_KEY(k) ((__entry)->key = (k))
958 #define SGEN_HASH_TABLE_FOREACH_END \
960 __entry = __entry->next; \
965 #endif /* HAVE_SGEN_GC */
967 #endif /* __MONO_SGENGC_H__ */