Fix potential crash for Encoder.Convert (#20522)
[mono-project.git] / mono / sgen / sgen-gc.h
blob2bf292940b695c2bd34ce9d3ee3352c16f1717a3
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 #ifndef MONO_ATOMIC_USES_LOCK
108 #define SGEN_ATOMIC_ADD_I64(x,i) do { \
109 mono_atomic_add_i64 ((volatile gint64 *)&x, i); \
110 } while (0)
111 #else
112 #define SGEN_ATOMIC_ADD_I64(x,i) do { \
113 gint64 __old_x; \
114 do { \
115 __old_x = (x); \
116 } while (mono_sgen_atomic_cas_i64 ((volatile gint64 *)&(x), __old_x + (i), __old_x) != __old_x); \
117 } while (0)
118 #endif /* BROKEN_64BIT_ATOMICS_INTRINSIC */
120 #ifdef HEAVY_STATISTICS
121 extern guint64 stat_objects_alloced_degraded;
122 extern guint64 stat_bytes_alloced_degraded;
123 extern guint64 stat_copy_object_called_major;
124 extern guint64 stat_objects_copied_major;
125 #endif
127 #define SGEN_ASSERT(level, a, ...) do { \
128 if (G_UNLIKELY ((level) <= SGEN_MAX_ASSERT_LEVEL && !(a))) { \
129 g_error (__VA_ARGS__); \
130 } } while (0)
132 #ifdef HAVE_LOCALTIME_R
133 # define LOG_TIMESTAMP \
134 do { \
135 time_t t; \
136 struct tm tod; \
137 time(&t); \
138 localtime_r(&t, &tod); \
139 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, &tod); \
140 } while (0)
141 #else
142 # define LOG_TIMESTAMP \
143 do { \
144 time_t t; \
145 struct tm *tod; \
146 time(&t); \
147 tod = localtime(&t); \
148 strftime(logTime, sizeof(logTime), MONO_STRFTIME_F " " MONO_STRFTIME_T, tod); \
149 } while (0)
150 #endif
152 #define SGEN_LOG(level, format, ...) do { \
153 if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= sgen_gc_debug_level)) { \
154 char logTime[80]; \
155 LOG_TIMESTAMP; \
156 mono_gc_printf (sgen_gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__); \
157 } } while (0)
159 #define SGEN_COND_LOG(level, cond, format, ...) do { \
160 if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= sgen_gc_debug_level)) { \
161 if (cond) { \
162 char logTime[80]; \
163 LOG_TIMESTAMP; \
164 mono_gc_printf (sgen_gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__); \
166 } } while (0)
168 extern int sgen_gc_debug_level;
169 extern FILE* sgen_gc_debug_file;
171 extern int sgen_current_collection_generation;
173 extern unsigned int sgen_global_stop_count;
175 #define SGEN_ALIGN_UP_TO(val,align) (((val) + (align - 1)) & ~(align - 1))
176 #define SGEN_ALIGN_DOWN_TO(val,align) ((val) & ~(align - 1))
178 #define SGEN_ALLOC_ALIGN 8
179 #define SGEN_ALLOC_ALIGN_BITS 3
181 /* s must be non-negative */
182 #define SGEN_CAN_ALIGN_UP(s) ((s) <= SIZE_MAX - (SGEN_ALLOC_ALIGN - 1))
183 #define SGEN_ALIGN_UP(s) SGEN_ALIGN_UP_TO(s, SGEN_ALLOC_ALIGN)
184 #define SGEN_ALIGN_DOWN(s) SGEN_ALIGN_DOWN_TO(s, SGEN_ALLOC_ALIGN)
186 #if SIZEOF_VOID_P == 4
187 #define ONE_P 1
188 #else
189 #define ONE_P 1ll
190 #endif
192 static inline guint
193 sgen_aligned_addr_hash (gconstpointer ptr)
195 return GPOINTER_TO_UINT (ptr) >> 3;
198 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~(((mword)1 << (bits)) - 1)) == (mword)(start))
200 extern size_t sgen_nursery_size;
201 extern size_t sgen_nursery_max_size;
202 extern int sgen_nursery_bits;
204 extern char *sgen_nursery_start;
205 extern char *sgen_nursery_end;
207 static inline MONO_ALWAYS_INLINE gboolean
208 sgen_ptr_in_nursery (void *p)
210 return SGEN_PTR_IN_NURSERY ((p), sgen_nursery_bits, sgen_nursery_start, sgen_nursery_end);
213 static inline MONO_ALWAYS_INLINE char*
214 sgen_get_nursery_start (void)
216 return sgen_nursery_start;
219 static inline MONO_ALWAYS_INLINE char*
220 sgen_get_nursery_end (void)
222 return sgen_nursery_end;
226 * We use the lowest three bits in the vtable pointer of objects to tag whether they're
227 * forwarded, pinned, and/or cemented. These are the valid states:
229 * | State | bits |
230 * |------------------+------+
231 * | default | 000 |
232 * | forwarded | 001 |
233 * | pinned | 010 |
234 * | pinned, cemented | 110 |
236 * We store them in the vtable slot because the bits are used in the sync block for other
237 * purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
238 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
239 * would be an invalid combination for the monitor and hash code).
242 #include "sgen-tagged-pointer.h"
244 #define SGEN_VTABLE_BITS_MASK SGEN_TAGGED_POINTER_MASK
246 #define SGEN_POINTER_IS_TAGGED_FORWARDED(p) SGEN_POINTER_IS_TAGGED_1((p))
247 #define SGEN_POINTER_TAG_FORWARDED(p) SGEN_POINTER_TAG_1((p))
249 #define SGEN_POINTER_IS_TAGGED_PINNED(p) SGEN_POINTER_IS_TAGGED_2((p))
250 #define SGEN_POINTER_TAG_PINNED(p) SGEN_POINTER_TAG_2((p))
252 #define SGEN_POINTER_IS_TAGGED_CEMENTED(p) SGEN_POINTER_IS_TAGGED_4((p))
253 #define SGEN_POINTER_TAG_CEMENTED(p) SGEN_POINTER_TAG_4((p))
255 #define SGEN_POINTER_UNTAG_VTABLE(p) SGEN_POINTER_UNTAG_ALL((p))
257 /* returns NULL if not forwarded, or the forwarded address */
258 #define SGEN_VTABLE_IS_FORWARDED(vtable) ((GCObject *)(SGEN_POINTER_IS_TAGGED_FORWARDED ((vtable)) ? SGEN_POINTER_UNTAG_VTABLE ((vtable)) : NULL))
259 #define SGEN_OBJECT_IS_FORWARDED(obj) ((GCObject *)SGEN_VTABLE_IS_FORWARDED (((mword*)(obj))[0]))
261 #define SGEN_VTABLE_IS_PINNED(vtable) SGEN_POINTER_IS_TAGGED_PINNED ((vtable))
262 #define SGEN_OBJECT_IS_PINNED(obj) (SGEN_VTABLE_IS_PINNED (((mword*)(obj))[0]))
264 #define SGEN_OBJECT_IS_CEMENTED(obj) (SGEN_POINTER_IS_TAGGED_CEMENTED (((mword*)(obj))[0]))
266 /* set the forwarded address fw_addr for object obj */
267 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
268 *(void**)(obj) = SGEN_POINTER_TAG_FORWARDED ((fw_addr)); \
269 } while (0)
270 #define SGEN_FORWARD_OBJECT_PAR(obj,fw_addr,final_fw_addr) do { \
271 gpointer old_vtable_word = *(gpointer*)obj; \
272 gpointer new_vtable_word; \
273 final_fw_addr = SGEN_VTABLE_IS_FORWARDED (old_vtable_word); \
274 if (final_fw_addr) \
275 break; \
276 new_vtable_word = SGEN_POINTER_TAG_FORWARDED ((fw_addr)); \
277 old_vtable_word = mono_atomic_cas_ptr ((gpointer*)obj, new_vtable_word, old_vtable_word); \
278 final_fw_addr = SGEN_VTABLE_IS_FORWARDED (old_vtable_word); \
279 if (!final_fw_addr) \
280 final_fw_addr = (fw_addr); \
281 } while (0)
282 #define SGEN_PIN_OBJECT(obj) do { \
283 *(void**)(obj) = SGEN_POINTER_TAG_PINNED (*(void**)(obj)); \
284 } while (0)
285 #define SGEN_CEMENT_OBJECT(obj) do { \
286 *(void**)(obj) = SGEN_POINTER_TAG_CEMENTED (*(void**)(obj)); \
287 } while (0)
288 /* Unpins and uncements */
289 #define SGEN_UNPIN_OBJECT(obj) do { \
290 *(void**)(obj) = SGEN_POINTER_UNTAG_VTABLE (*(void**)(obj)); \
291 } while (0)
294 * Since we set bits in the vtable, use the macro to load it from the pointer to
295 * an object that is potentially pinned.
297 #define SGEN_LOAD_VTABLE(obj) ((GCVTable)(SGEN_POINTER_UNTAG_ALL (SGEN_LOAD_VTABLE_UNCHECKED ((GCObject *)(obj)))))
300 List of what each bit on of the vtable gc bits means.
302 enum {
303 // When the Java bridge has determined an object is "bridged", it uses these two bits to cache that information.
304 SGEN_GC_BIT_BRIDGE_OBJECT = 1,
305 SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT = 2,
306 SGEN_GC_BIT_FINALIZER_AWARE = 4,
309 void sgen_gc_init (void)
310 MONO_PERMIT (need (sgen_lock_gc));
312 void sgen_os_init (void);
314 void sgen_update_heap_boundaries (mword low, mword high);
316 void sgen_check_section_scan_starts (GCMemSection *section);
318 void sgen_conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type);
320 gboolean sgen_gc_initialized (void);
322 /* Keep in sync with description_for_type() in sgen-internal.c! */
323 enum {
324 INTERNAL_MEM_PIN_QUEUE,
325 INTERNAL_MEM_FRAGMENT,
326 INTERNAL_MEM_SECTION,
327 INTERNAL_MEM_SCAN_STARTS,
328 INTERNAL_MEM_FIN_TABLE,
329 INTERNAL_MEM_FINALIZE_ENTRY,
330 INTERNAL_MEM_FINALIZE_READY,
331 INTERNAL_MEM_DISLINK_TABLE,
332 INTERNAL_MEM_DISLINK,
333 INTERNAL_MEM_ROOTS_TABLE,
334 INTERNAL_MEM_ROOT_RECORD,
335 INTERNAL_MEM_STATISTICS,
336 INTERNAL_MEM_STAT_PINNED_CLASS,
337 INTERNAL_MEM_STAT_REMSET_CLASS,
338 INTERNAL_MEM_STAT_GCHANDLE_CLASS,
339 INTERNAL_MEM_GRAY_QUEUE,
340 INTERNAL_MEM_MS_TABLES,
341 INTERNAL_MEM_MS_BLOCK_INFO,
342 INTERNAL_MEM_MS_BLOCK_INFO_SORT,
343 INTERNAL_MEM_WORKER_DATA,
344 INTERNAL_MEM_THREAD_POOL_JOB,
345 INTERNAL_MEM_BRIDGE_DATA,
346 INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE,
347 INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY,
348 INTERNAL_MEM_BRIDGE_HASH_TABLE,
349 INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY,
350 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE,
351 INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY,
352 INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE,
353 INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE_ENTRY,
354 INTERNAL_MEM_TARJAN_OBJ_BUCKET,
355 INTERNAL_MEM_BRIDGE_DEBUG,
356 INTERNAL_MEM_TOGGLEREF_DATA,
357 INTERNAL_MEM_CARDTABLE_MOD_UNION,
358 INTERNAL_MEM_BINARY_PROTOCOL,
359 INTERNAL_MEM_TEMPORARY,
360 INTERNAL_MEM_LOG_ENTRY,
361 INTERNAL_MEM_COMPLEX_DESCRIPTORS,
362 INTERNAL_MEM_FIRST_CLIENT
365 enum {
366 GENERATION_NURSERY,
367 GENERATION_OLD,
368 GENERATION_MAX
371 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
372 #define BINARY_PROTOCOL_ARG(x) ,x
373 #else
374 #define BINARY_PROTOCOL_ARG(x)
375 #endif
377 void sgen_init_internal_allocator (void);
379 #define SGEN_DEFINE_OBJECT_VTABLE
380 #ifdef SGEN_CLIENT_HEADER
381 #include SGEN_CLIENT_HEADER
382 #else
383 #include "metadata/sgen-client-mono.h"
384 #endif
385 #undef SGEN_DEFINE_OBJECT_VTABLE
387 #include "mono/sgen/sgen-descriptor.h"
388 #include "mono/sgen/sgen-gray.h"
390 /* the runtime can register areas of memory as roots: we keep two lists of roots,
391 * a pinned root set for conservatively scanned roots and a normal one for
392 * precisely scanned roots (currently implemented as a single list).
394 typedef struct _RootRecord RootRecord;
395 struct _RootRecord {
396 char *end_root;
397 SgenDescriptor root_desc;
398 int source;
399 const char *msg;
402 enum {
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 */
406 ROOT_TYPE_NUM
409 extern SgenHashTable sgen_roots_hash [ROOT_TYPE_NUM];
411 int sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_type, MonoGCRootSource source, void *key, const char *msg)
412 MONO_PERMIT (need (sgen_lock_gc));
413 void sgen_deregister_root (char* addr)
414 MONO_PERMIT (need (sgen_lock_gc));
416 typedef void (*IterateObjectCallbackFunc) (GCObject*, size_t, void*);
417 typedef gboolean (*IterateObjectResultCallbackFunc) (GCObject*, size_t, void*);
419 void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags, gboolean fail_on_canaries);
421 /* eventually share with MonoThread? */
423 * This structure extends the MonoThreadInfo structure.
425 struct _SgenThreadInfo {
426 SgenClientThreadInfo client_info;
428 char *tlab_start;
429 char *tlab_next;
430 char *tlab_temp_end;
431 char *tlab_real_end;
433 /* Total bytes allocated by this thread in its lifetime so far. */
434 gint64 total_bytes_allocated;
437 gboolean sgen_is_worker_thread (MonoNativeThreadId thread);
439 typedef void (*CopyOrMarkObjectFunc) (GCObject**, SgenGrayQueue*);
440 typedef void (*ScanObjectFunc) (GCObject *obj, SgenDescriptor desc, SgenGrayQueue*);
441 typedef void (*ScanVTypeFunc) (GCObject *full_object, char *start, SgenDescriptor desc, SgenGrayQueue* BINARY_PROTOCOL_ARG (size_t size));
442 typedef void (*ScanPtrFieldFunc) (GCObject *obj, GCObject **ptr, SgenGrayQueue* queue);
443 typedef gboolean (*DrainGrayStackFunc) (SgenGrayQueue *queue);
445 typedef struct {
446 CopyOrMarkObjectFunc copy_or_mark_object;
447 ScanObjectFunc scan_object;
448 ScanVTypeFunc scan_vtype;
449 ScanPtrFieldFunc scan_ptr_field;
450 /* Drain stack optimized for the above functions */
451 DrainGrayStackFunc drain_gray_stack;
452 /*FIXME add allocation function? */
453 } SgenObjectOperations;
455 typedef struct
457 SgenObjectOperations *ops;
458 SgenGrayQueue *queue;
459 } ScanCopyContext;
461 // error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion
462 // An inline function or constructor would work here too.
463 #ifdef _MSC_VER
464 #define MONO_MSC_WARNING_SUPPRESS(warn, body) __pragma (warning (suppress:warn)) body
465 #else
466 #define MONO_MSC_WARNING_SUPPRESS(warn, body) body
467 #endif
468 #define CONTEXT_FROM_OBJECT_OPERATIONS(ops, queue) MONO_MSC_WARNING_SUPPRESS (4576, ((ScanCopyContext) { (ops), (queue) }))
470 void sgen_report_internal_mem_usage (void);
471 void sgen_dump_internal_mem_usage (FILE *heap_dump_file);
472 void sgen_dump_section (GCMemSection *section, const char *type);
473 void sgen_dump_occupied (char *start, char *end, char *section_start);
475 void sgen_register_fixed_internal_mem_type (int type, size_t size);
477 void* sgen_alloc_internal (int type);
478 void sgen_free_internal (void *addr, int type);
480 void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure);
481 void sgen_free_internal_dynamic (void *addr, size_t size, int type);
483 void sgen_pin_stats_enable (void);
484 void sgen_pin_stats_register_object (GCObject *obj, int generation);
485 void sgen_pin_stats_register_global_remset (GCObject *obj);
486 void sgen_pin_stats_report (void);
488 void sgen_gchandle_stats_enable (void);
489 void sgen_gchandle_stats_report (void);
491 void sgen_sort_addresses (void **array, size_t size);
492 void sgen_add_to_global_remset (gpointer ptr, GCObject *obj);
494 int sgen_get_current_collection_generation (void);
495 gboolean sgen_collection_is_concurrent (void);
496 gboolean sgen_get_concurrent_collection_in_progress (void);
498 void sgen_set_bytes_allocated_attached (guint64 bytes);
499 void sgen_increment_bytes_allocated_detached (guint64 bytes);
501 typedef struct _SgenFragment SgenFragment;
503 struct _SgenFragment {
504 SgenFragment *next;
505 char *fragment_start;
506 char *fragment_next; /* the current soft limit for allocation */
507 char *fragment_end;
508 SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
511 typedef struct {
512 SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
513 SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
514 } SgenFragmentAllocator;
516 void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end);
517 void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator);
518 void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size);
519 void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
520 void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
521 SgenFragment* sgen_fragment_allocator_alloc (void);
522 void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator);
523 void sgen_clear_range (char *start, char *end);
527 This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
528 and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
529 L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
531 Experimental results on how much space we waste with a 4Mb nursery:
533 Note that the wastage applies to the half nursery, or 2Mb:
535 Test 1 (compiling corlib):
536 9: avg: 3.1k
537 8: avg: 1.6k
540 #define SGEN_TO_SPACE_GRANULE_BITS 9
541 #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
543 extern char *sgen_space_bitmap;
544 extern size_t sgen_space_bitmap_size;
546 static inline gboolean
547 sgen_nursery_is_to_space (void *object)
549 size_t idx = ((char*)object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
550 size_t byte = idx >> 3;
551 size_t bit = idx & 0x7;
553 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 ());
554 SGEN_ASSERT (4, byte < sgen_space_bitmap_size, "byte index %" G_GSIZE_FORMAT "d out of range (%" G_GSIZE_FORMAT "d)", byte, sgen_space_bitmap_size);
556 return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
559 static inline gboolean
560 sgen_nursery_is_from_space (void *object)
562 return !sgen_nursery_is_to_space (object);
565 static inline gboolean
566 sgen_nursery_is_object_alive (GCObject *obj)
568 /* FIXME put this asserts under a non default level */
569 g_assert (sgen_ptr_in_nursery (obj));
571 if (sgen_nursery_is_to_space (obj))
572 return TRUE;
574 if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
575 return TRUE;
577 return FALSE;
580 typedef struct {
581 gboolean is_split;
582 gboolean is_parallel;
584 GCObject* (*alloc_for_promotion) (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references);
585 GCObject* (*alloc_for_promotion_par) (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references);
587 SgenObjectOperations serial_ops;
588 SgenObjectOperations serial_ops_with_concurrent_major;
589 SgenObjectOperations parallel_ops;
590 SgenObjectOperations parallel_ops_with_concurrent_major;
592 void (*prepare_to_space) (char *to_space_bitmap, size_t space_bitmap_size);
593 void (*clear_fragments) (void);
594 SgenFragment* (*build_fragments_get_exclude_head) (void);
595 void (*build_fragments_release_exclude_head) (void);
596 void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
597 void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
599 gboolean (*handle_gc_param) (const char *opt); /* Optional */
600 void (*print_gc_param_usage) (void); /* Optional */
601 } SgenMinorCollector;
603 extern SgenMinorCollector sgen_minor_collector;
605 void sgen_simple_nursery_init (SgenMinorCollector *collector, gboolean parallel);
606 void sgen_split_nursery_init (SgenMinorCollector *collector);
608 /* Updating references */
610 #ifdef SGEN_CHECK_UPDATE_REFERENCE
611 gboolean sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId some_thread);
613 static inline void
614 sgen_update_reference (GCObject **p, GCObject *o, gboolean allow_null)
616 if (!allow_null)
617 SGEN_ASSERT (0, o, "Cannot update a reference with a NULL pointer");
618 SGEN_ASSERT (0, !sgen_workers_is_worker_thread (mono_native_thread_id_get ()), "Can't update a reference in the worker thread");
619 *p = o;
622 #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o) sgen_update_reference ((GCObject**)(p), (GCObject*)(o), TRUE)
623 #define SGEN_UPDATE_REFERENCE(p,o) sgen_update_reference ((GCObject**)(p), (GCObject*)(o), FALSE)
624 #else
625 #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o) (*(GCObject**)(p) = (GCObject*)(o))
626 #define SGEN_UPDATE_REFERENCE(p,o) SGEN_UPDATE_REFERENCE_ALLOW_NULL ((p), (o))
627 #endif
629 /* Major collector */
631 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
632 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
633 void sgen_major_collector_iterate_block_ranges (sgen_cardtable_block_callback callback);
635 typedef enum {
636 ITERATE_OBJECTS_SWEEP = 1,
637 ITERATE_OBJECTS_NON_PINNED = 2,
638 ITERATE_OBJECTS_PINNED = 4,
639 ITERATE_OBJECTS_SWEEP_NON_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED,
640 ITERATE_OBJECTS_SWEEP_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_PINNED,
641 ITERATE_OBJECTS_SWEEP_ALL = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED | ITERATE_OBJECTS_PINNED
642 } IterateObjectsFlags;
644 typedef struct
646 size_t num_scanned_objects;
647 size_t num_unique_scanned_objects;
648 } ScannedObjectCounts;
650 typedef enum {
651 CARDTABLE_SCAN_GLOBAL = 0,
652 CARDTABLE_SCAN_MOD_UNION = 1,
653 CARDTABLE_SCAN_MOD_UNION_PRECLEAN = CARDTABLE_SCAN_MOD_UNION | 2,
654 } CardTableScanType;
656 typedef struct _SgenMajorCollector SgenMajorCollector;
657 struct _SgenMajorCollector {
658 size_t section_size;
659 gboolean is_concurrent;
660 gboolean is_parallel;
661 gboolean supports_cardtable;
662 gboolean sweeps_lazily;
664 void* (*alloc_heap) (mword nursery_size, mword nursery_align);
665 gboolean (*is_object_live) (GCObject *obj);
666 GCObject* (*alloc_small_pinned_obj) (GCVTable vtable, size_t size, gboolean has_references);
667 GCObject* (*alloc_degraded) (GCVTable vtable, size_t size);
669 SgenObjectOperations major_ops_serial;
670 SgenObjectOperations major_ops_concurrent_start;
671 SgenObjectOperations major_ops_concurrent_finish;
672 SgenObjectOperations major_ops_conc_par_start;
673 SgenObjectOperations major_ops_conc_par_finish;
675 GCObject* (*alloc_object) (GCVTable vtable, size_t size, gboolean has_references);
676 GCObject* (*alloc_object_par) (GCVTable vtable, size_t size, gboolean has_references);
677 void (*free_pinned_object) (GCObject *obj, size_t size);
680 * This is used for domain unloading, heap walking from the logging profiler, and
681 * debugging. Can assume the world is stopped.
683 void (*iterate_objects) (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data);
685 void (*free_non_pinned_object) (GCObject *obj, size_t size);
686 void (*pin_objects) (SgenGrayQueue *queue);
687 void (*pin_major_object) (GCObject *obj, SgenGrayQueue *queue);
688 void (*scan_card_table) (CardTableScanType scan_type, ScanCopyContext ctx, int job_index, int job_split_count, int block_count);
689 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
690 void (*iterate_block_ranges) (sgen_cardtable_block_callback callback);
691 void (*update_cardtable_mod_union) (void);
692 void (*init_to_space) (void);
693 void (*sweep) (void);
694 gboolean (*have_swept) (void);
695 void (*finish_sweeping) (void);
696 void (*free_swept_blocks) (size_t section_reserve);
697 void (*check_scan_starts) (void);
698 void (*dump_heap) (FILE *heap_dump_file);
699 gint64 (*get_used_size) (void);
700 void (*start_nursery_collection) (void);
701 void (*finish_nursery_collection) (void);
702 void (*start_major_collection) (void);
703 void (*finish_major_collection) (ScannedObjectCounts *counts);
704 gboolean (*ptr_is_in_non_pinned_space) (char *ptr, char **start);
705 gboolean (*ptr_is_from_pinned_alloc) (char *ptr);
706 void (*report_pinned_memory_usage) (void);
707 size_t (*get_num_major_sections) (void);
708 size_t (*get_bytes_survived_last_sweep) (void);
709 gboolean (*handle_gc_param) (const char *opt);
710 void (*print_gc_param_usage) (void);
711 void (*post_param_init) (SgenMajorCollector *collector);
712 gboolean (*is_valid_object) (char *ptr);
713 GCVTable (*describe_pointer) (char *pointer);
714 guint8* (*get_cardtable_mod_union_for_reference) (char *object);
715 long long (*get_and_reset_num_major_objects_marked) (void);
716 void (*count_cards) (long long *num_total_cards, long long *num_marked_cards);
717 void (*init_block_free_lists) (gpointer *list_p);
720 extern SgenMajorCollector sgen_major_collector;
722 void sgen_marksweep_init (SgenMajorCollector *collector);
723 void sgen_marksweep_conc_init (SgenMajorCollector *collector);
724 void sgen_marksweep_conc_par_init (SgenMajorCollector *collector);
725 SgenMajorCollector* sgen_get_major_collector (void);
726 SgenMinorCollector* sgen_get_minor_collector (void);
729 typedef struct _SgenRememberedSet {
730 void (*wbarrier_set_field) (GCObject *obj, gpointer field_ptr, GCObject* value);
731 void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gconstpointer src_ptr, int count);
732 void (*wbarrier_value_copy) (gpointer dest, gconstpointer src, int count, size_t element_size);
733 void (*wbarrier_object_copy) (GCObject* obj, GCObject *src);
734 void (*wbarrier_generic_nostore) (gpointer ptr);
735 void (*record_pointer) (gpointer ptr);
736 void (*wbarrier_range_copy) (gpointer dest, gconstpointer src, int count);
738 void (*start_scan_remsets) (void);
740 void (*clear_cards) (void);
742 gboolean (*find_address) (char *addr);
743 gboolean (*find_address_with_cards) (char *cards_start, guint8 *cards, char *addr);
744 } SgenRememberedSet;
746 typedef struct _SgenGCInfo {
747 guint64 fragmented_bytes;
748 guint64 heap_size_bytes;
749 guint64 high_memory_load_threshold_bytes;
750 guint64 memory_load_bytes;
751 guint64 total_available_memory_bytes;
752 } SgenGCInfo;
754 extern SgenGCInfo sgen_gc_info;
756 SgenRememberedSet *sgen_get_remset (void);
759 * These must be kept in sync with object.h. They're here for using SGen independently of
760 * Mono.
762 void mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, /*const*/ void* src_ptr, int count);
763 void mono_gc_wbarrier_generic_nostore (gpointer ptr);
764 void mono_gc_wbarrier_generic_store (gpointer ptr, GCObject* value);
765 void mono_gc_wbarrier_generic_store_atomic (gpointer ptr, GCObject *value);
767 void sgen_wbarrier_range_copy (gpointer _dest, gconstpointer _src, int size);
769 static inline SgenDescriptor
770 sgen_obj_get_descriptor (GCObject *obj)
772 GCVTable vtable = SGEN_LOAD_VTABLE_UNCHECKED (obj);
773 SGEN_ASSERT (9, !SGEN_POINTER_IS_TAGGED_ANY (vtable), "Object can't be tagged");
774 return sgen_vtable_get_descriptor (vtable);
777 static inline SgenDescriptor
778 sgen_obj_get_descriptor_safe (GCObject *obj)
780 GCVTable vtable = SGEN_LOAD_VTABLE (obj);
781 return sgen_vtable_get_descriptor (vtable);
784 static mword sgen_client_par_object_get_size (GCVTable vtable, GCObject* o);
785 static mword sgen_client_slow_object_get_size (GCVTable vtable, GCObject* o);
787 static inline mword
788 sgen_safe_object_get_size (GCObject *obj)
790 GCObject *forwarded;
791 GCVTable vtable = SGEN_LOAD_VTABLE_UNCHECKED (obj);
794 * Once we load the vtable, we must always use it, in case we are in parallel case.
795 * Otherwise the object might get forwarded in the meantime and we would read an
796 * invalid vtable. An object cannot be forwarded for a second time during same GC.
798 if ((forwarded = SGEN_VTABLE_IS_FORWARDED (vtable)))
799 return sgen_client_par_object_get_size (SGEN_LOAD_VTABLE (forwarded), obj);
800 else
801 return sgen_client_par_object_get_size ((GCVTable)SGEN_POINTER_UNTAG_ALL (vtable), obj);
804 static inline gboolean
805 sgen_safe_object_is_small (GCObject *obj, int type)
807 if (type <= DESC_TYPE_MAX_SMALL_OBJ)
808 return TRUE;
809 return SGEN_ALIGN_UP (sgen_safe_object_get_size ((GCObject*)obj)) <= SGEN_MAX_SMALL_OBJ_SIZE;
813 * This variant guarantees to return the exact size of the object
814 * before alignment. Needed for canary support.
816 static inline guint
817 sgen_safe_object_get_size_unaligned (GCObject *obj)
819 GCObject *forwarded;
821 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
822 obj = (GCObject*)forwarded;
825 return sgen_client_slow_object_get_size (SGEN_LOAD_VTABLE (obj), obj);
828 #ifdef SGEN_CLIENT_HEADER
829 #include SGEN_CLIENT_HEADER
830 #else
831 #include "metadata/sgen-client-mono.h"
832 #endif
834 gboolean sgen_object_is_live (GCObject *obj);
836 void sgen_init_fin_weak_hash (void);
838 void sgen_register_obj_with_weak_fields (GCObject *obj);
840 /* FIXME: move the toggleref stuff out of here */
841 void sgen_mark_togglerefs (char *start, char *end, ScanCopyContext ctx);
842 void sgen_clear_togglerefs (char *start, char *end, ScanCopyContext ctx);
844 void sgen_process_togglerefs (void);
845 void sgen_register_test_toggleref_callback (void);
847 void sgen_mark_bridge_object (GCObject *obj)
848 MONO_PERMIT (need (sgen_gc_locked));
849 void sgen_collect_bridge_objects (int generation, ScanCopyContext ctx)
850 MONO_PERMIT (need (sgen_gc_locked));
852 typedef gboolean (*SgenObjectPredicateFunc) (GCObject *obj, void *user_data);
854 void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track)
855 MONO_PERMIT (need (sgen_gc_locked, sgen_world_stopped));
857 gboolean sgen_gc_is_object_ready_for_finalization (GCObject *object);
858 void sgen_gc_lock (void) MONO_PERMIT (use (sgen_lock_gc), grant (sgen_gc_locked), revoke (sgen_lock_gc));
859 void sgen_gc_unlock (void) MONO_PERMIT (use (sgen_gc_locked), revoke (sgen_gc_locked), grant (sgen_lock_gc));
861 void sgen_queue_finalization_entry (GCObject *obj);
862 const char* sgen_generation_name (int generation);
864 void sgen_finalize_in_range (int generation, ScanCopyContext ctx)
865 MONO_PERMIT (need (sgen_gc_locked));
866 void sgen_null_link_in_range (int generation, ScanCopyContext ctx, gboolean track)
867 MONO_PERMIT (need (sgen_gc_locked, sgen_world_stopped));
868 void sgen_process_fin_stage_entries (void)
869 MONO_PERMIT (need (sgen_gc_locked));
870 gboolean sgen_have_pending_finalizers (void);
872 typedef void (*SGenFinalizationProc)(gpointer, gpointer); // same as MonoFinalizationProc, GC_finalization_proc
874 void sgen_object_register_for_finalization (GCObject *obj, SGenFinalizationProc user_data)
875 MONO_PERMIT (need (sgen_lock_gc));
877 void sgen_finalize_if (SgenObjectPredicateFunc predicate, void *user_data)
878 MONO_PERMIT (need (sgen_lock_gc));
879 void sgen_remove_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, int generation);
880 void sgen_set_suspend_finalizers (void);
882 void sgen_wbroots_iterate_live_block_ranges (sgen_cardtable_block_callback cb);
883 void sgen_wbroots_scan_card_table (ScanCopyContext ctx);
885 void sgen_register_disappearing_link (GCObject *obj, void **link, gboolean track, gboolean in_gc);
887 GCObject* sgen_weak_link_get (void **link_addr);
889 gboolean sgen_drain_gray_stack (ScanCopyContext ctx);
891 enum {
892 SPACE_NURSERY,
893 SPACE_MAJOR,
894 SPACE_LOS
897 void sgen_pin_object (GCObject *object, SgenGrayQueue *queue);
898 void sgen_set_pinned_from_failed_allocation (mword objsize);
900 void sgen_ensure_free_space (size_t size, int generation)
901 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
902 void sgen_gc_collect (int generation)
903 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
904 void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish, gboolean stw)
905 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
907 int sgen_gc_collection_count (int generation);
908 /* FIXME: what exactly does this return? */
909 size_t sgen_gc_get_used_size (void)
910 MONO_PERMIT (need (sgen_lock_gc));
911 size_t sgen_gc_get_total_heap_allocation (void);
913 /* STW */
915 void sgen_stop_world (int generation, gboolean serial_collection)
916 MONO_PERMIT (need (sgen_gc_locked), use (sgen_stop_world), grant (sgen_world_stopped), revoke (sgen_stop_world));
917 void sgen_restart_world (int generation, gboolean serial_collection)
918 MONO_PERMIT (need (sgen_gc_locked), use (sgen_world_stopped), revoke (sgen_world_stopped), grant (sgen_stop_world));
919 gboolean sgen_is_world_stopped (void);
921 gboolean sgen_set_allow_synchronous_major (gboolean flag);
923 /* LOS */
925 typedef struct _LOSObject LOSObject;
926 struct _LOSObject {
927 mword size; /* this is the object size, lowest bit used for pin/mark */
928 guint8 * volatile cardtable_mod_union; /* only used by the concurrent collector */
929 GCObject data [MONO_ZERO_LEN_ARRAY];
932 extern mword sgen_los_memory_usage;
933 extern mword sgen_los_memory_usage_total;
935 void sgen_los_free_object (LOSObject *obj);
936 void* sgen_los_alloc_large_inner (GCVTable vtable, size_t size)
937 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
938 void sgen_los_sweep (void);
939 gboolean sgen_ptr_is_in_los (char *ptr, char **start);
940 void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data);
941 void sgen_los_iterate_objects_free (IterateObjectResultCallbackFunc cb, void *user_data);
942 void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
943 void sgen_los_scan_card_table (CardTableScanType scan_type, ScanCopyContext ctx, int job_index, int job_split_count);
944 void sgen_los_update_cardtable_mod_union (void);
945 void sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards);
946 gboolean sgen_los_is_valid_object (char *object);
947 gboolean mono_sgen_los_describe_pointer (char *ptr);
948 LOSObject* sgen_los_header_for_object (GCObject *data);
949 mword sgen_los_object_size (LOSObject *obj);
950 void sgen_los_pin_object (GCObject *obj);
951 void sgen_los_pin_objects (SgenGrayQueue *gray_queue, gboolean finish_concurrent_mode);
952 gboolean sgen_los_pin_object_par (GCObject *obj);
953 gboolean sgen_los_object_is_pinned (GCObject *obj);
954 void sgen_los_mark_mod_union_card (GCObject *mono_obj, void **ptr);
957 /* nursery allocator */
959 void sgen_clear_nursery_fragments (void);
960 void sgen_nursery_allocator_prepare_for_pinning (void);
961 void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, size_t min_size, size_t max_size);
962 void sgen_resize_nursery (gboolean need_shrink);
963 mword sgen_build_nursery_fragments (GCMemSection *nursery_section);
964 void sgen_init_nursery_allocator (void);
965 void sgen_nursery_allocator_init_heavy_stats (void);
966 void sgen_init_allocator (void);
967 void* sgen_nursery_alloc (size_t size);
968 void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size);
969 gboolean sgen_can_alloc_size (size_t size);
970 void sgen_nursery_retire_region (void *address, ptrdiff_t size);
972 void sgen_nursery_alloc_prepare_for_minor (void);
973 void sgen_nursery_alloc_prepare_for_major (void);
975 GCObject* sgen_alloc_for_promotion (GCObject *obj, size_t objsize, gboolean has_references);
977 GCObject* sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
978 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
979 GCObject* sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size);
981 /* Threads */
983 void* sgen_thread_attach (SgenThreadInfo* info);
984 void sgen_thread_detach_with_lock (SgenThreadInfo *p);
986 /* Finalization/ephemeron support */
988 static inline gboolean
989 sgen_major_is_object_alive (GCObject *object)
991 mword objsize;
993 /* Oldgen objects can be pinned and forwarded too */
994 if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
995 return TRUE;
998 * FIXME: major_collector.is_object_live() also calculates the
999 * size. Avoid the double calculation.
1001 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size (object));
1002 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
1003 return sgen_los_object_is_pinned (object);
1005 return sgen_major_collector.is_object_live (object);
1010 * If the object has been forwarded it means it's still referenced from a root.
1011 * If it is pinned it's still alive as well.
1012 * A LOS object is only alive if we have pinned it.
1013 * Return TRUE if @obj is ready to be finalized.
1015 static inline gboolean
1016 sgen_is_object_alive (GCObject *object)
1018 if (sgen_ptr_in_nursery (object))
1019 return sgen_nursery_is_object_alive (object);
1021 return sgen_major_is_object_alive (object);
1026 * This function returns true if @object is either alive or it belongs to the old gen
1027 * and we're currently doing a minor collection.
1029 static inline int
1030 sgen_is_object_alive_for_current_gen (GCObject *object)
1032 if (sgen_ptr_in_nursery (object))
1033 return sgen_nursery_is_object_alive (object);
1035 if (sgen_current_collection_generation == GENERATION_NURSERY)
1036 return TRUE;
1038 return sgen_major_is_object_alive (object);
1041 int sgen_gc_invoke_finalizers (void)
1042 MONO_PERMIT (need (sgen_lock_gc));
1044 /* GC handles */
1046 void sgen_init_gchandles (void);
1048 void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track)
1049 MONO_PERMIT (need (sgen_gc_locked));
1051 typedef gpointer (*SgenGCHandleIterateCallback) (gpointer hidden, GCHandleType handle_type, int max_generation, gpointer user);
1053 guint32 sgen_gchandle_new (GCObject *obj, gboolean pinned);
1054 guint32 sgen_gchandle_new_weakref (GCObject *obj, gboolean track_resurrection);
1055 void sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, SgenGCHandleIterateCallback callback, gpointer user)
1056 MONO_PERMIT (need (sgen_world_stopped));
1057 void sgen_gchandle_set_target (guint32 gchandle, GCObject *obj);
1058 void sgen_mark_normal_gc_handles (void *addr, SgenUserMarkFunc mark_func, void *gc_data)
1059 MONO_PERMIT (need (sgen_world_stopped));
1060 void sgen_gc_handles_report_roots (SgenUserReportRootFunc mark_func, void *gc_data)
1061 MONO_PERMIT (need (sgen_world_stopped));
1062 gpointer sgen_gchandle_get_metadata (guint32 gchandle);
1063 GCObject *sgen_gchandle_get_target (guint32 gchandle);
1064 void sgen_gchandle_free (guint32 gchandle);
1066 /* Other globals */
1068 extern GCMemSection *sgen_nursery_section;
1069 extern guint32 sgen_collect_before_allocs;
1070 extern guint32 sgen_verify_before_allocs;
1071 extern gboolean sgen_has_per_allocation_action;
1072 extern size_t sgen_degraded_mode;
1073 extern int default_nursery_size;
1074 extern guint32 sgen_tlab_size;
1075 extern NurseryClearPolicy sgen_nursery_clear_policy;
1076 extern gboolean sgen_try_free_some_memory;
1077 extern mword sgen_total_promoted_size;
1078 extern mword sgen_total_allocated_major;
1079 extern volatile gboolean sgen_suspend_finalizers;
1080 extern MonoCoopMutex sgen_gc_mutex;
1081 extern volatile gboolean sgen_concurrent_collection_in_progress;
1083 /* Nursery helpers. */
1085 static inline void
1086 sgen_set_nursery_scan_start (char *p)
1088 size_t idx = (p - (char*)sgen_nursery_section->data) / SGEN_SCAN_START_SIZE;
1089 char *old = sgen_nursery_section->scan_starts [idx];
1090 if (!old || old > p)
1091 sgen_nursery_section->scan_starts [idx] = p;
1095 /* Object Allocation */
1097 typedef enum {
1098 ATYPE_NORMAL,
1099 ATYPE_VECTOR,
1100 ATYPE_SMALL,
1101 ATYPE_STRING,
1102 ATYPE_NUM
1103 } SgenAllocatorType;
1105 void sgen_clear_tlabs (void);
1106 void sgen_update_allocation_count (void);
1107 guint64 sgen_get_total_allocated_bytes (MonoBoolean precise);
1109 GCObject* sgen_alloc_obj (GCVTable vtable, size_t size)
1110 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1111 GCObject* sgen_alloc_obj_pinned (GCVTable vtable, size_t size)
1112 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1113 GCObject* sgen_alloc_obj_mature (GCVTable vtable, size_t size)
1114 MONO_PERMIT (need (sgen_lock_gc, sgen_stop_world));
1116 /* Debug support */
1118 void sgen_check_remset_consistency (void);
1119 void sgen_check_mod_union_consistency (void);
1120 void sgen_check_major_refs (void);
1121 void sgen_check_whole_heap (gboolean allow_missing_pinning);
1122 void sgen_check_whole_heap_stw (void)
1123 MONO_PERMIT (need (sgen_gc_locked, sgen_stop_world));
1124 void sgen_check_objref (char *obj);
1125 void sgen_check_heap_marked (gboolean nursery_must_be_pinned);
1126 void sgen_check_nursery_objects_untag (void);
1127 void sgen_check_for_xdomain_refs (void);
1128 GCObject* sgen_find_object_for_ptr (char *ptr);
1130 void mono_gc_scan_for_specific_ref (GCObject *key, gboolean precise);
1132 void sgen_debug_enable_heap_dump (const char *filename);
1133 void sgen_debug_dump_heap (const char *type, int num, const char *reason);
1135 void sgen_debug_verify_nursery (gboolean do_dump_nursery_content);
1136 void sgen_debug_check_nursery_is_clean (void);
1138 /* Environment variable parsing */
1140 #define MONO_GC_PARAMS_NAME "MONO_GC_PARAMS"
1141 #define MONO_GC_DEBUG_NAME "MONO_GC_DEBUG"
1143 void sgen_env_var_error (const char *env_var, const char *fallback, const char *description_format, ...);
1145 /* Utilities */
1147 void sgen_qsort (void *const array, const size_t count, const size_t element_size, int (*compare) (const void*, const void*));
1148 gint64 sgen_timestamp (void);
1151 * Canary (guard word) support
1152 * Notes:
1153 * - CANARY_SIZE must be multiple of word size in bytes
1154 * - Canary space is not included on checks against SGEN_MAX_SMALL_OBJ_SIZE
1157 gboolean sgen_nursery_canaries_enabled (void);
1159 #define CANARY_SIZE 8
1160 #define CANARY_STRING "koupepia"
1162 #define CANARIFY_SIZE(size) if (sgen_nursery_canaries_enabled ()) { \
1163 size = size + CANARY_SIZE; \
1166 #define CANARIFY_ALLOC(addr,size) if (sgen_nursery_canaries_enabled ()) { \
1167 memcpy ((char*) (addr) + (size), CANARY_STRING, CANARY_SIZE); \
1170 #define CANARY_VALID(addr) (strncmp ((char*) (addr), CANARY_STRING, CANARY_SIZE) == 0)
1172 void
1173 sgen_check_canary_for_object (gpointer addr);
1175 guint64 sgen_get_precise_allocation_count (void);
1177 #define CHECK_CANARY_FOR_OBJECT(addr, ignored) \
1178 (sgen_nursery_canaries_enabled () ? sgen_check_canary_for_object (addr) : (void)0)
1181 * This causes the compile to extend the liveness of 'v' till the call to dummy_use
1183 static inline void
1184 sgen_dummy_use (gpointer v)
1186 #if defined(_MSC_VER) || defined(HOST_WASM)
1187 static volatile gpointer ptr;
1188 ptr = v;
1189 #elif defined(__GNUC__)
1190 __asm__ volatile ("" : "=r"(v) : "r"(v));
1191 #else
1192 #error "Implement sgen_dummy_use for your compiler"
1193 #endif
1196 #endif /* HAVE_SGEN_GC */
1198 #endif /* __MONO_SGENGC_H__ */