Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mono / metadata / sgen-gc.h
1 /*
2  * sgen-gc.c: Simple generational GC.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
7  * Copyright (C) 2012 Xamarin Inc
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License 2.0 as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License 2.0 along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 #ifndef __MONO_SGENGC_H__
23 #define __MONO_SGENGC_H__
24
25 /* pthread impl */
26 #include "config.h"
27
28 #ifdef HAVE_SGEN_GC
29
30 typedef struct _SgenThreadInfo SgenThreadInfo;
31 #define THREAD_INFO_TYPE SgenThreadInfo
32
33 #include <glib.h>
34 #ifdef HAVE_PTHREAD_H
35 #include <pthread.h>
36 #endif
37 #include <signal.h>
38 #include <mono/utils/mono-compiler.h>
39 #include <mono/utils/mono-threads.h>
40 #include <mono/utils/dtrace.h>
41 #include <mono/utils/mono-logger-internal.h>
42 #include <mono/utils/atomic.h>
43 #include <mono/utils/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>
50 #include <mono/metadata/sgen-hash-table.h>
51
52 /* The method used to clear the nursery */
53 /* Clearing at nursery collections is the safest, but has bad interactions with caches.
54  * Clearing at TLAB creation is much faster, but more complex and it might expose hard
55  * to find bugs.
56  */
57 typedef enum {
58         CLEAR_AT_GC,
59         CLEAR_AT_TLAB_CREATION
60 } NurseryClearPolicy;
61
62 NurseryClearPolicy sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
63
64 #define SGEN_TV_DECLARE(name) gint64 name
65 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
66 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
67 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
68
69 #if !defined(__MACH__) && !MONO_MACH_ARCH_SUPPORTED && defined(HAVE_PTHREAD_KILL)
70 #define SGEN_POSIX_STW 1
71 #endif
72
73 /* eventually share with MonoThread? */
74 /*
75  * This structure extends the MonoThreadInfo structure.
76  */
77 struct _SgenThreadInfo {
78         MonoThreadInfo info;
79         /*
80         This is set to TRUE when STW fails to suspend a thread, most probably because the
81         underlying thread is dead.
82         */
83         int skip;
84         volatile int in_critical_region;
85
86         /*
87         This is set the argument of mono_gc_set_skip_thread.
88
89         A thread that knowingly holds no managed state can call this
90         function around blocking loops to reduce the GC burden by not
91         been scanned.
92         */
93         gboolean gc_disabled;
94         void *stack_end;
95         void *stack_start;
96         void *stack_start_limit;
97         char **tlab_next_addr;
98         char **tlab_start_addr;
99         char **tlab_temp_end_addr;
100         char **tlab_real_end_addr;
101         gpointer runtime_data;
102
103 #ifdef SGEN_POSIX_STW
104         /* This is -1 until the first suspend. */
105         int signal;
106         /* FIXME: kill this, we only use signals on systems that have rt-posix, which doesn't have issues with duplicates. */
107         unsigned int stop_count; /* to catch duplicate signals. */
108 #endif
109
110         gpointer stopped_ip;    /* only valid if the thread is stopped */
111         MonoDomain *stopped_domain; /* dsto */
112
113         /*FIXME pretty please finish killing ARCH_NUM_REGS */
114 #ifdef USE_MONO_CTX
115         MonoContext ctx;                /* ditto */
116 #else
117         gpointer regs[ARCH_NUM_REGS];       /* ditto */
118 #endif
119
120 #ifndef HAVE_KW_THREAD
121         char *tlab_start;
122         char *tlab_next;
123         char *tlab_temp_end;
124         char *tlab_real_end;
125 #endif
126 };
127
128 /*
129  * The nursery section uses this struct.
130  */
131 typedef struct _GCMemSection GCMemSection;
132 struct _GCMemSection {
133         char *data;
134         mword size;
135         /* pointer where more data could be allocated if it fits */
136         char *next_data;
137         char *end_data;
138         /*
139          * scan starts is an array of pointers to objects equally spaced in the allocation area
140          * They let use quickly find pinned objects from pinning pointers.
141          */
142         char **scan_starts;
143         /* in major collections indexes in the pin_queue for objects that pin this section */
144         void **pin_queue_start;
145         int pin_queue_num_entries;
146         unsigned int num_scan_start;
147 };
148
149 /*
150  * Recursion is not allowed for the thread lock.
151  */
152 #define LOCK_DECLARE(name) mono_mutex_t name
153 /* if changing LOCK_INIT to something that isn't idempotent, look at
154    its use in mono_gc_base_init in sgen-gc.c */
155 #define LOCK_INIT(name) mono_mutex_init (&(name))
156 #define LOCK_GC do {                                            \
157                 mono_mutex_lock (&gc_mutex);                    \
158                 MONO_GC_LOCKED ();                              \
159         } while (0)
160 #define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
161 #define UNLOCK_GC do { sgen_gc_unlock (); } while (0)
162
163 extern LOCK_DECLARE (sgen_interruption_mutex);
164
165 #define LOCK_INTERRUPTION mono_mutex_lock (&sgen_interruption_mutex)
166 #define UNLOCK_INTERRUPTION mono_mutex_unlock (&sgen_interruption_mutex)
167
168 /* FIXME: Use InterlockedAdd & InterlockedAdd64 to reduce the CAS cost. */
169 #define SGEN_CAS_PTR    InterlockedCompareExchangePointer
170 #define SGEN_ATOMIC_ADD(x,i)    do {                                    \
171                 int __old_x;                                            \
172                 do {                                                    \
173                         __old_x = (x);                                  \
174                 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
175         } while (0)
176 #define SGEN_ATOMIC_ADD_P(x,i) do { \
177                 size_t __old_x;                                            \
178                 do {                                                    \
179                         __old_x = (x);                                  \
180                 } while (InterlockedCompareExchangePointer ((void**)&(x), (void*)(__old_x + (i)), (void*)__old_x) != (void*)__old_x); \
181         } while (0)
182
183
184 #ifndef HOST_WIN32
185 /* we intercept pthread_create calls to know which threads exist */
186 #define USE_PTHREAD_INTERCEPT 1
187 #endif
188
189 #ifdef HEAVY_STATISTICS
190 #define HEAVY_STAT(x)   x
191
192 extern long long stat_objects_alloced_degraded;
193 extern long long stat_bytes_alloced_degraded;
194 extern long long stat_copy_object_called_major;
195 extern long long stat_objects_copied_major;
196 #else
197 #define HEAVY_STAT(x)
198 #endif
199
200 #define SGEN_ASSERT(level, a, ...) do { \
201         if (G_UNLIKELY ((level) <= SGEN_MAX_ASSERT_LEVEL && !(a))) {    \
202                 g_error (__VA_ARGS__);  \
203 } } while (0)
204
205
206 #define SGEN_LOG(level, format, ...) do {      \
207         if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {        \
208                 mono_gc_printf (gc_debug_file, format, ##__VA_ARGS__);  \
209 } } while (0)
210
211 #define SGEN_COND_LOG(level, cond, format, ...) do {    \
212         if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {        \
213                 if (cond)       \
214                         mono_gc_printf (gc_debug_file, format, ##__VA_ARGS__);  \
215 } } while (0)
216
217 #define SGEN_LOG_DO(level, fun) do {    \
218         if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {        \
219                 fun;    \
220 } } while (0)
221
222 extern int gc_debug_level;
223 extern FILE* gc_debug_file;
224
225 extern int current_collection_generation;
226
227 extern unsigned int sgen_global_stop_count;
228
229 extern gboolean bridge_processing_in_progress;
230
231 extern int num_ready_finalizers;
232
233 #define SGEN_ALLOC_ALIGN                8
234 #define SGEN_ALLOC_ALIGN_BITS   3
235
236 /* s must be non-negative */
237 #define SGEN_CAN_ALIGN_UP(s)            ((s) <= SIZE_MAX - (SGEN_ALLOC_ALIGN - 1))
238 #define SGEN_ALIGN_UP(s)                (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
239
240 /*
241  * The link pointer is hidden by negating each bit.  We use the lowest
242  * bit of the link (before negation) to store whether it needs
243  * resurrection tracking.
244  */
245 #define HIDE_POINTER(p,t)       ((gpointer)(~((gulong)(p)|((t)?1:0))))
246 #define REVEAL_POINTER(p)       ((gpointer)((~(gulong)(p))&~3L))
247
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
253
254 #ifdef USER_CONFIG
255
256 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
257 #define DEFAULT_NURSERY_SIZE (sgen_nursery_size)
258 extern int 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 (sgen_nursery_bits)
262 extern int sgen_nursery_bits MONO_INTERNAL;
263 #endif
264
265 #else
266
267 #define DEFAULT_NURSERY_SIZE (4*1024*1024)
268 #ifdef SGEN_ALIGN_NURSERY
269 #define DEFAULT_NURSERY_BITS 22
270 #endif
271
272 #endif
273
274 #ifndef SGEN_ALIGN_NURSERY
275 #define DEFAULT_NURSERY_BITS -1
276 #endif
277
278 extern char *sgen_nursery_start MONO_INTERNAL;
279 extern char *sgen_nursery_end MONO_INTERNAL;
280
281 static inline MONO_ALWAYS_INLINE gboolean
282 sgen_ptr_in_nursery (void *p)
283 {
284         return SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, sgen_nursery_start, sgen_nursery_end);
285 }
286
287 static inline MONO_ALWAYS_INLINE char*
288 sgen_get_nursery_start (void)
289 {
290         return sgen_nursery_start;
291 }
292
293 static inline MONO_ALWAYS_INLINE char*
294 sgen_get_nursery_end (void)
295 {
296         return sgen_nursery_end;
297 }
298
299 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
300  * no cast from a pointer to an integer
301  */
302 typedef struct {
303         MonoClass *klass;
304         mword desc;
305 } GCVTable;
306
307 /* these bits are set in the object vtable: we could merge them since an object can be
308  * either pinned or forwarded but not both.
309  * We store them in the vtable slot because the bits are used in the sync block for
310  * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
311  * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
312  * would be an invalid combination for the monitor and hash code).
313  * The values are already shifted.
314  * The forwarding address is stored in the sync block.
315  */
316 #define SGEN_FORWARDED_BIT 1
317 #define SGEN_PINNED_BIT 2
318 #define SGEN_VTABLE_BITS_MASK 0x3
319
320 /* returns NULL if not forwarded, or the forwarded address */
321 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
322 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
323
324 /* set the forwarded address fw_addr for object obj */
325 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do {                           \
326                 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
327         } while (0)
328 #define SGEN_PIN_OBJECT(obj) do {       \
329                 ((mword*)(obj))[0] |= SGEN_PINNED_BIT;  \
330         } while (0)
331 #define SGEN_UNPIN_OBJECT(obj) do {     \
332                 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
333         } while (0)
334
335 /*
336  * Since we set bits in the vtable, use the macro to load it from the pointer to
337  * an object that is potentially pinned.
338  */
339 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
340
341 #if defined(SGEN_GRAY_OBJECT_ENQUEUE) || SGEN_MAX_DEBUG_LEVEL >= 9
342 #define GRAY_OBJECT_ENQUEUE sgen_gray_object_enqueue
343 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = sgen_gray_object_dequeue ((queue)))
344 #else
345 #define GRAY_OBJECT_ENQUEUE(queue,o) do {                               \
346                 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
347                         sgen_gray_object_enqueue ((queue), (o));        \
348                 else                                                    \
349                         (queue)->first->objects [(queue)->first->end++] = (o); \
350                 PREFETCH ((o));                                         \
351         } while (0)
352 #define GRAY_OBJECT_DEQUEUE(queue,o) do {                               \
353                 if (!(queue)->first)                                    \
354                         (o) = NULL;                                     \
355                 else if (G_UNLIKELY ((queue)->first->end == 1))         \
356                         (o) = sgen_gray_object_dequeue ((queue));               \
357                 else                                                    \
358                         (o) = (queue)->first->objects [--(queue)->first->end]; \
359         } while (0)
360 #endif
361
362 /*
363 List of what each bit on of the vtable gc bits means. 
364 */
365 enum {
366         SGEN_GC_BIT_BRIDGE_OBJECT = 1,
367 };
368
369 /* the runtime can register areas of memory as roots: we keep two lists of roots,
370  * a pinned root set for conservatively scanned roots and a normal one for
371  * precisely scanned roots (currently implemented as a single list).
372  */
373 typedef struct _RootRecord RootRecord;
374 struct _RootRecord {
375         char *end_root;
376         mword root_desc;
377 };
378
379 enum {
380         ROOT_TYPE_NORMAL = 0, /* "normal" roots */
381         ROOT_TYPE_PINNED = 1, /* roots without a GC descriptor */
382         ROOT_TYPE_WBARRIER = 2, /* roots with a write barrier */
383         ROOT_TYPE_NUM
384 };
385
386 extern SgenHashTable roots_hash [ROOT_TYPE_NUM];
387
388 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
389
390 int sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
391 gboolean sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
392 gboolean sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
393 void sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
394 void sgen_os_init (void) MONO_INTERNAL;
395
396 gboolean sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
397
398 void sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
399
400 void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
401 void sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
402
403 /* Keep in sync with description_for_type() in sgen-internal.c! */
404 enum {
405         INTERNAL_MEM_PIN_QUEUE,
406         INTERNAL_MEM_FRAGMENT,
407         INTERNAL_MEM_SECTION,
408         INTERNAL_MEM_SCAN_STARTS,
409         INTERNAL_MEM_FIN_TABLE,
410         INTERNAL_MEM_FINALIZE_ENTRY,
411         INTERNAL_MEM_FINALIZE_READY_ENTRY,
412         INTERNAL_MEM_DISLINK_TABLE,
413         INTERNAL_MEM_DISLINK,
414         INTERNAL_MEM_ROOTS_TABLE,
415         INTERNAL_MEM_ROOT_RECORD,
416         INTERNAL_MEM_STATISTICS,
417         INTERNAL_MEM_STAT_PINNED_CLASS,
418         INTERNAL_MEM_STAT_REMSET_CLASS,
419         INTERNAL_MEM_GRAY_QUEUE,
420         INTERNAL_MEM_MS_TABLES,
421         INTERNAL_MEM_MS_BLOCK_INFO,
422         INTERNAL_MEM_MS_BLOCK_INFO_SORT,
423         INTERNAL_MEM_EPHEMERON_LINK,
424         INTERNAL_MEM_WORKER_DATA,
425         INTERNAL_MEM_WORKER_JOB_DATA,
426         INTERNAL_MEM_BRIDGE_DATA,
427         INTERNAL_MEM_BRIDGE_HASH_TABLE,
428         INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY,
429         INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE,
430         INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY,
431         INTERNAL_MEM_JOB_QUEUE_ENTRY,
432         INTERNAL_MEM_TOGGLEREF_DATA,
433         INTERNAL_MEM_CARDTABLE_MOD_UNION,
434         INTERNAL_MEM_BINARY_PROTOCOL,
435         INTERNAL_MEM_MAX
436 };
437
438 enum {
439         GENERATION_NURSERY,
440         GENERATION_OLD,
441         GENERATION_MAX
442 };
443
444 #ifdef SGEN_BINARY_PROTOCOL
445 #define BINARY_PROTOCOL_ARG(x)  ,x
446 #else
447 #define BINARY_PROTOCOL_ARG(x)
448 #endif
449
450 void sgen_init_internal_allocator (void) MONO_INTERNAL;
451
452 typedef struct _ObjectList ObjectList;
453 struct _ObjectList {
454         MonoObject *obj;
455         ObjectList *next;
456 };
457
458 typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
459 typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
460 typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue* BINARY_PROTOCOL_ARG (size_t size));
461
462 typedef struct
463 {
464         ScanObjectFunc scan_func;
465         CopyOrMarkObjectFunc copy_func;
466         SgenGrayQueue *queue;
467 } ScanCopyContext;
468
469 void sgen_report_internal_mem_usage (void) MONO_INTERNAL;
470 void sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
471 void sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
472 void sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
473
474 void sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
475
476 void sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
477
478 void* sgen_alloc_internal (int type) MONO_INTERNAL;
479 void sgen_free_internal (void *addr, int type) MONO_INTERNAL;
480
481 void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure) MONO_INTERNAL;
482 void sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
483
484 void** sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
485 void sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
486 void sgen_pin_objects_in_section (GCMemSection *section, ScanCopyContext ctx) MONO_INTERNAL;
487
488 void sgen_pin_stats_register_object (char *obj, size_t size);
489 void sgen_pin_stats_register_global_remset (char *obj);
490 void sgen_pin_stats_print_class_stats (void);
491
492 void sgen_sort_addresses (void **array, int size) MONO_INTERNAL;
493 void sgen_add_to_global_remset (gpointer ptr, gpointer obj) MONO_INTERNAL;
494
495 int sgen_get_current_collection_generation (void) MONO_INTERNAL;
496 gboolean sgen_collection_is_parallel (void) MONO_INTERNAL;
497 gboolean sgen_collection_is_concurrent (void) MONO_INTERNAL;
498 gboolean sgen_concurrent_collection_in_progress (void) MONO_INTERNAL;
499
500 typedef struct {
501         CopyOrMarkObjectFunc copy_or_mark_object;
502         ScanObjectFunc scan_object;
503         ScanVTypeFunc scan_vtype;
504         /*FIXME add allocation function? */
505 } SgenObjectOperations;
506
507 SgenObjectOperations *sgen_get_current_object_ops (void) MONO_INTERNAL;
508
509 typedef struct _SgenFragment SgenFragment;
510
511 struct _SgenFragment {
512         SgenFragment *next;
513         char *fragment_start;
514         char *fragment_next; /* the current soft limit for allocation */
515         char *fragment_end;
516         SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
517 };
518
519 typedef struct {
520         SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
521         SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
522 } SgenFragmentAllocator;
523
524 void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end) MONO_INTERNAL;
525 void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator) MONO_INTERNAL;
526 void* sgen_fragment_allocator_serial_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
527 void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size) MONO_INTERNAL;
528 void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
529 void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size) MONO_INTERNAL;
530 SgenFragment* sgen_fragment_allocator_alloc (void) MONO_INTERNAL;
531 void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator) MONO_INTERNAL;
532 void sgen_clear_range (char *start, char *end) MONO_INTERNAL;
533
534
535 /*
536 This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
537 and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
538 L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
539
540 Experimental results on how much space we waste with a 4Mb nursery:
541
542 Note that the wastage applies to the half nursery, or 2Mb:
543
544 Test 1 (compiling corlib):
545 9: avg: 3.1k
546 8: avg: 1.6k
547
548 */
549 #define SGEN_TO_SPACE_GRANULE_BITS 9
550 #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
551
552 extern char *sgen_space_bitmap MONO_INTERNAL;
553 extern int sgen_space_bitmap_size MONO_INTERNAL;
554
555 static inline gboolean
556 sgen_nursery_is_to_space (char *object)
557 {
558         int idx = (object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
559         int byte = idx / 8;
560         int bit = idx & 0x7;
561
562         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 ());
563         SGEN_ASSERT (4, byte < sgen_space_bitmap_size, "byte index %d out of range", byte, sgen_space_bitmap_size);
564
565         return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
566 }
567
568 static inline gboolean
569 sgen_nursery_is_from_space (char *object)
570 {
571         return !sgen_nursery_is_to_space (object);
572 }
573
574 static inline gboolean
575 sgen_nursery_is_object_alive (char *obj)
576 {
577         /* FIXME put this asserts under a non default level */
578         g_assert (sgen_ptr_in_nursery (obj));
579
580         if (sgen_nursery_is_to_space (obj))
581                 return TRUE;
582
583         if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
584                 return TRUE;
585
586         return FALSE;
587 }
588
589 typedef struct {
590         gboolean is_split;
591
592         char* (*alloc_for_promotion) (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references);
593         char* (*par_alloc_for_promotion) (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references);
594
595         SgenObjectOperations serial_ops;
596         SgenObjectOperations parallel_ops;
597
598         void (*prepare_to_space) (char *to_space_bitmap, int space_bitmap_size);
599         void (*clear_fragments) (void);
600         SgenFragment* (*build_fragments_get_exclude_head) (void);
601         void (*build_fragments_release_exclude_head) (void);
602         void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
603         void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
604
605         gboolean (*handle_gc_param) (const char *opt); /* Optional */
606         void (*print_gc_param_usage) (void); /* Optional */
607 } SgenMinorCollector;
608
609 extern SgenMinorCollector sgen_minor_collector;
610
611 void sgen_simple_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
612 void sgen_split_nursery_init (SgenMinorCollector *collector) MONO_INTERNAL;
613
614 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
615 void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
616
617 typedef struct _SgenMajorCollector SgenMajorCollector;
618 struct _SgenMajorCollector {
619         size_t section_size;
620         gboolean is_parallel;
621         gboolean is_concurrent;
622         gboolean supports_cardtable;
623         gboolean sweeps_lazily;
624
625         /*
626          * This is set to TRUE if the sweep for the last major
627          * collection has been completed.
628          */
629         gboolean *have_swept;
630         /*
631          * This is set to TRUE by the sweep if the next major
632          * collection should be synchronous (for evacuation).  For
633          * non-concurrent collectors, this should be NULL.
634          */
635         gboolean *want_synchronous_collection;
636
637         void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
638         gboolean (*is_object_live) (char *obj);
639         void* (*alloc_small_pinned_obj) (MonoVTable *vtable, size_t size, gboolean has_references);
640         void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
641
642         SgenObjectOperations major_ops;
643         SgenObjectOperations major_concurrent_ops;
644
645         void* (*alloc_object) (MonoVTable *vtable, int size, gboolean has_references);
646         void* (*par_alloc_object) (MonoVTable *vtable, int size, gboolean has_references);
647         void (*free_pinned_object) (char *obj, size_t size);
648         void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
649         void (*free_non_pinned_object) (char *obj, size_t size);
650         void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
651         void (*pin_objects) (SgenGrayQueue *queue);
652         void (*pin_major_object) (char *obj, SgenGrayQueue *queue);
653         void (*scan_card_table) (gboolean mod_union, SgenGrayQueue *queue);
654         void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
655         void (*update_cardtable_mod_union) (void);
656         void (*init_to_space) (void);
657         void (*sweep) (void);
658         void (*check_scan_starts) (void);
659         void (*dump_heap) (FILE *heap_dump_file);
660         gint64 (*get_used_size) (void);
661         void (*start_nursery_collection) (void);
662         void (*finish_nursery_collection) (void);
663         void (*start_major_collection) (void);
664         void (*finish_major_collection) (void);
665         void (*have_computed_minor_collection_allowance) (void);
666         gboolean (*ptr_is_in_non_pinned_space) (char *ptr, char **start);
667         gboolean (*obj_is_from_pinned_alloc) (char *obj);
668         void (*report_pinned_memory_usage) (void);
669         int (*get_num_major_sections) (void);
670         gboolean (*handle_gc_param) (const char *opt);
671         void (*print_gc_param_usage) (void);
672         gboolean (*is_worker_thread) (MonoNativeThreadId thread);
673         void (*post_param_init) (SgenMajorCollector *collector);
674         void* (*alloc_worker_data) (void);
675         void (*init_worker_thread) (void *data);
676         void (*reset_worker_data) (void *data);
677         gboolean (*is_valid_object) (char *object);
678         MonoVTable* (*describe_pointer) (char *pointer);
679         guint8* (*get_cardtable_mod_union_for_object) (char *object);
680         long long (*get_and_reset_num_major_objects_marked) (void);
681 };
682
683 extern SgenMajorCollector major_collector;
684
685 void sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
686 void sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
687 void sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
688 void sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
689 void sgen_marksweep_conc_init (SgenMajorCollector *collector) MONO_INTERNAL;
690 SgenMajorCollector* sgen_get_major_collector (void) MONO_INTERNAL;
691
692
693 typedef struct {
694         void (*wbarrier_set_field) (MonoObject *obj, gpointer field_ptr, MonoObject* value);
695         void (*wbarrier_set_arrayref) (MonoArray *arr, gpointer slot_ptr, MonoObject* value);
696         void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gpointer src_ptr, int count);
697         void (*wbarrier_value_copy) (gpointer dest, gpointer src, int count, MonoClass *klass);
698         void (*wbarrier_object_copy) (MonoObject* obj, MonoObject *src);
699         void (*wbarrier_generic_nostore) (gpointer ptr);
700         void (*record_pointer) (gpointer ptr);
701
702         void (*finish_scan_remsets) (void *start_nursery, void *end_nursery, SgenGrayQueue *queue);
703
704         void (*prepare_for_major_collection) (void);
705
706         void (*finish_minor_collection) (void);
707         gboolean (*find_address) (char *addr);
708         gboolean (*find_address_with_cards) (char *cards_start, guint8 *cards, char *addr);
709 } SgenRemeberedSet;
710
711 SgenRemeberedSet *sgen_get_remset (void) MONO_INTERNAL;
712
713 static guint /*__attribute__((noinline)) not sure if this hint is a good idea*/
714 slow_object_get_size (MonoVTable *vtable, MonoObject* o)
715 {
716         MonoClass *klass = vtable->klass;
717
718         /*
719          * We depend on mono_string_length_fast and
720          * mono_array_length_fast not using the object's vtable.
721          */
722         if (klass == mono_defaults.string_class) {
723                 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
724         } else if (klass->rank) {
725                 MonoArray *array = (MonoArray*)o;
726                 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
727                 if (G_UNLIKELY (array->bounds)) {
728                         size += sizeof (mono_array_size_t) - 1;
729                         size &= ~(sizeof (mono_array_size_t) - 1);
730                         size += sizeof (MonoArrayBounds) * klass->rank;
731                 }
732                 return size;
733         } else {
734                 /* from a created object: the class must be inited already */
735                 return klass->instance_size;
736         }
737 }
738
739 /*
740  * This function can be called on an object whose first word, the
741  * vtable field, is not intact.  This is necessary for the parallel
742  * collector.
743  */
744 static inline guint
745 sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
746 {
747         mword descr = (mword)vtable->gc_descr;
748         mword type = descr & 0x7;
749
750         if (type == DESC_TYPE_RUN_LENGTH || type == DESC_TYPE_SMALL_BITMAP) {
751                 mword size = descr & 0xfff8;
752                 if (size == 0) /* This is used to encode a string */
753                         return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
754                 return size;
755         } else if (type == DESC_TYPE_VECTOR) {
756                 int element_size = ((descr) >> VECTOR_ELSIZE_SHIFT) & MAX_ELEMENT_SIZE;
757                 MonoArray *array = (MonoArray*)o;
758                 size_t size = sizeof (MonoArray) + element_size * mono_array_length_fast (array);
759
760                 if (descr & VECTOR_KIND_ARRAY) {
761                         size += sizeof (mono_array_size_t) - 1;
762                         size &= ~(sizeof (mono_array_size_t) - 1);
763                         size += sizeof (MonoArrayBounds) * vtable->klass->rank;
764                 }
765                 return size;
766         }
767
768         return slow_object_get_size (vtable, o);
769 }
770
771 static inline guint
772 sgen_safe_object_get_size (MonoObject *obj)
773 {
774        char *forwarded;
775
776        if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
777                obj = (MonoObject*)forwarded;
778
779        return sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
780 }
781
782 const char* sgen_safe_name (void* obj) MONO_INTERNAL;
783
784 gboolean sgen_object_is_live (void *obj) MONO_INTERNAL;
785
786 void  sgen_init_fin_weak_hash (void) MONO_INTERNAL;
787
788 gboolean sgen_need_bridge_processing (void) MONO_INTERNAL;
789 void sgen_bridge_reset_data (void) MONO_INTERNAL;
790 void sgen_bridge_processing_stw_step (void) MONO_INTERNAL;
791 void sgen_bridge_processing_finish (int generation) MONO_INTERNAL;
792 void sgen_register_test_bridge_callbacks (const char *bridge_class_name) MONO_INTERNAL;
793 gboolean sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
794 gboolean sgen_is_bridge_class (MonoClass *class) MONO_INTERNAL;
795 void sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
796 void sgen_bridge_register_finalized_object (MonoObject *object) MONO_INTERNAL;
797 void sgen_bridge_describe_pointer (MonoObject *object) MONO_INTERNAL;
798
799 void sgen_scan_togglerefs (char *start, char *end, ScanCopyContext ctx) MONO_INTERNAL;
800 void sgen_process_togglerefs (void) MONO_INTERNAL;
801
802 typedef mono_bool (*WeakLinkAlivePredicateFunc) (MonoObject*, void*);
803
804 void sgen_null_links_with_predicate (int generation, WeakLinkAlivePredicateFunc predicate, void *data) MONO_INTERNAL;
805
806 gboolean sgen_gc_is_object_ready_for_finalization (void *object) MONO_INTERNAL;
807 void sgen_gc_lock (void) MONO_INTERNAL;
808 void sgen_gc_unlock (void) MONO_INTERNAL;
809 void sgen_gc_event_moves (void) MONO_INTERNAL;
810
811 void sgen_queue_finalization_entry (MonoObject *obj) MONO_INTERNAL;
812 const char* sgen_generation_name (int generation) MONO_INTERNAL;
813
814 void sgen_collect_bridge_objects (int generation, ScanCopyContext ctx) MONO_INTERNAL;
815 void sgen_finalize_in_range (int generation, ScanCopyContext ctx) MONO_INTERNAL;
816 void sgen_null_link_in_range (int generation, gboolean before_finalization, ScanCopyContext ctx) MONO_INTERNAL;
817 void sgen_null_links_for_domain (MonoDomain *domain, int generation) MONO_INTERNAL;
818 void sgen_remove_finalizers_for_domain (MonoDomain *domain, int generation) MONO_INTERNAL;
819 void sgen_process_fin_stage_entries (void) MONO_INTERNAL;
820 void sgen_process_dislink_stage_entries (void) MONO_INTERNAL;
821 void sgen_register_disappearing_link (MonoObject *obj, void **link, gboolean track, gboolean in_gc) MONO_INTERNAL;
822
823 gboolean sgen_drain_gray_stack (int max_objs, ScanCopyContext ctx) MONO_INTERNAL;
824
825 enum {
826         SPACE_NURSERY,
827         SPACE_MAJOR,
828         SPACE_LOS
829 };
830
831 void sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
832 void sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue) MONO_INTERNAL;
833 void sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
834
835 void sgen_ensure_free_space (size_t size) MONO_INTERNAL;
836 void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish) MONO_INTERNAL;
837 gboolean sgen_has_critical_method (void) MONO_INTERNAL;
838 gboolean sgen_is_critical_method (MonoMethod *method) MONO_INTERNAL;
839
840 /* STW */
841
842 typedef struct {
843         int generation;
844         const char *reason;
845         gboolean is_overflow;
846         SGEN_TV_DECLARE (total_time);
847         SGEN_TV_DECLARE (stw_time);
848         SGEN_TV_DECLARE (bridge_time);
849 } GGTimingInfo;
850
851 int sgen_stop_world (int generation) MONO_INTERNAL;
852 int sgen_restart_world (int generation, GGTimingInfo *timing) MONO_INTERNAL;
853
854 /* LOS */
855
856 typedef struct _LOSObject LOSObject;
857 struct _LOSObject {
858         LOSObject *next;
859         mword size; /* this is the object size, lowest bit used for pin/mark */
860         guint8 *cardtable_mod_union; /* only used by the concurrent collector */
861 #if SIZEOF_VOID_P < 8
862         mword dummy;            /* to align object to sizeof (double) */
863 #endif
864         char data [MONO_ZERO_LEN_ARRAY];
865 };
866
867 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
868
869 extern LOSObject *los_object_list;
870 extern mword los_memory_usage;
871
872 void sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
873 void* sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
874 void sgen_los_sweep (void) MONO_INTERNAL;
875 gboolean sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
876 void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
877 void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
878 void sgen_los_scan_card_table (gboolean mod_union, SgenGrayQueue *queue) MONO_INTERNAL;
879 void sgen_los_update_cardtable_mod_union (void) MONO_INTERNAL;
880 void sgen_major_collector_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
881 gboolean sgen_los_is_valid_object (char *object) MONO_INTERNAL;
882 gboolean mono_sgen_los_describe_pointer (char *ptr) MONO_INTERNAL;
883 LOSObject* sgen_los_header_for_object (char *data) MONO_INTERNAL;
884 mword sgen_los_object_size (LOSObject *obj) MONO_INTERNAL;
885 void sgen_los_pin_object (char *obj) MONO_INTERNAL;
886 void sgen_los_unpin_object (char *obj) MONO_INTERNAL;
887 gboolean sgen_los_object_is_pinned (char *obj) MONO_INTERNAL;
888
889
890 /* nursery allocator */
891
892 void sgen_clear_nursery_fragments (void) MONO_INTERNAL;
893 void sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
894 void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
895 mword sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries, SgenGrayQueue *unpin_queue) MONO_INTERNAL;
896 void sgen_init_nursery_allocator (void) MONO_INTERNAL;
897 void sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
898 void sgen_alloc_init_heavy_stats (void) MONO_INTERNAL;
899 char* sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
900 void* sgen_nursery_alloc (size_t size) MONO_INTERNAL;
901 void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size) MONO_INTERNAL;
902 MonoVTable* sgen_get_array_fill_vtable (void) MONO_INTERNAL;
903 gboolean sgen_can_alloc_size (size_t size) MONO_INTERNAL;
904 void sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
905
906 void sgen_nursery_alloc_prepare_for_minor (void) MONO_INTERNAL;
907 void sgen_nursery_alloc_prepare_for_major (void) MONO_INTERNAL;
908
909 char* sgen_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
910 char* sgen_par_alloc_for_promotion (char *obj, size_t objsize, gboolean has_references) MONO_INTERNAL;
911
912 /* TLS Data */
913
914 extern MonoNativeTlsKey thread_info_key;
915
916 #ifdef HAVE_KW_THREAD
917 extern __thread SgenThreadInfo *sgen_thread_info;
918 extern __thread char *stack_end;
919 #endif
920
921 #ifdef HAVE_KW_THREAD
922 #define TLAB_ACCESS_INIT
923 #define IN_CRITICAL_REGION sgen_thread_info->in_critical_region
924 #else
925 #define TLAB_ACCESS_INIT        SgenThreadInfo *__thread_info__ = mono_native_tls_get_value (thread_info_key)
926 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
927 #endif
928
929 #ifndef DISABLE_CRITICAL_REGION
930
931 #ifdef HAVE_KW_THREAD
932 #define IN_CRITICAL_REGION sgen_thread_info->in_critical_region
933 #else
934 #define IN_CRITICAL_REGION (__thread_info__->in_critical_region)
935 #endif
936
937 /* Enter must be visible before anything is done in the critical region. */
938 #define ENTER_CRITICAL_REGION do { mono_atomic_store_acquire (&IN_CRITICAL_REGION, 1); } while (0)
939
940 /* Exit must make sure all critical regions stores are visible before it signal the end of the region. 
941  * We don't need to emit a full barrier since we
942  */
943 #define EXIT_CRITICAL_REGION  do { mono_atomic_store_release (&IN_CRITICAL_REGION, 0); } while (0)
944
945 #endif
946
947 #ifdef HAVE_KW_THREAD
948 #define EMIT_TLS_ACCESS(mb,member,key)  do {    \
949         mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);   \
950         mono_mb_emit_byte ((mb), CEE_MONO_TLS);         \
951         mono_mb_emit_i4 ((mb), (key));          \
952         } while (0)
953 #else
954
955 #if defined(__APPLE__) || defined (HOST_WIN32)
956 #define EMIT_TLS_ACCESS(mb,member,key)  do {    \
957         mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);   \
958         mono_mb_emit_byte ((mb), CEE_MONO_TLS);         \
959         mono_mb_emit_i4 ((mb), TLS_KEY_SGEN_THREAD_INFO);       \
960         mono_mb_emit_icon ((mb), G_STRUCT_OFFSET (SgenThreadInfo, member));     \
961         mono_mb_emit_byte ((mb), CEE_ADD);              \
962         mono_mb_emit_byte ((mb), CEE_LDIND_I);          \
963         } while (0)
964 #else
965 #define EMIT_TLS_ACCESS(mb,member,key)  do { g_error ("sgen is not supported when using --with-tls=pthread.\n"); } while (0)
966 #endif
967
968 #endif
969
970 /* Other globals */
971
972 extern GCMemSection *nursery_section;
973 extern int stat_major_gcs;
974 extern guint32 collect_before_allocs;
975 extern guint32 verify_before_allocs;
976 extern gboolean has_per_allocation_action;
977 extern int degraded_mode;
978 extern int default_nursery_size;
979 extern guint32 tlab_size;
980 extern NurseryClearPolicy nursery_clear_policy;
981 extern gboolean sgen_try_free_some_memory;
982
983 extern LOCK_DECLARE (gc_mutex);
984
985 extern int do_pin_stats;
986
987 /* Nursery helpers. */
988
989 static inline void
990 sgen_set_nursery_scan_start (char *p)
991 {
992         int idx = (p - (char*)nursery_section->data) / SGEN_SCAN_START_SIZE;
993         char *old = nursery_section->scan_starts [idx];
994         if (!old || old > p)
995                 nursery_section->scan_starts [idx] = p;
996 }
997
998
999 /* Object Allocation */
1000
1001 typedef enum {
1002         ATYPE_NORMAL,
1003         ATYPE_VECTOR,
1004         ATYPE_SMALL,
1005         ATYPE_STRING,
1006         ATYPE_NUM
1007 } SgenAllocatorType;
1008
1009 void sgen_init_tlab_info (SgenThreadInfo* info);
1010 void sgen_clear_tlabs (void);
1011 void sgen_set_use_managed_allocator (gboolean flag);
1012 gboolean sgen_is_managed_allocator (MonoMethod *method);
1013 gboolean sgen_has_managed_allocator (void);
1014
1015 /* Debug support */
1016
1017 void sgen_check_consistency (void);
1018 void sgen_check_mod_union_consistency (void);
1019 void sgen_check_major_refs (void);
1020 void sgen_check_whole_heap (gboolean allow_missing_pinning);
1021 void sgen_check_whole_heap_stw (void) MONO_INTERNAL;
1022 void sgen_check_objref (char *obj);
1023 void sgen_check_major_heap_marked (void) MONO_INTERNAL;
1024 void sgen_check_nursery_objects_pinned (gboolean pinned) MONO_INTERNAL;
1025
1026 /* Write barrier support */
1027
1028 /*
1029  * This causes the compile to extend the liveness of 'v' till the call to dummy_use
1030  */
1031 static inline void
1032 sgen_dummy_use (gpointer v) {
1033 #if defined(__GNUC__)
1034         __asm__ volatile ("" : "=r"(v) : "r"(v));
1035 #elif defined(_MSC_VER)
1036         __asm {
1037                 mov eax, v;
1038                 and eax, eax;
1039         };
1040 #else
1041 #error "Implement sgen_dummy_use for your compiler"
1042 #endif
1043 }
1044
1045 /* Environment variable parsing */
1046
1047 #define MONO_GC_PARAMS_NAME     "MONO_GC_PARAMS"
1048 #define MONO_GC_DEBUG_NAME      "MONO_GC_DEBUG"
1049
1050 gboolean sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
1051 void sgen_env_var_error (const char *env_var, const char *fallback, const char *description_format, ...) MONO_INTERNAL;
1052
1053 /* Utilities */
1054
1055 void sgen_qsort (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*)) MONO_INTERNAL;
1056
1057 #endif /* HAVE_SGEN_GC */
1058
1059 #endif /* __MONO_SGENGC_H__ */