Update test
[mono-project.git] / mono / metadata / sgen-gc.h
blobb3a6b707ecacc6f872234243a5b7d5f9bc0a5053
1 /*
2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
4 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
5 *
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__
28 /* pthread impl */
29 #include "config.h"
31 #ifdef HAVE_SGEN_GC
33 typedef struct _SgenThreadInfo SgenThreadInfo;
34 #define THREAD_INFO_TYPE SgenThreadInfo
36 #include <glib.h>
37 #ifdef HAVE_PTHREAD_H
38 #include <pthread.h>
39 #endif
40 #include <signal.h>
41 #include <mono/utils/mono-compiler.h>
42 #include <mono/utils/mono-threads.h>
43 #include <mono/io-layer/mono-mutex.h>
44 #include <mono/metadata/class-internals.h>
45 #include <mono/metadata/object-internals.h>
46 #include <mono/metadata/sgen-conf.h>
47 #include <mono/metadata/sgen-archdep.h>
48 #include <mono/metadata/sgen-descriptor.h>
49 #include <mono/metadata/sgen-gray.h>
51 #if defined(__MACH__)
52 #include <mach/mach_port.h>
53 #endif
55 /* The method used to clear the nursery */
56 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
57 * Clearing at TLAB creation is much faster, but more complex and it might expose hard
58 * to find bugs.
60 typedef enum {
61 CLEAR_AT_GC,
62 CLEAR_AT_TLAB_CREATION
63 } NurseryClearPolicy;
65 NurseryClearPolicy mono_sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
67 #define SGEN_TV_DECLARE(name) gint64 name
68 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
69 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
70 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
72 /* for use with write barriers */
73 typedef struct _RememberedSet RememberedSet;
74 struct _RememberedSet {
75 mword *store_next;
76 mword *end_set;
77 RememberedSet *next;
78 mword data [MONO_ZERO_LEN_ARRAY];
82 * We're never actually using the first element. It's always set to
83 * NULL to simplify the elimination of consecutive duplicate
84 * entries.
86 #define STORE_REMSET_BUFFER_SIZE 1023
88 typedef struct _GenericStoreRememberedSet GenericStoreRememberedSet;
89 struct _GenericStoreRememberedSet {
90 GenericStoreRememberedSet *next;
91 /* We need one entry less because the first entry of store
92 remset buffers is always a dummy and we don't copy it. */
93 gpointer data [STORE_REMSET_BUFFER_SIZE - 1];
96 /* we have 4 possible values in the low 2 bits */
97 enum {
98 REMSET_LOCATION, /* just a pointer to the exact location */
99 REMSET_RANGE, /* range of pointer fields */
100 REMSET_OBJECT, /* mark all the object for scanning */
101 REMSET_VTYPE, /* a valuetype array described by a gc descriptor, a count and a size */
102 REMSET_TYPE_MASK = 0x3
105 /* eventually share with MonoThread? */
106 struct _SgenThreadInfo {
107 MonoThreadInfo info;
108 #if defined(__MACH__)
109 thread_port_t mach_port;
110 #else
111 int signal;
112 unsigned int stop_count; /* to catch duplicate signals */
113 #endif
114 int skip;
115 volatile int in_critical_region;
116 gboolean doing_handshake;
117 gboolean thread_is_dying;
118 gboolean gc_disabled;
119 void *stack_end;
120 void *stack_start;
121 void *stack_start_limit;
122 char **tlab_next_addr;
123 char **tlab_start_addr;
124 char **tlab_temp_end_addr;
125 char **tlab_real_end_addr;
126 gpointer **store_remset_buffer_addr;
127 long *store_remset_buffer_index_addr;
128 RememberedSet *remset;
129 gpointer runtime_data;
130 gpointer stopped_ip; /* only valid if the thread is stopped */
131 MonoDomain *stopped_domain; /* ditto */
133 #ifdef USE_MONO_CTX
134 #ifdef __MACH__
135 MonoContext ctx; /* ditto */
136 #endif
137 MonoContext *monoctx; /* ditto */
139 #else
141 #if defined(__MACH__) || defined(HOST_WIN32)
142 gpointer regs[ARCH_NUM_REGS]; /* ditto */
143 #endif
144 gpointer *stopped_regs; /* ditto */
145 #endif
147 #ifndef HAVE_KW_THREAD
148 char *tlab_start;
149 char *tlab_next;
150 char *tlab_temp_end;
151 char *tlab_real_end;
152 gpointer *store_remset_buffer;
153 long store_remset_buffer_index;
154 #endif
157 enum {
158 MEMORY_ROLE_GEN0,
159 MEMORY_ROLE_GEN1,
160 MEMORY_ROLE_PINNED
163 typedef struct _SgenBlock SgenBlock;
164 struct _SgenBlock {
165 void *next;
166 unsigned char role;
170 * The nursery section and the major copying collector's sections use
171 * this struct.
173 typedef struct _GCMemSection GCMemSection;
174 struct _GCMemSection {
175 SgenBlock block;
176 char *data;
177 mword size;
178 /* pointer where more data could be allocated if it fits */
179 char *next_data;
180 char *end_data;
182 * scan starts is an array of pointers to objects equally spaced in the allocation area
183 * They let use quickly find pinned objects from pinning pointers.
185 char **scan_starts;
186 /* in major collections indexes in the pin_queue for objects that pin this section */
187 void **pin_queue_start;
188 int pin_queue_num_entries;
189 unsigned short num_scan_start;
190 gboolean is_to_space;
193 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
195 typedef struct _SgenPinnedChunk SgenPinnedChunk;
198 * Recursion is not allowed for the thread lock.
200 #define LOCK_DECLARE(name) mono_mutex_t name
201 /* if changing LOCK_INIT to something that isn't idempotent, look at
202 its use in mono_gc_base_init in sgen-gc.c */
203 #define LOCK_INIT(name) mono_mutex_init (&(name), NULL)
204 #define LOCK_GC mono_mutex_lock (&gc_mutex)
205 #define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
206 #define UNLOCK_GC mono_mutex_unlock (&gc_mutex)
207 #define LOCK_INTERRUPTION mono_mutex_lock (&interruption_mutex)
208 #define UNLOCK_INTERRUPTION mono_mutex_unlock (&interruption_mutex)
210 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
211 #define SGEN_ATOMIC_ADD(x,i) do { \
212 int __old_x; \
213 do { \
214 __old_x = (x); \
215 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
216 } while (0)
218 #ifndef HOST_WIN32
219 /* we intercept pthread_create calls to know which threads exist */
220 #define USE_PTHREAD_INTERCEPT 1
221 #endif
223 #ifdef HEAVY_STATISTICS
224 #define HEAVY_STAT(x) x
226 extern long long stat_objects_alloced_degraded;
227 extern long long stat_bytes_alloced_degraded;
228 extern long long stat_copy_object_called_major;
229 extern long long stat_objects_copied_major;
230 #else
231 #define HEAVY_STAT(x)
232 #endif
234 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) { a; fflush (gc_debug_file); } } while (0)
236 extern int gc_debug_level;
237 extern FILE* gc_debug_file;
239 extern int current_collection_generation;
241 extern unsigned int mono_sgen_global_stop_count;
243 #define SGEN_ALLOC_ALIGN 8
244 #define SGEN_ALLOC_ALIGN_BITS 3
246 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
248 #ifdef SGEN_ALIGN_NURSERY
249 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
250 #else
251 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
252 #endif
254 #ifdef USER_CONFIG
256 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
257 #define DEFAULT_NURSERY_SIZE (mono_sgen_nursery_size)
258 extern int mono_sgen_nursery_size MONO_INTERNAL;
259 #ifdef SGEN_ALIGN_NURSERY
260 /* The number of trailing 0 bits in DEFAULT_NURSERY_SIZE */
261 #define DEFAULT_NURSERY_BITS (mono_sgen_nursery_bits)
262 extern int mono_sgen_nursery_bits MONO_INTERNAL;
263 #endif
265 #else
267 #define DEFAULT_NURSERY_SIZE (4*1024*1024)
268 #ifdef SGEN_ALIGN_NURSERY
269 #define DEFAULT_NURSERY_BITS 22
270 #endif
272 #endif
274 #ifndef SGEN_ALIGN_NURSERY
275 #define DEFAULT_NURSERY_BITS -1
276 #endif
278 extern char *mono_sgen_nursery_start MONO_INTERNAL;
279 extern char *mono_sgen_nursery_end MONO_INTERNAL;
281 static inline gboolean
282 mono_sgen_ptr_in_nursery (void *p)
284 return SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, mono_sgen_nursery_start, mono_sgen_nursery_end);
287 static inline char*
288 mono_sgen_get_nursery_start (void)
290 return mono_sgen_nursery_start;
293 static inline char*
294 mono_sgen_get_nursery_end (void)
296 return mono_sgen_nursery_end;
300 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
301 * no cast from a pointer to an integer
303 typedef struct {
304 MonoClass *klass;
305 mword desc;
306 } GCVTable;
308 /* these bits are set in the object vtable: we could merge them since an object can be
309 * either pinned or forwarded but not both.
310 * We store them in the vtable slot because the bits are used in the sync block for
311 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
312 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
313 * would be an invalid combination for the monitor and hash code).
314 * The values are already shifted.
315 * The forwarding address is stored in the sync block.
317 #define SGEN_FORWARDED_BIT 1
318 #define SGEN_PINNED_BIT 2
319 #define SGEN_VTABLE_BITS_MASK 0x3
321 /* returns NULL if not forwarded, or the forwarded address */
322 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
323 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
325 /* set the forwarded address fw_addr for object obj */
326 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
327 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
328 } while (0)
329 #define SGEN_PIN_OBJECT(obj) do { \
330 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
331 } while (0)
332 #define SGEN_UNPIN_OBJECT(obj) do { \
333 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
334 } while (0)
337 * Since we set bits in the vtable, use the macro to load it from the pointer to
338 * an object that is potentially pinned.
340 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
342 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
343 typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
344 typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue*);
346 #if SGEN_MAX_DEBUG_LEVEL >= 9
347 #define GRAY_OBJECT_ENQUEUE mono_sgen_gray_object_enqueue
348 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = mono_sgen_gray_object_dequeue ((queue)))
349 #else
350 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
351 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
352 mono_sgen_gray_object_enqueue ((queue), (o)); \
353 else \
354 (queue)->first->objects [(queue)->first->end++] = (o); \
355 PREFETCH ((o)); \
356 } while (0)
357 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
358 if (!(queue)->first) \
359 (o) = NULL; \
360 else if (G_UNLIKELY ((queue)->first->end == 1)) \
361 (o) = mono_sgen_gray_object_dequeue ((queue)); \
362 else \
363 (o) = (queue)->first->objects [--(queue)->first->end]; \
364 } while (0)
365 #endif
368 List of what each bit on of the vtable gc bits means.
370 enum {
371 SGEN_GC_BIT_BRIDGE_OBJECT = 1,
374 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
376 void* mono_sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
377 void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
378 void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
380 int mono_sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
381 gboolean mono_sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
382 gboolean mono_sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
383 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
384 gboolean mono_sgen_park_current_thread_if_doing_handshake (SgenThreadInfo *p) MONO_INTERNAL;
385 void mono_sgen_os_init (void) MONO_INTERNAL;
387 void mono_sgen_fill_thread_info_for_suspend (SgenThreadInfo *info) MONO_INTERNAL;
389 gboolean mono_sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
391 void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
393 void mono_sgen_register_major_sections_alloced (int num_sections) MONO_INTERNAL;
394 mword mono_sgen_get_minor_collection_allowance (void) MONO_INTERNAL;
396 void mono_sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
397 void mono_sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
399 /* Keep in sync with mono_sgen_dump_internal_mem_usage() in dump_heap()! */
400 enum {
401 INTERNAL_MEM_PIN_QUEUE,
402 INTERNAL_MEM_FRAGMENT,
403 INTERNAL_MEM_SECTION,
404 INTERNAL_MEM_SCAN_STARTS,
405 INTERNAL_MEM_FIN_TABLE,
406 INTERNAL_MEM_FINALIZE_ENTRY,
407 INTERNAL_MEM_FINALIZE_READY_ENTRY,
408 INTERNAL_MEM_DISLINK_TABLE,
409 INTERNAL_MEM_DISLINK,
410 INTERNAL_MEM_ROOTS_TABLE,
411 INTERNAL_MEM_ROOT_RECORD,
412 INTERNAL_MEM_STATISTICS,
413 INTERNAL_MEM_STAT_PINNED_CLASS,
414 INTERNAL_MEM_STAT_REMSET_CLASS,
415 INTERNAL_MEM_REMSET,
416 INTERNAL_MEM_GRAY_QUEUE,
417 INTERNAL_MEM_STORE_REMSET,
418 INTERNAL_MEM_MS_TABLES,
419 INTERNAL_MEM_MS_BLOCK_INFO,
420 INTERNAL_MEM_EPHEMERON_LINK,
421 INTERNAL_MEM_WORKER_DATA,
422 INTERNAL_MEM_BRIDGE_DATA,
423 INTERNAL_MEM_JOB_QUEUE_ENTRY,
424 INTERNAL_MEM_TOGGLEREF_DATA,
425 INTERNAL_MEM_MAX
428 #define SGEN_PINNED_FREELIST_NUM_SLOTS 30
430 typedef struct {
431 SgenPinnedChunk *chunk_list;
432 SgenPinnedChunk *free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
433 void *delayed_free_lists [SGEN_PINNED_FREELIST_NUM_SLOTS];
434 } SgenPinnedAllocator;
436 enum {
437 GENERATION_NURSERY,
438 GENERATION_OLD,
439 GENERATION_MAX
442 void mono_sgen_init_internal_allocator (void) MONO_INTERNAL;
443 void mono_sgen_init_pinned_allocator (void) MONO_INTERNAL;
445 typedef struct _ObjectList ObjectList;
446 struct _ObjectList {
447 MonoObject *obj;
448 ObjectList *next;
451 void mono_sgen_report_internal_mem_usage (void) MONO_INTERNAL;
452 void mono_sgen_report_pinned_mem_usage (SgenPinnedAllocator *alc) MONO_INTERNAL;
453 void mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
454 void mono_sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
455 void mono_sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
457 void mono_sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
459 void mono_sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
461 void* mono_sgen_alloc_internal (int type) MONO_INTERNAL;
462 void mono_sgen_free_internal (void *addr, int type) MONO_INTERNAL;
464 void* mono_sgen_alloc_internal_dynamic (size_t size, int type) MONO_INTERNAL;
465 void mono_sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
467 void* mono_sgen_alloc_pinned (SgenPinnedAllocator *allocator, size_t size) MONO_INTERNAL;
468 void mono_sgen_free_pinned (SgenPinnedAllocator *allocator, void *addr, size_t size) MONO_INTERNAL;
471 void mono_sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
473 gboolean mono_sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
475 void mono_sgen_pinned_scan_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
476 void mono_sgen_pinned_scan_pinned_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
478 void mono_sgen_pinned_update_heap_boundaries (SgenPinnedAllocator *alc) MONO_INTERNAL;
480 void** mono_sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
481 void mono_sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
482 void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
484 void mono_sgen_pin_stats_register_object (char *obj, size_t size);
485 void mono_sgen_pin_stats_register_global_remset (char *obj);
486 void mono_sgen_pin_stats_print_class_stats (void);
488 void mono_sgen_sort_addresses (void **array, int size) MONO_INTERNAL;
489 void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
491 int mono_sgen_get_current_collection_generation (void) MONO_INTERNAL;
492 gboolean mono_sgen_nursery_collection_is_parallel (void) MONO_INTERNAL;
493 CopyOrMarkObjectFunc mono_sgen_get_copy_object (void) MONO_INTERNAL;
494 ScanObjectFunc mono_sgen_get_minor_scan_object (void) MONO_INTERNAL;
495 ScanVTypeFunc mono_sgen_get_minor_scan_vtype (void) MONO_INTERNAL;
496 gboolean mono_sgen_collection_is_parallel (void) MONO_INTERNAL;
499 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
500 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
502 typedef struct _SgenMajorCollector SgenMajorCollector;
503 struct _SgenMajorCollector {
504 size_t section_size;
505 gboolean is_parallel;
506 gboolean supports_cardtable;
509 * This is set to TRUE if the sweep for the last major
510 * collection has been completed.
512 gboolean *have_swept;
514 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
515 gboolean (*is_object_live) (char *obj);
516 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
517 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
518 void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
519 void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
520 void (*nopar_minor_scan_object) (char *start, SgenGrayQueue *queue);
521 void (*minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
522 void (*nopar_minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
523 void (*major_scan_object) (char *start, SgenGrayQueue *queue);
524 void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
525 void (*nopar_copy_object) (void **obj_slot, SgenGrayQueue *queue);
526 void* (*alloc_object) (int size, gboolean has_references);
527 void (*free_pinned_object) (char *obj, size_t size);
528 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
529 void (*free_non_pinned_object) (char *obj, size_t size);
530 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
531 void (*pin_objects) (SgenGrayQueue *queue);
532 void (*scan_card_table) (SgenGrayQueue *queue);
533 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
534 void (*init_to_space) (void);
535 void (*sweep) (void);
536 void (*check_scan_starts) (void);
537 void (*dump_heap) (FILE *heap_dump_file);
538 gint64 (*get_used_size) (void);
539 void (*start_nursery_collection) (void);
540 void (*finish_nursery_collection) (void);
541 void (*start_major_collection) (void);
542 void (*finish_major_collection) (void);
543 void (*have_computed_minor_collection_allowance) (void);
544 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
545 gboolean (*obj_is_from_pinned_alloc) (char *obj);
546 void (*report_pinned_memory_usage) (void);
547 int (*get_num_major_sections) (void);
548 gboolean (*handle_gc_param) (const char *opt);
549 void (*print_gc_param_usage) (void);
550 gboolean (*is_worker_thread) (MonoNativeThreadId thread);
551 void (*post_param_init) (void);
552 void* (*alloc_worker_data) (void);
553 void (*init_worker_thread) (void *data);
554 void (*reset_worker_data) (void *data);
557 extern SgenMajorCollector major_collector;
559 void mono_sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
560 void mono_sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
561 void mono_sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
562 void mono_sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
563 void mono_sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
564 SgenMajorCollector* mono_sgen_get_major_collector (void) MONO_INTERNAL;
567 typedef struct {
568 void (*wbarrier_set_field) (MonoObject *obj, gpointer field_ptr, MonoObject* value);
569 void (*wbarrier_set_arrayref) (MonoArray *arr, gpointer slot_ptr, MonoObject* value);
570 void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gpointer src_ptr, int count);
571 void (*wbarrier_value_copy) (gpointer dest, gpointer src, int count, MonoClass *klass);
572 void (*wbarrier_object_copy) (MonoObject* obj, MonoObject *src);
573 void (*wbarrier_generic_nostore) (gpointer ptr);
574 void (*record_pointer) (gpointer ptr);
576 void (*begin_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue); /* OPTIONAL */
577 void (*finish_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue);
579 void (*register_thread) (SgenThreadInfo *p); /* OPTIONAL */
580 void (*cleanup_thread) (SgenThreadInfo *p); /* OPTIONAL */
581 void (*fill_thread_info_for_suspend) (SgenThreadInfo *info); /* OPTIONAL */
582 void (*prepare_for_minor_collection) (void); /* OPTIONAL */
583 void (*prepare_for_major_collection) (void);
585 void (*finish_minor_collection) (void); /* OPTIONAL */
586 gboolean (*find_address) (char *addr);
587 } SgenRemeberedSet;
589 SgenRemeberedSet *mono_sgen_get_remset (void) MONO_INTERNAL;
591 static guint /*__attribute__((noinline)) not sure if this hint is a good idea*/
592 slow_object_get_size (MonoVTable *vtable, MonoObject* o)
594 MonoClass *klass = vtable->klass;
597 * We depend on mono_string_length_fast and
598 * mono_array_length_fast not using the object's vtable.
600 if (klass == mono_defaults.string_class) {
601 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
602 } else if (klass->rank) {
603 MonoArray *array = (MonoArray*)o;
604 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
605 if (G_UNLIKELY (array->bounds)) {
606 size += sizeof (mono_array_size_t) - 1;
607 size &= ~(sizeof (mono_array_size_t) - 1);
608 size += sizeof (MonoArrayBounds) * klass->rank;
610 return size;
611 } else {
612 /* from a created object: the class must be inited already */
613 return klass->instance_size;
618 * This function can be called on an object whose first word, the
619 * vtable field, is not intact. This is necessary for the parallel
620 * collector.
622 static inline guint
623 mono_sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
625 mword descr = (mword)vtable->gc_descr;
626 mword type = descr & 0x7;
628 if (type == DESC_TYPE_RUN_LENGTH || type == DESC_TYPE_SMALL_BITMAP) {
629 mword size = descr & 0xfff8;
630 if (size == 0) /* This is used to encode a string */
631 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
632 return size;
633 } else if (type == DESC_TYPE_VECTOR) {
634 int element_size = ((descr) >> VECTOR_ELSIZE_SHIFT) & MAX_ELEMENT_SIZE;
635 MonoArray *array = (MonoArray*)o;
636 size_t size = sizeof (MonoArray) + element_size * mono_array_length_fast (array);
638 if (descr & VECTOR_KIND_ARRAY) {
639 size += sizeof (mono_array_size_t) - 1;
640 size &= ~(sizeof (mono_array_size_t) - 1);
641 size += sizeof (MonoArrayBounds) * vtable->klass->rank;
643 return size;
646 return slow_object_get_size (vtable, o);
649 static inline guint
650 mono_sgen_safe_object_get_size (MonoObject *obj)
652 char *forwarded;
654 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
655 obj = (MonoObject*)forwarded;
657 return mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
660 const char* mono_sgen_safe_name (void* obj) MONO_INTERNAL;
662 gboolean mono_sgen_object_is_live (void *obj) MONO_INTERNAL;
664 gboolean mono_sgen_need_bridge_processing (void) MONO_INTERNAL;
665 void mono_sgen_bridge_reset_data (void) MONO_INTERNAL;
666 void mono_sgen_bridge_processing_stw_step (void) MONO_INTERNAL;
667 void mono_sgen_bridge_processing_finish (void) MONO_INTERNAL;
668 void mono_sgen_register_test_bridge_callbacks (const char *bridge_class_name) MONO_INTERNAL;
669 gboolean mono_sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
670 gboolean mono_sgen_is_bridge_class (MonoClass *class) MONO_INTERNAL;
671 void mono_sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
672 void mono_sgen_bridge_register_finalized_object (MonoObject *object) MONO_INTERNAL;
674 void mono_sgen_scan_togglerefs (CopyOrMarkObjectFunc copy_func, char *start, char *end, SgenGrayQueue *queue) MONO_INTERNAL;
675 void mono_sgen_process_togglerefs (void) MONO_INTERNAL;
678 gboolean mono_sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
679 void mono_sgen_gc_lock (void) MONO_INTERNAL;
680 void mono_sgen_gc_unlock (void) MONO_INTERNAL;
682 enum {
683 SPACE_MAJOR,
684 SPACE_LOS
687 gboolean mono_sgen_try_alloc_space (mword size, int space) MONO_INTERNAL;
688 void mono_sgen_release_space (mword size, int space) MONO_INTERNAL;
689 void mono_sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
690 void sgen_collect_major_no_lock (const char *reason) MONO_INTERNAL;
691 void mono_sgen_collect_nursery_no_lock (size_t requested_size) MONO_INTERNAL;
692 void mono_sgen_minor_collect_or_expand_inner (size_t size) MONO_INTERNAL;
693 gboolean mono_sgen_need_major_collection (mword space_needed) MONO_INTERNAL;
694 void mono_sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
696 /* LOS */
698 typedef struct _LOSObject LOSObject;
699 struct _LOSObject {
700 LOSObject *next;
701 mword size; /* this is the object size */
702 guint16 huge_object;
703 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
704 char data [MONO_ZERO_LEN_ARRAY];
707 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
709 extern LOSObject *los_object_list;
710 extern mword los_memory_usage;
712 void mono_sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
713 void* mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
714 void mono_sgen_los_sweep (void) MONO_INTERNAL;
715 gboolean mono_sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
716 void mono_sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
717 void mono_sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
718 void mono_sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
719 void sgen_major_collector_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
720 FILE *mono_sgen_get_logfile (void) MONO_INTERNAL;
722 /* nursery allocator */
724 void mono_sgen_clear_nursery_fragments (void) MONO_INTERNAL;
725 void mono_sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
726 void mono_sgen_clear_current_nursery_fragment (void) MONO_INTERNAL;
727 void mono_sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
728 mword mono_sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries) MONO_INTERNAL;
729 void mono_sgen_init_nursery_allocator (void) MONO_INTERNAL;
730 void mono_sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
731 void mono_sgen_alloc_init_heavy_stats (void) MONO_INTERNAL;
732 char* mono_sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
733 void* mono_sgen_nursery_alloc (size_t size) MONO_INTERNAL;
734 void* mono_sgen_nursery_alloc_range (size_t size, size_t min_size, int *out_alloc_size) MONO_INTERNAL;
735 MonoVTable* mono_sgen_get_array_fill_vtable (void) MONO_INTERNAL;
736 gboolean mono_sgen_can_alloc_size (size_t size) MONO_INTERNAL;
737 void mono_sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
739 /* hash tables */
741 typedef struct _SgenHashTableEntry SgenHashTableEntry;
742 struct _SgenHashTableEntry {
743 SgenHashTableEntry *next;
744 gpointer key;
745 char data [MONO_ZERO_LEN_ARRAY]; /* data is pointer-aligned */
748 typedef struct {
749 int table_mem_type;
750 int entry_mem_type;
751 size_t data_size;
752 GHashFunc hash_func;
753 GEqualFunc equal_func;
754 SgenHashTableEntry **table;
755 guint size;
756 guint num_entries;
757 } SgenHashTable;
759 #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 }
760 #define SGEN_HASH_TABLE_ENTRY_SIZE(data_size) ((data_size) + sizeof (SgenHashTableEntry*) + sizeof (gpointer))
762 gpointer mono_sgen_hash_table_lookup (SgenHashTable *table, gpointer key) MONO_INTERNAL;
763 gboolean mono_sgen_hash_table_replace (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
764 gboolean mono_sgen_hash_table_set_value (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
765 gboolean mono_sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key) MONO_INTERNAL;
766 gboolean mono_sgen_hash_table_remove (SgenHashTable *table, gpointer key, gpointer data_return) MONO_INTERNAL;
768 void mono_sgen_hash_table_clean (SgenHashTable *table) MONO_INTERNAL;
770 #define mono_sgen_hash_table_num_entries(h) ((h)->num_entries)
772 #define SGEN_HASH_TABLE_FOREACH(h,k,v) do { \
773 SgenHashTable *__hash_table = (h); \
774 SgenHashTableEntry **__table = __hash_table->table; \
775 guint __i; \
776 for (__i = 0; __i < (h)->size; ++__i) { \
777 SgenHashTableEntry **__iter, **__next; \
778 for (__iter = &__table [__i]; *__iter; __iter = __next) { \
779 SgenHashTableEntry *__entry = *__iter; \
780 __next = &__entry->next; \
781 (k) = __entry->key; \
782 (v) = (gpointer)__entry->data;
784 /* The loop must be continue'd after using this! */
785 #define SGEN_HASH_TABLE_FOREACH_REMOVE(free) do { \
786 *__iter = *__next; \
787 __next = __iter; \
788 --__hash_table->num_entries; \
789 if ((free)) \
790 mono_sgen_free_internal (__entry, __hash_table->entry_mem_type); \
791 } while (0)
793 #define SGEN_HASH_TABLE_FOREACH_SET_KEY(k) ((__entry)->key = (k))
795 #define SGEN_HASH_TABLE_FOREACH_END \
798 } while (0)
800 /* TLS Data */
802 extern MonoNativeTlsKey thread_info_key;
804 #ifdef HAVE_KW_THREAD
805 extern __thread SgenThreadInfo *thread_info;
806 extern __thread gpointer *store_remset_buffer;
807 extern __thread long store_remset_buffer_index;
808 extern __thread char *stack_end;
809 extern __thread long *store_remset_buffer_index_addr;
810 #endif
812 #ifdef HAVE_KW_THREAD
813 #define TLAB_ACCESS_INIT
814 #define REMEMBERED_SET remembered_set
815 #define STORE_REMSET_BUFFER store_remset_buffer
816 #define STORE_REMSET_BUFFER_INDEX store_remset_buffer_index
817 #define IN_CRITICAL_REGION thread_info->in_critical_region
818 #else
819 #define TLAB_ACCESS_INIT SgenThreadInfo *__thread_info__ = mono_native_tls_get_value (thread_info_key)
820 #define REMEMBERED_SET (__thread_info__->remset)
821 #define STORE_REMSET_BUFFER (__thread_info__->store_remset_buffer)
822 #define STORE_REMSET_BUFFER_INDEX (__thread_info__->store_remset_buffer_index)
823 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
824 #endif
826 #ifndef DISABLE_CRITICAL_REGION
828 #ifdef HAVE_KW_THREAD
829 #define IN_CRITICAL_REGION thread_info->in_critical_region
830 #else
831 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
832 #endif
834 /* Enter must be visible before anything is done in the critical region. */
835 #define ENTER_CRITICAL_REGION do { mono_atomic_store_acquire (&IN_CRITICAL_REGION, 1); } while (0)
837 /* Exit must make sure all critical regions stores are visible before it signal the end of the region.
838 * We don't need to emit a full barrier since we
840 #define EXIT_CRITICAL_REGION do { mono_atomic_store_release (&IN_CRITICAL_REGION, 0); } while (0)
842 #endif
844 #ifdef HAVE_KW_THREAD
845 #define EMIT_TLS_ACCESS(mb,dummy,offset) do { \
846 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX); \
847 mono_mb_emit_byte ((mb), CEE_MONO_TLS); \
848 mono_mb_emit_i4 ((mb), (offset)); \
849 } while (0)
850 #else
853 * CEE_MONO_TLS requires the tls offset, not the key, so the code below only works on darwin,
854 * where the two are the same.
856 #if defined(__APPLE__) || defined (HOST_WIN32)
857 #define EMIT_TLS_ACCESS(mb,member,dummy) do { \
858 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX); \
859 mono_mb_emit_byte ((mb), CEE_MONO_TLS); \
860 mono_mb_emit_i4 ((mb), thread_info_key); \
861 mono_mb_emit_icon ((mb), G_STRUCT_OFFSET (SgenThreadInfo, member)); \
862 mono_mb_emit_byte ((mb), CEE_ADD); \
863 mono_mb_emit_byte ((mb), CEE_LDIND_I); \
864 } while (0)
865 #else
866 #define EMIT_TLS_ACCESS(mb,member,dummy) do { g_error ("sgen is not supported when using --with-tls=pthread.\n"); } while (0)
867 #endif
869 #endif
871 /* Other globals */
873 extern GCMemSection *nursery_section;
874 extern int stat_major_gcs;
875 extern guint32 collect_before_allocs;
876 extern int degraded_mode;
877 extern int default_nursery_size;
878 extern guint32 tlab_size;
879 extern NurseryClearPolicy nursery_clear_policy;
881 extern LOCK_DECLARE (gc_mutex);
883 extern int do_pin_stats;
885 /* Object Allocation */
887 typedef enum {
888 ATYPE_NORMAL,
889 ATYPE_VECTOR,
890 ATYPE_SMALL,
891 ATYPE_NUM
892 } SgenAllocatorType;
894 void mono_sgen_init_tlab_info (SgenThreadInfo* info);
895 void mono_sgen_clear_tlabs (void);
896 gboolean mono_sgen_is_managed_allocator (MonoMethod *method);
898 /* Debug support */
900 void mono_sgen_check_consistency (void);
901 void mono_sgen_check_major_refs (void);
904 /* Write barrier support */
907 * This causes the compile to extend the liveness of 'v' till the call to dummy_use
909 static inline void
910 mono_sgen_dummy_use (gpointer v) {
911 #if defined(__GNUC__)
912 __asm__ volatile ("" : "=r"(v) : "r"(v));
913 #elif defined(_MSC_VER)
914 __asm {
915 mov eax, v;
916 and eax, eax;
918 #else
919 #error "Implement mono_sgen_dummy_use for your compiler"
920 #endif
923 #endif /* HAVE_SGEN_GC */
925 #endif /* __MONO_SGENGC_H__ */