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/utils/dtrace.h>
44 #include <mono/io-layer/mono-mutex.h>
45 #include <mono/metadata/class-internals.h>
46 #include <mono/metadata/object-internals.h>
47 #include <mono/metadata/sgen-conf.h>
48 #include <mono/metadata/sgen-archdep.h>
49 #include <mono/metadata/sgen-descriptor.h>
50 #include <mono/metadata/sgen-gray.h>
51 #include <mono/metadata/sgen-hash-table.h>
53 /* The method used to clear the nursery */
54 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
55 * Clearing at TLAB creation is much faster, but more complex and it might expose hard
60 CLEAR_AT_TLAB_CREATION
63 NurseryClearPolicy sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
65 #define SGEN_TV_DECLARE(name) gint64 name
66 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
67 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
68 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
70 /* for use with write barriers */
71 typedef struct _RememberedSet RememberedSet;
72 struct _RememberedSet {
76 mword data [MONO_ZERO_LEN_ARRAY];
80 * We're never actually using the first element. It's always set to
81 * NULL to simplify the elimination of consecutive duplicate
84 #define STORE_REMSET_BUFFER_SIZE 1023
86 typedef struct _GenericStoreRememberedSet GenericStoreRememberedSet;
87 struct _GenericStoreRememberedSet {
88 GenericStoreRememberedSet *next;
89 /* We need one entry less because the first entry of store
90 remset buffers is always a dummy and we don't copy it. */
91 gpointer data [STORE_REMSET_BUFFER_SIZE - 1];
94 /* we have 4 possible values in the low 2 bits */
96 REMSET_LOCATION, /* just a pointer to the exact location */
97 REMSET_RANGE, /* range of pointer fields */
98 REMSET_OBJECT, /* mark all the object for scanning */
99 REMSET_VTYPE, /* a valuetype array described by a gc descriptor, a count and a size */
100 REMSET_TYPE_MASK = 0x3
103 /* eventually share with MonoThread? */
105 * This structure extends the MonoThreadInfo structure.
107 struct _SgenThreadInfo {
110 volatile int in_critical_region;
112 gboolean doing_handshake;
113 gboolean thread_is_dying;
114 gboolean gc_disabled;
117 void *stack_start_limit;
118 char **tlab_next_addr;
119 char **tlab_start_addr;
120 char **tlab_temp_end_addr;
121 char **tlab_real_end_addr;
122 gpointer **store_remset_buffer_addr;
123 long *store_remset_buffer_index_addr;
124 RememberedSet *remset;
125 gpointer runtime_data;
127 /* Only used on POSIX platforms */
130 unsigned int stop_count; /* to catch duplicate signals */
132 gpointer stopped_ip; /* only valid if the thread is stopped */
133 MonoDomain *stopped_domain; /* ditto */
136 MonoContext ctx; /* ditto */
138 gpointer regs[ARCH_NUM_REGS]; /* ditto */
141 #ifndef HAVE_KW_THREAD
146 gpointer *store_remset_buffer;
147 long store_remset_buffer_index;
157 typedef struct _SgenBlock SgenBlock;
164 * The nursery section and the major copying collector's sections use
167 typedef struct _GCMemSection GCMemSection;
168 struct _GCMemSection {
172 /* pointer where more data could be allocated if it fits */
176 * scan starts is an array of pointers to objects equally spaced in the allocation area
177 * They let use quickly find pinned objects from pinning pointers.
180 /* in major collections indexes in the pin_queue for objects that pin this section */
181 void **pin_queue_start;
182 int pin_queue_num_entries;
183 unsigned short num_scan_start;
184 gboolean is_to_space;
187 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
189 typedef struct _SgenPinnedChunk SgenPinnedChunk;
192 * Recursion is not allowed for the thread lock.
194 #define LOCK_DECLARE(name) mono_mutex_t name
195 /* if changing LOCK_INIT to something that isn't idempotent, look at
196 its use in mono_gc_base_init in sgen-gc.c */
197 #define LOCK_INIT(name) mono_mutex_init (&(name), NULL)
198 #define LOCK_GC do { \
199 mono_mutex_lock (&gc_mutex); \
202 #define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
203 #define UNLOCK_GC do { \
204 mono_mutex_unlock (&gc_mutex); \
205 MONO_GC_UNLOCKED (); \
208 extern LOCK_DECLARE (sgen_interruption_mutex);
210 #define LOCK_INTERRUPTION mono_mutex_lock (&sgen_interruption_mutex)
211 #define UNLOCK_INTERRUPTION mono_mutex_unlock (&sgen_interruption_mutex)
213 /* FIXME: Use InterlockedAdd & InterlockedAdd64 to reduce the CAS cost. */
214 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
215 #define SGEN_ATOMIC_ADD(x,i) do { \
219 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
221 #define SGEN_ATOMIC_ADD_P(x,i) do { \
225 } while (InterlockedCompareExchangePointer ((void**)&(x), (void*)(__old_x + (i)), (void*)__old_x) != (void*)__old_x); \
230 /* we intercept pthread_create calls to know which threads exist */
231 #define USE_PTHREAD_INTERCEPT 1
234 #ifdef HEAVY_STATISTICS
235 #define HEAVY_STAT(x) x
237 extern long long stat_objects_alloced_degraded;
238 extern long long stat_bytes_alloced_degraded;
239 extern long long stat_copy_object_called_major;
240 extern long long stat_objects_copied_major;
242 #define HEAVY_STAT(x)
245 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) { a; fflush (gc_debug_file); } } while (0)
247 extern int gc_debug_level;
248 extern FILE* gc_debug_file;
250 extern int current_collection_generation;
252 extern unsigned int sgen_global_stop_count;
254 extern gboolean bridge_processing_in_progress;
256 extern int num_ready_finalizers;
258 #define SGEN_ALLOC_ALIGN 8
259 #define SGEN_ALLOC_ALIGN_BITS 3
261 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
264 * The link pointer is hidden by negating each bit. We use the lowest
265 * bit of the link (before negation) to store whether it needs
266 * resurrection tracking.
268 #define HIDE_POINTER(p,t) ((gpointer)(~((gulong)(p)|((t)?1:0))))
269 #define REVEAL_POINTER(p) ((gpointer)((~(gulong)(p))&~3L))
271 #ifdef SGEN_ALIGN_NURSERY
272 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
274 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
279 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
280 #define DEFAULT_NURSERY_SIZE (sgen_nursery_size)
281 extern int sgen_nursery_size MONO_INTERNAL;
282 #ifdef SGEN_ALIGN_NURSERY
283 /* The number of trailing 0 bits in DEFAULT_NURSERY_SIZE */
284 #define DEFAULT_NURSERY_BITS (sgen_nursery_bits)
285 extern int sgen_nursery_bits MONO_INTERNAL;
290 #define DEFAULT_NURSERY_SIZE (4*1024*1024)
291 #ifdef SGEN_ALIGN_NURSERY
292 #define DEFAULT_NURSERY_BITS 22
297 #ifndef SGEN_ALIGN_NURSERY
298 #define DEFAULT_NURSERY_BITS -1
301 extern char *sgen_nursery_start MONO_INTERNAL;
302 extern char *sgen_nursery_end MONO_INTERNAL;
304 static inline gboolean
305 sgen_ptr_in_nursery (void *p)
307 return SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, sgen_nursery_start, sgen_nursery_end);
311 sgen_get_nursery_start (void)
313 return sgen_nursery_start;
317 sgen_get_nursery_end (void)
319 return sgen_nursery_end;
322 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
323 * no cast from a pointer to an integer
330 /* these bits are set in the object vtable: we could merge them since an object can be
331 * either pinned or forwarded but not both.
332 * We store them in the vtable slot because the bits are used in the sync block for
333 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
334 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
335 * would be an invalid combination for the monitor and hash code).
336 * The values are already shifted.
337 * The forwarding address is stored in the sync block.
339 #define SGEN_FORWARDED_BIT 1
340 #define SGEN_PINNED_BIT 2
341 #define SGEN_VTABLE_BITS_MASK 0x3
343 /* returns NULL if not forwarded, or the forwarded address */
344 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
345 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
347 /* set the forwarded address fw_addr for object obj */
348 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
349 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
351 #define SGEN_PIN_OBJECT(obj) do { \
352 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
354 #define SGEN_UNPIN_OBJECT(obj) do { \
355 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
359 * Since we set bits in the vtable, use the macro to load it from the pointer to
360 * an object that is potentially pinned.
362 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
364 #if SGEN_MAX_DEBUG_LEVEL >= 9
365 #define GRAY_OBJECT_ENQUEUE sgen_gray_object_enqueue
366 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = sgen_gray_object_dequeue ((queue)))
368 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
369 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
370 sgen_gray_object_enqueue ((queue), (o)); \
372 (queue)->first->objects [(queue)->first->end++] = (o); \
375 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
376 if (!(queue)->first) \
378 else if (G_UNLIKELY ((queue)->first->end == 1)) \
379 (o) = sgen_gray_object_dequeue ((queue)); \
381 (o) = (queue)->first->objects [--(queue)->first->end]; \
386 List of what each bit on of the vtable gc bits means.
389 SGEN_GC_BIT_BRIDGE_OBJECT = 1,
392 /* the runtime can register areas of memory as roots: we keep two lists of roots,
393 * a pinned root set for conservatively scanned roots and a normal one for
394 * precisely scanned roots (currently implemented as a single list).
396 typedef struct _RootRecord RootRecord;
403 ROOT_TYPE_NORMAL = 0, /* "normal" roots */
404 ROOT_TYPE_PINNED = 1, /* roots without a GC descriptor */
405 ROOT_TYPE_WBARRIER = 2, /* roots with a write barrier */
409 extern SgenHashTable roots_hash [ROOT_TYPE_NUM];
411 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
413 int sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
414 gboolean sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
415 gboolean sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
416 void sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
417 gboolean sgen_park_current_thread_if_doing_handshake (SgenThreadInfo *p) MONO_INTERNAL;
418 void sgen_os_init (void) MONO_INTERNAL;
420 void sgen_fill_thread_info_for_suspend (SgenThreadInfo *info) MONO_INTERNAL;
422 gboolean sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
424 void sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
426 void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
427 void sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
429 /* Keep in sync with description_for_type() in sgen-internal.c! */
431 INTERNAL_MEM_PIN_QUEUE,
432 INTERNAL_MEM_FRAGMENT,
433 INTERNAL_MEM_SECTION,
434 INTERNAL_MEM_SCAN_STARTS,
435 INTERNAL_MEM_FIN_TABLE,
436 INTERNAL_MEM_FINALIZE_ENTRY,
437 INTERNAL_MEM_FINALIZE_READY_ENTRY,
438 INTERNAL_MEM_DISLINK_TABLE,
439 INTERNAL_MEM_DISLINK,
440 INTERNAL_MEM_ROOTS_TABLE,
441 INTERNAL_MEM_ROOT_RECORD,
442 INTERNAL_MEM_STATISTICS,
443 INTERNAL_MEM_STAT_PINNED_CLASS,
444 INTERNAL_MEM_STAT_REMSET_CLASS,
446 INTERNAL_MEM_GRAY_QUEUE,
447 INTERNAL_MEM_STORE_REMSET,
448 INTERNAL_MEM_MS_TABLES,
449 INTERNAL_MEM_MS_BLOCK_INFO,
450 INTERNAL_MEM_EPHEMERON_LINK,
451 INTERNAL_MEM_WORKER_DATA,
452 INTERNAL_MEM_BRIDGE_DATA,
453 INTERNAL_MEM_BRIDGE_HASH_TABLE,
454 INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY,
455 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE,
456 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY,
457 INTERNAL_MEM_JOB_QUEUE_ENTRY,
458 INTERNAL_MEM_TOGGLEREF_DATA,
462 #define SGEN_PINNED_FREELIST_NUM_SLOTS 30
465 SgenPinnedChunk *chunk_list;
466 SgenPinnedChunk *free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
467 void *delayed_free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
468 } SgenPinnedAllocator;
476 void sgen_init_internal_allocator (void) MONO_INTERNAL;
477 void sgen_init_pinned_allocator (void) MONO_INTERNAL;
479 typedef struct _ObjectList ObjectList;
485 void sgen_report_internal_mem_usage (void) MONO_INTERNAL;
486 void sgen_report_pinned_mem_usage (SgenPinnedAllocator *alc) MONO_INTERNAL;
487 void sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
488 void sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
489 void sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
491 void sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
493 void sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
495 void* sgen_alloc_internal (int type) MONO_INTERNAL;
496 void sgen_free_internal (void *addr, int type) MONO_INTERNAL;
498 void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure) MONO_INTERNAL;
499 void sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
501 void* sgen_alloc_pinned (SgenPinnedAllocator *allocator, size_t size) MONO_INTERNAL;
502 void sgen_free_pinned (SgenPinnedAllocator *allocator, void *addr, size_t size) MONO_INTERNAL;
505 void sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
507 gboolean sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
509 void sgen_pinned_scan_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
510 void sgen_pinned_scan_pinned_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
512 void sgen_pinned_update_heap_boundaries (SgenPinnedAllocator *alc) MONO_INTERNAL;
514 void** sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
515 void sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
516 void sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
518 void sgen_pin_stats_register_object (char *obj, size_t size);
519 void sgen_pin_stats_register_global_remset (char *obj);
520 void sgen_pin_stats_print_class_stats (void);
522 void sgen_sort_addresses (void **array, int size) MONO_INTERNAL;
523 void sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
525 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
526 typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
527 typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue*);
529 int sgen_get_current_collection_generation (void) MONO_INTERNAL;
530 gboolean sgen_collection_is_parallel (void) MONO_INTERNAL;
533 CopyOrMarkObjectFunc copy_or_mark_object;
534 ScanObjectFunc scan_object;
535 ScanVTypeFunc scan_vtype;
536 /*FIXME add allocation function? */
537 } SgenObjectOperations;
539 SgenObjectOperations *sgen_get_current_object_ops (void) MONO_INTERNAL;
541 typedef struct _SgenFragment SgenFragment;
543 struct _SgenFragment {
545 char *fragment_start;
546 char *fragment_next; /* the current soft limit for allocation */
548 SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
552 SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
553 SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
554 } SgenFragmentAllocator;
556 void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end) MONO_INTERNAL;
557 void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator) MONO_INTERNAL;
558 void* sgen_fragment_allocator_serial_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
559 void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
560 void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
561 void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
562 SgenFragment* sgen_fragment_allocator_alloc (void) MONO_INTERNAL;
563 void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator) MONO_INTERNAL;
564 void sgen_clear_range (char *start, char *end) MONO_INTERNAL;
568 This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
569 and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
570 L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
572 Experimental results on how much space we waste with a 4Mb nursery:
574 Note that the wastage applies to the half nursery, or 2Mb:
576 Test 1 (compiling corlib):
581 #define SGEN_TO_SPACE_GRANULE_BITS 9
582 #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
584 extern char *sgen_space_bitmap MONO_INTERNAL;
585 extern int sgen_space_bitmap_size MONO_INTERNAL;
587 static inline gboolean
588 sgen_nursery_is_to_space (char *object)
590 int idx = (object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
594 DEBUG (4, g_assert (sgen_ptr_in_nursery (object)));
595 DEBUG (4, g_assert (byte < sgen_space_bitmap_size));
597 return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
600 static inline gboolean
601 sgen_nursery_is_from_space (char *object)
603 return !sgen_nursery_is_to_space (object);
606 static inline gboolean
607 sgen_nursery_is_object_alive (char *obj)
609 /* FIXME put this asserts under a non default level */
610 g_assert (sgen_ptr_in_nursery (obj));
612 if (sgen_nursery_is_to_space (obj))
615 if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
622 char* (*alloc_for_promotion) (char *obj, size_t objsize, gboolean has_references);
623 char* (*par_alloc_for_promotion) (char *obj, size_t objsize, gboolean has_references);
625 SgenObjectOperations serial_ops;
626 SgenObjectOperations parallel_ops;
628 void (*prepare_to_space) (char *to_space_bitmap, int space_bitmap_size);
629 void (*clear_fragments) (void);
630 SgenFragment* (*build_fragments_get_exclude_head) (void);
631 void (*build_fragments_release_exclude_head) (void);
632 void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
633 void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
635 gboolean (*handle_gc_param) (const char *opt); /* Optional */
636 void (*print_gc_param_usage) (void); /* Optional */
637 } SgenMinorCollector;
639 extern SgenMinorCollector sgen_minor_collector;
641 void sgen_simple_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
642 void sgen_split_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
644 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
645 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
647 typedef struct _SgenMajorCollector SgenMajorCollector;
648 struct _SgenMajorCollector {
650 gboolean is_parallel;
651 gboolean supports_cardtable;
654 * This is set to TRUE if the sweep for the last major
655 * collection has been completed.
657 gboolean *have_swept;
659 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
660 gboolean (*is_object_live) (char *obj);
661 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
662 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
664 SgenObjectOperations major_ops;
666 void* (*alloc_object) (int size, gboolean has_references);
667 void* (*par_alloc_object) (int size, gboolean has_references);
668 void (*free_pinned_object) (char *obj, size_t size);
669 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
670 void (*free_non_pinned_object) (char *obj, size_t size);
671 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
672 void (*pin_objects) (SgenGrayQueue *queue);
673 void (*pin_major_object) (char *obj, SgenGrayQueue *queue);
674 void (*scan_card_table) (SgenGrayQueue *queue);
675 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
676 void (*init_to_space) (void);
677 void (*sweep) (void);
678 void (*check_scan_starts) (void);
679 void (*dump_heap) (FILE *heap_dump_file);
680 gint64 (*get_used_size) (void);
681 void (*start_nursery_collection) (void);
682 void (*finish_nursery_collection) (void);
683 void (*start_major_collection) (void);
684 void (*finish_major_collection) (void);
685 void (*have_computed_minor_collection_allowance) (void);
686 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
687 gboolean (*obj_is_from_pinned_alloc) (char *obj);
688 void (*report_pinned_memory_usage) (void);
689 int (*get_num_major_sections) (void);
690 gboolean (*handle_gc_param) (const char *opt);
691 void (*print_gc_param_usage) (void);
692 gboolean (*is_worker_thread) (MonoNativeThreadId thread);
693 void (*post_param_init) (void);
694 void* (*alloc_worker_data) (void);
695 void (*init_worker_thread) (void *data);
696 void (*reset_worker_data) (void *data);
697 gboolean (*is_valid_object) (char *object);
698 gboolean (*describe_pointer) (char *pointer);
701 extern SgenMajorCollector major_collector;
703 void sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
704 void sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
705 void sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
706 void sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
707 void sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
708 SgenMajorCollector* sgen_get_major_collector (void) MONO_INTERNAL;
712 void (*wbarrier_set_field) (MonoObject *obj, gpointer field_ptr, MonoObject* value);
713 void (*wbarrier_set_arrayref) (MonoArray *arr, gpointer slot_ptr, MonoObject* value);
714 void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gpointer src_ptr, int count);
715 void (*wbarrier_value_copy) (gpointer dest, gpointer src, int count, MonoClass *klass);
716 void (*wbarrier_object_copy) (MonoObject* obj, MonoObject *src);
717 void (*wbarrier_generic_nostore) (gpointer ptr);
718 void (*record_pointer) (gpointer ptr);
720 void (*begin_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue); /* OPTIONAL */
721 void (*finish_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue);
723 void (*register_thread) (SgenThreadInfo *p); /* OPTIONAL */
724 void (*cleanup_thread) (SgenThreadInfo *p); /* OPTIONAL */
725 void (*fill_thread_info_for_suspend) (SgenThreadInfo *info); /* OPTIONAL */
726 void (*prepare_for_minor_collection) (void); /* OPTIONAL */
727 void (*prepare_for_major_collection) (void);
729 void (*finish_minor_collection) (void); /* OPTIONAL */
730 gboolean (*find_address) (char *addr);
733 SgenRemeberedSet *sgen_get_remset (void) MONO_INTERNAL;
735 static guint /*__attribute__((noinline)) not sure if this hint is a good idea*/
736 slow_object_get_size (MonoVTable *vtable, MonoObject* o)
738 MonoClass *klass = vtable->klass;
741 * We depend on mono_string_length_fast and
742 * mono_array_length_fast not using the object's vtable.
744 if (klass == mono_defaults.string_class) {
745 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
746 } else if (klass->rank) {
747 MonoArray *array = (MonoArray*)o;
748 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
749 if (G_UNLIKELY (array->bounds)) {
750 size += sizeof (mono_array_size_t) - 1;
751 size &= ~(sizeof (mono_array_size_t) - 1);
752 size += sizeof (MonoArrayBounds) * klass->rank;
756 /* from a created object: the class must be inited already */
757 return klass->instance_size;
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
767 sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
769 mword descr = (mword)vtable->gc_descr;
770 mword type = descr & 0x7;
772 if (type == DESC_TYPE_RUN_LENGTH || type == DESC_TYPE_SMALL_BITMAP) {
773 mword size = descr & 0xfff8;
774 if (size == 0) /* This is used to encode a string */
775 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
777 } else if (type == DESC_TYPE_VECTOR) {
778 int element_size = ((descr) >> VECTOR_ELSIZE_SHIFT) & MAX_ELEMENT_SIZE;
779 MonoArray *array = (MonoArray*)o;
780 size_t size = sizeof (MonoArray) + element_size * mono_array_length_fast (array);
782 if (descr & VECTOR_KIND_ARRAY) {
783 size += sizeof (mono_array_size_t) - 1;
784 size &= ~(sizeof (mono_array_size_t) - 1);
785 size += sizeof (MonoArrayBounds) * vtable->klass->rank;
790 return slow_object_get_size (vtable, o);
794 sgen_safe_object_get_size (MonoObject *obj)
798 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
799 obj = (MonoObject*)forwarded;
801 return sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
804 const char* sgen_safe_name (void* obj) MONO_INTERNAL;
806 gboolean sgen_object_is_live (void *obj) MONO_INTERNAL;
808 gboolean sgen_need_bridge_processing (void) MONO_INTERNAL;
809 void sgen_bridge_reset_data (void) MONO_INTERNAL;
810 void sgen_bridge_processing_stw_step (void) MONO_INTERNAL;
811 void sgen_bridge_processing_finish (int generation) MONO_INTERNAL;
812 void sgen_register_test_bridge_callbacks (const char *bridge_class_name) MONO_INTERNAL;
813 gboolean sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
814 gboolean sgen_is_bridge_class (MonoClass *class) MONO_INTERNAL;
815 void sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
816 void sgen_bridge_register_finalized_object (MonoObject *object) MONO_INTERNAL;
818 void sgen_scan_togglerefs (CopyOrMarkObjectFunc copy_func, char *start, char *end, SgenGrayQueue *queue) MONO_INTERNAL;
819 void sgen_process_togglerefs (void) MONO_INTERNAL;
821 typedef mono_bool (*WeakLinkAlivePredicateFunc) (MonoObject*, void*);
823 void sgen_null_links_with_predicate (int generation, WeakLinkAlivePredicateFunc predicate, void *data) MONO_INTERNAL;
825 gboolean sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
826 void sgen_gc_lock (void) MONO_INTERNAL;
827 void sgen_gc_unlock (void) MONO_INTERNAL;
828 void sgen_gc_event_moves (void) MONO_INTERNAL;
830 void sgen_queue_finalization_entry (MonoObject *obj) MONO_INTERNAL;
831 const char* sgen_generation_name (int generation) MONO_INTERNAL;
833 void sgen_collect_bridge_objects (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, SgenGrayQueue *queue) MONO_INTERNAL;
834 void sgen_finalize_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, SgenGrayQueue *queue) MONO_INTERNAL;
835 void sgen_null_link_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, gboolean before_finalization, SgenGrayQueue *queue) MONO_INTERNAL;
836 void sgen_null_links_for_domain (MonoDomain *domain, int generation) MONO_INTERNAL;
837 void sgen_remove_finalizers_for_domain (MonoDomain *domain, int generation) MONO_INTERNAL;
838 void sgen_process_fin_stage_entries (void) MONO_INTERNAL;
839 void sgen_process_dislink_stage_entries (void) MONO_INTERNAL;
840 void sgen_register_disappearing_link (MonoObject *obj, void **link, gboolean track, gboolean in_gc) MONO_INTERNAL;
848 void sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
849 void sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue) MONO_INTERNAL;
850 void sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
852 void sgen_ensure_free_space (size_t size) MONO_INTERNAL;
853 void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason) MONO_INTERNAL;
854 gboolean sgen_has_critical_method (void) MONO_INTERNAL;
855 gboolean sgen_is_critical_method (MonoMethod *method) MONO_INTERNAL;
862 gboolean is_overflow;
863 SGEN_TV_DECLARE (total_time);
864 SGEN_TV_DECLARE (stw_time);
865 SGEN_TV_DECLARE (bridge_time);
868 int sgen_stop_world (int generation) MONO_INTERNAL;
869 int sgen_restart_world (int generation, GGTimingInfo *timing) MONO_INTERNAL;
873 typedef struct _LOSObject LOSObject;
876 mword size; /* this is the object size */
878 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
879 char data [MONO_ZERO_LEN_ARRAY];
882 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
884 extern LOSObject *los_object_list;
885 extern mword los_memory_usage;
887 void sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
888 void* sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
889 void sgen_los_sweep (void) MONO_INTERNAL;
890 gboolean sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
891 void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
892 void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
893 void sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
894 void sgen_major_collector_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
895 FILE *sgen_get_logfile (void) MONO_INTERNAL;
896 gboolean sgen_los_is_valid_object (char *object) MONO_INTERNAL;
897 gboolean mono_sgen_los_describe_pointer (char *ptr) MONO_INTERNAL;
899 /* nursery allocator */
901 void sgen_clear_nursery_fragments (void) MONO_INTERNAL;
902 void sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
903 void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
904 mword sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries) MONO_INTERNAL;
905 void sgen_init_nursery_allocator (void) MONO_INTERNAL;
906 void sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
907 void sgen_alloc_init_heavy_stats (void) MONO_INTERNAL;
908 char* sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
909 void* sgen_nursery_alloc (size_t size) MONO_INTERNAL;
910 void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size) MONO_INTERNAL;
911 MonoVTable* sgen_get_array_fill_vtable (void) MONO_INTERNAL;
912 gboolean sgen_can_alloc_size (size_t size) MONO_INTERNAL;
913 void sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
915 void sgen_nursery_alloc_prepare_for_minor (void) MONO_INTERNAL;
916 void sgen_nursery_alloc_prepare_for_major (void) MONO_INTERNAL;
918 char* sgen_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
919 char* sgen_par_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
923 extern MonoNativeTlsKey thread_info_key;
925 #ifdef HAVE_KW_THREAD
926 extern __thread SgenThreadInfo *thread_info;
927 extern __thread gpointer *store_remset_buffer;
928 extern __thread long store_remset_buffer_index;
929 extern __thread char *stack_end;
930 extern __thread long *store_remset_buffer_index_addr;
933 #ifdef HAVE_KW_THREAD
934 #define TLAB_ACCESS_INIT
935 #define REMEMBERED_SET remembered_set
936 #define STORE_REMSET_BUFFER store_remset_buffer
937 #define STORE_REMSET_BUFFER_INDEX store_remset_buffer_index
938 #define IN_CRITICAL_REGION thread_info->in_critical_region
940 #define TLAB_ACCESS_INIT SgenThreadInfo *__thread_info__ = mono_native_tls_get_value (thread_info_key)
941 #define REMEMBERED_SET (__thread_info__->remset)
942 #define STORE_REMSET_BUFFER (__thread_info__->store_remset_buffer)
943 #define STORE_REMSET_BUFFER_INDEX (__thread_info__->store_remset_buffer_index)
944 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
947 #ifndef DISABLE_CRITICAL_REGION
949 #ifdef HAVE_KW_THREAD
950 #define IN_CRITICAL_REGION thread_info->in_critical_region
952 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
955 /* Enter must be visible before anything is done in the critical region. */
956 #define ENTER_CRITICAL_REGION do { mono_atomic_store_acquire (&IN_CRITICAL_REGION, 1); } while (0)
958 /* Exit must make sure all critical regions stores are visible before it signal the end of the region.
959 * We don't need to emit a full barrier since we
961 #define EXIT_CRITICAL_REGION do { mono_atomic_store_release (&IN_CRITICAL_REGION, 0); } while (0)
965 #ifdef HAVE_KW_THREAD
966 #define EMIT_TLS_ACCESS(mb,dummy,offset) do { \
967 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX); \
968 mono_mb_emit_byte ((mb), CEE_MONO_TLS); \
969 mono_mb_emit_i4 ((mb), (offset)); \
974 * CEE_MONO_TLS requires the tls offset, not the key, so the code below only works on darwin,
975 * where the two are the same.
977 #if defined(__APPLE__) || defined (HOST_WIN32)
978 #define EMIT_TLS_ACCESS(mb,member,dummy) do { \
979 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX); \
980 mono_mb_emit_byte ((mb), CEE_MONO_TLS); \
981 mono_mb_emit_i4 ((mb), thread_info_key); \
982 mono_mb_emit_icon ((mb), G_STRUCT_OFFSET (SgenThreadInfo, member)); \
983 mono_mb_emit_byte ((mb), CEE_ADD); \
984 mono_mb_emit_byte ((mb), CEE_LDIND_I); \
987 #define EMIT_TLS_ACCESS(mb,member,dummy) do { g_error ("sgen is not supported when using --with-tls=pthread.\n"); } while (0)
994 extern GCMemSection *nursery_section;
995 extern int stat_major_gcs;
996 extern guint32 collect_before_allocs;
997 extern guint32 verify_before_allocs;
998 extern gboolean has_per_allocation_action;
999 extern int degraded_mode;
1000 extern int default_nursery_size;
1001 extern guint32 tlab_size;
1002 extern NurseryClearPolicy nursery_clear_policy;
1004 extern LOCK_DECLARE (gc_mutex);
1006 extern int do_pin_stats;
1008 /* Nursery helpers. */
1011 sgen_set_nursery_scan_start (char *p)
1013 int idx = (p - (char*)nursery_section->data) / SGEN_SCAN_START_SIZE;
1014 char *old = nursery_section->scan_starts [idx];
1015 if (!old || old > p)
1016 nursery_section->scan_starts [idx] = p;
1020 /* Object Allocation */
1027 } SgenAllocatorType;
1029 void sgen_init_tlab_info (SgenThreadInfo* info);
1030 void sgen_clear_tlabs (void);
1031 void sgen_set_use_managed_allocator (gboolean flag);
1032 gboolean sgen_is_managed_allocator (MonoMethod *method);
1033 gboolean sgen_has_managed_allocator (void);
1037 void sgen_check_consistency (void);
1038 void sgen_check_major_refs (void);
1039 void sgen_check_whole_heap (void);
1040 void sgen_check_whole_heap_stw (void) MONO_INTERNAL;
1041 void sgen_check_objref (char *obj);
1043 /* Write barrier support */
1046 * This causes the compile to extend the liveness of 'v' till the call to dummy_use
1049 sgen_dummy_use (gpointer v) {
1050 #if defined(__GNUC__)
1051 __asm__ volatile ("" : "=r"(v) : "r"(v));
1052 #elif defined(_MSC_VER)
1058 #error "Implement sgen_dummy_use for your compiler"
1062 #endif /* HAVE_SGEN_GC */
1064 #endif /* __MONO_SGENGC_H__ */