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>
53 #include <mach/mach_port.h>
56 /* The method used to clear the nursery */
57 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
58 * Clearing at TLAB creation is much faster, but more complex and it might expose hard
63 CLEAR_AT_TLAB_CREATION
66 NurseryClearPolicy sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
68 #define SGEN_TV_DECLARE(name) gint64 name
69 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
70 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
71 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
73 /* for use with write barriers */
74 typedef struct _RememberedSet RememberedSet;
75 struct _RememberedSet {
79 mword data [MONO_ZERO_LEN_ARRAY];
83 * We're never actually using the first element. It's always set to
84 * NULL to simplify the elimination of consecutive duplicate
87 #define STORE_REMSET_BUFFER_SIZE 1023
89 typedef struct _GenericStoreRememberedSet GenericStoreRememberedSet;
90 struct _GenericStoreRememberedSet {
91 GenericStoreRememberedSet *next;
92 /* We need one entry less because the first entry of store
93 remset buffers is always a dummy and we don't copy it. */
94 gpointer data [STORE_REMSET_BUFFER_SIZE - 1];
97 /* we have 4 possible values in the low 2 bits */
99 REMSET_LOCATION, /* just a pointer to the exact location */
100 REMSET_RANGE, /* range of pointer fields */
101 REMSET_OBJECT, /* mark all the object for scanning */
102 REMSET_VTYPE, /* a valuetype array described by a gc descriptor, a count and a size */
103 REMSET_TYPE_MASK = 0x3
106 /* eventually share with MonoThread? */
107 struct _SgenThreadInfo {
109 #if defined(__MACH__)
110 thread_port_t mach_port;
113 unsigned int stop_count; /* to catch duplicate signals */
116 volatile int in_critical_region;
118 gboolean doing_handshake;
119 gboolean thread_is_dying;
120 gboolean gc_disabled;
123 void *stack_start_limit;
124 char **tlab_next_addr;
125 char **tlab_start_addr;
126 char **tlab_temp_end_addr;
127 char **tlab_real_end_addr;
128 gpointer **store_remset_buffer_addr;
129 long *store_remset_buffer_index_addr;
130 RememberedSet *remset;
131 gpointer runtime_data;
132 gpointer stopped_ip; /* only valid if the thread is stopped */
133 MonoDomain *stopped_domain; /* ditto */
137 MonoContext ctx; /* ditto */
139 MonoContext *monoctx; /* ditto */
143 #if defined(__MACH__) || defined(HOST_WIN32)
144 gpointer regs[ARCH_NUM_REGS]; /* ditto */
146 gpointer *stopped_regs; /* ditto */
149 #ifndef HAVE_KW_THREAD
154 gpointer *store_remset_buffer;
155 long store_remset_buffer_index;
165 typedef struct _SgenBlock SgenBlock;
172 * The nursery section and the major copying collector's sections use
175 typedef struct _GCMemSection GCMemSection;
176 struct _GCMemSection {
180 /* pointer where more data could be allocated if it fits */
184 * scan starts is an array of pointers to objects equally spaced in the allocation area
185 * They let use quickly find pinned objects from pinning pointers.
188 /* in major collections indexes in the pin_queue for objects that pin this section */
189 void **pin_queue_start;
190 int pin_queue_num_entries;
191 unsigned short num_scan_start;
192 gboolean is_to_space;
195 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
197 typedef struct _SgenPinnedChunk SgenPinnedChunk;
200 * Recursion is not allowed for the thread lock.
202 #define LOCK_DECLARE(name) mono_mutex_t name
203 /* if changing LOCK_INIT to something that isn't idempotent, look at
204 its use in mono_gc_base_init in sgen-gc.c */
205 #define LOCK_INIT(name) mono_mutex_init (&(name), NULL)
206 #define LOCK_GC do { \
207 mono_mutex_lock (&gc_mutex); \
208 MONO_PROBE_GC_LOCKED (); \
210 #define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
211 #define UNLOCK_GC do { \
212 mono_mutex_unlock (&gc_mutex); \
213 MONO_PROBE_GC_UNLOCKED (); \
215 #define LOCK_INTERRUPTION mono_mutex_lock (&interruption_mutex)
216 #define UNLOCK_INTERRUPTION mono_mutex_unlock (&interruption_mutex)
218 /* FIXME: Use InterlockedAdd & InterlockedAdd64 to reduce the CAS cost. */
219 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
220 #define SGEN_ATOMIC_ADD(x,i) do { \
224 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
226 #define SGEN_ATOMIC_ADD_P(x,i) do { \
230 } while (InterlockedCompareExchangePointer ((void**)&(x), (void*)(__old_x + (i)), (void*)__old_x) != (void*)__old_x); \
235 /* we intercept pthread_create calls to know which threads exist */
236 #define USE_PTHREAD_INTERCEPT 1
239 #ifdef HEAVY_STATISTICS
240 #define HEAVY_STAT(x) x
242 extern long long stat_objects_alloced_degraded;
243 extern long long stat_bytes_alloced_degraded;
244 extern long long stat_copy_object_called_major;
245 extern long long stat_objects_copied_major;
247 #define HEAVY_STAT(x)
250 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) { a; fflush (gc_debug_file); } } while (0)
252 extern int gc_debug_level;
253 extern FILE* gc_debug_file;
255 extern int current_collection_generation;
257 extern unsigned int sgen_global_stop_count;
259 #define SGEN_ALLOC_ALIGN 8
260 #define SGEN_ALLOC_ALIGN_BITS 3
262 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
264 #ifdef SGEN_ALIGN_NURSERY
265 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
267 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
272 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
273 #define DEFAULT_NURSERY_SIZE (sgen_nursery_size)
274 extern int sgen_nursery_size MONO_INTERNAL;
275 #ifdef SGEN_ALIGN_NURSERY
276 /* The number of trailing 0 bits in DEFAULT_NURSERY_SIZE */
277 #define DEFAULT_NURSERY_BITS (sgen_nursery_bits)
278 extern int sgen_nursery_bits MONO_INTERNAL;
283 #define DEFAULT_NURSERY_SIZE (4*1024*1024)
284 #ifdef SGEN_ALIGN_NURSERY
285 #define DEFAULT_NURSERY_BITS 22
290 #ifndef SGEN_ALIGN_NURSERY
291 #define DEFAULT_NURSERY_BITS -1
294 extern char *sgen_nursery_start MONO_INTERNAL;
295 extern char *sgen_nursery_end MONO_INTERNAL;
297 static inline gboolean
298 sgen_ptr_in_nursery (void *p)
300 return SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, sgen_nursery_start, sgen_nursery_end);
304 sgen_get_nursery_start (void)
306 return sgen_nursery_start;
310 sgen_get_nursery_end (void)
312 return sgen_nursery_end;
315 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
316 * no cast from a pointer to an integer
323 /* these bits are set in the object vtable: we could merge them since an object can be
324 * either pinned or forwarded but not both.
325 * We store them in the vtable slot because the bits are used in the sync block for
326 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
327 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
328 * would be an invalid combination for the monitor and hash code).
329 * The values are already shifted.
330 * The forwarding address is stored in the sync block.
332 #define SGEN_FORWARDED_BIT 1
333 #define SGEN_PINNED_BIT 2
334 #define SGEN_VTABLE_BITS_MASK 0x3
336 /* returns NULL if not forwarded, or the forwarded address */
337 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
338 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
340 /* set the forwarded address fw_addr for object obj */
341 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
342 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
344 #define SGEN_PIN_OBJECT(obj) do { \
345 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
347 #define SGEN_UNPIN_OBJECT(obj) do { \
348 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
352 * Since we set bits in the vtable, use the macro to load it from the pointer to
353 * an object that is potentially pinned.
355 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
357 #if SGEN_MAX_DEBUG_LEVEL >= 9
358 #define GRAY_OBJECT_ENQUEUE sgen_gray_object_enqueue
359 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = sgen_gray_object_dequeue ((queue)))
361 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
362 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
363 sgen_gray_object_enqueue ((queue), (o)); \
365 (queue)->first->objects [(queue)->first->end++] = (o); \
368 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
369 if (!(queue)->first) \
371 else if (G_UNLIKELY ((queue)->first->end == 1)) \
372 (o) = sgen_gray_object_dequeue ((queue)); \
374 (o) = (queue)->first->objects [--(queue)->first->end]; \
379 List of what each bit on of the vtable gc bits means.
382 SGEN_GC_BIT_BRIDGE_OBJECT = 1,
385 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
387 int sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
388 gboolean sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
389 gboolean sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
390 void sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
391 gboolean sgen_park_current_thread_if_doing_handshake (SgenThreadInfo *p) MONO_INTERNAL;
392 void sgen_os_init (void) MONO_INTERNAL;
394 void sgen_fill_thread_info_for_suspend (SgenThreadInfo *info) MONO_INTERNAL;
396 gboolean sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
398 void sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
400 void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
401 void sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
403 /* Keep in sync with description_for_type() in sgen-internal.c! */
405 INTERNAL_MEM_PIN_QUEUE,
406 INTERNAL_MEM_FRAGMENT,
407 INTERNAL_MEM_SECTION,
408 INTERNAL_MEM_SCAN_STARTS,
409 INTERNAL_MEM_FIN_TABLE,
410 INTERNAL_MEM_FINALIZE_ENTRY,
411 INTERNAL_MEM_FINALIZE_READY_ENTRY,
412 INTERNAL_MEM_DISLINK_TABLE,
413 INTERNAL_MEM_DISLINK,
414 INTERNAL_MEM_ROOTS_TABLE,
415 INTERNAL_MEM_ROOT_RECORD,
416 INTERNAL_MEM_STATISTICS,
417 INTERNAL_MEM_STAT_PINNED_CLASS,
418 INTERNAL_MEM_STAT_REMSET_CLASS,
420 INTERNAL_MEM_GRAY_QUEUE,
421 INTERNAL_MEM_STORE_REMSET,
422 INTERNAL_MEM_MS_TABLES,
423 INTERNAL_MEM_MS_BLOCK_INFO,
424 INTERNAL_MEM_EPHEMERON_LINK,
425 INTERNAL_MEM_WORKER_DATA,
426 INTERNAL_MEM_BRIDGE_DATA,
427 INTERNAL_MEM_JOB_QUEUE_ENTRY,
428 INTERNAL_MEM_TOGGLEREF_DATA,
432 #define SGEN_PINNED_FREELIST_NUM_SLOTS 30
435 SgenPinnedChunk *chunk_list;
436 SgenPinnedChunk *free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
437 void *delayed_free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
438 } SgenPinnedAllocator;
446 void sgen_init_internal_allocator (void) MONO_INTERNAL;
447 void sgen_init_pinned_allocator (void) MONO_INTERNAL;
449 typedef struct _ObjectList ObjectList;
455 void sgen_report_internal_mem_usage (void) MONO_INTERNAL;
456 void sgen_report_pinned_mem_usage (SgenPinnedAllocator *alc) MONO_INTERNAL;
457 void sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
458 void sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
459 void sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
461 void sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
463 void sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
465 void* sgen_alloc_internal (int type) MONO_INTERNAL;
466 void sgen_free_internal (void *addr, int type) MONO_INTERNAL;
468 void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure) MONO_INTERNAL;
469 void sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
471 void* sgen_alloc_pinned (SgenPinnedAllocator *allocator, size_t size) MONO_INTERNAL;
472 void sgen_free_pinned (SgenPinnedAllocator *allocator, void *addr, size_t size) MONO_INTERNAL;
475 void sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
477 gboolean sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
479 void sgen_pinned_scan_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
480 void sgen_pinned_scan_pinned_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
482 void sgen_pinned_update_heap_boundaries (SgenPinnedAllocator *alc) MONO_INTERNAL;
484 void** sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
485 void sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
486 void sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
488 void sgen_pin_stats_register_object (char *obj, size_t size);
489 void sgen_pin_stats_register_global_remset (char *obj);
490 void sgen_pin_stats_print_class_stats (void);
492 void sgen_sort_addresses (void **array, int size) MONO_INTERNAL;
493 void sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
495 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
496 typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
497 typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue*);
499 int sgen_get_current_collection_generation (void) MONO_INTERNAL;
500 gboolean sgen_collection_is_parallel (void) MONO_INTERNAL;
503 CopyOrMarkObjectFunc copy_or_mark_object;
504 ScanObjectFunc scan_object;
505 ScanVTypeFunc scan_vtype;
506 /*FIXME add allocation function? */
507 } SgenObjectOperations;
509 SgenObjectOperations *sgen_get_current_object_ops (void) MONO_INTERNAL;
511 typedef struct _SgenFragment SgenFragment;
513 struct _SgenFragment {
515 char *fragment_start;
516 char *fragment_next; /* the current soft limit for allocation */
518 SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
522 SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
523 SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
524 } SgenFragmentAllocator;
526 void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end) MONO_INTERNAL;
527 void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator) MONO_INTERNAL;
528 void* sgen_fragment_allocator_serial_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
529 void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
530 void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
531 void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
532 SgenFragment* sgen_fragment_allocator_alloc (void) MONO_INTERNAL;
533 void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator) MONO_INTERNAL;
534 void sgen_clear_range (char *start, char *end) MONO_INTERNAL;
538 This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
539 and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
540 L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
542 Experimental results on how much space we waste with a 4Mb nursery:
544 Note that the wastage applies to the half nursery, or 2Mb:
546 Test 1 (compiling corlib):
551 #define SGEN_TO_SPACE_GRANULE_BITS 9
552 #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
554 extern char *sgen_space_bitmap MONO_INTERNAL;
555 extern int sgen_space_bitmap_size MONO_INTERNAL;
557 static inline gboolean
558 sgen_nursery_is_to_space (char *object)
560 int idx = (object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
564 DEBUG (4, g_assert (sgen_ptr_in_nursery (object)));
565 DEBUG (4, g_assert (byte < sgen_space_bitmap_size));
567 return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
570 static inline gboolean
571 sgen_nursery_is_from_space (char *object)
573 return !sgen_nursery_is_to_space (object);
576 static inline gboolean
577 sgen_nursery_is_object_alive (char *obj)
579 /* FIXME put this asserts under a non default level */
580 g_assert (sgen_ptr_in_nursery (obj));
582 if (sgen_nursery_is_to_space (obj))
585 if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
592 char* (*alloc_for_promotion) (char *obj, size_t objsize, gboolean has_references);
593 char* (*par_alloc_for_promotion) (char *obj, size_t objsize, gboolean has_references);
595 SgenObjectOperations serial_ops;
596 SgenObjectOperations parallel_ops;
598 void (*prepare_to_space) (char *to_space_bitmap, int space_bitmap_size);
599 void (*clear_fragments) (void);
600 SgenFragment* (*build_fragments_get_exclude_head) (void);
601 void (*build_fragments_release_exclude_head) (void);
602 void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
603 void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
605 gboolean (*handle_gc_param) (const char *opt); /* Optional */
606 void (*print_gc_param_usage) (void); /* Optional */
607 } SgenMinorCollector;
609 extern SgenMinorCollector sgen_minor_collector;
611 void sgen_simple_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
612 void sgen_split_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
614 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
615 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
617 typedef struct _SgenMajorCollector SgenMajorCollector;
618 struct _SgenMajorCollector {
620 gboolean is_parallel;
621 gboolean supports_cardtable;
624 * This is set to TRUE if the sweep for the last major
625 * collection has been completed.
627 gboolean *have_swept;
629 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
630 gboolean (*is_object_live) (char *obj);
631 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
632 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
634 SgenObjectOperations major_ops;
636 void* (*alloc_object) (int size, gboolean has_references);
637 void* (*par_alloc_object) (int size, gboolean has_references);
638 void (*free_pinned_object) (char *obj, size_t size);
639 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
640 void (*free_non_pinned_object) (char *obj, size_t size);
641 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
642 void (*pin_objects) (SgenGrayQueue *queue);
643 void (*pin_major_object) (char *obj, SgenGrayQueue *queue);
644 void (*scan_card_table) (SgenGrayQueue *queue);
645 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
646 void (*init_to_space) (void);
647 void (*sweep) (void);
648 void (*check_scan_starts) (void);
649 void (*dump_heap) (FILE *heap_dump_file);
650 gint64 (*get_used_size) (void);
651 void (*start_nursery_collection) (void);
652 void (*finish_nursery_collection) (void);
653 void (*start_major_collection) (void);
654 void (*finish_major_collection) (void);
655 void (*have_computed_minor_collection_allowance) (void);
656 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
657 gboolean (*obj_is_from_pinned_alloc) (char *obj);
658 void (*report_pinned_memory_usage) (void);
659 int (*get_num_major_sections) (void);
660 gboolean (*handle_gc_param) (const char *opt);
661 void (*print_gc_param_usage) (void);
662 gboolean (*is_worker_thread) (MonoNativeThreadId thread);
663 void (*post_param_init) (void);
664 void* (*alloc_worker_data) (void);
665 void (*init_worker_thread) (void *data);
666 void (*reset_worker_data) (void *data);
667 gboolean (*is_valid_object) (char *object);
668 gboolean (*describe_pointer) (char *pointer);
671 extern SgenMajorCollector major_collector;
673 void sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
674 void sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
675 void sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
676 void sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
677 void sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
678 SgenMajorCollector* sgen_get_major_collector (void) MONO_INTERNAL;
682 void (*wbarrier_set_field) (MonoObject *obj, gpointer field_ptr, MonoObject* value);
683 void (*wbarrier_set_arrayref) (MonoArray *arr, gpointer slot_ptr, MonoObject* value);
684 void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gpointer src_ptr, int count);
685 void (*wbarrier_value_copy) (gpointer dest, gpointer src, int count, MonoClass *klass);
686 void (*wbarrier_object_copy) (MonoObject* obj, MonoObject *src);
687 void (*wbarrier_generic_nostore) (gpointer ptr);
688 void (*record_pointer) (gpointer ptr);
690 void (*begin_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue); /* OPTIONAL */
691 void (*finish_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue);
693 void (*register_thread) (SgenThreadInfo *p); /* OPTIONAL */
694 void (*cleanup_thread) (SgenThreadInfo *p); /* OPTIONAL */
695 void (*fill_thread_info_for_suspend) (SgenThreadInfo *info); /* OPTIONAL */
696 void (*prepare_for_minor_collection) (void); /* OPTIONAL */
697 void (*prepare_for_major_collection) (void);
699 void (*finish_minor_collection) (void); /* OPTIONAL */
700 gboolean (*find_address) (char *addr);
703 SgenRemeberedSet *sgen_get_remset (void) MONO_INTERNAL;
705 static guint /*__attribute__((noinline)) not sure if this hint is a good idea*/
706 slow_object_get_size (MonoVTable *vtable, MonoObject* o)
708 MonoClass *klass = vtable->klass;
711 * We depend on mono_string_length_fast and
712 * mono_array_length_fast not using the object's vtable.
714 if (klass == mono_defaults.string_class) {
715 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
716 } else if (klass->rank) {
717 MonoArray *array = (MonoArray*)o;
718 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
719 if (G_UNLIKELY (array->bounds)) {
720 size += sizeof (mono_array_size_t) - 1;
721 size &= ~(sizeof (mono_array_size_t) - 1);
722 size += sizeof (MonoArrayBounds) * klass->rank;
726 /* from a created object: the class must be inited already */
727 return klass->instance_size;
732 * This function can be called on an object whose first word, the
733 * vtable field, is not intact. This is necessary for the parallel
737 sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
739 mword descr = (mword)vtable->gc_descr;
740 mword type = descr & 0x7;
742 if (type == DESC_TYPE_RUN_LENGTH || type == DESC_TYPE_SMALL_BITMAP) {
743 mword size = descr & 0xfff8;
744 if (size == 0) /* This is used to encode a string */
745 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
747 } else if (type == DESC_TYPE_VECTOR) {
748 int element_size = ((descr) >> VECTOR_ELSIZE_SHIFT) & MAX_ELEMENT_SIZE;
749 MonoArray *array = (MonoArray*)o;
750 size_t size = sizeof (MonoArray) + element_size * mono_array_length_fast (array);
752 if (descr & VECTOR_KIND_ARRAY) {
753 size += sizeof (mono_array_size_t) - 1;
754 size &= ~(sizeof (mono_array_size_t) - 1);
755 size += sizeof (MonoArrayBounds) * vtable->klass->rank;
760 return slow_object_get_size (vtable, o);
764 sgen_safe_object_get_size (MonoObject *obj)
768 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
769 obj = (MonoObject*)forwarded;
771 return sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
774 const char* sgen_safe_name (void* obj) MONO_INTERNAL;
776 gboolean sgen_object_is_live (void *obj) MONO_INTERNAL;
778 gboolean sgen_need_bridge_processing (void) MONO_INTERNAL;
779 void sgen_bridge_reset_data (void) MONO_INTERNAL;
780 void sgen_bridge_processing_stw_step (void) MONO_INTERNAL;
781 void sgen_bridge_processing_finish (void) MONO_INTERNAL;
782 void sgen_register_test_bridge_callbacks (const char *bridge_class_name) MONO_INTERNAL;
783 gboolean sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
784 gboolean sgen_is_bridge_class (MonoClass *class) MONO_INTERNAL;
785 void sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
786 void sgen_bridge_register_finalized_object (MonoObject *object) MONO_INTERNAL;
788 void sgen_scan_togglerefs (CopyOrMarkObjectFunc copy_func, char *start, char *end, SgenGrayQueue *queue) MONO_INTERNAL;
789 void sgen_process_togglerefs (void) MONO_INTERNAL;
792 gboolean sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
793 void sgen_gc_lock (void) MONO_INTERNAL;
794 void sgen_gc_unlock (void) MONO_INTERNAL;
802 void sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
803 void sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue) MONO_INTERNAL;
804 void sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
806 void sgen_ensure_free_space (size_t size) MONO_INTERNAL;
807 void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason) MONO_INTERNAL;
812 typedef struct _LOSObject LOSObject;
815 mword size; /* this is the object size */
817 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
818 char data [MONO_ZERO_LEN_ARRAY];
821 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
823 extern LOSObject *los_object_list;
824 extern mword los_memory_usage;
826 void sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
827 void* sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
828 void sgen_los_sweep (void) MONO_INTERNAL;
829 gboolean sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
830 void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
831 void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
832 void sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
833 void sgen_major_collector_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
834 FILE *sgen_get_logfile (void) MONO_INTERNAL;
835 gboolean sgen_los_is_valid_object (char *object) MONO_INTERNAL;
836 gboolean mono_sgen_los_describe_pointer (char *ptr) MONO_INTERNAL;
838 /* nursery allocator */
840 void sgen_clear_nursery_fragments (void) MONO_INTERNAL;
841 void sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
842 void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
843 mword sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries) MONO_INTERNAL;
844 void sgen_init_nursery_allocator (void) MONO_INTERNAL;
845 void sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
846 void sgen_alloc_init_heavy_stats (void) MONO_INTERNAL;
847 char* sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
848 void* sgen_nursery_alloc (size_t size) MONO_INTERNAL;
849 void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size) MONO_INTERNAL;
850 MonoVTable* sgen_get_array_fill_vtable (void) MONO_INTERNAL;
851 gboolean sgen_can_alloc_size (size_t size) MONO_INTERNAL;
852 void sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
854 void sgen_nursery_alloc_prepare_for_minor (void) MONO_INTERNAL;
855 void sgen_nursery_alloc_prepare_for_major (void) MONO_INTERNAL;
857 char* sgen_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
858 char* sgen_par_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
862 typedef struct _SgenHashTableEntry SgenHashTableEntry;
863 struct _SgenHashTableEntry {
864 SgenHashTableEntry *next;
866 char data [MONO_ZERO_LEN_ARRAY]; /* data is pointer-aligned */
874 GEqualFunc equal_func;
875 SgenHashTableEntry **table;
880 #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 }
881 #define SGEN_HASH_TABLE_ENTRY_SIZE(data_size) ((data_size) + sizeof (SgenHashTableEntry*) + sizeof (gpointer))
883 gpointer sgen_hash_table_lookup (SgenHashTable *table, gpointer key) MONO_INTERNAL;
884 gboolean sgen_hash_table_replace (SgenHashTable *table, gpointer key, gpointer new_value, gpointer old_value) MONO_INTERNAL;
885 gboolean sgen_hash_table_set_value (SgenHashTable *table, gpointer key, gpointer new_value, gpointer old_value) MONO_INTERNAL;
886 gboolean sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key) MONO_INTERNAL;
887 gboolean sgen_hash_table_remove (SgenHashTable *table, gpointer key, gpointer data_return) MONO_INTERNAL;
889 void sgen_hash_table_clean (SgenHashTable *table) MONO_INTERNAL;
891 #define sgen_hash_table_num_entries(h) ((h)->num_entries)
893 #define SGEN_HASH_TABLE_FOREACH(h,k,v) do { \
894 SgenHashTable *__hash_table = (h); \
895 SgenHashTableEntry **__table = __hash_table->table; \
897 for (__i = 0; __i < (h)->size; ++__i) { \
898 SgenHashTableEntry **__iter, **__next; \
899 for (__iter = &__table [__i]; *__iter; __iter = __next) { \
900 SgenHashTableEntry *__entry = *__iter; \
901 __next = &__entry->next; \
902 (k) = __entry->key; \
903 (v) = (gpointer)__entry->data;
905 /* The loop must be continue'd after using this! */
906 #define SGEN_HASH_TABLE_FOREACH_REMOVE(free) do { \
909 --__hash_table->num_entries; \
911 sgen_free_internal (__entry, __hash_table->entry_mem_type); \
914 #define SGEN_HASH_TABLE_FOREACH_SET_KEY(k) ((__entry)->key = (k))
916 #define SGEN_HASH_TABLE_FOREACH_END \
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 gboolean sgen_is_managed_allocator (MonoMethod *method);
1035 void sgen_check_consistency (void);
1036 void sgen_check_major_refs (void);
1037 void sgen_check_whole_heap (void);
1038 void sgen_check_whole_heap_stw (void) MONO_INTERNAL;
1040 /* Write barrier support */
1043 * This causes the compile to extend the liveness of 'v' till the call to dummy_use
1046 sgen_dummy_use (gpointer v) {
1047 #if defined(__GNUC__)
1048 __asm__ volatile ("" : "=r"(v) : "r"(v));
1049 #elif defined(_MSC_VER)
1055 #error "Implement sgen_dummy_use for your compiler"
1063 gboolean is_overflow;
1064 SGEN_TV_DECLARE (total_time);
1065 SGEN_TV_DECLARE (stw_time);
1066 SGEN_TV_DECLARE (bridge_time);
1069 #endif /* HAVE_SGEN_GC */
1071 #endif /* __MONO_SGENGC_H__ */