[sgen] Add stats for allocated gchandles (#17074)
[mono-project.git] / mono / sgen / sgen-gc.h
blob313c28e953579d7e6258359ddd024c80bcfe0f0a
1 /**
2 * \file
3 * Simple generational GC.
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
8 * Copyright (C) 2012 Xamarin Inc
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #ifndef __MONO_SGENGC_H__
13 #define __MONO_SGENGC_H__
15 /* pthread impl */
16 #include "config.h"
18 #ifdef HAVE_SGEN_GC
20 #include <mono/utils/mono-forward-internal.h>
21 #undef THREAD_INFO_TYPE
22 #define THREAD_INFO_TYPE SgenThreadInfo
24 #include <glib.h>
25 #include <stdio.h>
26 #ifdef HAVE_PTHREAD_H
27 #include <pthread.h>
28 #endif
29 #include <stdint.h>
30 #include "mono/utils/mono-compiler.h"
31 #include "mono/utils/atomic.h"
32 #include "mono/utils/mono-os-mutex.h"
33 #include "mono/utils/mono-coop-mutex.h"
34 #include "mono/utils/ward.h"
35 #include "mono/sgen/sgen-conf.h"
36 #include "mono/sgen/sgen-hash-table.h"
37 #include "mono/sgen/sgen-protocol.h"
38 #include "mono/sgen/gc-internal-agnostic.h"
39 #include "mono/sgen/sgen-thread-pool.h"
41 /* The method used to clear the nursery */
42 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
43 * Clearing at TLAB creation is much faster, but more complex and it might expose hard
44 * to find bugs.
46 typedef enum {
47 CLEAR_AT_GC,
48 CLEAR_AT_TLAB_CREATION,
49 CLEAR_AT_TLAB_CREATION_DEBUG
50 } NurseryClearPolicy;
52 NurseryClearPolicy sgen_get_nursery_clear_policy (void);
54 #if !defined(__MACH__) && !MONO_MACH_ARCH_SUPPORTED && defined(HAVE_PTHREAD_KILL)
55 #define SGEN_POSIX_STW 1
56 #endif
59 * The nursery section uses this struct.
61 typedef struct _GCMemSection GCMemSection;
62 struct _GCMemSection {
63 char *data;
64 char *end_data;
66 * scan starts is an array of pointers to objects equally spaced in the allocation area
67 * They let use quickly find pinned objects from pinning pointers.
69 char **scan_starts;
70 /* in major collections indexes in the pin_queue for objects that pin this section */
71 size_t pin_queue_first_entry;
72 size_t pin_queue_last_entry;
73 size_t num_scan_start;
77 * Recursion is not allowed for the thread lock.
79 #define LOCK_DECLARE(name) mono_mutex_t name
80 /* if changing LOCK_INIT to something that isn't idempotent, look at
81 its use in mono_gc_base_init in sgen-gc.c */
82 #define LOCK_INIT(name) mono_os_mutex_init (&(name))
83 #define LOCK_GC do { sgen_gc_lock (); } while (0)
84 #define UNLOCK_GC do { sgen_gc_unlock (); } while (0)
86 extern MonoCoopMutex sgen_interruption_mutex;
88 #define LOCK_INTERRUPTION mono_coop_mutex_lock (&sgen_interruption_mutex)
89 #define UNLOCK_INTERRUPTION mono_coop_mutex_unlock (&sgen_interruption_mutex)
91 /* FIXME: Use mono_atomic_add_i32 & mono_atomic_add_i64 to reduce the CAS cost. */
92 #define SGEN_CAS mono_atomic_cas_i32
93 #define SGEN_CAS_PTR mono_atomic_cas_ptr
94 #define SGEN_ATOMIC_ADD(x,i) do { \
95 int __old_x; \
96 do { \
97 __old_x = (x); \
98 } while (mono_atomic_cas_i32 (&(x), __old_x + (i), __old_x) != __old_x); \
99 } while (0)
100 #define SGEN_ATOMIC_ADD_P(x,i) do { \
101 size_t __old_x; \
102 do { \
103 __old_x = (x); \
104 } while (mono_atomic_cas_ptr ((void**)&(x), (void*)(__old_x + (i)), (void*)__old_x) != (void*)__old_x); \
105 } while (0)
107 #ifdef HEAVY_STATISTICS
108 extern guint64 stat_objects_alloced_degraded;
109 extern guint64 stat_bytes_alloced_degraded;
110 extern guint64 stat_copy_object_called_major;
111 extern guint64 stat_objects_copied_major;
112 #endif
114 #define SGEN_ASSERT(level, a, ...) do { \
115 if (G_UNLIKELY ((level) <= SGEN_MAX_ASSERT_LEVEL && !(a))) { \
116 g_error (__VA_ARGS__); \
117 } } while (0)
119 #ifdef HAVE_LOCALTIME_R
120 # define LOG_TIMESTAMP \
121 do { \
122 time_t t; \
123 struct tm tod; \
124 time(&t); \
125 localtime_r(&t, &tod); \
126 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, &tod); \
127 } while (0)
128 #else
129 # define LOG_TIMESTAMP \
130 do { \
131 time_t t; \
132 struct tm *tod; \
133 time(&t); \
134 tod = localtime(&t); \
135 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, tod); \
136 } while (0)
137 #endif
139 #define SGEN_LOG(level, format, ...) do { \
140 if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= sgen_gc_debug_level)) { \
141 char logTime[80]; \
142 LOG_TIMESTAMP; \
143 mono_gc_printf (sgen_gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__); \
144 } } while (0)
146 #define SGEN_COND_LOG(level, cond, format, ...) do { \
147 if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= sgen_gc_debug_level)) { \
148 if (cond) { \
149 char logTime[80]; \
150 LOG_TIMESTAMP; \
151 mono_gc_printf (sgen_gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__); \
153 } } while (0)
155 extern int sgen_gc_debug_level;
156 extern FILE* sgen_gc_debug_file;
158 extern int sgen_current_collection_generation;
160 extern unsigned int sgen_global_stop_count;
162 #define SGEN_ALIGN_UP_TO(val,align) (((val) + (align - 1)) & ~(align - 1))
163 #define SGEN_ALIGN_DOWN_TO(val,align) ((val) & ~(align - 1))
165 #define SGEN_ALLOC_ALIGN 8
166 #define SGEN_ALLOC_ALIGN_BITS 3
168 /* s must be non-negative */
169 #define SGEN_CAN_ALIGN_UP(s) ((s) <= SIZE_MAX - (SGEN_ALLOC_ALIGN - 1))
170 #define SGEN_ALIGN_UP(s) SGEN_ALIGN_UP_TO(s, SGEN_ALLOC_ALIGN)
171 #define SGEN_ALIGN_DOWN(s) SGEN_ALIGN_DOWN_TO(s, SGEN_ALLOC_ALIGN)
173 #if SIZEOF_VOID_P == 4
174 #define ONE_P 1
175 #else
176 #define ONE_P 1ll
177 #endif
179 static inline guint
180 sgen_aligned_addr_hash (gconstpointer ptr)
182 return GPOINTER_TO_UINT (ptr) >> 3;
185 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~(((mword)1 << (bits)) - 1)) == (mword)(start))
187 extern size_t sgen_nursery_size;
188 extern size_t sgen_nursery_max_size;
189 extern int sgen_nursery_bits;
191 extern char *sgen_nursery_start;
192 extern char *sgen_nursery_end;
194 static inline MONO_ALWAYS_INLINE gboolean
195 sgen_ptr_in_nursery (void *p)
197 return SGEN_PTR_IN_NURSERY ((p), sgen_nursery_bits, sgen_nursery_start, sgen_nursery_end);
200 static inline MONO_ALWAYS_INLINE char*
201 sgen_get_nursery_start (void)
203 return sgen_nursery_start;
206 static inline MONO_ALWAYS_INLINE char*
207 sgen_get_nursery_end (void)
209 return sgen_nursery_end;
213 * We use the lowest three bits in the vtable pointer of objects to tag whether they're
214 * forwarded, pinned, and/or cemented. These are the valid states:
216 * | State | bits |
217 * |------------------+------+
218 * | default | 000 |
219 * | forwarded | 001 |
220 * | pinned | 010 |
221 * | pinned, cemented | 110 |
223 * We store them in the vtable slot because the bits are used in the sync block for other
224 * purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
225 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
226 * would be an invalid combination for the monitor and hash code).
229 #include "sgen-tagged-pointer.h"
231 #define SGEN_VTABLE_BITS_MASK SGEN_TAGGED_POINTER_MASK
233 #define SGEN_POINTER_IS_TAGGED_FORWARDED(p) SGEN_POINTER_IS_TAGGED_1((p))
234 #define SGEN_POINTER_TAG_FORWARDED(p) SGEN_POINTER_TAG_1((p))
236 #define SGEN_POINTER_IS_TAGGED_PINNED(p) SGEN_POINTER_IS_TAGGED_2((p))
237 #define SGEN_POINTER_TAG_PINNED(p) SGEN_POINTER_TAG_2((p))
239 #define SGEN_POINTER_IS_TAGGED_CEMENTED(p) SGEN_POINTER_IS_TAGGED_4((p))
240 #define SGEN_POINTER_TAG_CEMENTED(p) SGEN_POINTER_TAG_4((p))
242 #define SGEN_POINTER_UNTAG_VTABLE(p) SGEN_POINTER_UNTAG_ALL((p))
244 /* returns NULL if not forwarded, or the forwarded address */
245 #define SGEN_VTABLE_IS_FORWARDED(vtable) ((GCObject *)(SGEN_POINTER_IS_TAGGED_FORWARDED ((vtable)) ? SGEN_POINTER_UNTAG_VTABLE ((vtable)) : NULL))
246 #define SGEN_OBJECT_IS_FORWARDED(obj) ((GCObject *)SGEN_VTABLE_IS_FORWARDED (((mword*)(obj))[0]))
248 #define SGEN_VTABLE_IS_PINNED(vtable) SGEN_POINTER_IS_TAGGED_PINNED ((vtable))
249 #define SGEN_OBJECT_IS_PINNED(obj) (SGEN_VTABLE_IS_PINNED (((mword*)(obj))[0]))
251 #define SGEN_OBJECT_IS_CEMENTED(obj) (SGEN_POINTER_IS_TAGGED_CEMENTED (((mword*)(obj))[0]))
253 /* set the forwarded address fw_addr for object obj */
254 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
255 *(void**)(obj) = SGEN_POINTER_TAG_FORWARDED ((fw_addr)); \
256 } while (0)
257 #define SGEN_FORWARD_OBJECT_PAR(obj,fw_addr,final_fw_addr) do { \
258 gpointer old_vtable_word = *(gpointer*)obj; \
259 gpointer new_vtable_word; \
260 final_fw_addr = SGEN_VTABLE_IS_FORWARDED (old_vtable_word); \
261 if (final_fw_addr) \
262 break; \
263 new_vtable_word = SGEN_POINTER_TAG_FORWARDED ((fw_addr)); \
264 old_vtable_word = mono_atomic_cas_ptr ((gpointer*)obj, new_vtable_word, old_vtable_word); \
265 final_fw_addr = SGEN_VTABLE_IS_FORWARDED (old_vtable_word); \
266 if (!final_fw_addr) \
267 final_fw_addr = (fw_addr); \
268 } while (0)
269 #define SGEN_PIN_OBJECT(obj) do { \
270 *(void**)(obj) = SGEN_POINTER_TAG_PINNED (*(void**)(obj)); \
271 } while (0)
272 #define SGEN_CEMENT_OBJECT(obj) do { \
273 *(void**)(obj) = SGEN_POINTER_TAG_CEMENTED (*(void**)(obj)); \
274 } while (0)
275 /* Unpins and uncements */
276 #define SGEN_UNPIN_OBJECT(obj) do { \
277 *(void**)(obj) = SGEN_POINTER_UNTAG_VTABLE (*(void**)(obj)); \
278 } while (0)
281 * Since we set bits in the vtable, use the macro to load it from the pointer to
282 * an object that is potentially pinned.
284 #define SGEN_LOAD_VTABLE(obj) ((GCVTable)(SGEN_POINTER_UNTAG_ALL (SGEN_LOAD_VTABLE_UNCHECKED ((GCObject *)(obj)))))
287 List of what each bit on of the vtable gc bits means.
289 enum {
290 // When the Java bridge has determined an object is "bridged", it uses these two bits to cache that information.
291 SGEN_GC_BIT_BRIDGE_OBJECT = 1,
292 SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT = 2,
293 SGEN_GC_BIT_FINALIZER_AWARE = 4,
296 void sgen_gc_init (void)
297 MONO_PERMIT (need (sgen_lock_gc));
299 void sgen_os_init (void);
301 void sgen_update_heap_boundaries (mword low, mword high);
303 void sgen_check_section_scan_starts (GCMemSection *section);
305 void sgen_conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type);
307 gboolean sgen_gc_initialized (void);
309 /* Keep in sync with description_for_type() in sgen-internal.c! */
310 enum {
311 INTERNAL_MEM_PIN_QUEUE,
312 INTERNAL_MEM_FRAGMENT,
313 INTERNAL_MEM_SECTION,
314 INTERNAL_MEM_SCAN_STARTS,
315 INTERNAL_MEM_FIN_TABLE,
316 INTERNAL_MEM_FINALIZE_ENTRY,
317 INTERNAL_MEM_FINALIZE_READY,
318 INTERNAL_MEM_DISLINK_TABLE,
319 INTERNAL_MEM_DISLINK,
320 INTERNAL_MEM_ROOTS_TABLE,
321 INTERNAL_MEM_ROOT_RECORD,
322 INTERNAL_MEM_STATISTICS,
323 INTERNAL_MEM_STAT_PINNED_CLASS,
324 INTERNAL_MEM_STAT_REMSET_CLASS,
325 INTERNAL_MEM_STAT_GCHANDLE_CLASS,
326 INTERNAL_MEM_GRAY_QUEUE,
327 INTERNAL_MEM_MS_TABLES,
328 INTERNAL_MEM_MS_BLOCK_INFO,
329 INTERNAL_MEM_MS_BLOCK_INFO_SORT,
330 INTERNAL_MEM_WORKER_DATA,
331 INTERNAL_MEM_THREAD_POOL_JOB,
332 INTERNAL_MEM_BRIDGE_DATA,
333 INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE,
334 INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY,
335 INTERNAL_MEM_BRIDGE_HASH_TABLE,
336 INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY,
337 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE,
338 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY,
339 INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE,
340 INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE_ENTRY,
341 INTERNAL_MEM_TARJAN_OBJ_BUCKET,
342 INTERNAL_MEM_BRIDGE_DEBUG,
343 INTERNAL_MEM_TOGGLEREF_DATA,
344 INTERNAL_MEM_CARDTABLE_MOD_UNION,
345 INTERNAL_MEM_BINARY_PROTOCOL,
346 INTERNAL_MEM_TEMPORARY,
347 INTERNAL_MEM_LOG_ENTRY,
348 INTERNAL_MEM_COMPLEX_DESCRIPTORS,
349 INTERNAL_MEM_FIRST_CLIENT
352 enum {
353 GENERATION_NURSERY,
354 GENERATION_OLD,
355 GENERATION_MAX
358 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
359 #define BINARY_PROTOCOL_ARG(x) ,x
360 #else
361 #define BINARY_PROTOCOL_ARG(x)
362 #endif
364 void sgen_init_internal_allocator (void);
366 #define SGEN_DEFINE_OBJECT_VTABLE
367 #ifdef SGEN_CLIENT_HEADER
368 #include SGEN_CLIENT_HEADER
369 #else
370 #include "metadata/sgen-client-mono.h"
371 #endif
372 #undef SGEN_DEFINE_OBJECT_VTABLE
374 #include "mono/sgen/sgen-descriptor.h"
375 #include "mono/sgen/sgen-gray.h"
377 /* the runtime can register areas of memory as roots: we keep two lists of roots,
378 * a pinned root set for conservatively scanned roots and a normal one for
379 * precisely scanned roots (currently implemented as a single list).
381 typedef struct _RootRecord RootRecord;
382 struct _RootRecord {
383 char *end_root;
384 SgenDescriptor root_desc;
385 int source;
386 const char *msg;
389 enum {
390 ROOT_TYPE_NORMAL = 0, /* "normal" roots */
391 ROOT_TYPE_PINNED = 1, /* roots without a GC descriptor */
392 ROOT_TYPE_WBARRIER = 2, /* roots with a write barrier */
393 ROOT_TYPE_NUM
396 extern SgenHashTable sgen_roots_hash [ROOT_TYPE_NUM];
398 int sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_type, MonoGCRootSource source, void *key, const char *msg)
399 MONO_PERMIT (need (sgen_lock_gc));
400 void sgen_deregister_root (char* addr)
401 MONO_PERMIT (need (sgen_lock_gc));
403 typedef void (*IterateObjectCallbackFunc) (GCObject*, size_t, void*);
405 void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags, gboolean fail_on_canaries);
407 /* eventually share with MonoThread? */
409 * This structure extends the MonoThreadInfo structure.
411 struct _SgenThreadInfo {
412 SgenClientThreadInfo client_info;
414 char *tlab_start;
415 char *tlab_next;
416 char *tlab_temp_end;
417 char *tlab_real_end;
419 /* Total bytes allocated by this thread in its lifetime so far. */
420 gint64 total_bytes_allocated;
423 gboolean sgen_is_worker_thread (MonoNativeThreadId thread);
425 typedef void (*CopyOrMarkObjectFunc) (GCObject**, SgenGrayQueue*);
426 typedef void (*ScanObjectFunc) (GCObject *obj, SgenDescriptor desc, SgenGrayQueue*);
427 typedef void (*ScanVTypeFunc) (GCObject *full_object, char *start, SgenDescriptor desc, SgenGrayQueue* BINARY_PROTOCOL_ARG (size_t size));
428 typedef void (*ScanPtrFieldFunc) (GCObject *obj, GCObject **ptr, SgenGrayQueue* queue);
429 typedef gboolean (*DrainGrayStackFunc) (SgenGrayQueue *queue);
431 typedef struct {
432 CopyOrMarkObjectFunc copy_or_mark_object;
433 ScanObjectFunc scan_object;
434 ScanVTypeFunc scan_vtype;
435 ScanPtrFieldFunc scan_ptr_field;
436 /* Drain stack optimized for the above functions */
437 DrainGrayStackFunc drain_gray_stack;
438 /*FIXME add allocation function? */
439 } SgenObjectOperations;
441 typedef struct
443 SgenObjectOperations *ops;
444 SgenGrayQueue *queue;
445 } ScanCopyContext;
447 // error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion
448 // An inline function or constructor would work here too.
449 #ifdef _MSC_VER
450 #define MONO_MSC_WARNING_SUPPRESS(warn, body) __pragma (warning (suppress:warn)) body
451 #else
452 #define MONO_MSC_WARNING_SUPPRESS(warn, body) body
453 #endif
454 #define CONTEXT_FROM_OBJECT_OPERATIONS(ops, queue) MONO_MSC_WARNING_SUPPRESS (4576, ((ScanCopyContext) { (ops), (queue) }))
456 void sgen_report_internal_mem_usage (void);
457 void sgen_dump_internal_mem_usage (FILE *heap_dump_file);
458 void sgen_dump_section (GCMemSection *section, const char *type);
459 void sgen_dump_occupied (char *start, char *end, char *section_start);
461 void sgen_register_fixed_internal_mem_type (int type, size_t size);
463 void* sgen_alloc_internal (int type);
464 void sgen_free_internal (void *addr, int type);
466 void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure);
467 void sgen_free_internal_dynamic (void *addr, size_t size, int type);
469 void sgen_pin_stats_enable (void);
470 void sgen_pin_stats_register_object (GCObject *obj, int generation);
471 void sgen_pin_stats_register_global_remset (GCObject *obj);
472 void sgen_pin_stats_report (void);
474 void sgen_gchandle_stats_enable (void);
475 void sgen_gchandle_stats_report (void);
477 void sgen_sort_addresses (void **array, size_t size);
478 void sgen_add_to_global_remset (gpointer ptr, GCObject *obj);
480 int sgen_get_current_collection_generation (void);
481 gboolean sgen_collection_is_concurrent (void);
482 gboolean sgen_get_concurrent_collection_in_progress (void);
484 void sgen_set_bytes_allocated_attached (guint64 bytes);
485 void sgen_increment_bytes_allocated_detached (guint64 bytes);
487 typedef struct _SgenFragment SgenFragment;
489 struct _SgenFragment {
490 SgenFragment *next;
491 char *fragment_start;
492 char *fragment_next; /* the current soft limit for allocation */
493 char *fragment_end;
494 SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
497 typedef struct {
498 SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
499 SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
500 } SgenFragmentAllocator;
502 void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end);
503 void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator);
504 void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size);
505 void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
506 void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
507 SgenFragment* sgen_fragment_allocator_alloc (void);
508 void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator);
509 void sgen_clear_range (char *start, char *end);
513 This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
514 and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
515 L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
517 Experimental results on how much space we waste with a 4Mb nursery:
519 Note that the wastage applies to the half nursery, or 2Mb:
521 Test 1 (compiling corlib):
522 9: avg: 3.1k
523 8: avg: 1.6k
526 #define SGEN_TO_SPACE_GRANULE_BITS 9
527 #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
529 extern char *sgen_space_bitmap;
530 extern size_t sgen_space_bitmap_size;
532 static inline gboolean
533 sgen_nursery_is_to_space (void *object)
535 size_t idx = ((char*)object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
536 size_t byte = idx >> 3;
537 size_t bit = idx & 0x7;
539 SGEN_ASSERT (4, sgen_ptr_in_nursery (object), "object %p is not in nursery [%p - %p]", object, sgen_get_nursery_start (), sgen_get_nursery_end ());
540 SGEN_ASSERT (4, byte < sgen_space_bitmap_size, "byte index %zd out of range (%zd)", byte, sgen_space_bitmap_size);
542 return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
545 static inline gboolean
546 sgen_nursery_is_from_space (void *object)
548 return !sgen_nursery_is_to_space (object);
551 static inline gboolean
552 sgen_nursery_is_object_alive (GCObject *obj)
554 /* FIXME put this asserts under a non default level */
555 g_assert (sgen_ptr_in_nursery (obj));
557 if (sgen_nursery_is_to_space (obj))
558 return TRUE;
560 if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
561 return TRUE;
563 return FALSE;
566 typedef struct {
567 gboolean is_split;
568 gboolean is_parallel;
570 GCObject* (*alloc_for_promotion) (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references);
571 GCObject* (*alloc_for_promotion_par) (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references);
573 SgenObjectOperations serial_ops;
574 SgenObjectOperations serial_ops_with_concurrent_major;
575 SgenObjectOperations parallel_ops;
576 SgenObjectOperations parallel_ops_with_concurrent_major;
578 void (*prepare_to_space) (char *to_space_bitmap, size_t space_bitmap_size);
579 void (*clear_fragments) (void);
580 SgenFragment* (*build_fragments_get_exclude_head) (void);
581 void (*build_fragments_release_exclude_head) (void);
582 void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
583 void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
585 gboolean (*handle_gc_param) (const char *opt); /* Optional */
586 void (*print_gc_param_usage) (void); /* Optional */
587 } SgenMinorCollector;
589 extern SgenMinorCollector sgen_minor_collector;
591 void sgen_simple_nursery_init (SgenMinorCollector *collector, gboolean parallel);
592 void sgen_split_nursery_init (SgenMinorCollector *collector);
594 /* Updating references */
596 #ifdef SGEN_CHECK_UPDATE_REFERENCE
597 gboolean sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId some_thread);
599 static inline void
600 sgen_update_reference (GCObject **p, GCObject *o, gboolean allow_null)
602 if (!allow_null)
603 SGEN_ASSERT (0, o, "Cannot update a reference with a NULL pointer");
604 SGEN_ASSERT (0, !sgen_workers_is_worker_thread (mono_native_thread_id_get ()), "Can't update a reference in the worker thread");
605 *p = o;
608 #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o) sgen_update_reference ((GCObject**)(p), (GCObject*)(o), TRUE)
609 #define SGEN_UPDATE_REFERENCE(p,o) sgen_update_reference ((GCObject**)(p), (GCObject*)(o), FALSE)
610 #else
611 #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o) (*(GCObject**)(p) = (GCObject*)(o))
612 #define SGEN_UPDATE_REFERENCE(p,o) SGEN_UPDATE_REFERENCE_ALLOW_NULL ((p), (o))
613 #endif
615 /* Major collector */
617 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
618 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
619 void sgen_major_collector_iterate_block_ranges (sgen_cardtable_block_callback callback);
621 typedef enum {
622 ITERATE_OBJECTS_SWEEP = 1,
623 ITERATE_OBJECTS_NON_PINNED = 2,
624 ITERATE_OBJECTS_PINNED = 4,
625 ITERATE_OBJECTS_SWEEP_NON_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED,
626 ITERATE_OBJECTS_SWEEP_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_PINNED,
627 ITERATE_OBJECTS_SWEEP_ALL = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED | ITERATE_OBJECTS_PINNED
628 } IterateObjectsFlags;
630 typedef struct
632 size_t num_scanned_objects;
633 size_t num_unique_scanned_objects;
634 } ScannedObjectCounts;
636 typedef enum {
637 CARDTABLE_SCAN_GLOBAL = 0,
638 CARDTABLE_SCAN_MOD_UNION = 1,
639 CARDTABLE_SCAN_MOD_UNION_PRECLEAN = CARDTABLE_SCAN_MOD_UNION | 2,
640 } CardTableScanType;
642 typedef struct _SgenMajorCollector SgenMajorCollector;
643 struct _SgenMajorCollector {
644 size_t section_size;
645 gboolean is_concurrent;
646 gboolean is_parallel;
647 gboolean supports_cardtable;
648 gboolean sweeps_lazily;
650 void* (*alloc_heap) (mword nursery_size, mword nursery_align);
651 gboolean (*is_object_live) (GCObject *obj);
652 GCObject* (*alloc_small_pinned_obj) (GCVTable vtable, size_t size, gboolean has_references);
653 GCObject* (*alloc_degraded) (GCVTable vtable, size_t size);
655 SgenObjectOperations major_ops_serial;
656 SgenObjectOperations major_ops_concurrent_start;
657 SgenObjectOperations major_ops_concurrent_finish;
658 SgenObjectOperations major_ops_conc_par_start;
659 SgenObjectOperations major_ops_conc_par_finish;
661 GCObject* (*alloc_object) (GCVTable vtable, size_t size, gboolean has_references);
662 GCObject* (*alloc_object_par) (GCVTable vtable, size_t size, gboolean has_references);
663 void (*free_pinned_object) (GCObject *obj, size_t size);
666 * This is used for domain unloading, heap walking from the logging profiler, and
667 * debugging. Can assume the world is stopped.
669 void (*iterate_objects) (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data);
671 void (*free_non_pinned_object) (GCObject *obj, size_t size);
672 void (*pin_objects) (SgenGrayQueue *queue);
673 void (*pin_major_object) (GCObject *obj, SgenGrayQueue *queue);
674 void (*scan_card_table) (CardTableScanType scan_type, ScanCopyContext ctx, int job_index, int job_split_count, int block_count);
675 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
676 void (*iterate_block_ranges) (sgen_cardtable_block_callback callback);
677 void (*update_cardtable_mod_union) (void);
678 void (*init_to_space) (void);
679 void (*sweep) (void);
680 gboolean (*have_swept) (void);
681 void (*finish_sweeping) (void);
682 void (*free_swept_blocks) (size_t section_reserve);
683 void (*check_scan_starts) (void);
684 void (*dump_heap) (FILE *heap_dump_file);
685 gint64 (*get_used_size) (void);
686 void (*start_nursery_collection) (void);
687 void (*finish_nursery_collection) (void);
688 void (*start_major_collection) (void);
689 void (*finish_major_collection) (ScannedObjectCounts *counts);
690 gboolean (*ptr_is_in_non_pinned_space) (char *ptr, char **start);
691 gboolean (*ptr_is_from_pinned_alloc) (char *ptr);
692 void (*report_pinned_memory_usage) (void);
693 size_t (*get_num_major_sections) (void);
694 size_t (*get_bytes_survived_last_sweep) (void);
695 gboolean (*handle_gc_param) (const char *opt);
696 void (*print_gc_param_usage) (void);
697 void (*post_param_init) (SgenMajorCollector *collector);
698 gboolean (*is_valid_object) (char *ptr);
699 GCVTable (*describe_pointer) (char *pointer);
700 guint8* (*get_cardtable_mod_union_for_reference) (char *object);
701 long long (*get_and_reset_num_major_objects_marked) (void);
702 void (*count_cards) (long long *num_total_cards, long long *num_marked_cards);
703 void (*init_block_free_lists) (gpointer *list_p);
706 extern SgenMajorCollector sgen_major_collector;
708 void sgen_marksweep_init (SgenMajorCollector *collector);
709 void sgen_marksweep_conc_init (SgenMajorCollector *collector);
710 void sgen_marksweep_conc_par_init (SgenMajorCollector *collector);
711 SgenMajorCollector* sgen_get_major_collector (void);
712 SgenMinorCollector* sgen_get_minor_collector (void);
715 typedef struct _SgenRememberedSet {
716 void (*wbarrier_set_field) (GCObject *obj, gpointer field_ptr, GCObject* value);
717 void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gconstpointer src_ptr, int count);
718 void (*wbarrier_value_copy) (gpointer dest, gconstpointer src, int count, size_t element_size);
719 void (*wbarrier_object_copy) (GCObject* obj, GCObject *src);
720 void (*wbarrier_generic_nostore) (gpointer ptr);
721 void (*record_pointer) (gpointer ptr);
722 void (*wbarrier_range_copy) (gpointer dest, gconstpointer src, int count);
724 void (*start_scan_remsets) (void);
726 void (*clear_cards) (void);
728 gboolean (*find_address) (char *addr);
729 gboolean (*find_address_with_cards) (char *cards_start, guint8 *cards, char *addr);
730 } SgenRememberedSet;
732 SgenRememberedSet *sgen_get_remset (void);
735 * These must be kept in sync with object.h. They're here for using SGen independently of
736 * Mono.
738 void mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, /*const*/ void* src_ptr, int count);
739 void mono_gc_wbarrier_generic_nostore (gpointer ptr);
740 void mono_gc_wbarrier_generic_store (gpointer ptr, GCObject* value);
741 void mono_gc_wbarrier_generic_store_atomic (gpointer ptr, GCObject *value);
743 void sgen_wbarrier_range_copy (gpointer _dest, gconstpointer _src, int size);
745 static inline SgenDescriptor
746 sgen_obj_get_descriptor (GCObject *obj)
748 GCVTable vtable = SGEN_LOAD_VTABLE_UNCHECKED (obj);
749 SGEN_ASSERT (9, !SGEN_POINTER_IS_TAGGED_ANY (vtable), "Object can't be tagged");
750 return sgen_vtable_get_descriptor (vtable);
753 static inline SgenDescriptor
754 sgen_obj_get_descriptor_safe (GCObject *obj)
756 GCVTable vtable = SGEN_LOAD_VTABLE (obj);
757 return sgen_vtable_get_descriptor (vtable);
760 static mword sgen_client_par_object_get_size (GCVTable vtable, GCObject* o);
761 static mword sgen_client_slow_object_get_size (GCVTable vtable, GCObject* o);
763 static inline mword
764 sgen_safe_object_get_size (GCObject *obj)
766 GCObject *forwarded;
767 GCVTable vtable = SGEN_LOAD_VTABLE_UNCHECKED (obj);
770 * Once we load the vtable, we must always use it, in case we are in parallel case.
771 * Otherwise the object might get forwarded in the meantime and we would read an
772 * invalid vtable. An object cannot be forwarded for a second time during same GC.
774 if ((forwarded = SGEN_VTABLE_IS_FORWARDED (vtable)))
775 return sgen_client_par_object_get_size (SGEN_LOAD_VTABLE (forwarded), obj);
776 else
777 return sgen_client_par_object_get_size ((GCVTable)SGEN_POINTER_UNTAG_ALL (vtable), obj);
780 static inline gboolean
781 sgen_safe_object_is_small (GCObject *obj, int type)
783 if (type <= DESC_TYPE_MAX_SMALL_OBJ)
784 return TRUE;
785 return SGEN_ALIGN_UP (sgen_safe_object_get_size ((GCObject*)obj)) <= SGEN_MAX_SMALL_OBJ_SIZE;
789 * This variant guarantees to return the exact size of the object
790 * before alignment. Needed for canary support.
792 static inline guint
793 sgen_safe_object_get_size_unaligned (GCObject *obj)
795 GCObject *forwarded;
797 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
798 obj = (GCObject*)forwarded;
801 return sgen_client_slow_object_get_size (SGEN_LOAD_VTABLE (obj), obj);
804 #ifdef SGEN_CLIENT_HEADER
805 #include SGEN_CLIENT_HEADER
806 #else
807 #include "metadata/sgen-client-mono.h"
808 #endif
810 gboolean sgen_object_is_live (GCObject *obj);
812 void sgen_init_fin_weak_hash (void);
814 void sgen_register_obj_with_weak_fields (GCObject *obj);
816 /* FIXME: move the toggleref stuff out of here */
817 void sgen_mark_togglerefs (char *start, char *end, ScanCopyContext ctx);
818 void sgen_clear_togglerefs (char *start, char *end, ScanCopyContext ctx);
820 void sgen_process_togglerefs (void);
821 void sgen_register_test_toggleref_callback (void);
823 void sgen_mark_bridge_object (GCObject *obj)
824 MONO_PERMIT (need (sgen_gc_locked));
825 void sgen_collect_bridge_objects (int generation, ScanCopyContext ctx)
826 MONO_PERMIT (need (sgen_gc_locked));
828 typedef gboolean (*SgenObjectPredicateFunc) (GCObject *obj, void *user_data);
830 void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track)
831 MONO_PERMIT (need (sgen_gc_locked, sgen_world_stopped));
833 gboolean sgen_gc_is_object_ready_for_finalization (GCObject *object);
834 void sgen_gc_lock (void) MONO_PERMIT (use (sgen_lock_gc), grant (sgen_gc_locked), revoke (sgen_lock_gc));
835 void sgen_gc_unlock (void) MONO_PERMIT (use (sgen_gc_locked), revoke (sgen_gc_locked), grant (sgen_lock_gc));
837 void sgen_queue_finalization_entry (GCObject *obj);
838 const char* sgen_generation_name (int generation);
840 void sgen_finalize_in_range (int generation, ScanCopyContext ctx)
841 MONO_PERMIT (need (sgen_gc_locked));
842 void sgen_null_link_in_range (int generation, ScanCopyContext ctx, gboolean track)
843 MONO_PERMIT (need (sgen_gc_locked, sgen_world_stopped));
844 void sgen_process_fin_stage_entries (void)
845 MONO_PERMIT (need (sgen_gc_locked));
846 gboolean sgen_have_pending_finalizers (void);
848 typedef void (*SGenFinalizationProc)(gpointer, gpointer); // same as MonoFinalizationProc, GC_finalization_proc
850 void sgen_object_register_for_finalization (GCObject *obj, SGenFinalizationProc user_data)
851 MONO_PERMIT (need (sgen_lock_gc));
853 void sgen_finalize_if (SgenObjectPredicateFunc predicate, void *user_data)
854 MONO_PERMIT (need (sgen_lock_gc));
855 void sgen_remove_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, int generation);
856 void sgen_set_suspend_finalizers (void);
858 void sgen_wbroots_iterate_live_block_ranges (sgen_cardtable_block_callback cb);
859 void sgen_wbroots_scan_card_table (ScanCopyContext ctx);
861 void sgen_register_disappearing_link (GCObject *obj, void **link, gboolean track, gboolean in_gc);
863 GCObject* sgen_weak_link_get (void **link_addr);
865 gboolean sgen_drain_gray_stack (ScanCopyContext ctx);
867 enum {
868 SPACE_NURSERY,
869 SPACE_MAJOR,
870 SPACE_LOS
873 void sgen_pin_object (GCObject *object, SgenGrayQueue *queue);
874 void sgen_set_pinned_from_failed_allocation (mword objsize);
876 void sgen_ensure_free_space (size_t size, int generation)
877 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
878 void sgen_gc_collect (int generation)
879 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
880 void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish, gboolean stw)
881 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
883 int sgen_gc_collection_count (int generation);
884 /* FIXME: what exactly does this return? */
885 size_t sgen_gc_get_used_size (void)
886 MONO_PERMIT (need (sgen_lock_gc));
887 size_t sgen_gc_get_total_heap_allocation (void);
889 /* STW */
891 void sgen_stop_world (int generation, gboolean serial_collection)
892 MONO_PERMIT (need (sgen_gc_locked), use (sgen_stop_world), grant (sgen_world_stopped), revoke (sgen_stop_world));
893 void sgen_restart_world (int generation, gboolean serial_collection)
894 MONO_PERMIT (need (sgen_gc_locked), use (sgen_world_stopped), revoke (sgen_world_stopped), grant (sgen_stop_world));
895 gboolean sgen_is_world_stopped (void);
897 gboolean sgen_set_allow_synchronous_major (gboolean flag);
899 /* LOS */
901 typedef struct _LOSObject LOSObject;
902 struct _LOSObject {
903 LOSObject *next;
904 mword size; /* this is the object size, lowest bit used for pin/mark */
905 guint8 * volatile cardtable_mod_union; /* only used by the concurrent collector */
906 #if SIZEOF_VOID_P < 8
907 mword dummy; /* to align object to sizeof (double) */
908 #endif
909 GCObject data [MONO_ZERO_LEN_ARRAY];
912 extern LOSObject *sgen_los_object_list;
913 extern mword sgen_los_memory_usage;
914 extern mword sgen_los_memory_usage_total;
916 void sgen_los_free_object (LOSObject *obj);
917 void* sgen_los_alloc_large_inner (GCVTable vtable, size_t size)
918 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
919 void sgen_los_sweep (void);
920 gboolean sgen_ptr_is_in_los (char *ptr, char **start);
921 void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data);
922 void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
923 void sgen_los_scan_card_table (CardTableScanType scan_type, ScanCopyContext ctx, int job_index, int job_split_count);
924 void sgen_los_update_cardtable_mod_union (void);
925 void sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards);
926 gboolean sgen_los_is_valid_object (char *object);
927 gboolean mono_sgen_los_describe_pointer (char *ptr);
928 LOSObject* sgen_los_header_for_object (GCObject *data);
929 mword sgen_los_object_size (LOSObject *obj);
930 void sgen_los_pin_object (GCObject *obj);
931 gboolean sgen_los_pin_object_par (GCObject *obj);
932 gboolean sgen_los_object_is_pinned (GCObject *obj);
933 void sgen_los_mark_mod_union_card (GCObject *mono_obj, void **ptr);
936 /* nursery allocator */
938 void sgen_clear_nursery_fragments (void);
939 void sgen_nursery_allocator_prepare_for_pinning (void);
940 void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, size_t min_size, size_t max_size);
941 void sgen_resize_nursery (gboolean need_shrink);
942 mword sgen_build_nursery_fragments (GCMemSection *nursery_section);
943 void sgen_init_nursery_allocator (void);
944 void sgen_nursery_allocator_init_heavy_stats (void);
945 void sgen_init_allocator (void);
946 void* sgen_nursery_alloc (size_t size);
947 void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size);
948 gboolean sgen_can_alloc_size (size_t size);
949 void sgen_nursery_retire_region (void *address, ptrdiff_t size);
951 void sgen_nursery_alloc_prepare_for_minor (void);
952 void sgen_nursery_alloc_prepare_for_major (void);
954 GCObject* sgen_alloc_for_promotion (GCObject *obj, size_t objsize, gboolean has_references);
956 GCObject* sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
957 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
958 GCObject* sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size);
960 /* Threads */
962 void* sgen_thread_attach (SgenThreadInfo* info);
963 void sgen_thread_detach_with_lock (SgenThreadInfo *p);
965 /* Finalization/ephemeron support */
967 static inline gboolean
968 sgen_major_is_object_alive (GCObject *object)
970 mword objsize;
972 /* Oldgen objects can be pinned and forwarded too */
973 if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
974 return TRUE;
977 * FIXME: major_collector.is_object_live() also calculates the
978 * size. Avoid the double calculation.
980 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size (object));
981 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
982 return sgen_los_object_is_pinned (object);
984 return sgen_major_collector.is_object_live (object);
989 * If the object has been forwarded it means it's still referenced from a root.
990 * If it is pinned it's still alive as well.
991 * A LOS object is only alive if we have pinned it.
992 * Return TRUE if @obj is ready to be finalized.
994 static inline gboolean
995 sgen_is_object_alive (GCObject *object)
997 if (sgen_ptr_in_nursery (object))
998 return sgen_nursery_is_object_alive (object);
1000 return sgen_major_is_object_alive (object);
1005 * This function returns true if @object is either alive or it belongs to the old gen
1006 * and we're currently doing a minor collection.
1008 static inline int
1009 sgen_is_object_alive_for_current_gen (GCObject *object)
1011 if (sgen_ptr_in_nursery (object))
1012 return sgen_nursery_is_object_alive (object);
1014 if (sgen_current_collection_generation == GENERATION_NURSERY)
1015 return TRUE;
1017 return sgen_major_is_object_alive (object);
1020 int sgen_gc_invoke_finalizers (void)
1021 MONO_PERMIT (need (sgen_lock_gc));
1023 /* GC handles */
1025 void sgen_init_gchandles (void);
1027 void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track)
1028 MONO_PERMIT (need (sgen_gc_locked));
1030 typedef gpointer (*SgenGCHandleIterateCallback) (gpointer hidden, GCHandleType handle_type, int max_generation, gpointer user);
1032 guint32 sgen_gchandle_new (GCObject *obj, gboolean pinned);
1033 guint32 sgen_gchandle_new_weakref (GCObject *obj, gboolean track_resurrection);
1034 void sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, SgenGCHandleIterateCallback callback, gpointer user)
1035 MONO_PERMIT (need (sgen_world_stopped));
1036 void sgen_gchandle_set_target (guint32 gchandle, GCObject *obj);
1037 void sgen_mark_normal_gc_handles (void *addr, SgenUserMarkFunc mark_func, void *gc_data)
1038 MONO_PERMIT (need (sgen_world_stopped));
1039 void sgen_gc_handles_report_roots (SgenUserReportRootFunc mark_func, void *gc_data)
1040 MONO_PERMIT (need (sgen_world_stopped));
1041 gpointer sgen_gchandle_get_metadata (guint32 gchandle);
1042 GCObject *sgen_gchandle_get_target (guint32 gchandle);
1043 void sgen_gchandle_free (guint32 gchandle);
1045 /* Other globals */
1047 extern GCMemSection *sgen_nursery_section;
1048 extern guint32 sgen_collect_before_allocs;
1049 extern guint32 sgen_verify_before_allocs;
1050 extern gboolean sgen_has_per_allocation_action;
1051 extern size_t sgen_degraded_mode;
1052 extern int default_nursery_size;
1053 extern guint32 sgen_tlab_size;
1054 extern NurseryClearPolicy sgen_nursery_clear_policy;
1055 extern gboolean sgen_try_free_some_memory;
1056 extern mword sgen_total_promoted_size;
1057 extern mword sgen_total_allocated_major;
1058 extern volatile gboolean sgen_suspend_finalizers;
1059 extern MonoCoopMutex sgen_gc_mutex;
1060 extern volatile gboolean sgen_concurrent_collection_in_progress;
1062 /* Nursery helpers. */
1064 static inline void
1065 sgen_set_nursery_scan_start (char *p)
1067 size_t idx = (p - (char*)sgen_nursery_section->data) / SGEN_SCAN_START_SIZE;
1068 char *old = sgen_nursery_section->scan_starts [idx];
1069 if (!old || old > p)
1070 sgen_nursery_section->scan_starts [idx] = p;
1074 /* Object Allocation */
1076 typedef enum {
1077 ATYPE_NORMAL,
1078 ATYPE_VECTOR,
1079 ATYPE_SMALL,
1080 ATYPE_STRING,
1081 ATYPE_NUM
1082 } SgenAllocatorType;
1084 void sgen_clear_tlabs (void);
1085 void sgen_update_allocation_count (void);
1086 guint64 sgen_get_total_allocated_bytes (MonoBoolean precise);
1088 GCObject* sgen_alloc_obj (GCVTable vtable, size_t size)
1089 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1090 GCObject* sgen_alloc_obj_pinned (GCVTable vtable, size_t size)
1091 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1092 GCObject* sgen_alloc_obj_mature (GCVTable vtable, size_t size)
1093 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1095 /* Debug support */
1097 void sgen_check_remset_consistency (void);
1098 void sgen_check_mod_union_consistency (void);
1099 void sgen_check_major_refs (void);
1100 void sgen_check_whole_heap (gboolean allow_missing_pinning);
1101 void sgen_check_whole_heap_stw (void)
1102 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
1103 void sgen_check_objref (char *obj);
1104 void sgen_check_heap_marked (gboolean nursery_must_be_pinned);
1105 void sgen_check_nursery_objects_untag (void);
1106 void sgen_check_for_xdomain_refs (void);
1107 GCObject* sgen_find_object_for_ptr (char *ptr);
1109 void mono_gc_scan_for_specific_ref (GCObject *key, gboolean precise);
1111 void sgen_debug_enable_heap_dump (const char *filename);
1112 void sgen_debug_dump_heap (const char *type, int num, const char *reason);
1114 void sgen_debug_verify_nursery (gboolean do_dump_nursery_content);
1115 void sgen_debug_check_nursery_is_clean (void);
1117 /* Environment variable parsing */
1119 #define MONO_GC_PARAMS_NAME "MONO_GC_PARAMS"
1120 #define MONO_GC_DEBUG_NAME "MONO_GC_DEBUG"
1122 void sgen_env_var_error (const char *env_var, const char *fallback, const char *description_format, ...);
1124 /* Utilities */
1126 void sgen_qsort (void *const array, const size_t count, const size_t element_size, int (*compare) (const void*, const void*));
1127 gint64 sgen_timestamp (void);
1130 * Canary (guard word) support
1131 * Notes:
1132 * - CANARY_SIZE must be multiple of word size in bytes
1133 * - Canary space is not included on checks against SGEN_MAX_SMALL_OBJ_SIZE
1136 gboolean sgen_nursery_canaries_enabled (void);
1138 #define CANARY_SIZE 8
1139 #define CANARY_STRING "koupepia"
1141 #define CANARIFY_SIZE(size) if (sgen_nursery_canaries_enabled ()) { \
1142 size = size + CANARY_SIZE; \
1145 #define CANARIFY_ALLOC(addr,size) if (sgen_nursery_canaries_enabled ()) { \
1146 memcpy ((char*) (addr) + (size), CANARY_STRING, CANARY_SIZE); \
1149 #define CANARY_VALID(addr) (strncmp ((char*) (addr), CANARY_STRING, CANARY_SIZE) == 0)
1151 void
1152 sgen_check_canary_for_object (gpointer addr);
1154 guint64 sgen_get_precise_allocation_count (void);
1156 #define CHECK_CANARY_FOR_OBJECT(addr, ignored) \
1157 (sgen_nursery_canaries_enabled () ? sgen_check_canary_for_object (addr) : (void)0)
1160 * This causes the compile to extend the liveness of 'v' till the call to dummy_use
1162 static inline void
1163 sgen_dummy_use (gpointer v)
1165 #if defined(_MSC_VER) || defined(HOST_WASM)
1166 static volatile gpointer ptr;
1167 ptr = v;
1168 #elif defined(__GNUC__)
1169 __asm__ volatile ("" : "=r"(v) : "r"(v));
1170 #else
1171 #error "Implement sgen_dummy_use for your compiler"
1172 #endif
1175 #endif /* HAVE_SGEN_GC */
1177 #endif /* __MONO_SGENGC_H__ */