f32315e6f04ac4217a1298d7924f1bbd8f2bb387
[mono.git] / mono / metadata / sgen-gc.c
1 /*
2  * sgen-gc.c: Simple generational GC.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *  Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10  *
11  * Thread start/stop adapted from Boehm's GC:
12  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
13  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
14  * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
15  * Copyright (c) 2000-2004 by Hewlett-Packard Company.  All rights reserved.
16  * Copyright 2001-2003 Ximian, Inc
17  * Copyright 2003-2010 Novell, Inc.
18  * Copyright 2011 Xamarin, Inc.
19  * Copyright (C) 2012 Xamarin Inc
20  *
21  * This library is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Library General Public
23  * License 2.0 as published by the Free Software Foundation;
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License 2.0 along with this library; if not, write to the Free
32  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  * Important: allocation provides always zeroed memory, having to do
35  * a memset after allocation is deadly for performance.
36  * Memory usage at startup is currently as follows:
37  * 64 KB pinned space
38  * 64 KB internal space
39  * size of nursery
40  * We should provide a small memory config with half the sizes
41  *
42  * We currently try to make as few mono assumptions as possible:
43  * 1) 2-word header with no GC pointers in it (first vtable, second to store the
44  *    forwarding ptr)
45  * 2) gc descriptor is the second word in the vtable (first word in the class)
46  * 3) 8 byte alignment is the minimum and enough (not true for special structures (SIMD), FIXME)
47  * 4) there is a function to get an object's size and the number of
48  *    elements in an array.
49  * 5) we know the special way bounds are allocated for complex arrays
50  * 6) we know about proxies and how to treat them when domains are unloaded
51  *
52  * Always try to keep stack usage to a minimum: no recursive behaviour
53  * and no large stack allocs.
54  *
55  * General description.
56  * Objects are initially allocated in a nursery using a fast bump-pointer technique.
57  * When the nursery is full we start a nursery collection: this is performed with a
58  * copying GC.
59  * When the old generation is full we start a copying GC of the old generation as well:
60  * this will be changed to mark&sweep with copying when fragmentation becomes to severe
61  * in the future.  Maybe we'll even do both during the same collection like IMMIX.
62  *
63  * The things that complicate this description are:
64  * *) pinned objects: we can't move them so we need to keep track of them
65  * *) no precise info of the thread stacks and registers: we need to be able to
66  *    quickly find the objects that may be referenced conservatively and pin them
67  *    (this makes the first issues more important)
68  * *) large objects are too expensive to be dealt with using copying GC: we handle them
69  *    with mark/sweep during major collections
70  * *) some objects need to not move even if they are small (interned strings, Type handles):
71  *    we use mark/sweep for them, too: they are not allocated in the nursery, but inside
72  *    PinnedChunks regions
73  */
74
75 /*
76  * TODO:
77
78  *) we could have a function pointer in MonoClass to implement
79   customized write barriers for value types
80
81  *) investigate the stuff needed to advance a thread to a GC-safe
82   point (single-stepping, read from unmapped memory etc) and implement it.
83   This would enable us to inline allocations and write barriers, for example,
84   or at least parts of them, like the write barrier checks.
85   We may need this also for handling precise info on stacks, even simple things
86   as having uninitialized data on the stack and having to wait for the prolog
87   to zero it. Not an issue for the last frame that we scan conservatively.
88   We could always not trust the value in the slots anyway.
89
90  *) modify the jit to save info about references in stack locations:
91   this can be done just for locals as a start, so that at least
92   part of the stack is handled precisely.
93
94  *) test/fix endianess issues
95
96  *) Implement a card table as the write barrier instead of remembered
97     sets?  Card tables are not easy to implement with our current
98     memory layout.  We have several different kinds of major heap
99     objects: Small objects in regular blocks, small objects in pinned
100     chunks and LOS objects.  If we just have a pointer we have no way
101     to tell which kind of object it points into, therefore we cannot
102     know where its card table is.  The least we have to do to make
103     this happen is to get rid of write barriers for indirect stores.
104     (See next item)
105
106  *) Get rid of write barriers for indirect stores.  We can do this by
107     telling the GC to wbarrier-register an object once we do an ldloca
108     or ldelema on it, and to unregister it once it's not used anymore
109     (it can only travel downwards on the stack).  The problem with
110     unregistering is that it needs to happen eventually no matter
111     what, even if exceptions are thrown, the thread aborts, etc.
112     Rodrigo suggested that we could do only the registering part and
113     let the collector find out (pessimistically) when it's safe to
114     unregister, namely when the stack pointer of the thread that
115     registered the object is higher than it was when the registering
116     happened.  This might make for a good first implementation to get
117     some data on performance.
118
119  *) Some sort of blacklist support?  Blacklists is a concept from the
120     Boehm GC: if during a conservative scan we find pointers to an
121     area which we might use as heap, we mark that area as unusable, so
122     pointer retention by random pinning pointers is reduced.
123
124  *) experiment with max small object size (very small right now - 2kb,
125     because it's tied to the max freelist size)
126
127   *) add an option to mmap the whole heap in one chunk: it makes for many
128      simplifications in the checks (put the nursery at the top and just use a single
129      check for inclusion/exclusion): the issue this has is that on 32 bit systems it's
130      not flexible (too much of the address space may be used by default or we can't
131      increase the heap as needed) and we'd need a race-free mechanism to return memory
132      back to the system (mprotect(PROT_NONE) will still keep the memory allocated if it
133      was written to, munmap is needed, but the following mmap may not find the same segment
134      free...)
135
136  *) memzero the major fragments after restarting the world and optionally a smaller
137     chunk at a time
138
139  *) investigate having fragment zeroing threads
140
141  *) separate locks for finalization and other minor stuff to reduce
142     lock contention
143
144  *) try a different copying order to improve memory locality
145
146  *) a thread abort after a store but before the write barrier will
147     prevent the write barrier from executing
148
149  *) specialized dynamically generated markers/copiers
150
151  *) Dynamically adjust TLAB size to the number of threads.  If we have
152     too many threads that do allocation, we might need smaller TLABs,
153     and we might get better performance with larger TLABs if we only
154     have a handful of threads.  We could sum up the space left in all
155     assigned TLABs and if that's more than some percentage of the
156     nursery size, reduce the TLAB size.
157
158  *) Explore placing unreachable objects on unused nursery memory.
159         Instead of memset'ng a region to zero, place an int[] covering it.
160         A good place to start is add_nursery_frag. The tricky thing here is
161         placing those objects atomically outside of a collection.
162
163  *) Allocation should use asymmetric Dekker synchronization:
164         http://blogs.oracle.com/dave/resource/Asymmetric-Dekker-Synchronization.txt
165         This should help weak consistency archs.
166  */
167 #include "config.h"
168 #ifdef HAVE_SGEN_GC
169
170 #ifdef __MACH__
171 #undef _XOPEN_SOURCE
172 #define _XOPEN_SOURCE
173 #define _DARWIN_C_SOURCE
174 #endif
175
176 #ifdef HAVE_UNISTD_H
177 #include <unistd.h>
178 #endif
179 #ifdef HAVE_PTHREAD_H
180 #include <pthread.h>
181 #endif
182 #ifdef HAVE_SEMAPHORE_H
183 #include <semaphore.h>
184 #endif
185 #include <stdio.h>
186 #include <string.h>
187 #include <signal.h>
188 #include <errno.h>
189 #include <assert.h>
190
191 #include "metadata/sgen-gc.h"
192 #include "metadata/metadata-internals.h"
193 #include "metadata/class-internals.h"
194 #include "metadata/gc-internal.h"
195 #include "metadata/object-internals.h"
196 #include "metadata/threads.h"
197 #include "metadata/sgen-cardtable.h"
198 #include "metadata/sgen-ssb.h"
199 #include "metadata/sgen-protocol.h"
200 #include "metadata/sgen-archdep.h"
201 #include "metadata/sgen-bridge.h"
202 #include "metadata/sgen-memory-governor.h"
203 #include "metadata/sgen-hash-table.h"
204 #include "metadata/mono-gc.h"
205 #include "metadata/method-builder.h"
206 #include "metadata/profiler-private.h"
207 #include "metadata/monitor.h"
208 #include "metadata/threadpool-internals.h"
209 #include "metadata/mempool-internals.h"
210 #include "metadata/marshal.h"
211 #include "metadata/runtime.h"
212 #include "metadata/sgen-cardtable.h"
213 #include "metadata/sgen-pinning.h"
214 #include "metadata/sgen-workers.h"
215 #include "utils/mono-mmap.h"
216 #include "utils/mono-time.h"
217 #include "utils/mono-semaphore.h"
218 #include "utils/mono-counters.h"
219 #include "utils/mono-proclib.h"
220 #include "utils/mono-memory-model.h"
221 #include "utils/mono-logger-internal.h"
222 #include "utils/dtrace.h"
223
224 #include <mono/utils/mono-logger-internal.h>
225 #include <mono/utils/memcheck.h>
226
227 #if defined(__MACH__)
228 #include "utils/mach-support.h"
229 #endif
230
231 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
232         a = i,
233
234 enum {
235 #include "mono/cil/opcode.def"
236         CEE_LAST
237 };
238
239 #undef OPDEF
240
241 #undef pthread_create
242 #undef pthread_join
243 #undef pthread_detach
244
245 /*
246  * ######################################################################
247  * ########  Types and constants used by the GC.
248  * ######################################################################
249  */
250
251 /* 0 means not initialized, 1 is initialized, -1 means in progress */
252 static int gc_initialized = 0;
253 /* If set, check if we need to do something every X allocations */
254 gboolean has_per_allocation_action;
255 /* If set, do a heap check every X allocation */
256 guint32 verify_before_allocs = 0;
257 /* If set, do a minor collection before every X allocation */
258 guint32 collect_before_allocs = 0;
259 /* If set, do a whole heap check before each collection */
260 static gboolean whole_heap_check_before_collection = FALSE;
261 /* If set, do a heap consistency check before each minor collection */
262 static gboolean consistency_check_at_minor_collection = FALSE;
263 /* If set, check whether mark bits are consistent after major collections */
264 static gboolean check_mark_bits_after_major_collection = FALSE;
265 /* If set, check that all nursery objects are pinned/not pinned, depending on context */
266 static gboolean check_nursery_objects_pinned = FALSE;
267 /* If set, do a few checks when the concurrent collector is used */
268 static gboolean do_concurrent_checks = FALSE;
269 /* If set, check that there are no references to the domain left at domain unload */
270 static gboolean xdomain_checks = FALSE;
271 /* If not null, dump the heap after each collection into this file */
272 static FILE *heap_dump_file = NULL;
273 /* If set, mark stacks conservatively, even if precise marking is possible */
274 static gboolean conservative_stack_mark = FALSE;
275 /* If set, do a plausibility check on the scan_starts before and after
276    each collection */
277 static gboolean do_scan_starts_check = FALSE;
278 static gboolean nursery_collection_is_parallel = FALSE;
279 static gboolean disable_minor_collections = FALSE;
280 static gboolean disable_major_collections = FALSE;
281 gboolean do_pin_stats = FALSE;
282 static gboolean do_verify_nursery = FALSE;
283 static gboolean do_dump_nursery_content = FALSE;
284
285 #ifdef HEAVY_STATISTICS
286 long long stat_objects_alloced_degraded = 0;
287 long long stat_bytes_alloced_degraded = 0;
288
289 long long stat_copy_object_called_nursery = 0;
290 long long stat_objects_copied_nursery = 0;
291 long long stat_copy_object_called_major = 0;
292 long long stat_objects_copied_major = 0;
293
294 long long stat_scan_object_called_nursery = 0;
295 long long stat_scan_object_called_major = 0;
296
297 long long stat_slots_allocated_in_vain;
298
299 long long stat_nursery_copy_object_failed_from_space = 0;
300 long long stat_nursery_copy_object_failed_forwarded = 0;
301 long long stat_nursery_copy_object_failed_pinned = 0;
302 long long stat_nursery_copy_object_failed_to_space = 0;
303
304 static int stat_wbarrier_set_field = 0;
305 static int stat_wbarrier_set_arrayref = 0;
306 static int stat_wbarrier_arrayref_copy = 0;
307 static int stat_wbarrier_generic_store = 0;
308 static int stat_wbarrier_set_root = 0;
309 static int stat_wbarrier_value_copy = 0;
310 static int stat_wbarrier_object_copy = 0;
311 #endif
312
313 int stat_minor_gcs = 0;
314 int stat_major_gcs = 0;
315
316 static long long stat_pinned_objects = 0;
317
318 static long long time_minor_pre_collection_fragment_clear = 0;
319 static long long time_minor_pinning = 0;
320 static long long time_minor_scan_remsets = 0;
321 static long long time_minor_scan_pinned = 0;
322 static long long time_minor_scan_registered_roots = 0;
323 static long long time_minor_scan_thread_data = 0;
324 static long long time_minor_finish_gray_stack = 0;
325 static long long time_minor_fragment_creation = 0;
326
327 static long long time_major_pre_collection_fragment_clear = 0;
328 static long long time_major_pinning = 0;
329 static long long time_major_scan_pinned = 0;
330 static long long time_major_scan_registered_roots = 0;
331 static long long time_major_scan_thread_data = 0;
332 static long long time_major_scan_alloc_pinned = 0;
333 static long long time_major_scan_finalized = 0;
334 static long long time_major_scan_big_objects = 0;
335 static long long time_major_finish_gray_stack = 0;
336 static long long time_major_free_bigobjs = 0;
337 static long long time_major_los_sweep = 0;
338 static long long time_major_sweep = 0;
339 static long long time_major_fragment_creation = 0;
340
341 int gc_debug_level = 0;
342 FILE* gc_debug_file;
343
344 /*
345 void
346 mono_gc_flush_info (void)
347 {
348         fflush (gc_debug_file);
349 }
350 */
351
352 #define TV_DECLARE SGEN_TV_DECLARE
353 #define TV_GETTIME SGEN_TV_GETTIME
354 #define TV_ELAPSED SGEN_TV_ELAPSED
355 #define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
356
357 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
358
359 NurseryClearPolicy nursery_clear_policy = CLEAR_AT_TLAB_CREATION;
360
361 #define object_is_forwarded     SGEN_OBJECT_IS_FORWARDED
362 #define object_is_pinned        SGEN_OBJECT_IS_PINNED
363 #define pin_object              SGEN_PIN_OBJECT
364 #define unpin_object            SGEN_UNPIN_OBJECT
365
366 #define ptr_in_nursery sgen_ptr_in_nursery
367
368 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
369
370 static const char*
371 safe_name (void* obj)
372 {
373         MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
374         return vt->klass->name;
375 }
376
377 #define safe_object_get_size    sgen_safe_object_get_size
378
379 const char*
380 sgen_safe_name (void* obj)
381 {
382         return safe_name (obj);
383 }
384
385 /*
386  * ######################################################################
387  * ########  Global data.
388  * ######################################################################
389  */
390 LOCK_DECLARE (gc_mutex);
391 static int gc_disabled = 0;
392
393 static gboolean use_cardtable;
394
395 #define SCAN_START_SIZE SGEN_SCAN_START_SIZE
396
397 static mword pagesize = 4096;
398 int degraded_mode = 0;
399
400 static mword bytes_pinned_from_failed_allocation = 0;
401
402 GCMemSection *nursery_section = NULL;
403 static mword lowest_heap_address = ~(mword)0;
404 static mword highest_heap_address = 0;
405
406 LOCK_DECLARE (sgen_interruption_mutex);
407 static LOCK_DECLARE (pin_queue_mutex);
408
409 #define LOCK_PIN_QUEUE mono_mutex_lock (&pin_queue_mutex)
410 #define UNLOCK_PIN_QUEUE mono_mutex_unlock (&pin_queue_mutex)
411
412 typedef struct _FinalizeReadyEntry FinalizeReadyEntry;
413 struct _FinalizeReadyEntry {
414         FinalizeReadyEntry *next;
415         void *object;
416 };
417
418 typedef struct _EphemeronLinkNode EphemeronLinkNode;
419
420 struct _EphemeronLinkNode {
421         EphemeronLinkNode *next;
422         char *array;
423 };
424
425 typedef struct {
426        void *key;
427        void *value;
428 } Ephemeron;
429
430 int current_collection_generation = -1;
431 volatile gboolean concurrent_collection_in_progress = FALSE;
432
433 /* objects that are ready to be finalized */
434 static FinalizeReadyEntry *fin_ready_list = NULL;
435 static FinalizeReadyEntry *critical_fin_list = NULL;
436
437 static EphemeronLinkNode *ephemeron_list;
438
439 /* registered roots: the key to the hash is the root start address */
440 /* 
441  * Different kinds of roots are kept separate to speed up pin_from_roots () for example.
442  */
443 SgenHashTable roots_hash [ROOT_TYPE_NUM] = {
444         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
445         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
446         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL)
447 };
448 static mword roots_size = 0; /* amount of memory in the root set */
449
450 #define GC_ROOT_NUM 32
451 typedef struct {
452         int count;              /* must be the first field */
453         void *objects [GC_ROOT_NUM];
454         int root_types [GC_ROOT_NUM];
455         uintptr_t extra_info [GC_ROOT_NUM];
456 } GCRootReport;
457
458 static void
459 notify_gc_roots (GCRootReport *report)
460 {
461         if (!report->count)
462                 return;
463         mono_profiler_gc_roots (report->count, report->objects, report->root_types, report->extra_info);
464         report->count = 0;
465 }
466
467 static void
468 add_profile_gc_root (GCRootReport *report, void *object, int rtype, uintptr_t extra_info)
469 {
470         if (report->count == GC_ROOT_NUM)
471                 notify_gc_roots (report);
472         report->objects [report->count] = object;
473         report->root_types [report->count] = rtype;
474         report->extra_info [report->count++] = (uintptr_t)((MonoVTable*)LOAD_VTABLE (object))->klass;
475 }
476
477 MonoNativeTlsKey thread_info_key;
478
479 #ifdef HAVE_KW_THREAD
480 __thread SgenThreadInfo *sgen_thread_info;
481 __thread gpointer *store_remset_buffer;
482 __thread long store_remset_buffer_index;
483 __thread char *stack_end;
484 __thread long *store_remset_buffer_index_addr;
485 #endif
486
487 /* The size of a TLAB */
488 /* The bigger the value, the less often we have to go to the slow path to allocate a new 
489  * one, but the more space is wasted by threads not allocating much memory.
490  * FIXME: Tune this.
491  * FIXME: Make this self-tuning for each thread.
492  */
493 guint32 tlab_size = (1024 * 4);
494
495 #define MAX_SMALL_OBJ_SIZE      SGEN_MAX_SMALL_OBJ_SIZE
496
497 /* Functions supplied by the runtime to be called by the GC */
498 static MonoGCCallbacks gc_callbacks;
499
500 #define ALLOC_ALIGN             SGEN_ALLOC_ALIGN
501 #define ALLOC_ALIGN_BITS        SGEN_ALLOC_ALIGN_BITS
502
503 #define ALIGN_UP                SGEN_ALIGN_UP
504
505 #define MOVED_OBJECTS_NUM 64
506 static void *moved_objects [MOVED_OBJECTS_NUM];
507 static int moved_objects_idx = 0;
508
509 /* Vtable of the objects used to fill out nursery fragments before a collection */
510 static MonoVTable *array_fill_vtable;
511
512 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
513 MonoNativeThreadId main_gc_thread = NULL;
514 #endif
515
516 /*Object was pinned during the current collection*/
517 static mword objects_pinned;
518
519 /*
520  * ######################################################################
521  * ########  Macros and function declarations.
522  * ######################################################################
523  */
524
525 inline static void*
526 align_pointer (void *ptr)
527 {
528         mword p = (mword)ptr;
529         p += sizeof (gpointer) - 1;
530         p &= ~ (sizeof (gpointer) - 1);
531         return (void*)p;
532 }
533
534 typedef SgenGrayQueue GrayQueue;
535
536 /* forward declarations */
537 static void scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, GrayQueue *queue);
538 static void scan_from_registered_roots (char *addr_start, char *addr_end, int root_type, ScanCopyContext ctx);
539 static void scan_finalizer_entries (FinalizeReadyEntry *list, ScanCopyContext ctx);
540 static void report_finalizer_roots (void);
541 static void report_registered_roots (void);
542
543 static void pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue);
544 static int pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, ScanCopyContext ctx);
545 static void finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue);
546
547 void mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise);
548
549
550 static void init_stats (void);
551
552 static int mark_ephemerons_in_range (ScanCopyContext ctx);
553 static void clear_unreachable_ephemerons (ScanCopyContext ctx);
554 static void null_ephemerons_for_domain (MonoDomain *domain);
555
556 SgenObjectOperations current_object_ops;
557 SgenMajorCollector major_collector;
558 SgenMinorCollector sgen_minor_collector;
559 static GrayQueue gray_queue;
560 static GrayQueue remember_major_objects_gray_queue;
561
562 static SgenRemeberedSet remset;
563
564 /* The gray queue to use from the main collection thread. */
565 #define WORKERS_DISTRIBUTE_GRAY_QUEUE   (&gray_queue)
566
567 /*
568  * The gray queue a worker job must use.  If we're not parallel or
569  * concurrent, we use the main gray queue.
570  */
571 static SgenGrayQueue*
572 sgen_workers_get_job_gray_queue (WorkerData *worker_data)
573 {
574         return worker_data ? &worker_data->private_gray_queue : WORKERS_DISTRIBUTE_GRAY_QUEUE;
575 }
576
577 static gboolean have_non_collection_major_object_remembers = FALSE;
578
579 gboolean
580 sgen_remember_major_object_for_concurrent_mark (char *obj)
581 {
582         if (!major_collector.is_concurrent)
583                 return FALSE;
584
585         g_assert (current_collection_generation == GENERATION_NURSERY || current_collection_generation == -1);
586
587         if (!concurrent_collection_in_progress)
588                 return FALSE;
589
590         GRAY_OBJECT_ENQUEUE (&remember_major_objects_gray_queue, obj);
591
592         if (current_collection_generation != GENERATION_NURSERY) {
593                 /*
594                  * This happens when the mutator allocates large or
595                  * pinned objects or when allocating in degraded
596                  * mode.
597                  */
598                 have_non_collection_major_object_remembers = TRUE;
599         }
600
601         return TRUE;
602 }
603
604 static void
605 gray_queue_redirect (SgenGrayQueue *queue)
606 {
607         gboolean wake = FALSE;
608
609
610         for (;;) {
611                 GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
612                 if (!section)
613                         break;
614                 sgen_section_gray_queue_enqueue (queue->alloc_prepare_data, section);
615                 wake = TRUE;
616         }
617
618         if (wake) {
619                 g_assert (concurrent_collection_in_progress ||
620                                 (current_collection_generation == GENERATION_OLD && major_collector.is_parallel));
621                 if (sgen_workers_have_started ()) {
622                         sgen_workers_wake_up_all ();
623                 } else {
624                         if (concurrent_collection_in_progress)
625                                 g_assert (current_collection_generation == -1);
626                 }
627         }
628 }
629
630 static void
631 redirect_major_object_remembers (void)
632 {
633         gray_queue_redirect (&remember_major_objects_gray_queue);
634         have_non_collection_major_object_remembers = FALSE;
635 }
636
637 static gboolean
638 is_xdomain_ref_allowed (gpointer *ptr, char *obj, MonoDomain *domain)
639 {
640         MonoObject *o = (MonoObject*)(obj);
641         MonoObject *ref = (MonoObject*)*(ptr);
642         int offset = (char*)(ptr) - (char*)o;
643
644         if (o->vtable->klass == mono_defaults.thread_class && offset == G_STRUCT_OFFSET (MonoThread, internal_thread))
645                 return TRUE;
646         if (o->vtable->klass == mono_defaults.internal_thread_class && offset == G_STRUCT_OFFSET (MonoInternalThread, current_appcontext))
647                 return TRUE;
648         if (mono_class_has_parent_fast (o->vtable->klass, mono_defaults.real_proxy_class) &&
649                         offset == G_STRUCT_OFFSET (MonoRealProxy, unwrapped_server))
650                 return TRUE;
651         /* Thread.cached_culture_info */
652         if (!strcmp (ref->vtable->klass->name_space, "System.Globalization") &&
653                         !strcmp (ref->vtable->klass->name, "CultureInfo") &&
654                         !strcmp(o->vtable->klass->name_space, "System") &&
655                         !strcmp(o->vtable->klass->name, "Object[]"))
656                 return TRUE;
657         /*
658          *  at System.IO.MemoryStream.InternalConstructor (byte[],int,int,bool,bool) [0x0004d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:121
659          * at System.IO.MemoryStream..ctor (byte[]) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:81
660          * at (wrapper remoting-invoke-with-check) System.IO.MemoryStream..ctor (byte[]) <IL 0x00020, 0xffffffff>
661          * at System.Runtime.Remoting.Messaging.CADMethodCallMessage.GetArguments () [0x0000d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/CADMessages.cs:327
662          * at System.Runtime.Remoting.Messaging.MethodCall..ctor (System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/MethodCall.cs:87
663          * at System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) [0x00018] in /home/schani/Work/novell/trunk/mcs/class/corlib/System/AppDomain.cs:1213
664          * at (wrapper remoting-invoke-with-check) System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) <IL 0x0003d, 0xffffffff>
665          * at System.Runtime.Remoting.Channels.CrossAppDomainSink.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00008] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Channels/CrossAppDomainChannel.cs:198
666          * at (wrapper runtime-invoke) object.runtime_invoke_CrossAppDomainSink/ProcessMessageRes_object_object (object,intptr,intptr,intptr) <IL 0x0004c, 0xffffffff>
667          */
668         if (!strcmp (ref->vtable->klass->name_space, "System") &&
669                         !strcmp (ref->vtable->klass->name, "Byte[]") &&
670                         !strcmp (o->vtable->klass->name_space, "System.IO") &&
671                         !strcmp (o->vtable->klass->name, "MemoryStream"))
672                 return TRUE;
673         /* append_job() in threadpool.c */
674         if (!strcmp (ref->vtable->klass->name_space, "System.Runtime.Remoting.Messaging") &&
675                         !strcmp (ref->vtable->klass->name, "AsyncResult") &&
676                         !strcmp (o->vtable->klass->name_space, "System") &&
677                         !strcmp (o->vtable->klass->name, "Object[]") &&
678                         mono_thread_pool_is_queue_array ((MonoArray*) o))
679                 return TRUE;
680         return FALSE;
681 }
682
683 static void
684 check_reference_for_xdomain (gpointer *ptr, char *obj, MonoDomain *domain)
685 {
686         MonoObject *o = (MonoObject*)(obj);
687         MonoObject *ref = (MonoObject*)*(ptr);
688         int offset = (char*)(ptr) - (char*)o;
689         MonoClass *class;
690         MonoClassField *field;
691         char *str;
692
693         if (!ref || ref->vtable->domain == domain)
694                 return;
695         if (is_xdomain_ref_allowed (ptr, obj, domain))
696                 return;
697
698         field = NULL;
699         for (class = o->vtable->klass; class; class = class->parent) {
700                 int i;
701
702                 for (i = 0; i < class->field.count; ++i) {
703                         if (class->fields[i].offset == offset) {
704                                 field = &class->fields[i];
705                                 break;
706                         }
707                 }
708                 if (field)
709                         break;
710         }
711
712         if (ref->vtable->klass == mono_defaults.string_class)
713                 str = mono_string_to_utf8 ((MonoString*)ref);
714         else
715                 str = NULL;
716         g_print ("xdomain reference in %p (%s.%s) at offset %d (%s) to %p (%s.%s) (%s)  -  pointed to by:\n",
717                         o, o->vtable->klass->name_space, o->vtable->klass->name,
718                         offset, field ? field->name : "",
719                         ref, ref->vtable->klass->name_space, ref->vtable->klass->name, str ? str : "");
720         mono_gc_scan_for_specific_ref (o, TRUE);
721         if (str)
722                 g_free (str);
723 }
724
725 #undef HANDLE_PTR
726 #define HANDLE_PTR(ptr,obj)     check_reference_for_xdomain ((ptr), (obj), domain)
727
728 static void
729 scan_object_for_xdomain_refs (char *start, mword size, void *data)
730 {
731         MonoDomain *domain = ((MonoObject*)start)->vtable->domain;
732
733         #include "sgen-scan-object.h"
734 }
735
736 static gboolean scan_object_for_specific_ref_precise = TRUE;
737
738 #undef HANDLE_PTR
739 #define HANDLE_PTR(ptr,obj) do {                \
740         if ((MonoObject*)*(ptr) == key) {       \
741         g_print ("found ref to %p in object %p (%s) at offset %td\n",   \
742                         key, (obj), safe_name ((obj)), ((char*)(ptr) - (char*)(obj))); \
743         }                                                               \
744         } while (0)
745
746 static void
747 scan_object_for_specific_ref (char *start, MonoObject *key)
748 {
749         char *forwarded;
750
751         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (start)))
752                 start = forwarded;
753
754         if (scan_object_for_specific_ref_precise) {
755                 #include "sgen-scan-object.h"
756         } else {
757                 mword *words = (mword*)start;
758                 size_t size = safe_object_get_size ((MonoObject*)start);
759                 int i;
760                 for (i = 0; i < size / sizeof (mword); ++i) {
761                         if (words [i] == (mword)key) {
762                                 g_print ("found possible ref to %p in object %p (%s) at offset %td\n",
763                                                 key, start, safe_name (start), i * sizeof (mword));
764                         }
765                 }
766         }
767 }
768
769 void
770 sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags)
771 {
772         while (start < end) {
773                 size_t size;
774                 char *obj;
775
776                 if (!*(void**)start) {
777                         start += sizeof (void*); /* should be ALLOC_ALIGN, really */
778                         continue;
779                 }
780
781                 if (allow_flags) {
782                         if (!(obj = SGEN_OBJECT_IS_FORWARDED (start)))
783                                 obj = start;
784                 } else {
785                         obj = start;
786                 }
787
788                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)obj));
789
790                 if ((MonoVTable*)SGEN_LOAD_VTABLE (obj) != array_fill_vtable)
791                         callback (obj, size, data);
792
793                 start += size;
794         }
795 }
796
797 static void
798 scan_object_for_specific_ref_callback (char *obj, size_t size, MonoObject *key)
799 {
800         scan_object_for_specific_ref (obj, key);
801 }
802
803 static void
804 check_root_obj_specific_ref (RootRecord *root, MonoObject *key, MonoObject *obj)
805 {
806         if (key != obj)
807                 return;
808         g_print ("found ref to %p in root record %p\n", key, root);
809 }
810
811 static MonoObject *check_key = NULL;
812 static RootRecord *check_root = NULL;
813
814 static void
815 check_root_obj_specific_ref_from_marker (void **obj)
816 {
817         check_root_obj_specific_ref (check_root, check_key, *obj);
818 }
819
820 static void
821 scan_roots_for_specific_ref (MonoObject *key, int root_type)
822 {
823         void **start_root;
824         RootRecord *root;
825         check_key = key;
826
827         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
828                 mword desc = root->root_desc;
829
830                 check_root = root;
831
832                 switch (desc & ROOT_DESC_TYPE_MASK) {
833                 case ROOT_DESC_BITMAP:
834                         desc >>= ROOT_DESC_TYPE_SHIFT;
835                         while (desc) {
836                                 if (desc & 1)
837                                         check_root_obj_specific_ref (root, key, *start_root);
838                                 desc >>= 1;
839                                 start_root++;
840                         }
841                         return;
842                 case ROOT_DESC_COMPLEX: {
843                         gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
844                         int bwords = (*bitmap_data) - 1;
845                         void **start_run = start_root;
846                         bitmap_data++;
847                         while (bwords-- > 0) {
848                                 gsize bmap = *bitmap_data++;
849                                 void **objptr = start_run;
850                                 while (bmap) {
851                                         if (bmap & 1)
852                                                 check_root_obj_specific_ref (root, key, *objptr);
853                                         bmap >>= 1;
854                                         ++objptr;
855                                 }
856                                 start_run += GC_BITS_PER_WORD;
857                         }
858                         break;
859                 }
860                 case ROOT_DESC_USER: {
861                         MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
862                         marker (start_root, check_root_obj_specific_ref_from_marker);
863                         break;
864                 }
865                 case ROOT_DESC_RUN_LEN:
866                         g_assert_not_reached ();
867                 default:
868                         g_assert_not_reached ();
869                 }
870         } SGEN_HASH_TABLE_FOREACH_END;
871
872         check_key = NULL;
873         check_root = NULL;
874 }
875
876 void
877 mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise)
878 {
879         void **ptr;
880         RootRecord *root;
881
882         scan_object_for_specific_ref_precise = precise;
883
884         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
885                         (IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key, TRUE);
886
887         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
888
889         sgen_los_iterate_objects ((IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
890
891         scan_roots_for_specific_ref (key, ROOT_TYPE_NORMAL);
892         scan_roots_for_specific_ref (key, ROOT_TYPE_WBARRIER);
893
894         SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], ptr, root) {
895                 while (ptr < (void**)root->end_root) {
896                         check_root_obj_specific_ref (root, *ptr, key);
897                         ++ptr;
898                 }
899         } SGEN_HASH_TABLE_FOREACH_END;
900 }
901
902 static gboolean
903 need_remove_object_for_domain (char *start, MonoDomain *domain)
904 {
905         if (mono_object_domain (start) == domain) {
906                 SGEN_LOG (4, "Need to cleanup object %p", start);
907                 binary_protocol_cleanup (start, (gpointer)LOAD_VTABLE (start), safe_object_get_size ((MonoObject*)start));
908                 return TRUE;
909         }
910         return FALSE;
911 }
912
913 static void
914 process_object_for_domain_clearing (char *start, MonoDomain *domain)
915 {
916         GCVTable *vt = (GCVTable*)LOAD_VTABLE (start);
917         if (vt->klass == mono_defaults.internal_thread_class)
918                 g_assert (mono_object_domain (start) == mono_get_root_domain ());
919         /* The object could be a proxy for an object in the domain
920            we're deleting. */
921         if (mono_class_has_parent_fast (vt->klass, mono_defaults.real_proxy_class)) {
922                 MonoObject *server = ((MonoRealProxy*)start)->unwrapped_server;
923
924                 /* The server could already have been zeroed out, so
925                    we need to check for that, too. */
926                 if (server && (!LOAD_VTABLE (server) || mono_object_domain (server) == domain)) {
927                         SGEN_LOG (4, "Cleaning up remote pointer in %p to object %p", start, server);
928                         ((MonoRealProxy*)start)->unwrapped_server = NULL;
929                 }
930         }
931 }
932
933 static MonoDomain *check_domain = NULL;
934
935 static void
936 check_obj_not_in_domain (void **o)
937 {
938         g_assert (((MonoObject*)(*o))->vtable->domain != check_domain);
939 }
940
941 static void
942 scan_for_registered_roots_in_domain (MonoDomain *domain, int root_type)
943 {
944         void **start_root;
945         RootRecord *root;
946         check_domain = domain;
947         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
948                 mword desc = root->root_desc;
949
950                 /* The MonoDomain struct is allowed to hold
951                    references to objects in its own domain. */
952                 if (start_root == (void**)domain)
953                         continue;
954
955                 switch (desc & ROOT_DESC_TYPE_MASK) {
956                 case ROOT_DESC_BITMAP:
957                         desc >>= ROOT_DESC_TYPE_SHIFT;
958                         while (desc) {
959                                 if ((desc & 1) && *start_root)
960                                         check_obj_not_in_domain (*start_root);
961                                 desc >>= 1;
962                                 start_root++;
963                         }
964                         break;
965                 case ROOT_DESC_COMPLEX: {
966                         gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
967                         int bwords = (*bitmap_data) - 1;
968                         void **start_run = start_root;
969                         bitmap_data++;
970                         while (bwords-- > 0) {
971                                 gsize bmap = *bitmap_data++;
972                                 void **objptr = start_run;
973                                 while (bmap) {
974                                         if ((bmap & 1) && *objptr)
975                                                 check_obj_not_in_domain (*objptr);
976                                         bmap >>= 1;
977                                         ++objptr;
978                                 }
979                                 start_run += GC_BITS_PER_WORD;
980                         }
981                         break;
982                 }
983                 case ROOT_DESC_USER: {
984                         MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
985                         marker (start_root, check_obj_not_in_domain);
986                         break;
987                 }
988                 case ROOT_DESC_RUN_LEN:
989                         g_assert_not_reached ();
990                 default:
991                         g_assert_not_reached ();
992                 }
993         } SGEN_HASH_TABLE_FOREACH_END;
994
995         check_domain = NULL;
996 }
997
998 static void
999 check_for_xdomain_refs (void)
1000 {
1001         LOSObject *bigobj;
1002
1003         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
1004                         (IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL, FALSE);
1005
1006         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL);
1007
1008         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
1009                 scan_object_for_xdomain_refs (bigobj->data, sgen_los_object_size (bigobj), NULL);
1010 }
1011
1012 static gboolean
1013 clear_domain_process_object (char *obj, MonoDomain *domain)
1014 {
1015         gboolean remove;
1016
1017         process_object_for_domain_clearing (obj, domain);
1018         remove = need_remove_object_for_domain (obj, domain);
1019
1020         if (remove && ((MonoObject*)obj)->synchronisation) {
1021                 void **dislink = mono_monitor_get_object_monitor_weak_link ((MonoObject*)obj);
1022                 if (dislink)
1023                         sgen_register_disappearing_link (NULL, dislink, FALSE, TRUE);
1024         }
1025
1026         return remove;
1027 }
1028
1029 static void
1030 clear_domain_process_minor_object_callback (char *obj, size_t size, MonoDomain *domain)
1031 {
1032         if (clear_domain_process_object (obj, domain))
1033                 memset (obj, 0, size);
1034 }
1035
1036 static void
1037 clear_domain_process_major_object_callback (char *obj, size_t size, MonoDomain *domain)
1038 {
1039         clear_domain_process_object (obj, domain);
1040 }
1041
1042 static void
1043 clear_domain_free_major_non_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
1044 {
1045         if (need_remove_object_for_domain (obj, domain))
1046                 major_collector.free_non_pinned_object (obj, size);
1047 }
1048
1049 static void
1050 clear_domain_free_major_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
1051 {
1052         if (need_remove_object_for_domain (obj, domain))
1053                 major_collector.free_pinned_object (obj, size);
1054 }
1055
1056 /*
1057  * When appdomains are unloaded we can easily remove objects that have finalizers,
1058  * but all the others could still be present in random places on the heap.
1059  * We need a sweep to get rid of them even though it's going to be costly
1060  * with big heaps.
1061  * The reason we need to remove them is because we access the vtable and class
1062  * structures to know the object size and the reference bitmap: once the domain is
1063  * unloaded the point to random memory.
1064  */
1065 void
1066 mono_gc_clear_domain (MonoDomain * domain)
1067 {
1068         LOSObject *bigobj, *prev;
1069         int i;
1070
1071         LOCK_GC;
1072
1073         sgen_process_fin_stage_entries ();
1074         sgen_process_dislink_stage_entries ();
1075
1076         sgen_clear_nursery_fragments ();
1077
1078         if (xdomain_checks && domain != mono_get_root_domain ()) {
1079                 scan_for_registered_roots_in_domain (domain, ROOT_TYPE_NORMAL);
1080                 scan_for_registered_roots_in_domain (domain, ROOT_TYPE_WBARRIER);
1081                 check_for_xdomain_refs ();
1082         }
1083
1084         /*Ephemerons and dislinks must be processed before LOS since they might end up pointing
1085         to memory returned to the OS.*/
1086         null_ephemerons_for_domain (domain);
1087
1088         for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
1089                 sgen_null_links_for_domain (domain, i);
1090
1091         for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
1092                 sgen_remove_finalizers_for_domain (domain, i);
1093
1094         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
1095                         (IterateObjectCallbackFunc)clear_domain_process_minor_object_callback, domain, FALSE);
1096
1097         /* We need two passes over major and large objects because
1098            freeing such objects might give their memory back to the OS
1099            (in the case of large objects) or obliterate its vtable
1100            (pinned objects with major-copying or pinned and non-pinned
1101            objects with major-mark&sweep), but we might need to
1102            dereference a pointer from an object to another object if
1103            the first object is a proxy. */
1104         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)clear_domain_process_major_object_callback, domain);
1105         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
1106                 clear_domain_process_object (bigobj->data, domain);
1107
1108         prev = NULL;
1109         for (bigobj = los_object_list; bigobj;) {
1110                 if (need_remove_object_for_domain (bigobj->data, domain)) {
1111                         LOSObject *to_free = bigobj;
1112                         if (prev)
1113                                 prev->next = bigobj->next;
1114                         else
1115                                 los_object_list = bigobj->next;
1116                         bigobj = bigobj->next;
1117                         SGEN_LOG (4, "Freeing large object %p", bigobj->data);
1118                         sgen_los_free_object (to_free);
1119                         continue;
1120                 }
1121                 prev = bigobj;
1122                 bigobj = bigobj->next;
1123         }
1124         major_collector.iterate_objects (TRUE, FALSE, (IterateObjectCallbackFunc)clear_domain_free_major_non_pinned_object_callback, domain);
1125         major_collector.iterate_objects (FALSE, TRUE, (IterateObjectCallbackFunc)clear_domain_free_major_pinned_object_callback, domain);
1126
1127         if (G_UNLIKELY (do_pin_stats)) {
1128                 if (domain == mono_get_root_domain ())
1129                         sgen_pin_stats_print_class_stats ();
1130         }
1131
1132         UNLOCK_GC;
1133 }
1134
1135 /*
1136  * sgen_add_to_global_remset:
1137  *
1138  *   The global remset contains locations which point into newspace after
1139  * a minor collection. This can happen if the objects they point to are pinned.
1140  *
1141  * LOCKING: If called from a parallel collector, the global remset
1142  * lock must be held.  For serial collectors that is not necessary.
1143  */
1144 void
1145 sgen_add_to_global_remset (gpointer ptr, gpointer obj)
1146 {
1147         if (!major_collector.is_concurrent) {
1148                 g_assert (object_is_pinned (obj));
1149                 g_assert (current_collection_generation != -1);
1150         }
1151
1152         /*
1153          * During concurrent collections we must always record global
1154          * remsets because cementing is reset at the end of the
1155          * concurrent collection, so we cannot miss a major->minor
1156          * reference.
1157          *
1158          * The reason we cannot reset cementing at the start of a
1159          * concurrent collection is that the nursery collections
1160          * running concurrently must keep pinning the cemented
1161          * objects, exactly because we don't have the global remsets
1162          * that point to them anymore.
1163          *
1164          * This results in nursery collections still being slowed down
1165          * by oft-referenced pinned objects during concurrent
1166          * collections.  One solution would be to keep separate,
1167          * dedicated global remset card tables during concurrent
1168          * collections, and when finishing the concurrent collection
1169          * to merge them into the main card table.
1170          *
1171          * To simplify and save memory, it should be possible to use
1172          * the mod union card table for that purpose: During
1173          * concurrent collections, always record global remsets to the
1174          * mod union card table.  When finishing the concurrent
1175          * collection, reset cementing, and when scanning the mod
1176          * union table, record global remsets again, like always.  The
1177          * downside to this is that we still have a long pause during
1178          * which all those objects must be scanned to process the
1179          * references.
1180          *
1181          * An alternative might be to reset cementing at the start of
1182          * concurrent collections in such a way that nursery
1183          * collections happening during the major collection still pin
1184          * the formerly cemented objects.  We'd just need a shadow
1185          * cementing table for that purpose.  The nursery collections
1186          * still work with the old cementing table (can they cement
1187          * new objects?), while the major collector builds up a new
1188          * cementing table, adding global remsets whenever needed like
1189          * usual.  When the major collector finishes, the old
1190          * cementing table is replaced by the new one.
1191          */
1192         if (!concurrent_collection_in_progress &&
1193                         sgen_cement_lookup_or_register (obj, current_collection_generation != -1)) {
1194                 return;
1195         }
1196
1197         remset.record_pointer (ptr);
1198
1199 #ifdef ENABLE_DTRACE
1200         if (G_UNLIKELY (do_pin_stats))
1201                 sgen_pin_stats_register_global_remset (obj);
1202
1203         SGEN_LOG (8, "Adding global remset for %p", ptr);
1204         binary_protocol_global_remset (ptr, obj, (gpointer)SGEN_LOAD_VTABLE (obj));
1205
1206         HEAVY_STAT (++stat_global_remsets_added);
1207
1208         if (G_UNLIKELY (MONO_GC_GLOBAL_REMSET_ADD_ENABLED ())) {
1209                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
1210                 MONO_GC_GLOBAL_REMSET_ADD ((mword)ptr, (mword)obj, sgen_safe_object_get_size (obj),
1211                                 vt->klass->name_space, vt->klass->name);
1212         }
1213 #endif
1214 }
1215
1216 /*
1217  * sgen_drain_gray_stack:
1218  *
1219  *   Scan objects in the gray stack until the stack is empty. This should be called
1220  * frequently after each object is copied, to achieve better locality and cache
1221  * usage.
1222  */
1223 gboolean
1224 sgen_drain_gray_stack (int max_objs, ScanCopyContext ctx)
1225 {
1226         char *obj;
1227         ScanObjectFunc scan_func = ctx.scan_func;
1228         GrayQueue *queue = ctx.queue;
1229
1230         if (max_objs == -1) {
1231                 for (;;) {
1232                         GRAY_OBJECT_DEQUEUE (queue, obj);
1233                         if (!obj)
1234                                 return TRUE;
1235                         SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, safe_name (obj));
1236                         scan_func (obj, queue);
1237                 }
1238         } else {
1239                 int i;
1240
1241                 do {
1242                         for (i = 0; i != max_objs; ++i) {
1243                                 GRAY_OBJECT_DEQUEUE (queue, obj);
1244                                 if (!obj)
1245                                         return TRUE;
1246                                 SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, safe_name (obj));
1247                                 scan_func (obj, queue);
1248                         }
1249                 } while (max_objs < 0);
1250                 return FALSE;
1251         }
1252 }
1253
1254 /*
1255  * Addresses from start to end are already sorted. This function finds
1256  * the object header for each address and pins the object. The
1257  * addresses must be inside the passed section.  The (start of the)
1258  * address array is overwritten with the addresses of the actually
1259  * pinned objects.  Return the number of pinned objects.
1260  */
1261 static int
1262 pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, ScanCopyContext ctx)
1263 {
1264         void *last = NULL;
1265         int count = 0;
1266         void *search_start;
1267         void *last_obj = NULL;
1268         size_t last_obj_size = 0;
1269         void *addr;
1270         int idx;
1271         void **definitely_pinned = start;
1272         ScanObjectFunc scan_func = ctx.scan_func;
1273         SgenGrayQueue *queue = ctx.queue;
1274
1275         sgen_nursery_allocator_prepare_for_pinning ();
1276
1277         while (start < end) {
1278                 addr = *start;
1279                 /* the range check should be reduntant */
1280                 if (addr != last && addr >= start_nursery && addr < end_nursery) {
1281                         SGEN_LOG (5, "Considering pinning addr %p", addr);
1282                         /* multiple pointers to the same object */
1283                         if (addr >= last_obj && (char*)addr < (char*)last_obj + last_obj_size) {
1284                                 start++;
1285                                 continue;
1286                         }
1287                         idx = ((char*)addr - (char*)section->data) / SCAN_START_SIZE;
1288                         g_assert (idx < section->num_scan_start);
1289                         search_start = (void*)section->scan_starts [idx];
1290                         if (!search_start || search_start > addr) {
1291                                 while (idx) {
1292                                         --idx;
1293                                         search_start = section->scan_starts [idx];
1294                                         if (search_start && search_start <= addr)
1295                                                 break;
1296                                 }
1297                                 if (!search_start || search_start > addr)
1298                                         search_start = start_nursery;
1299                         }
1300                         if (search_start < last_obj)
1301                                 search_start = (char*)last_obj + last_obj_size;
1302                         /* now addr should be in an object a short distance from search_start
1303                          * Note that search_start must point to zeroed mem or point to an object.
1304                          */
1305
1306                         do {
1307                                 if (!*(void**)search_start) {
1308                                         /* Consistency check */
1309                                         /*
1310                                         for (frag = nursery_fragments; frag; frag = frag->next) {
1311                                                 if (search_start >= frag->fragment_start && search_start < frag->fragment_end)
1312                                                         g_assert_not_reached ();
1313                                         }
1314                                         */
1315
1316                                         search_start = (void*)ALIGN_UP ((mword)search_start + sizeof (gpointer));
1317                                         continue;
1318                                 }
1319                                 last_obj = search_start;
1320                                 last_obj_size = ALIGN_UP (safe_object_get_size ((MonoObject*)search_start));
1321
1322                                 if (((MonoObject*)last_obj)->synchronisation == GINT_TO_POINTER (-1)) {
1323                                         /* Marks the beginning of a nursery fragment, skip */
1324                                 } else {
1325                                         SGEN_LOG (8, "Pinned try match %p (%s), size %zd", last_obj, safe_name (last_obj), last_obj_size);
1326                                         if (addr >= search_start && (char*)addr < (char*)last_obj + last_obj_size) {
1327                                                 if (scan_func) {
1328                                                         scan_func (search_start, queue);
1329                                                 } else {
1330                                                         SGEN_LOG (4, "Pinned object %p, vtable %p (%s), count %d\n",
1331                                                                         search_start, *(void**)search_start, safe_name (search_start), count);
1332                                                         binary_protocol_pin (search_start,
1333                                                                         (gpointer)LOAD_VTABLE (search_start),
1334                                                                         safe_object_get_size (search_start));
1335
1336 #ifdef ENABLE_DTRACE
1337                                                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1338                                                                 int gen = sgen_ptr_in_nursery (search_start) ? GENERATION_NURSERY : GENERATION_OLD;
1339                                                                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (search_start);
1340                                                                 MONO_GC_OBJ_PINNED ((mword)search_start,
1341                                                                                 sgen_safe_object_get_size (search_start),
1342                                                                                 vt->klass->name_space, vt->klass->name, gen);
1343                                                         }
1344 #endif
1345
1346                                                         pin_object (search_start);
1347                                                         GRAY_OBJECT_ENQUEUE (queue, search_start);
1348                                                         if (G_UNLIKELY (do_pin_stats))
1349                                                                 sgen_pin_stats_register_object (search_start, last_obj_size);
1350                                                         definitely_pinned [count] = search_start;
1351                                                         count++;
1352                                                 }
1353                                                 break;
1354                                         }
1355                                 }
1356                                 /* skip to the next object */
1357                                 search_start = (void*)((char*)search_start + last_obj_size);
1358                         } while (search_start <= addr);
1359                         /* we either pinned the correct object or we ignored the addr because
1360                          * it points to unused zeroed memory.
1361                          */
1362                         last = addr;
1363                 }
1364                 start++;
1365         }
1366         //printf ("effective pinned: %d (at the end: %d)\n", count, (char*)end_nursery - (char*)last);
1367         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS) {
1368                 GCRootReport report;
1369                 report.count = 0;
1370                 for (idx = 0; idx < count; ++idx)
1371                         add_profile_gc_root (&report, definitely_pinned [idx], MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
1372                 notify_gc_roots (&report);
1373         }
1374         stat_pinned_objects += count;
1375         return count;
1376 }
1377
1378 void
1379 sgen_pin_objects_in_section (GCMemSection *section, ScanCopyContext ctx)
1380 {
1381         int num_entries = section->pin_queue_num_entries;
1382         if (num_entries) {
1383                 void **start = section->pin_queue_start;
1384                 int reduced_to;
1385                 reduced_to = pin_objects_from_addresses (section, start, start + num_entries,
1386                                 section->data, section->next_data, ctx);
1387                 section->pin_queue_num_entries = reduced_to;
1388                 if (!reduced_to)
1389                         section->pin_queue_start = NULL;
1390         }
1391 }
1392
1393
1394 void
1395 sgen_pin_object (void *object, GrayQueue *queue)
1396 {
1397         g_assert (!concurrent_collection_in_progress);
1398
1399         if (sgen_collection_is_parallel ()) {
1400                 LOCK_PIN_QUEUE;
1401                 /*object arrives pinned*/
1402                 sgen_pin_stage_ptr (object);
1403                 ++objects_pinned ;
1404                 UNLOCK_PIN_QUEUE;
1405         } else {
1406                 SGEN_PIN_OBJECT (object);
1407                 sgen_pin_stage_ptr (object);
1408                 ++objects_pinned;
1409                 if (G_UNLIKELY (do_pin_stats))
1410                         sgen_pin_stats_register_object (object, safe_object_get_size (object));
1411         }
1412         GRAY_OBJECT_ENQUEUE (queue, object);
1413         binary_protocol_pin (object, (gpointer)LOAD_VTABLE (object), safe_object_get_size (object));
1414
1415 #ifdef ENABLE_DTRACE
1416         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1417                 int gen = sgen_ptr_in_nursery (object) ? GENERATION_NURSERY : GENERATION_OLD;
1418                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (object);
1419                 MONO_GC_OBJ_PINNED ((mword)object, sgen_safe_object_get_size (object), vt->klass->name_space, vt->klass->name, gen);
1420         }
1421 #endif
1422 }
1423
1424 void
1425 sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue)
1426 {
1427         for (;;) {
1428                 mword vtable_word;
1429                 gboolean major_pinned = FALSE;
1430
1431                 if (sgen_ptr_in_nursery (obj)) {
1432                         if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
1433                                 sgen_pin_object (obj, queue);
1434                                 break;
1435                         }
1436                 } else {
1437                         major_collector.pin_major_object (obj, queue);
1438                         major_pinned = TRUE;
1439                 }
1440
1441                 vtable_word = *(mword*)obj;
1442                 /*someone else forwarded it, update the pointer and bail out*/
1443                 if (vtable_word & SGEN_FORWARDED_BIT) {
1444                         *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1445                         break;
1446                 }
1447
1448                 /*someone pinned it, nothing to do.*/
1449                 if (vtable_word & SGEN_PINNED_BIT || major_pinned)
1450                         break;
1451         }
1452 }
1453
1454 /* Sort the addresses in array in increasing order.
1455  * Done using a by-the book heap sort. Which has decent and stable performance, is pretty cache efficient.
1456  */
1457 void
1458 sgen_sort_addresses (void **array, int size)
1459 {
1460         int i;
1461         void *tmp;
1462
1463         for (i = 1; i < size; ++i) {
1464                 int child = i;
1465                 while (child > 0) {
1466                         int parent = (child - 1) / 2;
1467
1468                         if (array [parent] >= array [child])
1469                                 break;
1470
1471                         tmp = array [parent];
1472                         array [parent] = array [child];
1473                         array [child] = tmp;
1474
1475                         child = parent;
1476                 }
1477         }
1478
1479         for (i = size - 1; i > 0; --i) {
1480                 int end, root;
1481                 tmp = array [i];
1482                 array [i] = array [0];
1483                 array [0] = tmp;
1484
1485                 end = i - 1;
1486                 root = 0;
1487
1488                 while (root * 2 + 1 <= end) {
1489                         int child = root * 2 + 1;
1490
1491                         if (child < end && array [child] < array [child + 1])
1492                                 ++child;
1493                         if (array [root] >= array [child])
1494                                 break;
1495
1496                         tmp = array [root];
1497                         array [root] = array [child];
1498                         array [child] = tmp;
1499
1500                         root = child;
1501                 }
1502         }
1503 }
1504
1505 /* 
1506  * Scan the memory between start and end and queue values which could be pointers
1507  * to the area between start_nursery and end_nursery for later consideration.
1508  * Typically used for thread stacks.
1509  */
1510 static void
1511 conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type)
1512 {
1513         int count = 0;
1514
1515 #ifdef VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE
1516         VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE (start, (char*)end - (char*)start);
1517 #endif
1518
1519         while (start < end) {
1520                 if (*start >= start_nursery && *start < end_nursery) {
1521                         /*
1522                          * *start can point to the middle of an object
1523                          * note: should we handle pointing at the end of an object?
1524                          * pinning in C# code disallows pointing at the end of an object
1525                          * but there is some small chance that an optimizing C compiler
1526                          * may keep the only reference to an object by pointing
1527                          * at the end of it. We ignore this small chance for now.
1528                          * Pointers to the end of an object are indistinguishable
1529                          * from pointers to the start of the next object in memory
1530                          * so if we allow that we'd need to pin two objects...
1531                          * We queue the pointer in an array, the
1532                          * array will then be sorted and uniqued. This way
1533                          * we can coalesce several pinning pointers and it should
1534                          * be faster since we'd do a memory scan with increasing
1535                          * addresses. Note: we can align the address to the allocation
1536                          * alignment, so the unique process is more effective.
1537                          */
1538                         mword addr = (mword)*start;
1539                         addr &= ~(ALLOC_ALIGN - 1);
1540                         if (addr >= (mword)start_nursery && addr < (mword)end_nursery) {
1541                                 SGEN_LOG (6, "Pinning address %p from %p", (void*)addr, start);
1542                                 sgen_pin_stage_ptr ((void*)addr);
1543                                 count++;
1544                         }
1545                         if (G_UNLIKELY (do_pin_stats)) { 
1546                                 if (ptr_in_nursery ((void*)addr))
1547                                         sgen_pin_stats_register_address ((char*)addr, pin_type);
1548                         }
1549                 }
1550                 start++;
1551         }
1552         if (count)
1553                 SGEN_LOG (7, "found %d potential pinned heap pointers", count);
1554 }
1555
1556 /*
1557  * The first thing we do in a collection is to identify pinned objects.
1558  * This function considers all the areas of memory that need to be
1559  * conservatively scanned.
1560  */
1561 static void
1562 pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue)
1563 {
1564         void **start_root;
1565         RootRecord *root;
1566         SGEN_LOG (2, "Scanning pinned roots (%d bytes, %d/%d entries)", (int)roots_size, roots_hash [ROOT_TYPE_NORMAL].num_entries, roots_hash [ROOT_TYPE_PINNED].num_entries);
1567         /* objects pinned from the API are inside these roots */
1568         SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], start_root, root) {
1569                 SGEN_LOG (6, "Pinned roots %p-%p", start_root, root->end_root);
1570                 conservatively_pin_objects_from (start_root, (void**)root->end_root, start_nursery, end_nursery, PIN_TYPE_OTHER);
1571         } SGEN_HASH_TABLE_FOREACH_END;
1572         /* now deal with the thread stacks
1573          * in the future we should be able to conservatively scan only:
1574          * *) the cpu registers
1575          * *) the unmanaged stack frames
1576          * *) the _last_ managed stack frame
1577          * *) pointers slots in managed frames
1578          */
1579         scan_thread_data (start_nursery, end_nursery, FALSE, queue);
1580 }
1581
1582 static void
1583 unpin_objects_from_queue (SgenGrayQueue *queue)
1584 {
1585         for (;;) {
1586                 char *addr;
1587                 GRAY_OBJECT_DEQUEUE (queue, addr);
1588                 if (!addr)
1589                         break;
1590                 g_assert (SGEN_OBJECT_IS_PINNED (addr));
1591                 SGEN_UNPIN_OBJECT (addr);
1592         }
1593 }
1594
1595 typedef struct {
1596         CopyOrMarkObjectFunc func;
1597         GrayQueue *queue;
1598 } UserCopyOrMarkData;
1599
1600 static MonoNativeTlsKey user_copy_or_mark_key;
1601
1602 static void
1603 init_user_copy_or_mark_key (void)
1604 {
1605         mono_native_tls_alloc (&user_copy_or_mark_key, NULL);
1606 }
1607
1608 static void
1609 set_user_copy_or_mark_data (UserCopyOrMarkData *data)
1610 {
1611         mono_native_tls_set_value (user_copy_or_mark_key, data);
1612 }
1613
1614 static void
1615 single_arg_user_copy_or_mark (void **obj)
1616 {
1617         UserCopyOrMarkData *data = mono_native_tls_get_value (user_copy_or_mark_key);
1618
1619         data->func (obj, data->queue);
1620 }
1621
1622 /*
1623  * The memory area from start_root to end_root contains pointers to objects.
1624  * Their position is precisely described by @desc (this means that the pointer
1625  * can be either NULL or the pointer to the start of an object).
1626  * This functions copies them to to_space updates them.
1627  *
1628  * This function is not thread-safe!
1629  */
1630 static void
1631 precisely_scan_objects_from (void** start_root, void** end_root, char* n_start, char *n_end, mword desc, ScanCopyContext ctx)
1632 {
1633         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
1634         SgenGrayQueue *queue = ctx.queue;
1635
1636         switch (desc & ROOT_DESC_TYPE_MASK) {
1637         case ROOT_DESC_BITMAP:
1638                 desc >>= ROOT_DESC_TYPE_SHIFT;
1639                 while (desc) {
1640                         if ((desc & 1) && *start_root) {
1641                                 copy_func (start_root, queue);
1642                                 SGEN_LOG (9, "Overwrote root at %p with %p", start_root, *start_root);
1643                                 sgen_drain_gray_stack (-1, ctx);
1644                         }
1645                         desc >>= 1;
1646                         start_root++;
1647                 }
1648                 return;
1649         case ROOT_DESC_COMPLEX: {
1650                 gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
1651                 int bwords = (*bitmap_data) - 1;
1652                 void **start_run = start_root;
1653                 bitmap_data++;
1654                 while (bwords-- > 0) {
1655                         gsize bmap = *bitmap_data++;
1656                         void **objptr = start_run;
1657                         while (bmap) {
1658                                 if ((bmap & 1) && *objptr) {
1659                                         copy_func (objptr, queue);
1660                                         SGEN_LOG (9, "Overwrote root at %p with %p", objptr, *objptr);
1661                                         sgen_drain_gray_stack (-1, ctx);
1662                                 }
1663                                 bmap >>= 1;
1664                                 ++objptr;
1665                         }
1666                         start_run += GC_BITS_PER_WORD;
1667                 }
1668                 break;
1669         }
1670         case ROOT_DESC_USER: {
1671                 UserCopyOrMarkData data = { copy_func, queue };
1672                 MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
1673                 set_user_copy_or_mark_data (&data);
1674                 marker (start_root, single_arg_user_copy_or_mark);
1675                 set_user_copy_or_mark_data (NULL);
1676                 break;
1677         }
1678         case ROOT_DESC_RUN_LEN:
1679                 g_assert_not_reached ();
1680         default:
1681                 g_assert_not_reached ();
1682         }
1683 }
1684
1685 static void
1686 reset_heap_boundaries (void)
1687 {
1688         lowest_heap_address = ~(mword)0;
1689         highest_heap_address = 0;
1690 }
1691
1692 void
1693 sgen_update_heap_boundaries (mword low, mword high)
1694 {
1695         mword old;
1696
1697         do {
1698                 old = lowest_heap_address;
1699                 if (low >= old)
1700                         break;
1701         } while (SGEN_CAS_PTR ((gpointer*)&lowest_heap_address, (gpointer)low, (gpointer)old) != (gpointer)old);
1702
1703         do {
1704                 old = highest_heap_address;
1705                 if (high <= old)
1706                         break;
1707         } while (SGEN_CAS_PTR ((gpointer*)&highest_heap_address, (gpointer)high, (gpointer)old) != (gpointer)old);
1708 }
1709
1710 /*
1711  * Allocate and setup the data structures needed to be able to allocate objects
1712  * in the nursery. The nursery is stored in nursery_section.
1713  */
1714 static void
1715 alloc_nursery (void)
1716 {
1717         GCMemSection *section;
1718         char *data;
1719         int scan_starts;
1720         int alloc_size;
1721
1722         if (nursery_section)
1723                 return;
1724         SGEN_LOG (2, "Allocating nursery size: %lu", (unsigned long)sgen_nursery_size);
1725         /* later we will alloc a larger area for the nursery but only activate
1726          * what we need. The rest will be used as expansion if we have too many pinned
1727          * objects in the existing nursery.
1728          */
1729         /* FIXME: handle OOM */
1730         section = sgen_alloc_internal (INTERNAL_MEM_SECTION);
1731
1732         alloc_size = sgen_nursery_size;
1733
1734         /* If there isn't enough space even for the nursery we should simply abort. */
1735         g_assert (sgen_memgov_try_alloc_space (alloc_size, SPACE_NURSERY));
1736
1737 #ifdef SGEN_ALIGN_NURSERY
1738         data = major_collector.alloc_heap (alloc_size, alloc_size, DEFAULT_NURSERY_BITS);
1739 #else
1740         data = major_collector.alloc_heap (alloc_size, 0, DEFAULT_NURSERY_BITS);
1741 #endif
1742         sgen_update_heap_boundaries ((mword)data, (mword)(data + sgen_nursery_size));
1743         SGEN_LOG (4, "Expanding nursery size (%p-%p): %lu, total: %lu", data, data + alloc_size, (unsigned long)sgen_nursery_size, (unsigned long)mono_gc_get_heap_size ());
1744         section->data = section->next_data = data;
1745         section->size = alloc_size;
1746         section->end_data = data + sgen_nursery_size;
1747         scan_starts = (alloc_size + SCAN_START_SIZE - 1) / SCAN_START_SIZE;
1748         section->scan_starts = sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts, INTERNAL_MEM_SCAN_STARTS, TRUE);
1749         section->num_scan_start = scan_starts;
1750
1751         nursery_section = section;
1752
1753         sgen_nursery_allocator_set_nursery_bounds (data, data + sgen_nursery_size);
1754 }
1755
1756 void*
1757 mono_gc_get_nursery (int *shift_bits, size_t *size)
1758 {
1759         *size = sgen_nursery_size;
1760 #ifdef SGEN_ALIGN_NURSERY
1761         *shift_bits = DEFAULT_NURSERY_BITS;
1762 #else
1763         *shift_bits = -1;
1764 #endif
1765         return sgen_get_nursery_start ();
1766 }
1767
1768 void
1769 mono_gc_set_current_thread_appdomain (MonoDomain *domain)
1770 {
1771         SgenThreadInfo *info = mono_thread_info_current ();
1772
1773         /* Could be called from sgen_thread_unregister () with a NULL info */
1774         if (domain) {
1775                 g_assert (info);
1776                 info->stopped_domain = domain;
1777         }
1778 }
1779
1780 gboolean
1781 mono_gc_precise_stack_mark_enabled (void)
1782 {
1783         return !conservative_stack_mark;
1784 }
1785
1786 FILE *
1787 mono_gc_get_logfile (void)
1788 {
1789         return gc_debug_file;
1790 }
1791
1792 static void
1793 report_finalizer_roots_list (FinalizeReadyEntry *list)
1794 {
1795         GCRootReport report;
1796         FinalizeReadyEntry *fin;
1797
1798         report.count = 0;
1799         for (fin = list; fin; fin = fin->next) {
1800                 if (!fin->object)
1801                         continue;
1802                 add_profile_gc_root (&report, fin->object, MONO_PROFILE_GC_ROOT_FINALIZER, 0);
1803         }
1804         notify_gc_roots (&report);
1805 }
1806
1807 static void
1808 report_finalizer_roots (void)
1809 {
1810         report_finalizer_roots_list (fin_ready_list);
1811         report_finalizer_roots_list (critical_fin_list);
1812 }
1813
1814 static GCRootReport *root_report;
1815
1816 static void
1817 single_arg_report_root (void **obj)
1818 {
1819         if (*obj)
1820                 add_profile_gc_root (root_report, *obj, MONO_PROFILE_GC_ROOT_OTHER, 0);
1821 }
1822
1823 static void
1824 precisely_report_roots_from (GCRootReport *report, void** start_root, void** end_root, mword desc)
1825 {
1826         switch (desc & ROOT_DESC_TYPE_MASK) {
1827         case ROOT_DESC_BITMAP:
1828                 desc >>= ROOT_DESC_TYPE_SHIFT;
1829                 while (desc) {
1830                         if ((desc & 1) && *start_root) {
1831                                 add_profile_gc_root (report, *start_root, MONO_PROFILE_GC_ROOT_OTHER, 0);
1832                         }
1833                         desc >>= 1;
1834                         start_root++;
1835                 }
1836                 return;
1837         case ROOT_DESC_COMPLEX: {
1838                 gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
1839                 int bwords = (*bitmap_data) - 1;
1840                 void **start_run = start_root;
1841                 bitmap_data++;
1842                 while (bwords-- > 0) {
1843                         gsize bmap = *bitmap_data++;
1844                         void **objptr = start_run;
1845                         while (bmap) {
1846                                 if ((bmap & 1) && *objptr) {
1847                                         add_profile_gc_root (report, *objptr, MONO_PROFILE_GC_ROOT_OTHER, 0);
1848                                 }
1849                                 bmap >>= 1;
1850                                 ++objptr;
1851                         }
1852                         start_run += GC_BITS_PER_WORD;
1853                 }
1854                 break;
1855         }
1856         case ROOT_DESC_USER: {
1857                 MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
1858                 root_report = report;
1859                 marker (start_root, single_arg_report_root);
1860                 break;
1861         }
1862         case ROOT_DESC_RUN_LEN:
1863                 g_assert_not_reached ();
1864         default:
1865                 g_assert_not_reached ();
1866         }
1867 }
1868
1869 static void
1870 report_registered_roots_by_type (int root_type)
1871 {
1872         GCRootReport report;
1873         void **start_root;
1874         RootRecord *root;
1875         report.count = 0;
1876         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
1877                 SGEN_LOG (6, "Precise root scan %p-%p (desc: %p)", start_root, root->end_root, (void*)root->root_desc);
1878                 precisely_report_roots_from (&report, start_root, (void**)root->end_root, root->root_desc);
1879         } SGEN_HASH_TABLE_FOREACH_END;
1880         notify_gc_roots (&report);
1881 }
1882
1883 static void
1884 report_registered_roots (void)
1885 {
1886         report_registered_roots_by_type (ROOT_TYPE_NORMAL);
1887         report_registered_roots_by_type (ROOT_TYPE_WBARRIER);
1888 }
1889
1890 static void
1891 scan_finalizer_entries (FinalizeReadyEntry *list, ScanCopyContext ctx)
1892 {
1893         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
1894         SgenGrayQueue *queue = ctx.queue;
1895         FinalizeReadyEntry *fin;
1896
1897         for (fin = list; fin; fin = fin->next) {
1898                 if (!fin->object)
1899                         continue;
1900                 SGEN_LOG (5, "Scan of fin ready object: %p (%s)\n", fin->object, safe_name (fin->object));
1901                 copy_func (&fin->object, queue);
1902         }
1903 }
1904
1905 static const char*
1906 generation_name (int generation)
1907 {
1908         switch (generation) {
1909         case GENERATION_NURSERY: return "nursery";
1910         case GENERATION_OLD: return "old";
1911         default: g_assert_not_reached ();
1912         }
1913 }
1914
1915 const char*
1916 sgen_generation_name (int generation)
1917 {
1918         return generation_name (generation);
1919 }
1920
1921 SgenObjectOperations *
1922 sgen_get_current_object_ops (void){
1923         return &current_object_ops;
1924 }
1925
1926
1927 static void
1928 finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue)
1929 {
1930         TV_DECLARE (atv);
1931         TV_DECLARE (btv);
1932         int done_with_ephemerons, ephemeron_rounds = 0;
1933         CopyOrMarkObjectFunc copy_func = current_object_ops.copy_or_mark_object;
1934         ScanObjectFunc scan_func = current_object_ops.scan_object;
1935         ScanCopyContext ctx = { scan_func, copy_func, queue };
1936
1937         /*
1938          * We copied all the reachable objects. Now it's the time to copy
1939          * the objects that were not referenced by the roots, but by the copied objects.
1940          * we built a stack of objects pointed to by gray_start: they are
1941          * additional roots and we may add more items as we go.
1942          * We loop until gray_start == gray_objects which means no more objects have
1943          * been added. Note this is iterative: no recursion is involved.
1944          * We need to walk the LO list as well in search of marked big objects
1945          * (use a flag since this is needed only on major collections). We need to loop
1946          * here as well, so keep a counter of marked LO (increasing it in copy_object).
1947          *   To achieve better cache locality and cache usage, we drain the gray stack 
1948          * frequently, after each object is copied, and just finish the work here.
1949          */
1950         sgen_drain_gray_stack (-1, ctx);
1951         TV_GETTIME (atv);
1952         SGEN_LOG (2, "%s generation done", generation_name (generation));
1953
1954         /*
1955         Reset bridge data, we might have lingering data from a previous collection if this is a major
1956         collection trigged by minor overflow.
1957
1958         We must reset the gathered bridges since their original block might be evacuated due to major
1959         fragmentation in the meanwhile and the bridge code should not have to deal with that.
1960         */
1961         sgen_bridge_reset_data ();
1962
1963         /*
1964          * Walk the ephemeron tables marking all values with reachable keys. This must be completely done
1965          * before processing finalizable objects and non-tracking weak links to avoid finalizing/clearing
1966          * objects that are in fact reachable.
1967          */
1968         done_with_ephemerons = 0;
1969         do {
1970                 done_with_ephemerons = mark_ephemerons_in_range (ctx);
1971                 sgen_drain_gray_stack (-1, ctx);
1972                 ++ephemeron_rounds;
1973         } while (!done_with_ephemerons);
1974
1975         sgen_scan_togglerefs (start_addr, end_addr, ctx);
1976         if (generation == GENERATION_OLD)
1977                 sgen_scan_togglerefs (sgen_get_nursery_start (), sgen_get_nursery_end (), ctx);
1978
1979         if (sgen_need_bridge_processing ()) {
1980                 sgen_collect_bridge_objects (generation, ctx);
1981                 if (generation == GENERATION_OLD)
1982                         sgen_collect_bridge_objects (GENERATION_NURSERY, ctx);
1983         }
1984
1985         /*
1986         Make sure we drain the gray stack before processing disappearing links and finalizers.
1987         If we don't make sure it is empty we might wrongly see a live object as dead.
1988         */
1989         sgen_drain_gray_stack (-1, ctx);
1990
1991         /*
1992         We must clear weak links that don't track resurrection before processing object ready for
1993         finalization so they can be cleared before that.
1994         */
1995         sgen_null_link_in_range (generation, TRUE, ctx);
1996         if (generation == GENERATION_OLD)
1997                 sgen_null_link_in_range (GENERATION_NURSERY, TRUE, ctx);
1998
1999
2000         /* walk the finalization queue and move also the objects that need to be
2001          * finalized: use the finalized objects as new roots so the objects they depend
2002          * on are also not reclaimed. As with the roots above, only objects in the nursery
2003          * are marked/copied.
2004          */
2005         sgen_finalize_in_range (generation, ctx);
2006         if (generation == GENERATION_OLD)
2007                 sgen_finalize_in_range (GENERATION_NURSERY, ctx);
2008         /* drain the new stack that might have been created */
2009         SGEN_LOG (6, "Precise scan of gray area post fin");
2010         sgen_drain_gray_stack (-1, ctx);
2011
2012         /*
2013          * This must be done again after processing finalizable objects since CWL slots are cleared only after the key is finalized.
2014          */
2015         done_with_ephemerons = 0;
2016         do {
2017                 done_with_ephemerons = mark_ephemerons_in_range (ctx);
2018                 sgen_drain_gray_stack (-1, ctx);
2019                 ++ephemeron_rounds;
2020         } while (!done_with_ephemerons);
2021
2022         /*
2023          * Clear ephemeron pairs with unreachable keys.
2024          * We pass the copy func so we can figure out if an array was promoted or not.
2025          */
2026         clear_unreachable_ephemerons (ctx);
2027
2028         TV_GETTIME (btv);
2029         SGEN_LOG (2, "Finalize queue handling scan for %s generation: %d usecs %d ephemeron rounds", generation_name (generation), TV_ELAPSED (atv, btv), ephemeron_rounds);
2030
2031         /*
2032          * handle disappearing links
2033          * Note we do this after checking the finalization queue because if an object
2034          * survives (at least long enough to be finalized) we don't clear the link.
2035          * This also deals with a possible issue with the monitor reclamation: with the Boehm
2036          * GC a finalized object my lose the monitor because it is cleared before the finalizer is
2037          * called.
2038          */
2039         g_assert (sgen_gray_object_queue_is_empty (queue));
2040         for (;;) {
2041                 sgen_null_link_in_range (generation, FALSE, ctx);
2042                 if (generation == GENERATION_OLD)
2043                         sgen_null_link_in_range (GENERATION_NURSERY, FALSE, ctx);
2044                 if (sgen_gray_object_queue_is_empty (queue))
2045                         break;
2046                 sgen_drain_gray_stack (-1, ctx);
2047         }
2048
2049         g_assert (sgen_gray_object_queue_is_empty (queue));
2050 }
2051
2052 void
2053 sgen_check_section_scan_starts (GCMemSection *section)
2054 {
2055         int i;
2056         for (i = 0; i < section->num_scan_start; ++i) {
2057                 if (section->scan_starts [i]) {
2058                         guint size = safe_object_get_size ((MonoObject*) section->scan_starts [i]);
2059                         g_assert (size >= sizeof (MonoObject) && size <= MAX_SMALL_OBJ_SIZE);
2060                 }
2061         }
2062 }
2063
2064 static void
2065 check_scan_starts (void)
2066 {
2067         if (!do_scan_starts_check)
2068                 return;
2069         sgen_check_section_scan_starts (nursery_section);
2070         major_collector.check_scan_starts ();
2071 }
2072
2073 static void
2074 scan_from_registered_roots (char *addr_start, char *addr_end, int root_type, ScanCopyContext ctx)
2075 {
2076         void **start_root;
2077         RootRecord *root;
2078         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
2079                 SGEN_LOG (6, "Precise root scan %p-%p (desc: %p)", start_root, root->end_root, (void*)root->root_desc);
2080                 precisely_scan_objects_from (start_root, (void**)root->end_root, addr_start, addr_end, root->root_desc, ctx);
2081         } SGEN_HASH_TABLE_FOREACH_END;
2082 }
2083
2084 void
2085 sgen_dump_occupied (char *start, char *end, char *section_start)
2086 {
2087         fprintf (heap_dump_file, "<occupied offset=\"%td\" size=\"%td\"/>\n", start - section_start, end - start);
2088 }
2089
2090 void
2091 sgen_dump_section (GCMemSection *section, const char *type)
2092 {
2093         char *start = section->data;
2094         char *end = section->data + section->size;
2095         char *occ_start = NULL;
2096         GCVTable *vt;
2097         char *old_start = NULL; /* just for debugging */
2098
2099         fprintf (heap_dump_file, "<section type=\"%s\" size=\"%lu\">\n", type, (unsigned long)section->size);
2100
2101         while (start < end) {
2102                 guint size;
2103                 MonoClass *class;
2104
2105                 if (!*(void**)start) {
2106                         if (occ_start) {
2107                                 sgen_dump_occupied (occ_start, start, section->data);
2108                                 occ_start = NULL;
2109                         }
2110                         start += sizeof (void*); /* should be ALLOC_ALIGN, really */
2111                         continue;
2112                 }
2113                 g_assert (start < section->next_data);
2114
2115                 if (!occ_start)
2116                         occ_start = start;
2117
2118                 vt = (GCVTable*)LOAD_VTABLE (start);
2119                 class = vt->klass;
2120
2121                 size = ALIGN_UP (safe_object_get_size ((MonoObject*) start));
2122
2123                 /*
2124                 fprintf (heap_dump_file, "<object offset=\"%d\" class=\"%s.%s\" size=\"%d\"/>\n",
2125                                 start - section->data,
2126                                 vt->klass->name_space, vt->klass->name,
2127                                 size);
2128                 */
2129
2130                 old_start = start;
2131                 start += size;
2132         }
2133         if (occ_start)
2134                 sgen_dump_occupied (occ_start, start, section->data);
2135
2136         fprintf (heap_dump_file, "</section>\n");
2137 }
2138
2139 static void
2140 dump_object (MonoObject *obj, gboolean dump_location)
2141 {
2142         static char class_name [1024];
2143
2144         MonoClass *class = mono_object_class (obj);
2145         int i, j;
2146
2147         /*
2148          * Python's XML parser is too stupid to parse angle brackets
2149          * in strings, so we just ignore them;
2150          */
2151         i = j = 0;
2152         while (class->name [i] && j < sizeof (class_name) - 1) {
2153                 if (!strchr ("<>\"", class->name [i]))
2154                         class_name [j++] = class->name [i];
2155                 ++i;
2156         }
2157         g_assert (j < sizeof (class_name));
2158         class_name [j] = 0;
2159
2160         fprintf (heap_dump_file, "<object class=\"%s.%s\" size=\"%d\"",
2161                         class->name_space, class_name,
2162                         safe_object_get_size (obj));
2163         if (dump_location) {
2164                 const char *location;
2165                 if (ptr_in_nursery (obj))
2166                         location = "nursery";
2167                 else if (safe_object_get_size (obj) <= MAX_SMALL_OBJ_SIZE)
2168                         location = "major";
2169                 else
2170                         location = "LOS";
2171                 fprintf (heap_dump_file, " location=\"%s\"", location);
2172         }
2173         fprintf (heap_dump_file, "/>\n");
2174 }
2175
2176 static void
2177 dump_heap (const char *type, int num, const char *reason)
2178 {
2179         ObjectList *list;
2180         LOSObject *bigobj;
2181
2182         fprintf (heap_dump_file, "<collection type=\"%s\" num=\"%d\"", type, num);
2183         if (reason)
2184                 fprintf (heap_dump_file, " reason=\"%s\"", reason);
2185         fprintf (heap_dump_file, ">\n");
2186         fprintf (heap_dump_file, "<other-mem-usage type=\"mempools\" size=\"%ld\"/>\n", mono_mempool_get_bytes_allocated ());
2187         sgen_dump_internal_mem_usage (heap_dump_file);
2188         fprintf (heap_dump_file, "<pinned type=\"stack\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_STACK));
2189         /* fprintf (heap_dump_file, "<pinned type=\"static-data\" bytes=\"%d\"/>\n", pinned_byte_counts [PIN_TYPE_STATIC_DATA]); */
2190         fprintf (heap_dump_file, "<pinned type=\"other\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_OTHER));
2191
2192         fprintf (heap_dump_file, "<pinned-objects>\n");
2193         for (list = sgen_pin_stats_get_object_list (); list; list = list->next)
2194                 dump_object (list->obj, TRUE);
2195         fprintf (heap_dump_file, "</pinned-objects>\n");
2196
2197         sgen_dump_section (nursery_section, "nursery");
2198
2199         major_collector.dump_heap (heap_dump_file);
2200
2201         fprintf (heap_dump_file, "<los>\n");
2202         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
2203                 dump_object ((MonoObject*)bigobj->data, FALSE);
2204         fprintf (heap_dump_file, "</los>\n");
2205
2206         fprintf (heap_dump_file, "</collection>\n");
2207 }
2208
2209 void
2210 sgen_register_moved_object (void *obj, void *destination)
2211 {
2212         g_assert (mono_profiler_events & MONO_PROFILE_GC_MOVES);
2213
2214         /* FIXME: handle this for parallel collector */
2215         g_assert (!sgen_collection_is_parallel ());
2216
2217         if (moved_objects_idx == MOVED_OBJECTS_NUM) {
2218                 mono_profiler_gc_moves (moved_objects, moved_objects_idx);
2219                 moved_objects_idx = 0;
2220         }
2221         moved_objects [moved_objects_idx++] = obj;
2222         moved_objects [moved_objects_idx++] = destination;
2223 }
2224
2225 static void
2226 init_stats (void)
2227 {
2228         static gboolean inited = FALSE;
2229
2230         if (inited)
2231                 return;
2232
2233         mono_counters_register ("Minor fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pre_collection_fragment_clear);
2234         mono_counters_register ("Minor pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pinning);
2235         mono_counters_register ("Minor scan remembered set", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_remsets);
2236         mono_counters_register ("Minor scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_pinned);
2237         mono_counters_register ("Minor scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_registered_roots);
2238         mono_counters_register ("Minor scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_thread_data);
2239         mono_counters_register ("Minor finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_finish_gray_stack);
2240         mono_counters_register ("Minor fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_fragment_creation);
2241
2242         mono_counters_register ("Major fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pre_collection_fragment_clear);
2243         mono_counters_register ("Major pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pinning);
2244         mono_counters_register ("Major scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_pinned);
2245         mono_counters_register ("Major scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_registered_roots);
2246         mono_counters_register ("Major scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_thread_data);
2247         mono_counters_register ("Major scan alloc_pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_alloc_pinned);
2248         mono_counters_register ("Major scan finalized", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_finalized);
2249         mono_counters_register ("Major scan big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_big_objects);
2250         mono_counters_register ("Major finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_finish_gray_stack);
2251         mono_counters_register ("Major free big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_free_bigobjs);
2252         mono_counters_register ("Major LOS sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_los_sweep);
2253         mono_counters_register ("Major sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_sweep);
2254         mono_counters_register ("Major fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_fragment_creation);
2255
2256         mono_counters_register ("Number of pinned objects", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_pinned_objects);
2257
2258 #ifdef HEAVY_STATISTICS
2259         mono_counters_register ("WBarrier set field", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_field);
2260         mono_counters_register ("WBarrier set arrayref", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_arrayref);
2261         mono_counters_register ("WBarrier arrayref copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_arrayref_copy);
2262         mono_counters_register ("WBarrier generic store called", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_generic_store);
2263         mono_counters_register ("WBarrier set root", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_root);
2264         mono_counters_register ("WBarrier value copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_value_copy);
2265         mono_counters_register ("WBarrier object copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_object_copy);
2266
2267         mono_counters_register ("# objects allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced_degraded);
2268         mono_counters_register ("bytes allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_degraded);
2269
2270         mono_counters_register ("# copy_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_nursery);
2271         mono_counters_register ("# objects copied (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_nursery);
2272         mono_counters_register ("# copy_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_major);
2273         mono_counters_register ("# objects copied (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_major);
2274
2275         mono_counters_register ("# scan_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_nursery);
2276         mono_counters_register ("# scan_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_major);
2277
2278         mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
2279
2280         mono_counters_register ("# nursery copy_object() failed from space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_from_space);
2281         mono_counters_register ("# nursery copy_object() failed forwarded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_forwarded);
2282         mono_counters_register ("# nursery copy_object() failed pinned", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_pinned);
2283         mono_counters_register ("# nursery copy_object() failed to space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_to_space);
2284
2285         sgen_nursery_allocator_init_heavy_stats ();
2286         sgen_alloc_init_heavy_stats ();
2287 #endif
2288
2289         inited = TRUE;
2290 }
2291
2292
2293 static void
2294 reset_pinned_from_failed_allocation (void)
2295 {
2296         bytes_pinned_from_failed_allocation = 0;
2297 }
2298
2299 void
2300 sgen_set_pinned_from_failed_allocation (mword objsize)
2301 {
2302         bytes_pinned_from_failed_allocation += objsize;
2303 }
2304
2305 gboolean
2306 sgen_collection_is_parallel (void)
2307 {
2308         switch (current_collection_generation) {
2309         case GENERATION_NURSERY:
2310                 return nursery_collection_is_parallel;
2311         case GENERATION_OLD:
2312                 return major_collector.is_parallel;
2313         default:
2314                 g_error ("Invalid current generation %d", current_collection_generation);
2315         }
2316 }
2317
2318 gboolean
2319 sgen_collection_is_concurrent (void)
2320 {
2321         switch (current_collection_generation) {
2322         case GENERATION_NURSERY:
2323                 return FALSE;
2324         case GENERATION_OLD:
2325                 return major_collector.is_concurrent;
2326         default:
2327                 g_error ("Invalid current generation %d", current_collection_generation);
2328         }
2329 }
2330
2331 gboolean
2332 sgen_concurrent_collection_in_progress (void)
2333 {
2334         return concurrent_collection_in_progress;
2335 }
2336
2337 typedef struct
2338 {
2339         char *heap_start;
2340         char *heap_end;
2341 } FinishRememberedSetScanJobData;
2342
2343 static void
2344 job_finish_remembered_set_scan (WorkerData *worker_data, void *job_data_untyped)
2345 {
2346         FinishRememberedSetScanJobData *job_data = job_data_untyped;
2347
2348         remset.finish_scan_remsets (job_data->heap_start, job_data->heap_end, sgen_workers_get_job_gray_queue (worker_data));
2349         sgen_free_internal_dynamic (job_data, sizeof (FinishRememberedSetScanJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2350 }
2351
2352 typedef struct
2353 {
2354         CopyOrMarkObjectFunc copy_or_mark_func;
2355         ScanObjectFunc scan_func;
2356         char *heap_start;
2357         char *heap_end;
2358         int root_type;
2359 } ScanFromRegisteredRootsJobData;
2360
2361 static void
2362 job_scan_from_registered_roots (WorkerData *worker_data, void *job_data_untyped)
2363 {
2364         ScanFromRegisteredRootsJobData *job_data = job_data_untyped;
2365         ScanCopyContext ctx = { job_data->scan_func, job_data->copy_or_mark_func,
2366                 sgen_workers_get_job_gray_queue (worker_data) };
2367
2368         scan_from_registered_roots (job_data->heap_start, job_data->heap_end, job_data->root_type, ctx);
2369         sgen_free_internal_dynamic (job_data, sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2370 }
2371
2372 typedef struct
2373 {
2374         char *heap_start;
2375         char *heap_end;
2376 } ScanThreadDataJobData;
2377
2378 static void
2379 job_scan_thread_data (WorkerData *worker_data, void *job_data_untyped)
2380 {
2381         ScanThreadDataJobData *job_data = job_data_untyped;
2382
2383         scan_thread_data (job_data->heap_start, job_data->heap_end, TRUE,
2384                         sgen_workers_get_job_gray_queue (worker_data));
2385         sgen_free_internal_dynamic (job_data, sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2386 }
2387
2388 typedef struct
2389 {
2390         FinalizeReadyEntry *list;
2391 } ScanFinalizerEntriesJobData;
2392
2393 static void
2394 job_scan_finalizer_entries (WorkerData *worker_data, void *job_data_untyped)
2395 {
2396         ScanFinalizerEntriesJobData *job_data = job_data_untyped;
2397         ScanCopyContext ctx = { NULL, current_object_ops.copy_or_mark_object, sgen_workers_get_job_gray_queue (worker_data) };
2398
2399         scan_finalizer_entries (job_data->list, ctx);
2400         sgen_free_internal_dynamic (job_data, sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2401 }
2402
2403 static void
2404 job_scan_major_mod_union_cardtable (WorkerData *worker_data, void *job_data_untyped)
2405 {
2406         g_assert (concurrent_collection_in_progress);
2407         major_collector.scan_card_table (TRUE, sgen_workers_get_job_gray_queue (worker_data));
2408 }
2409
2410 static void
2411 job_scan_los_mod_union_cardtable (WorkerData *worker_data, void *job_data_untyped)
2412 {
2413         g_assert (concurrent_collection_in_progress);
2414         sgen_los_scan_card_table (TRUE, sgen_workers_get_job_gray_queue (worker_data));
2415 }
2416
2417 static void
2418 verify_scan_starts (char *start, char *end)
2419 {
2420         int i;
2421
2422         for (i = 0; i < nursery_section->num_scan_start; ++i) {
2423                 char *addr = nursery_section->scan_starts [i];
2424                 if (addr > start && addr < end)
2425                         SGEN_LOG (1, "NFC-BAD SCAN START [%d] %p for obj [%p %p]", i, addr, start, end);
2426         }
2427 }
2428
2429 static void
2430 verify_nursery (void)
2431 {
2432         char *start, *end, *cur, *hole_start;
2433
2434         if (!do_verify_nursery)
2435                 return;
2436
2437         /*This cleans up unused fragments */
2438         sgen_nursery_allocator_prepare_for_pinning ();
2439
2440         hole_start = start = cur = sgen_get_nursery_start ();
2441         end = sgen_get_nursery_end ();
2442
2443         while (cur < end) {
2444                 size_t ss, size;
2445
2446                 if (!*(void**)cur) {
2447                         cur += sizeof (void*);
2448                         continue;
2449                 }
2450
2451                 if (object_is_forwarded (cur))
2452                         SGEN_LOG (1, "FORWARDED OBJ %p", cur);
2453                 else if (object_is_pinned (cur))
2454                         SGEN_LOG (1, "PINNED OBJ %p", cur);
2455
2456                 ss = safe_object_get_size ((MonoObject*)cur);
2457                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)cur));
2458                 verify_scan_starts (cur, cur + size);
2459                 if (do_dump_nursery_content) {
2460                         if (cur > hole_start)
2461                                 SGEN_LOG (1, "HOLE [%p %p %d]", hole_start, cur, (int)(cur - hole_start));
2462                         SGEN_LOG (1, "OBJ  [%p %p %d %d %s %d]", cur, cur + size, (int)size, (int)ss, sgen_safe_name ((MonoObject*)cur), (gpointer)LOAD_VTABLE (cur) == sgen_get_array_fill_vtable ());
2463                 }
2464                 cur += size;
2465                 hole_start = cur;
2466         }
2467 }
2468
2469 /*
2470  * Checks that no objects in the nursery are fowarded or pinned.  This
2471  * is a precondition to restarting the mutator while doing a
2472  * concurrent collection.  Note that we don't clear fragments because
2473  * we depend on that having happened earlier.
2474  */
2475 static void
2476 check_nursery_is_clean (void)
2477 {
2478         char *start, *end, *cur;
2479
2480         start = cur = sgen_get_nursery_start ();
2481         end = sgen_get_nursery_end ();
2482
2483         while (cur < end) {
2484                 size_t ss, size;
2485
2486                 if (!*(void**)cur) {
2487                         cur += sizeof (void*);
2488                         continue;
2489                 }
2490
2491                 g_assert (!object_is_forwarded (cur));
2492                 g_assert (!object_is_pinned (cur));
2493
2494                 ss = safe_object_get_size ((MonoObject*)cur);
2495                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)cur));
2496                 verify_scan_starts (cur, cur + size);
2497
2498                 cur += size;
2499         }
2500 }
2501
2502 static void
2503 init_gray_queue (void)
2504 {
2505         if (sgen_collection_is_parallel () || sgen_collection_is_concurrent ()) {
2506                 sgen_workers_init_distribute_gray_queue ();
2507                 sgen_gray_object_queue_init_with_alloc_prepare (&gray_queue, NULL,
2508                                 gray_queue_redirect, sgen_workers_get_distribute_section_gray_queue ());
2509         } else {
2510                 sgen_gray_object_queue_init (&gray_queue, NULL);
2511         }
2512
2513         if (major_collector.is_concurrent) {
2514                 sgen_gray_object_queue_init_with_alloc_prepare (&remember_major_objects_gray_queue, NULL,
2515                                 gray_queue_redirect, sgen_workers_get_distribute_section_gray_queue ());
2516         } else {
2517                 sgen_gray_object_queue_init_invalid (&remember_major_objects_gray_queue);
2518         }
2519 }
2520
2521 static void
2522 pin_stage_object_callback (char *obj, size_t size, void *data)
2523 {
2524         sgen_pin_stage_ptr (obj);
2525         /* FIXME: do pin stats if enabled */
2526 }
2527
2528 /*
2529  * Collect objects in the nursery.  Returns whether to trigger a major
2530  * collection.
2531  */
2532 static gboolean
2533 collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
2534 {
2535         gboolean needs_major;
2536         size_t max_garbage_amount;
2537         char *nursery_next;
2538         FinishRememberedSetScanJobData *frssjd;
2539         ScanFromRegisteredRootsJobData *scrrjd_normal, *scrrjd_wbarrier;
2540         ScanFinalizerEntriesJobData *sfejd_fin_ready, *sfejd_critical_fin;
2541         ScanThreadDataJobData *stdjd;
2542         mword fragment_total;
2543         ScanCopyContext ctx;
2544         TV_DECLARE (all_atv);
2545         TV_DECLARE (all_btv);
2546         TV_DECLARE (atv);
2547         TV_DECLARE (btv);
2548
2549         if (disable_minor_collections)
2550                 return TRUE;
2551
2552         MONO_GC_BEGIN (GENERATION_NURSERY);
2553         binary_protocol_collection_begin (stat_minor_gcs, GENERATION_NURSERY);
2554
2555         verify_nursery ();
2556
2557 #ifndef DISABLE_PERFCOUNTERS
2558         mono_perfcounters->gc_collections0++;
2559 #endif
2560
2561         current_collection_generation = GENERATION_NURSERY;
2562         if (sgen_collection_is_parallel ())
2563                 current_object_ops = sgen_minor_collector.parallel_ops;
2564         else
2565                 current_object_ops = sgen_minor_collector.serial_ops;
2566         
2567         reset_pinned_from_failed_allocation ();
2568
2569         check_scan_starts ();
2570
2571         sgen_nursery_alloc_prepare_for_minor ();
2572
2573         degraded_mode = 0;
2574         objects_pinned = 0;
2575         nursery_next = sgen_nursery_alloc_get_upper_alloc_bound ();
2576         /* FIXME: optimize later to use the higher address where an object can be present */
2577         nursery_next = MAX (nursery_next, sgen_get_nursery_end ());
2578
2579         SGEN_LOG (1, "Start nursery collection %d %p-%p, size: %d", stat_minor_gcs, sgen_get_nursery_start (), nursery_next, (int)(nursery_next - sgen_get_nursery_start ()));
2580         max_garbage_amount = nursery_next - sgen_get_nursery_start ();
2581         g_assert (nursery_section->size >= max_garbage_amount);
2582
2583         /* world must be stopped already */
2584         TV_GETTIME (all_atv);
2585         atv = all_atv;
2586
2587         TV_GETTIME (btv);
2588         time_minor_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
2589
2590         if (xdomain_checks) {
2591                 sgen_clear_nursery_fragments ();
2592                 check_for_xdomain_refs ();
2593         }
2594
2595         nursery_section->next_data = nursery_next;
2596
2597         major_collector.start_nursery_collection ();
2598
2599         sgen_memgov_minor_collection_start ();
2600
2601         init_gray_queue ();
2602
2603         stat_minor_gcs++;
2604         gc_stats.minor_gc_count ++;
2605
2606         if (remset.prepare_for_minor_collection)
2607                 remset.prepare_for_minor_collection ();
2608
2609         MONO_GC_CHECKPOINT_1 (GENERATION_NURSERY);
2610
2611         sgen_process_fin_stage_entries ();
2612         sgen_process_dislink_stage_entries ();
2613
2614         MONO_GC_CHECKPOINT_2 (GENERATION_NURSERY);
2615
2616         /* pin from pinned handles */
2617         sgen_init_pinning ();
2618         mono_profiler_gc_event (MONO_GC_EVENT_MARK_START, 0);
2619         pin_from_roots (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2620         /* pin cemented objects */
2621         sgen_cement_iterate (pin_stage_object_callback, NULL);
2622         /* identify pinned objects */
2623         sgen_optimize_pin_queue (0);
2624         sgen_pinning_setup_section (nursery_section);
2625         ctx.scan_func = NULL;
2626         ctx.copy_func = NULL;
2627         ctx.queue = WORKERS_DISTRIBUTE_GRAY_QUEUE;
2628         sgen_pin_objects_in_section (nursery_section, ctx);
2629         sgen_pinning_trim_queue_to_section (nursery_section);
2630
2631         TV_GETTIME (atv);
2632         time_minor_pinning += TV_ELAPSED (btv, atv);
2633         SGEN_LOG (2, "Finding pinned pointers: %d in %d usecs", sgen_get_pinned_count (), TV_ELAPSED (btv, atv));
2634         SGEN_LOG (4, "Start scan with %d pinned objects", sgen_get_pinned_count ());
2635
2636         MONO_GC_CHECKPOINT_3 (GENERATION_NURSERY);
2637
2638         if (whole_heap_check_before_collection) {
2639                 sgen_clear_nursery_fragments ();
2640                 sgen_check_whole_heap ();
2641         }
2642         if (consistency_check_at_minor_collection)
2643                 sgen_check_consistency ();
2644
2645         sgen_workers_start_all_workers ();
2646
2647         /*
2648          * Perform the sequential part of remembered set scanning.
2649          * This usually involves scanning global information that might later be produced by evacuation.
2650          */
2651         if (remset.begin_scan_remsets)
2652                 remset.begin_scan_remsets (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2653
2654         sgen_workers_start_marking ();
2655
2656         frssjd = sgen_alloc_internal_dynamic (sizeof (FinishRememberedSetScanJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2657         frssjd->heap_start = sgen_get_nursery_start ();
2658         frssjd->heap_end = nursery_next;
2659         sgen_workers_enqueue_job (job_finish_remembered_set_scan, frssjd);
2660
2661         /* we don't have complete write barrier yet, so we scan all the old generation sections */
2662         TV_GETTIME (btv);
2663         time_minor_scan_remsets += TV_ELAPSED (atv, btv);
2664         SGEN_LOG (2, "Old generation scan: %d usecs", TV_ELAPSED (atv, btv));
2665
2666         MONO_GC_CHECKPOINT_4 (GENERATION_NURSERY);
2667
2668         if (!sgen_collection_is_parallel ()) {
2669                 ctx.scan_func = current_object_ops.scan_object;
2670                 ctx.copy_func = NULL;
2671                 ctx.queue = &gray_queue;
2672                 sgen_drain_gray_stack (-1, ctx);
2673         }
2674
2675         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2676                 report_registered_roots ();
2677         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2678                 report_finalizer_roots ();
2679         TV_GETTIME (atv);
2680         time_minor_scan_pinned += TV_ELAPSED (btv, atv);
2681
2682         MONO_GC_CHECKPOINT_5 (GENERATION_NURSERY);
2683
2684         /* registered roots, this includes static fields */
2685         scrrjd_normal = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2686         scrrjd_normal->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2687         scrrjd_normal->scan_func = current_object_ops.scan_object;
2688         scrrjd_normal->heap_start = sgen_get_nursery_start ();
2689         scrrjd_normal->heap_end = nursery_next;
2690         scrrjd_normal->root_type = ROOT_TYPE_NORMAL;
2691         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_normal);
2692
2693         scrrjd_wbarrier = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2694         scrrjd_wbarrier->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2695         scrrjd_wbarrier->scan_func = current_object_ops.scan_object;
2696         scrrjd_wbarrier->heap_start = sgen_get_nursery_start ();
2697         scrrjd_wbarrier->heap_end = nursery_next;
2698         scrrjd_wbarrier->root_type = ROOT_TYPE_WBARRIER;
2699         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_wbarrier);
2700
2701         TV_GETTIME (btv);
2702         time_minor_scan_registered_roots += TV_ELAPSED (atv, btv);
2703
2704         MONO_GC_CHECKPOINT_6 (GENERATION_NURSERY);
2705
2706         /* thread data */
2707         stdjd = sgen_alloc_internal_dynamic (sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2708         stdjd->heap_start = sgen_get_nursery_start ();
2709         stdjd->heap_end = nursery_next;
2710         sgen_workers_enqueue_job (job_scan_thread_data, stdjd);
2711
2712         TV_GETTIME (atv);
2713         time_minor_scan_thread_data += TV_ELAPSED (btv, atv);
2714         btv = atv;
2715
2716         MONO_GC_CHECKPOINT_7 (GENERATION_NURSERY);
2717
2718         g_assert (!sgen_collection_is_parallel () && !sgen_collection_is_concurrent ());
2719
2720         if (sgen_collection_is_parallel () || sgen_collection_is_concurrent ())
2721                 g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
2722
2723         /* Scan the list of objects ready for finalization. If */
2724         sfejd_fin_ready = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2725         sfejd_fin_ready->list = fin_ready_list;
2726         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_fin_ready);
2727
2728         sfejd_critical_fin = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2729         sfejd_critical_fin->list = critical_fin_list;
2730         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_critical_fin);
2731
2732         MONO_GC_CHECKPOINT_8 (GENERATION_NURSERY);
2733
2734         finish_gray_stack (sgen_get_nursery_start (), nursery_next, GENERATION_NURSERY, &gray_queue);
2735         TV_GETTIME (atv);
2736         time_minor_finish_gray_stack += TV_ELAPSED (btv, atv);
2737         mono_profiler_gc_event (MONO_GC_EVENT_MARK_END, 0);
2738
2739         MONO_GC_CHECKPOINT_9 (GENERATION_NURSERY);
2740
2741         /*
2742          * The (single-threaded) finalization code might have done
2743          * some copying/marking so we can only reset the GC thread's
2744          * worker data here instead of earlier when we joined the
2745          * workers.
2746          */
2747         sgen_workers_reset_data ();
2748
2749         if (objects_pinned) {
2750                 sgen_optimize_pin_queue (0);
2751                 sgen_pinning_setup_section (nursery_section);
2752         }
2753
2754         /* walk the pin_queue, build up the fragment list of free memory, unmark
2755          * pinned objects as we go, memzero() the empty fragments so they are ready for the
2756          * next allocations.
2757          */
2758         mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_START, 0);
2759         fragment_total = sgen_build_nursery_fragments (nursery_section,
2760                         nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries,
2761                         unpin_queue);
2762         if (!fragment_total)
2763                 degraded_mode = 1;
2764
2765         /* Clear TLABs for all threads */
2766         sgen_clear_tlabs ();
2767
2768         mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_END, 0);
2769         TV_GETTIME (btv);
2770         time_minor_fragment_creation += TV_ELAPSED (atv, btv);
2771         SGEN_LOG (2, "Fragment creation: %d usecs, %lu bytes available", TV_ELAPSED (atv, btv), (unsigned long)fragment_total);
2772
2773         if (consistency_check_at_minor_collection)
2774                 sgen_check_major_refs ();
2775
2776         major_collector.finish_nursery_collection ();
2777
2778         TV_GETTIME (all_btv);
2779         gc_stats.minor_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
2780
2781         if (heap_dump_file)
2782                 dump_heap ("minor", stat_minor_gcs - 1, NULL);
2783
2784         /* prepare the pin queue for the next collection */
2785         sgen_finish_pinning ();
2786         if (fin_ready_list || critical_fin_list) {
2787                 SGEN_LOG (4, "Finalizer-thread wakeup: ready %d", num_ready_finalizers);
2788                 mono_gc_finalize_notify ();
2789         }
2790         sgen_pin_stats_reset ();
2791         /* clear cemented hash */
2792         sgen_cement_clear_below_threshold ();
2793
2794         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
2795
2796         if (remset.finish_minor_collection)
2797                 remset.finish_minor_collection ();
2798
2799         check_scan_starts ();
2800
2801         binary_protocol_flush_buffers (FALSE);
2802
2803         sgen_memgov_minor_collection_end ();
2804
2805         /*objects are late pinned because of lack of memory, so a major is a good call*/
2806         needs_major = objects_pinned > 0;
2807         current_collection_generation = -1;
2808         objects_pinned = 0;
2809
2810         MONO_GC_END (GENERATION_NURSERY);
2811         binary_protocol_collection_end (stat_minor_gcs - 1, GENERATION_NURSERY);
2812
2813         if (check_nursery_objects_pinned && !sgen_minor_collector.is_split)
2814                 sgen_check_nursery_objects_pinned (unpin_queue != NULL);
2815
2816         return needs_major;
2817 }
2818
2819 static void
2820 scan_nursery_objects_callback (char *obj, size_t size, ScanCopyContext *ctx)
2821 {
2822         ctx->scan_func (obj, ctx->queue);
2823 }
2824
2825 static void
2826 scan_nursery_objects (ScanCopyContext ctx)
2827 {
2828         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
2829                         (IterateObjectCallbackFunc)scan_nursery_objects_callback, (void*)&ctx, FALSE);
2830 }
2831
2832 static void
2833 major_copy_or_mark_from_roots (int *old_next_pin_slot, gboolean finish_up_concurrent_mark, gboolean scan_mod_union)
2834 {
2835         LOSObject *bigobj;
2836         TV_DECLARE (atv);
2837         TV_DECLARE (btv);
2838         /* FIXME: only use these values for the precise scan
2839          * note that to_space pointers should be excluded anyway...
2840          */
2841         char *heap_start = NULL;
2842         char *heap_end = (char*)-1;
2843         gboolean profile_roots = mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS;
2844         GCRootReport root_report = { 0 };
2845         ScanFromRegisteredRootsJobData *scrrjd_normal, *scrrjd_wbarrier;
2846         ScanThreadDataJobData *stdjd;
2847         ScanFinalizerEntriesJobData *sfejd_fin_ready, *sfejd_critical_fin;
2848         ScanCopyContext ctx;
2849
2850         if (major_collector.is_concurrent) {
2851                 /*This cleans up unused fragments */
2852                 sgen_nursery_allocator_prepare_for_pinning ();
2853
2854                 if (do_concurrent_checks)
2855                         check_nursery_is_clean ();
2856         } else {
2857                 /* The concurrent collector doesn't touch the nursery. */
2858                 sgen_nursery_alloc_prepare_for_major ();
2859         }
2860
2861         init_gray_queue ();
2862
2863         TV_GETTIME (atv);
2864
2865         /* Pinning depends on this */
2866         sgen_clear_nursery_fragments ();
2867
2868         if (whole_heap_check_before_collection)
2869                 sgen_check_whole_heap ();
2870
2871         TV_GETTIME (btv);
2872         time_major_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
2873
2874         if (!sgen_collection_is_concurrent ())
2875                 nursery_section->next_data = sgen_get_nursery_end ();
2876         /* we should also coalesce scanning from sections close to each other
2877          * and deal with pointers outside of the sections later.
2878          */
2879
2880         objects_pinned = 0;
2881         *major_collector.have_swept = FALSE;
2882
2883         if (xdomain_checks) {
2884                 sgen_clear_nursery_fragments ();
2885                 check_for_xdomain_refs ();
2886         }
2887
2888         if (!major_collector.is_concurrent) {
2889                 /* Remsets are not useful for a major collection */
2890                 remset.prepare_for_major_collection ();
2891         }
2892
2893         sgen_process_fin_stage_entries ();
2894         sgen_process_dislink_stage_entries ();
2895
2896         TV_GETTIME (atv);
2897         sgen_init_pinning ();
2898         SGEN_LOG (6, "Collecting pinned addresses");
2899         pin_from_roots ((void*)lowest_heap_address, (void*)highest_heap_address, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2900         sgen_optimize_pin_queue (0);
2901
2902         /*
2903          * Cementing is reset at the end of concurrent mark.  See
2904          * sgen_add_to_global_remset() for the explanation.
2905          */
2906         if (finish_up_concurrent_mark)
2907                 sgen_cement_reset ();
2908
2909         /*
2910          * The concurrent collector doesn't move objects, neither on
2911          * the major heap nor in the nursery, so we can mark even
2912          * before pinning has finished.  For the non-concurrent
2913          * collector we start the workers after pinning.
2914          */
2915         if (major_collector.is_concurrent) {
2916                 sgen_workers_start_all_workers ();
2917                 sgen_workers_start_marking ();
2918         }
2919
2920         /*
2921          * pin_queue now contains all candidate pointers, sorted and
2922          * uniqued.  We must do two passes now to figure out which
2923          * objects are pinned.
2924          *
2925          * The first is to find within the pin_queue the area for each
2926          * section.  This requires that the pin_queue be sorted.  We
2927          * also process the LOS objects and pinned chunks here.
2928          *
2929          * The second, destructive, pass is to reduce the section
2930          * areas to pointers to the actually pinned objects.
2931          */
2932         SGEN_LOG (6, "Pinning from sections");
2933         /* first pass for the sections */
2934         sgen_find_section_pin_queue_start_end (nursery_section);
2935         major_collector.find_pin_queue_start_ends (WORKERS_DISTRIBUTE_GRAY_QUEUE);
2936         /* identify possible pointers to the insize of large objects */
2937         SGEN_LOG (6, "Pinning from large objects");
2938         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next) {
2939                 int dummy;
2940                 if (sgen_find_optimized_pin_queue_area (bigobj->data, (char*)bigobj->data + sgen_los_object_size (bigobj), &dummy)) {
2941                         binary_protocol_pin (bigobj->data, (gpointer)LOAD_VTABLE (bigobj->data), safe_object_get_size (((MonoObject*)(bigobj->data))));
2942
2943 #ifdef ENABLE_DTRACE
2944                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
2945                                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (bigobj->data);
2946                                 MONO_GC_OBJ_PINNED ((mword)bigobj->data, sgen_safe_object_get_size ((MonoObject*)bigobj->data), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
2947                         }
2948 #endif
2949
2950                         if (sgen_los_object_is_pinned (bigobj->data)) {
2951                                 g_assert (finish_up_concurrent_mark);
2952                                 continue;
2953                         }
2954                         sgen_los_pin_object (bigobj->data);
2955                         /* FIXME: only enqueue if object has references */
2956                         GRAY_OBJECT_ENQUEUE (WORKERS_DISTRIBUTE_GRAY_QUEUE, bigobj->data);
2957                         if (G_UNLIKELY (do_pin_stats))
2958                                 sgen_pin_stats_register_object ((char*) bigobj->data, safe_object_get_size ((MonoObject*) bigobj->data));
2959                         SGEN_LOG (6, "Marked large object %p (%s) size: %lu from roots", bigobj->data, safe_name (bigobj->data), (unsigned long)sgen_los_object_size (bigobj));
2960
2961                         if (profile_roots)
2962                                 add_profile_gc_root (&root_report, bigobj->data, MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
2963                 }
2964         }
2965         if (profile_roots)
2966                 notify_gc_roots (&root_report);
2967         /* second pass for the sections */
2968         ctx.scan_func = concurrent_collection_in_progress ? current_object_ops.scan_object : NULL;
2969         ctx.copy_func = NULL;
2970         ctx.queue = WORKERS_DISTRIBUTE_GRAY_QUEUE;
2971
2972         if (major_collector.is_concurrent && sgen_minor_collector.is_split) {
2973                 /*
2974                  * With the split nursery, not all remaining nursery
2975                  * objects are pinned: those in to-space are not.  We
2976                  * need to scan all nursery objects, though, so we
2977                  * have to do it by iterating over the whole nursery.
2978                  */
2979                 scan_nursery_objects (ctx);
2980         } else {
2981                 sgen_pin_objects_in_section (nursery_section, ctx);
2982                 if (check_nursery_objects_pinned && !sgen_minor_collector.is_split)
2983                         sgen_check_nursery_objects_pinned (!concurrent_collection_in_progress || finish_up_concurrent_mark);
2984         }
2985
2986         major_collector.pin_objects (WORKERS_DISTRIBUTE_GRAY_QUEUE);
2987         if (old_next_pin_slot)
2988                 *old_next_pin_slot = sgen_get_pinned_count ();
2989
2990         TV_GETTIME (btv);
2991         time_major_pinning += TV_ELAPSED (atv, btv);
2992         SGEN_LOG (2, "Finding pinned pointers: %d in %d usecs", sgen_get_pinned_count (), TV_ELAPSED (atv, btv));
2993         SGEN_LOG (4, "Start scan with %d pinned objects", sgen_get_pinned_count ());
2994
2995         major_collector.init_to_space ();
2996
2997 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
2998         main_gc_thread = mono_native_thread_self ();
2999 #endif
3000
3001         if (!major_collector.is_concurrent) {
3002                 sgen_workers_start_all_workers ();
3003                 sgen_workers_start_marking ();
3004         }
3005
3006         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
3007                 report_registered_roots ();
3008         TV_GETTIME (atv);
3009         time_major_scan_pinned += TV_ELAPSED (btv, atv);
3010
3011         /* registered roots, this includes static fields */
3012         scrrjd_normal = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
3013         scrrjd_normal->copy_or_mark_func = current_object_ops.copy_or_mark_object;
3014         scrrjd_normal->scan_func = current_object_ops.scan_object;
3015         scrrjd_normal->heap_start = heap_start;
3016         scrrjd_normal->heap_end = heap_end;
3017         scrrjd_normal->root_type = ROOT_TYPE_NORMAL;
3018         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_normal);
3019
3020         scrrjd_wbarrier = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
3021         scrrjd_wbarrier->copy_or_mark_func = current_object_ops.copy_or_mark_object;
3022         scrrjd_wbarrier->scan_func = current_object_ops.scan_object;
3023         scrrjd_wbarrier->heap_start = heap_start;
3024         scrrjd_wbarrier->heap_end = heap_end;
3025         scrrjd_wbarrier->root_type = ROOT_TYPE_WBARRIER;
3026         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_wbarrier);
3027
3028         TV_GETTIME (btv);
3029         time_major_scan_registered_roots += TV_ELAPSED (atv, btv);
3030
3031         /* Threads */
3032         stdjd = sgen_alloc_internal_dynamic (sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
3033         stdjd->heap_start = heap_start;
3034         stdjd->heap_end = heap_end;
3035         sgen_workers_enqueue_job (job_scan_thread_data, stdjd);
3036
3037         TV_GETTIME (atv);
3038         time_major_scan_thread_data += TV_ELAPSED (btv, atv);
3039
3040         TV_GETTIME (btv);
3041         time_major_scan_alloc_pinned += TV_ELAPSED (atv, btv);
3042
3043         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
3044                 report_finalizer_roots ();
3045
3046         /* scan the list of objects ready for finalization */
3047         sfejd_fin_ready = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
3048         sfejd_fin_ready->list = fin_ready_list;
3049         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_fin_ready);
3050
3051         sfejd_critical_fin = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
3052         sfejd_critical_fin->list = critical_fin_list;
3053         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_critical_fin);
3054
3055         if (scan_mod_union) {
3056                 g_assert (finish_up_concurrent_mark);
3057
3058                 /* Mod union card table */
3059                 sgen_workers_enqueue_job (job_scan_major_mod_union_cardtable, NULL);
3060                 sgen_workers_enqueue_job (job_scan_los_mod_union_cardtable, NULL);
3061         }
3062
3063         TV_GETTIME (atv);
3064         time_major_scan_finalized += TV_ELAPSED (btv, atv);
3065         SGEN_LOG (2, "Root scan: %d usecs", TV_ELAPSED (btv, atv));
3066
3067         TV_GETTIME (btv);
3068         time_major_scan_big_objects += TV_ELAPSED (atv, btv);
3069
3070         if (major_collector.is_concurrent) {
3071                 /* prepare the pin queue for the next collection */
3072                 sgen_finish_pinning ();
3073
3074                 sgen_pin_stats_reset ();
3075
3076                 if (do_concurrent_checks)
3077                         check_nursery_is_clean ();
3078         }
3079 }
3080
3081 static void
3082 major_start_collection (int *old_next_pin_slot)
3083 {
3084         MONO_GC_BEGIN (GENERATION_OLD);
3085         binary_protocol_collection_begin (stat_major_gcs, GENERATION_OLD);
3086
3087         current_collection_generation = GENERATION_OLD;
3088 #ifndef DISABLE_PERFCOUNTERS
3089         mono_perfcounters->gc_collections1++;
3090 #endif
3091
3092         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
3093
3094         if (major_collector.is_concurrent)
3095                 concurrent_collection_in_progress = TRUE;
3096
3097         current_object_ops = major_collector.major_ops;
3098
3099         reset_pinned_from_failed_allocation ();
3100
3101         sgen_memgov_major_collection_start ();
3102
3103         //count_ref_nonref_objs ();
3104         //consistency_check ();
3105
3106         check_scan_starts ();
3107
3108         degraded_mode = 0;
3109         SGEN_LOG (1, "Start major collection %d", stat_major_gcs);
3110         stat_major_gcs++;
3111         gc_stats.major_gc_count ++;
3112
3113         if (major_collector.start_major_collection)
3114                 major_collector.start_major_collection ();
3115
3116         major_copy_or_mark_from_roots (old_next_pin_slot, FALSE, FALSE);
3117 }
3118
3119 static void
3120 wait_for_workers_to_finish (void)
3121 {
3122         g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
3123
3124         if (major_collector.is_parallel || major_collector.is_concurrent) {
3125                 gray_queue_redirect (&gray_queue);
3126                 sgen_workers_join ();
3127         }
3128
3129         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3130
3131 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
3132         main_gc_thread = NULL;
3133 #endif
3134 }
3135
3136 static void
3137 major_finish_collection (const char *reason, int old_next_pin_slot, gboolean scan_mod_union)
3138 {
3139         LOSObject *bigobj, *prevbo;
3140         TV_DECLARE (atv);
3141         TV_DECLARE (btv);
3142         char *heap_start = NULL;
3143         char *heap_end = (char*)-1;
3144
3145         TV_GETTIME (btv);
3146
3147         if (major_collector.is_concurrent || major_collector.is_parallel)
3148                 wait_for_workers_to_finish ();
3149
3150         current_object_ops = major_collector.major_ops;
3151
3152         if (major_collector.is_concurrent) {
3153                 major_copy_or_mark_from_roots (NULL, TRUE, scan_mod_union);
3154                 wait_for_workers_to_finish ();
3155
3156                 g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3157
3158                 if (do_concurrent_checks)
3159                         check_nursery_is_clean ();
3160         }
3161
3162         /*
3163          * The workers have stopped so we need to finish gray queue
3164          * work that might result from finalization in the main GC
3165          * thread.  Redirection must therefore be turned off.
3166          */
3167         sgen_gray_object_queue_disable_alloc_prepare (&gray_queue);
3168         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
3169
3170         /* all the objects in the heap */
3171         finish_gray_stack (heap_start, heap_end, GENERATION_OLD, &gray_queue);
3172         TV_GETTIME (atv);
3173         time_major_finish_gray_stack += TV_ELAPSED (btv, atv);
3174
3175         /*
3176          * The (single-threaded) finalization code might have done
3177          * some copying/marking so we can only reset the GC thread's
3178          * worker data here instead of earlier when we joined the
3179          * workers.
3180          */
3181         sgen_workers_reset_data ();
3182
3183         if (objects_pinned) {
3184                 g_assert (!major_collector.is_concurrent);
3185
3186                 /*This is slow, but we just OOM'd*/
3187                 sgen_pin_queue_clear_discarded_entries (nursery_section, old_next_pin_slot);
3188                 sgen_optimize_pin_queue (0);
3189                 sgen_find_section_pin_queue_start_end (nursery_section);
3190                 objects_pinned = 0;
3191         }
3192
3193         reset_heap_boundaries ();
3194         sgen_update_heap_boundaries ((mword)sgen_get_nursery_start (), (mword)sgen_get_nursery_end ());
3195
3196         if (check_mark_bits_after_major_collection)
3197                 sgen_check_major_heap_marked ();
3198
3199         MONO_GC_SWEEP_BEGIN (GENERATION_OLD, !major_collector.sweeps_lazily);
3200
3201         /* sweep the big objects list */
3202         prevbo = NULL;
3203         for (bigobj = los_object_list; bigobj;) {
3204                 g_assert (!object_is_pinned (bigobj->data));
3205                 if (sgen_los_object_is_pinned (bigobj->data)) {
3206                         sgen_los_unpin_object (bigobj->data);
3207                         sgen_update_heap_boundaries ((mword)bigobj->data, (mword)bigobj->data + sgen_los_object_size (bigobj));
3208                 } else {
3209                         LOSObject *to_free;
3210                         /* not referenced anywhere, so we can free it */
3211                         if (prevbo)
3212                                 prevbo->next = bigobj->next;
3213                         else
3214                                 los_object_list = bigobj->next;
3215                         to_free = bigobj;
3216                         bigobj = bigobj->next;
3217                         sgen_los_free_object (to_free);
3218                         continue;
3219                 }
3220                 prevbo = bigobj;
3221                 bigobj = bigobj->next;
3222         }
3223
3224         TV_GETTIME (btv);
3225         time_major_free_bigobjs += TV_ELAPSED (atv, btv);
3226
3227         sgen_los_sweep ();
3228
3229         TV_GETTIME (atv);
3230         time_major_los_sweep += TV_ELAPSED (btv, atv);
3231
3232         major_collector.sweep ();
3233
3234         MONO_GC_SWEEP_END (GENERATION_OLD, !major_collector.sweeps_lazily);
3235
3236         TV_GETTIME (btv);
3237         time_major_sweep += TV_ELAPSED (atv, btv);
3238
3239         if (!major_collector.is_concurrent) {
3240                 /* walk the pin_queue, build up the fragment list of free memory, unmark
3241                  * pinned objects as we go, memzero() the empty fragments so they are ready for the
3242                  * next allocations.
3243                  */
3244                 if (!sgen_build_nursery_fragments (nursery_section, nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries, NULL))
3245                         degraded_mode = 1;
3246
3247                 /* prepare the pin queue for the next collection */
3248                 sgen_finish_pinning ();
3249
3250                 /* Clear TLABs for all threads */
3251                 sgen_clear_tlabs ();
3252
3253                 sgen_pin_stats_reset ();
3254         }
3255
3256         sgen_cement_clear_below_threshold ();
3257
3258         TV_GETTIME (atv);
3259         time_major_fragment_creation += TV_ELAPSED (btv, atv);
3260
3261         if (heap_dump_file)
3262                 dump_heap ("major", stat_major_gcs - 1, reason);
3263
3264         if (fin_ready_list || critical_fin_list) {
3265                 SGEN_LOG (4, "Finalizer-thread wakeup: ready %d", num_ready_finalizers);
3266                 mono_gc_finalize_notify ();
3267         }
3268
3269         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3270
3271         sgen_memgov_major_collection_end ();
3272         current_collection_generation = -1;
3273
3274         major_collector.finish_major_collection ();
3275
3276         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
3277
3278         if (major_collector.is_concurrent)
3279                 concurrent_collection_in_progress = FALSE;
3280
3281         check_scan_starts ();
3282
3283         binary_protocol_flush_buffers (FALSE);
3284
3285         //consistency_check ();
3286
3287         MONO_GC_END (GENERATION_OLD);
3288         binary_protocol_collection_end (stat_major_gcs - 1, GENERATION_OLD);
3289 }
3290
3291 static gboolean
3292 major_do_collection (const char *reason)
3293 {
3294         TV_DECLARE (all_atv);
3295         TV_DECLARE (all_btv);
3296         int old_next_pin_slot;
3297
3298         if (major_collector.get_and_reset_num_major_objects_marked) {
3299                 long long num_marked = major_collector.get_and_reset_num_major_objects_marked ();
3300                 g_assert (!num_marked);
3301         }
3302
3303         /* world must be stopped already */
3304         TV_GETTIME (all_atv);
3305
3306         major_start_collection (&old_next_pin_slot);
3307         major_finish_collection (reason, old_next_pin_slot, FALSE);
3308
3309         TV_GETTIME (all_btv);
3310         gc_stats.major_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
3311
3312         /* FIXME: also report this to the user, preferably in gc-end. */
3313         if (major_collector.get_and_reset_num_major_objects_marked)
3314                 major_collector.get_and_reset_num_major_objects_marked ();
3315
3316         return bytes_pinned_from_failed_allocation > 0;
3317 }
3318
3319 static gboolean major_do_collection (const char *reason);
3320
3321 static void
3322 major_start_concurrent_collection (const char *reason)
3323 {
3324         long long num_objects_marked = major_collector.get_and_reset_num_major_objects_marked ();
3325
3326         g_assert (num_objects_marked == 0);
3327
3328         MONO_GC_CONCURRENT_START_BEGIN (GENERATION_OLD);
3329
3330         // FIXME: store reason and pass it when finishing
3331         major_start_collection (NULL);
3332
3333         gray_queue_redirect (&gray_queue);
3334         sgen_workers_wait_for_jobs ();
3335
3336         num_objects_marked = major_collector.get_and_reset_num_major_objects_marked ();
3337         MONO_GC_CONCURRENT_START_END (GENERATION_OLD, num_objects_marked);
3338
3339         current_collection_generation = -1;
3340 }
3341
3342 static gboolean
3343 major_update_or_finish_concurrent_collection (gboolean force_finish)
3344 {
3345         SgenGrayQueue unpin_queue;
3346         memset (&unpin_queue, 0, sizeof (unpin_queue));
3347
3348         MONO_GC_CONCURRENT_UPDATE_FINISH_BEGIN (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3349
3350         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3351         if (!have_non_collection_major_object_remembers)
3352                 g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
3353
3354         major_collector.update_cardtable_mod_union ();
3355         sgen_los_update_cardtable_mod_union ();
3356
3357         if (!force_finish && !sgen_workers_all_done ()) {
3358                 MONO_GC_CONCURRENT_UPDATE_END (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3359                 return FALSE;
3360         }
3361
3362         collect_nursery (&unpin_queue, TRUE);
3363         redirect_major_object_remembers ();
3364
3365         current_collection_generation = GENERATION_OLD;
3366         major_finish_collection ("finishing", -1, TRUE);
3367
3368         unpin_objects_from_queue (&unpin_queue);
3369         sgen_gray_object_queue_deinit (&unpin_queue);
3370
3371         MONO_GC_CONCURRENT_FINISH_END (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3372
3373         current_collection_generation = -1;
3374
3375         if (whole_heap_check_before_collection)
3376                 sgen_check_whole_heap ();
3377
3378         return TRUE;
3379 }
3380
3381 /*
3382  * Ensure an allocation request for @size will succeed by freeing enough memory.
3383  *
3384  * LOCKING: The GC lock MUST be held.
3385  */
3386 void
3387 sgen_ensure_free_space (size_t size)
3388 {
3389         int generation_to_collect = -1;
3390         const char *reason = NULL;
3391
3392
3393         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
3394                 if (sgen_need_major_collection (size)) {
3395                         reason = "LOS overflow";
3396                         generation_to_collect = GENERATION_OLD;
3397                 }
3398         } else {
3399                 if (degraded_mode) {
3400                         if (sgen_need_major_collection (size)) {
3401                                 reason = "Degraded mode overflow";
3402                                 generation_to_collect = GENERATION_OLD;
3403                         }
3404                 } else if (sgen_need_major_collection (size)) {
3405                         reason = "Minor allowance";
3406                         generation_to_collect = GENERATION_OLD;
3407                 } else {
3408                         generation_to_collect = GENERATION_NURSERY;
3409                         reason = "Nursery full";                        
3410                 }
3411         }
3412
3413         if (generation_to_collect == -1) {
3414                 if (concurrent_collection_in_progress && sgen_workers_all_done ()) {
3415                         generation_to_collect = GENERATION_OLD;
3416                         reason = "Finish concurrent collection";
3417                 }
3418         }
3419
3420         if (generation_to_collect == -1)
3421                 return;
3422         sgen_perform_collection (size, generation_to_collect, reason, FALSE);
3423 }
3424
3425 void
3426 sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish)
3427 {
3428         TV_DECLARE (gc_end);
3429         GGTimingInfo infos [2];
3430         int overflow_generation_to_collect = -1;
3431         int oldest_generation_collected = generation_to_collect;
3432         const char *overflow_reason = NULL;
3433
3434         MONO_GC_REQUESTED (generation_to_collect, requested_size, wait_to_finish ? 1 : 0);
3435
3436         g_assert (generation_to_collect == GENERATION_NURSERY || generation_to_collect == GENERATION_OLD);
3437
3438         if (have_non_collection_major_object_remembers) {
3439                 g_assert (concurrent_collection_in_progress);
3440                 redirect_major_object_remembers ();
3441         }
3442
3443         memset (infos, 0, sizeof (infos));
3444         mono_profiler_gc_event (MONO_GC_EVENT_START, generation_to_collect);
3445
3446         infos [0].generation = generation_to_collect;
3447         infos [0].reason = reason;
3448         infos [0].is_overflow = FALSE;
3449         TV_GETTIME (infos [0].total_time);
3450         infos [1].generation = -1;
3451
3452         sgen_stop_world (generation_to_collect);
3453
3454         if (concurrent_collection_in_progress) {
3455                 if (major_update_or_finish_concurrent_collection (wait_to_finish && generation_to_collect == GENERATION_OLD)) {
3456                         oldest_generation_collected = GENERATION_OLD;
3457                         goto done;
3458                 }
3459                 if (generation_to_collect == GENERATION_OLD)
3460                         goto done;
3461         }
3462
3463         //FIXME extract overflow reason
3464         if (generation_to_collect == GENERATION_NURSERY) {
3465                 if (collect_nursery (NULL, FALSE)) {
3466                         overflow_generation_to_collect = GENERATION_OLD;
3467                         overflow_reason = "Minor overflow";
3468                 }
3469                 if (concurrent_collection_in_progress) {
3470                         redirect_major_object_remembers ();
3471                         sgen_workers_wake_up_all ();
3472                 }
3473         } else {
3474                 SgenGrayQueue unpin_queue;
3475                 SgenGrayQueue *unpin_queue_ptr;
3476                 memset (&unpin_queue, 0, sizeof (unpin_queue));
3477
3478                 if (major_collector.is_concurrent && wait_to_finish)
3479                         unpin_queue_ptr = &unpin_queue;
3480                 else
3481                         unpin_queue_ptr = NULL;
3482
3483                 if (major_collector.is_concurrent) {
3484                         g_assert (!concurrent_collection_in_progress);
3485                         collect_nursery (unpin_queue_ptr, FALSE);
3486                 }
3487
3488                 if (major_collector.is_concurrent && !wait_to_finish) {
3489                         major_start_concurrent_collection (reason);
3490                         // FIXME: set infos[0] properly
3491                         goto done;
3492                 } else {
3493                         if (major_do_collection (reason)) {
3494                                 overflow_generation_to_collect = GENERATION_NURSERY;
3495                                 overflow_reason = "Excessive pinning";
3496                         }
3497                 }
3498
3499                 if (unpin_queue_ptr) {
3500                         unpin_objects_from_queue (unpin_queue_ptr);
3501                         sgen_gray_object_queue_deinit (unpin_queue_ptr);
3502                 }
3503         }
3504
3505         TV_GETTIME (gc_end);
3506         infos [0].total_time = SGEN_TV_ELAPSED (infos [0].total_time, gc_end);
3507
3508
3509         if (!major_collector.is_concurrent && overflow_generation_to_collect != -1) {
3510                 mono_profiler_gc_event (MONO_GC_EVENT_START, overflow_generation_to_collect);
3511                 infos [1].generation = overflow_generation_to_collect;
3512                 infos [1].reason = overflow_reason;
3513                 infos [1].is_overflow = TRUE;
3514                 infos [1].total_time = gc_end;
3515
3516                 if (overflow_generation_to_collect == GENERATION_NURSERY)
3517                         collect_nursery (NULL, FALSE);
3518                 else
3519                         major_do_collection (overflow_reason);
3520
3521                 TV_GETTIME (gc_end);
3522                 infos [1].total_time = SGEN_TV_ELAPSED (infos [1].total_time, gc_end);
3523
3524                 /* keep events symmetric */
3525                 mono_profiler_gc_event (MONO_GC_EVENT_END, overflow_generation_to_collect);
3526
3527                 oldest_generation_collected = MAX (oldest_generation_collected, overflow_generation_to_collect);
3528         }
3529
3530         SGEN_LOG (2, "Heap size: %lu, LOS size: %lu", (unsigned long)mono_gc_get_heap_size (), (unsigned long)los_memory_usage);
3531
3532         /* this also sets the proper pointers for the next allocation */
3533         if (generation_to_collect == GENERATION_NURSERY && !sgen_can_alloc_size (requested_size)) {
3534                 /* TypeBuilder and MonoMethod are killing mcs with fragmentation */
3535                 SGEN_LOG (1, "nursery collection didn't find enough room for %zd alloc (%d pinned)", requested_size, sgen_get_pinned_count ());
3536                 sgen_dump_pin_queue ();
3537                 degraded_mode = 1;
3538         }
3539
3540  done:
3541         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3542         g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
3543
3544         sgen_restart_world (oldest_generation_collected, infos);
3545
3546         mono_profiler_gc_event (MONO_GC_EVENT_END, generation_to_collect);
3547 }
3548
3549 /*
3550  * ######################################################################
3551  * ########  Memory allocation from the OS
3552  * ######################################################################
3553  * This section of code deals with getting memory from the OS and
3554  * allocating memory for GC-internal data structures.
3555  * Internal memory can be handled with a freelist for small objects.
3556  */
3557
3558 /*
3559  * Debug reporting.
3560  */
3561 G_GNUC_UNUSED static void
3562 report_internal_mem_usage (void)
3563 {
3564         printf ("Internal memory usage:\n");
3565         sgen_report_internal_mem_usage ();
3566         printf ("Pinned memory usage:\n");
3567         major_collector.report_pinned_memory_usage ();
3568 }
3569
3570 /*
3571  * ######################################################################
3572  * ########  Finalization support
3573  * ######################################################################
3574  */
3575
3576 static inline gboolean
3577 sgen_major_is_object_alive (void *object)
3578 {
3579         mword objsize;
3580
3581         /* Oldgen objects can be pinned and forwarded too */
3582         if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
3583                 return TRUE;
3584
3585         /*
3586          * FIXME: major_collector.is_object_live() also calculates the
3587          * size.  Avoid the double calculation.
3588          */
3589         objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)object));
3590         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
3591                 return sgen_los_object_is_pinned (object);
3592
3593         return major_collector.is_object_live (object);
3594 }
3595
3596 /*
3597  * If the object has been forwarded it means it's still referenced from a root. 
3598  * If it is pinned it's still alive as well.
3599  * A LOS object is only alive if we have pinned it.
3600  * Return TRUE if @obj is ready to be finalized.
3601  */
3602 static inline gboolean
3603 sgen_is_object_alive (void *object)
3604 {
3605         if (ptr_in_nursery (object))
3606                 return sgen_nursery_is_object_alive (object);
3607
3608         return sgen_major_is_object_alive (object);
3609 }
3610
3611 /*
3612  * This function returns true if @object is either alive or it belongs to the old gen
3613  * and we're currently doing a minor collection.
3614  */
3615 static inline int
3616 sgen_is_object_alive_for_current_gen (char *object)
3617 {
3618         if (ptr_in_nursery (object))
3619                 return sgen_nursery_is_object_alive (object);
3620
3621         if (current_collection_generation == GENERATION_NURSERY)
3622                 return TRUE;
3623
3624         return sgen_major_is_object_alive (object);
3625 }
3626
3627 /*
3628  * This function returns true if @object is either alive and belongs to the
3629  * current collection - major collections are full heap, so old gen objects
3630  * are never alive during a minor collection.
3631  */
3632 static inline int
3633 sgen_is_object_alive_and_on_current_collection (char *object)
3634 {
3635         if (ptr_in_nursery (object))
3636                 return sgen_nursery_is_object_alive (object);
3637
3638         if (current_collection_generation == GENERATION_NURSERY)
3639                 return FALSE;
3640
3641         return sgen_major_is_object_alive (object);
3642 }
3643
3644
3645 gboolean
3646 sgen_gc_is_object_ready_for_finalization (void *object)
3647 {
3648         return !sgen_is_object_alive (object);
3649 }
3650
3651 static gboolean
3652 has_critical_finalizer (MonoObject *obj)
3653 {
3654         MonoClass *class;
3655
3656         if (!mono_defaults.critical_finalizer_object)
3657                 return FALSE;
3658
3659         class = ((MonoVTable*)LOAD_VTABLE (obj))->klass;
3660
3661         return mono_class_has_parent_fast (class, mono_defaults.critical_finalizer_object);
3662 }
3663
3664 void
3665 sgen_queue_finalization_entry (MonoObject *obj)
3666 {
3667         FinalizeReadyEntry *entry = sgen_alloc_internal (INTERNAL_MEM_FINALIZE_READY_ENTRY);
3668         gboolean critical = has_critical_finalizer (obj);
3669         entry->object = obj;
3670         if (critical) {
3671                 entry->next = critical_fin_list;
3672                 critical_fin_list = entry;
3673         } else {
3674                 entry->next = fin_ready_list;
3675                 fin_ready_list = entry;
3676         }
3677
3678 #ifdef ENABLE_DTRACE
3679         if (G_UNLIKELY (MONO_GC_FINALIZE_ENQUEUE_ENABLED ())) {
3680                 int gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
3681                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
3682                 MONO_GC_FINALIZE_ENQUEUE ((mword)obj, sgen_safe_object_get_size (obj),
3683                                 vt->klass->name_space, vt->klass->name, gen, critical);
3684         }
3685 #endif
3686 }
3687
3688 gboolean
3689 sgen_object_is_live (void *obj)
3690 {
3691         return sgen_is_object_alive_and_on_current_collection (obj);
3692 }
3693
3694 /* LOCKING: requires that the GC lock is held */
3695 static void
3696 null_ephemerons_for_domain (MonoDomain *domain)
3697 {
3698         EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
3699
3700         while (current) {
3701                 MonoObject *object = (MonoObject*)current->array;
3702
3703                 if (object && !object->vtable) {
3704                         EphemeronLinkNode *tmp = current;
3705
3706                         if (prev)
3707                                 prev->next = current->next;
3708                         else
3709                                 ephemeron_list = current->next;
3710
3711                         current = current->next;
3712                         sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
3713                 } else {
3714                         prev = current;
3715                         current = current->next;
3716                 }
3717         }
3718 }
3719
3720 /* LOCKING: requires that the GC lock is held */
3721 static void
3722 clear_unreachable_ephemerons (ScanCopyContext ctx)
3723 {
3724         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
3725         GrayQueue *queue = ctx.queue;
3726         EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
3727         MonoArray *array;
3728         Ephemeron *cur, *array_end;
3729         char *tombstone;
3730
3731         while (current) {
3732                 char *object = current->array;
3733
3734                 if (!sgen_is_object_alive_for_current_gen (object)) {
3735                         EphemeronLinkNode *tmp = current;
3736
3737                         SGEN_LOG (5, "Dead Ephemeron array at %p", object);
3738
3739                         if (prev)
3740                                 prev->next = current->next;
3741                         else
3742                                 ephemeron_list = current->next;
3743
3744                         current = current->next;
3745                         sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
3746
3747                         continue;
3748                 }
3749
3750                 copy_func ((void**)&object, queue);
3751                 current->array = object;
3752
3753                 SGEN_LOG (5, "Clearing unreachable entries for ephemeron array at %p", object);
3754
3755                 array = (MonoArray*)object;
3756                 cur = mono_array_addr (array, Ephemeron, 0);
3757                 array_end = cur + mono_array_length_fast (array);
3758                 tombstone = (char*)((MonoVTable*)LOAD_VTABLE (object))->domain->ephemeron_tombstone;
3759
3760                 for (; cur < array_end; ++cur) {
3761                         char *key = (char*)cur->key;
3762
3763                         if (!key || key == tombstone)
3764                                 continue;
3765
3766                         SGEN_LOG (5, "[%td] key %p (%s) value %p (%s)", cur - mono_array_addr (array, Ephemeron, 0),
3767                                 key, sgen_is_object_alive_for_current_gen (key) ? "reachable" : "unreachable",
3768                                 cur->value, cur->value && sgen_is_object_alive_for_current_gen (cur->value) ? "reachable" : "unreachable");
3769
3770                         if (!sgen_is_object_alive_for_current_gen (key)) {
3771                                 cur->key = tombstone;
3772                                 cur->value = NULL;
3773                                 continue;
3774                         }
3775                 }
3776                 prev = current;
3777                 current = current->next;
3778         }
3779 }
3780
3781 /*
3782 LOCKING: requires that the GC lock is held
3783
3784 Limitations: We scan all ephemerons on every collection since the current design doesn't allow for a simple nursery/mature split.
3785 */
3786 static int
3787 mark_ephemerons_in_range (ScanCopyContext ctx)
3788 {
3789         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
3790         GrayQueue *queue = ctx.queue;
3791         int nothing_marked = 1;
3792         EphemeronLinkNode *current = ephemeron_list;
3793         MonoArray *array;
3794         Ephemeron *cur, *array_end;
3795         char *tombstone;
3796
3797         for (current = ephemeron_list; current; current = current->next) {
3798                 char *object = current->array;
3799                 SGEN_LOG (5, "Ephemeron array at %p", object);
3800
3801                 /*It has to be alive*/
3802                 if (!sgen_is_object_alive_for_current_gen (object)) {
3803                         SGEN_LOG (5, "\tnot reachable");
3804                         continue;
3805                 }
3806
3807                 copy_func ((void**)&object, queue);
3808
3809                 array = (MonoArray*)object;
3810                 cur = mono_array_addr (array, Ephemeron, 0);
3811                 array_end = cur + mono_array_length_fast (array);
3812                 tombstone = (char*)((MonoVTable*)LOAD_VTABLE (object))->domain->ephemeron_tombstone;
3813
3814                 for (; cur < array_end; ++cur) {
3815                         char *key = cur->key;
3816
3817                         if (!key || key == tombstone)
3818                                 continue;
3819
3820                         SGEN_LOG (5, "[%td] key %p (%s) value %p (%s)", cur - mono_array_addr (array, Ephemeron, 0),
3821                                 key, sgen_is_object_alive_for_current_gen (key) ? "reachable" : "unreachable",
3822                                 cur->value, cur->value && sgen_is_object_alive_for_current_gen (cur->value) ? "reachable" : "unreachable");
3823
3824                         if (sgen_is_object_alive_for_current_gen (key)) {
3825                                 char *value = cur->value;
3826
3827                                 copy_func ((void**)&cur->key, queue);
3828                                 if (value) {
3829                                         if (!sgen_is_object_alive_for_current_gen (value))
3830                                                 nothing_marked = 0;
3831                                         copy_func ((void**)&cur->value, queue);
3832                                 }
3833                         }
3834                 }
3835         }
3836
3837         SGEN_LOG (5, "Ephemeron run finished. Is it done %d", nothing_marked);
3838         return nothing_marked;
3839 }
3840
3841 int
3842 mono_gc_invoke_finalizers (void)
3843 {
3844         FinalizeReadyEntry *entry = NULL;
3845         gboolean entry_is_critical = FALSE;
3846         int count = 0;
3847         void *obj;
3848         /* FIXME: batch to reduce lock contention */
3849         while (fin_ready_list || critical_fin_list) {
3850                 LOCK_GC;
3851
3852                 if (entry) {
3853                         FinalizeReadyEntry **list = entry_is_critical ? &critical_fin_list : &fin_ready_list;
3854
3855                         /* We have finalized entry in the last
3856                            interation, now we need to remove it from
3857                            the list. */
3858                         if (*list == entry)
3859                                 *list = entry->next;
3860                         else {
3861                                 FinalizeReadyEntry *e = *list;
3862                                 while (e->next != entry)
3863                                         e = e->next;
3864                                 e->next = entry->next;
3865                         }
3866                         sgen_free_internal (entry, INTERNAL_MEM_FINALIZE_READY_ENTRY);
3867                         entry = NULL;
3868                 }
3869
3870                 /* Now look for the first non-null entry. */
3871                 for (entry = fin_ready_list; entry && !entry->object; entry = entry->next)
3872                         ;
3873                 if (entry) {
3874                         entry_is_critical = FALSE;
3875                 } else {
3876                         entry_is_critical = TRUE;
3877                         for (entry = critical_fin_list; entry && !entry->object; entry = entry->next)
3878                                 ;
3879                 }
3880
3881                 if (entry) {
3882                         g_assert (entry->object);
3883                         num_ready_finalizers--;
3884                         obj = entry->object;
3885                         entry->object = NULL;
3886                         SGEN_LOG (7, "Finalizing object %p (%s)", obj, safe_name (obj));
3887                 }
3888
3889                 UNLOCK_GC;
3890
3891                 if (!entry)
3892                         break;
3893
3894                 g_assert (entry->object == NULL);
3895                 count++;
3896                 /* the object is on the stack so it is pinned */
3897                 /*g_print ("Calling finalizer for object: %p (%s)\n", entry->object, safe_name (entry->object));*/
3898                 mono_gc_run_finalize (obj, NULL);
3899         }
3900         g_assert (!entry);
3901         return count;
3902 }
3903
3904 gboolean
3905 mono_gc_pending_finalizers (void)
3906 {
3907         return fin_ready_list || critical_fin_list;
3908 }
3909
3910 /*
3911  * ######################################################################
3912  * ########  registered roots support
3913  * ######################################################################
3914  */
3915
3916 /*
3917  * We do not coalesce roots.
3918  */
3919 static int
3920 mono_gc_register_root_inner (char *start, size_t size, void *descr, int root_type)
3921 {
3922         RootRecord new_root;
3923         int i;
3924         LOCK_GC;
3925         for (i = 0; i < ROOT_TYPE_NUM; ++i) {
3926                 RootRecord *root = sgen_hash_table_lookup (&roots_hash [i], start);
3927                 /* we allow changing the size and the descriptor (for thread statics etc) */
3928                 if (root) {
3929                         size_t old_size = root->end_root - start;
3930                         root->end_root = start + size;
3931                         g_assert (((root->root_desc != 0) && (descr != NULL)) ||
3932                                           ((root->root_desc == 0) && (descr == NULL)));
3933                         root->root_desc = (mword)descr;
3934                         roots_size += size;
3935                         roots_size -= old_size;
3936                         UNLOCK_GC;
3937                         return TRUE;
3938                 }
3939         }
3940
3941         new_root.end_root = start + size;
3942         new_root.root_desc = (mword)descr;
3943
3944         sgen_hash_table_replace (&roots_hash [root_type], start, &new_root, NULL);
3945         roots_size += size;
3946
3947         SGEN_LOG (3, "Added root for range: %p-%p, descr: %p  (%d/%d bytes)", start, new_root.end_root, descr, (int)size, (int)roots_size);
3948
3949         UNLOCK_GC;
3950         return TRUE;
3951 }
3952
3953 int
3954 mono_gc_register_root (char *start, size_t size, void *descr)
3955 {
3956         return mono_gc_register_root_inner (start, size, descr, descr ? ROOT_TYPE_NORMAL : ROOT_TYPE_PINNED);
3957 }
3958
3959 int
3960 mono_gc_register_root_wbarrier (char *start, size_t size, void *descr)
3961 {
3962         return mono_gc_register_root_inner (start, size, descr, ROOT_TYPE_WBARRIER);
3963 }
3964
3965 void
3966 mono_gc_deregister_root (char* addr)
3967 {
3968         int root_type;
3969         RootRecord root;
3970
3971         LOCK_GC;
3972         for (root_type = 0; root_type < ROOT_TYPE_NUM; ++root_type) {
3973                 if (sgen_hash_table_remove (&roots_hash [root_type], addr, &root))
3974                         roots_size -= (root.end_root - addr);
3975         }
3976         UNLOCK_GC;
3977 }
3978
3979 /*
3980  * ######################################################################
3981  * ########  Thread handling (stop/start code)
3982  * ######################################################################
3983  */
3984
3985 unsigned int sgen_global_stop_count = 0;
3986
3987 void
3988 sgen_fill_thread_info_for_suspend (SgenThreadInfo *info)
3989 {
3990         if (remset.fill_thread_info_for_suspend)
3991                 remset.fill_thread_info_for_suspend (info);
3992 }
3993
3994 int
3995 sgen_get_current_collection_generation (void)
3996 {
3997         return current_collection_generation;
3998 }
3999
4000 void
4001 mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks)
4002 {
4003         gc_callbacks = *callbacks;
4004 }
4005
4006 MonoGCCallbacks *
4007 mono_gc_get_gc_callbacks ()
4008 {
4009         return &gc_callbacks;
4010 }
4011
4012 /* Variables holding start/end nursery so it won't have to be passed at every call */
4013 static void *scan_area_arg_start, *scan_area_arg_end;
4014
4015 void
4016 mono_gc_conservatively_scan_area (void *start, void *end)
4017 {
4018         conservatively_pin_objects_from (start, end, scan_area_arg_start, scan_area_arg_end, PIN_TYPE_STACK);
4019 }
4020
4021 void*
4022 mono_gc_scan_object (void *obj)
4023 {
4024         UserCopyOrMarkData *data = mono_native_tls_get_value (user_copy_or_mark_key);
4025         current_object_ops.copy_or_mark_object (&obj, data->queue);
4026         return obj;
4027 }
4028
4029 /*
4030  * Mark from thread stacks and registers.
4031  */
4032 static void
4033 scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, GrayQueue *queue)
4034 {
4035         SgenThreadInfo *info;
4036
4037         scan_area_arg_start = start_nursery;
4038         scan_area_arg_end = end_nursery;
4039
4040         FOREACH_THREAD (info) {
4041                 if (info->skip) {
4042                         SGEN_LOG (3, "Skipping dead thread %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
4043                         continue;
4044                 }
4045                 if (info->gc_disabled) {
4046                         SGEN_LOG (3, "GC disabled for thread %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
4047                         continue;
4048                 }
4049
4050                 if (!info->joined_stw) {
4051                         SGEN_LOG (3, "Skipping thread not seen in STW %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
4052                         continue;
4053                 }
4054                 
4055                 SGEN_LOG (3, "Scanning thread %p, range: %p-%p, size: %td, pinned=%d", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start, sgen_get_pinned_count ());
4056                 if (!info->thread_is_dying) {
4057                         if (gc_callbacks.thread_mark_func && !conservative_stack_mark) {
4058                                 UserCopyOrMarkData data = { NULL, queue };
4059                                 set_user_copy_or_mark_data (&data);
4060                                 gc_callbacks.thread_mark_func (info->runtime_data, info->stack_start, info->stack_end, precise);
4061                                 set_user_copy_or_mark_data (NULL);
4062                         } else if (!precise) {
4063                                 if (!conservative_stack_mark) {
4064                                         fprintf (stderr, "Precise stack mark not supported - disabling.\n");
4065                                         conservative_stack_mark = TRUE;
4066                                 }
4067                                 conservatively_pin_objects_from (info->stack_start, info->stack_end, start_nursery, end_nursery, PIN_TYPE_STACK);
4068                         }
4069                 }
4070
4071                 if (!info->thread_is_dying && !precise) {
4072 #ifdef USE_MONO_CTX
4073                         conservatively_pin_objects_from ((void**)&info->ctx, (void**)&info->ctx + ARCH_NUM_REGS,
4074                                 start_nursery, end_nursery, PIN_TYPE_STACK);
4075 #else
4076                         conservatively_pin_objects_from (&info->regs, &info->regs + ARCH_NUM_REGS,
4077                                         start_nursery, end_nursery, PIN_TYPE_STACK);
4078 #endif
4079                 }
4080         } END_FOREACH_THREAD
4081 }
4082
4083 static gboolean
4084 ptr_on_stack (void *ptr)
4085 {
4086         gpointer stack_start = &stack_start;
4087         SgenThreadInfo *info = mono_thread_info_current ();
4088
4089         if (ptr >= stack_start && ptr < (gpointer)info->stack_end)
4090                 return TRUE;
4091         return FALSE;
4092 }
4093
4094 static void*
4095 sgen_thread_register (SgenThreadInfo* info, void *addr)
4096 {
4097 #ifndef HAVE_KW_THREAD
4098         SgenThreadInfo *__thread_info__ = info;
4099 #endif
4100
4101         LOCK_GC;
4102 #ifndef HAVE_KW_THREAD
4103         info->tlab_start = info->tlab_next = info->tlab_temp_end = info->tlab_real_end = NULL;
4104
4105         g_assert (!mono_native_tls_get_value (thread_info_key));
4106         mono_native_tls_set_value (thread_info_key, info);
4107 #else
4108         sgen_thread_info = info;
4109 #endif
4110
4111 #if !defined(__MACH__)
4112         info->stop_count = -1;
4113         info->signal = 0;
4114 #endif
4115         info->skip = 0;
4116         info->joined_stw = FALSE;
4117         info->doing_handshake = FALSE;
4118         info->thread_is_dying = FALSE;
4119         info->stack_start = NULL;
4120         info->store_remset_buffer_addr = &STORE_REMSET_BUFFER;
4121         info->store_remset_buffer_index_addr = &STORE_REMSET_BUFFER_INDEX;
4122         info->stopped_ip = NULL;
4123         info->stopped_domain = NULL;
4124 #ifdef USE_MONO_CTX
4125         memset (&info->ctx, 0, sizeof (MonoContext));
4126 #else
4127         memset (&info->regs, 0, sizeof (info->regs));
4128 #endif
4129
4130         sgen_init_tlab_info (info);
4131
4132         binary_protocol_thread_register ((gpointer)mono_thread_info_get_tid (info));
4133
4134 #ifdef HAVE_KW_THREAD
4135         store_remset_buffer_index_addr = &store_remset_buffer_index;
4136 #endif
4137
4138         /* try to get it with attributes first */
4139 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
4140         {
4141                 size_t size;
4142                 void *sstart;
4143                 pthread_attr_t attr;
4144                 pthread_getattr_np (pthread_self (), &attr);
4145                 pthread_attr_getstack (&attr, &sstart, &size);
4146                 info->stack_start_limit = sstart;
4147                 info->stack_end = (char*)sstart + size;
4148                 pthread_attr_destroy (&attr);
4149         }
4150 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
4151                  info->stack_end = (char*)pthread_get_stackaddr_np (pthread_self ());
4152                  info->stack_start_limit = (char*)info->stack_end - pthread_get_stacksize_np (pthread_self ());
4153 #else
4154         {
4155                 /* FIXME: we assume the stack grows down */
4156                 gsize stack_bottom = (gsize)addr;
4157                 stack_bottom += 4095;
4158                 stack_bottom &= ~4095;
4159                 info->stack_end = (char*)stack_bottom;
4160         }
4161 #endif
4162
4163 #ifdef HAVE_KW_THREAD
4164         stack_end = info->stack_end;
4165 #endif
4166
4167         if (remset.register_thread)
4168                 remset.register_thread (info);
4169
4170         SGEN_LOG (3, "registered thread %p (%p) stack end %p", info, (gpointer)mono_thread_info_get_tid (info), info->stack_end);
4171
4172         if (gc_callbacks.thread_attach_func)
4173                 info->runtime_data = gc_callbacks.thread_attach_func ();
4174
4175         UNLOCK_GC;
4176         return info;
4177 }
4178
4179 static void
4180 sgen_wbarrier_cleanup_thread (SgenThreadInfo *p)
4181 {
4182         if (remset.cleanup_thread)
4183                 remset.cleanup_thread (p);
4184 }
4185
4186 static void
4187 sgen_thread_unregister (SgenThreadInfo *p)
4188 {
4189         /* If a delegate is passed to native code and invoked on a thread we dont
4190          * know about, the jit will register it with mono_jit_thread_attach, but
4191          * we have no way of knowing when that thread goes away.  SGen has a TSD
4192          * so we assume that if the domain is still registered, we can detach
4193          * the thread
4194          */
4195         if (mono_domain_get ())
4196                 mono_thread_detach (mono_thread_current ());
4197
4198         p->thread_is_dying = TRUE;
4199
4200         /*
4201         There is a race condition between a thread finishing executing and been removed
4202         from the GC thread set.
4203         This happens on posix systems when TLS data is been cleaned-up, libpthread will
4204         set the thread_info slot to NULL before calling the cleanup function. This
4205         opens a window in which the thread is registered but has a NULL TLS.
4206
4207         The suspend signal handler needs TLS data to know where to store thread state
4208         data or otherwise it will simply ignore the thread.
4209
4210         This solution works because the thread doing STW will wait until all threads been
4211         suspended handshake back, so there is no race between the doing_hankshake test
4212         and the suspend_thread call.
4213
4214         This is not required on systems that do synchronous STW as those can deal with
4215         the above race at suspend time.
4216
4217         FIXME: I believe we could avoid this by using mono_thread_info_lookup when
4218         mono_thread_info_current returns NULL. Or fix mono_thread_info_lookup to do so.
4219         */
4220 #if (defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED) || !defined(HAVE_PTHREAD_KILL)
4221         LOCK_GC;
4222 #else
4223         while (!TRYLOCK_GC) {
4224                 if (!sgen_park_current_thread_if_doing_handshake (p))
4225                         g_usleep (50);
4226         }
4227         MONO_GC_LOCKED ();
4228 #endif
4229
4230         binary_protocol_thread_unregister ((gpointer)mono_thread_info_get_tid (p));
4231         SGEN_LOG (3, "unregister thread %p (%p)", p, (gpointer)mono_thread_info_get_tid (p));
4232
4233         if (gc_callbacks.thread_detach_func) {
4234                 gc_callbacks.thread_detach_func (p->runtime_data);
4235                 p->runtime_data = NULL;
4236         }
4237         sgen_wbarrier_cleanup_thread (p);
4238
4239         mono_threads_unregister_current_thread (p);
4240         UNLOCK_GC;
4241 }
4242
4243
4244 static void
4245 sgen_thread_attach (SgenThreadInfo *info)
4246 {
4247         LOCK_GC;
4248         /*this is odd, can we get attached before the gc is inited?*/
4249         init_stats ();
4250         UNLOCK_GC;
4251         
4252         if (gc_callbacks.thread_attach_func && !info->runtime_data)
4253                 info->runtime_data = gc_callbacks.thread_attach_func ();
4254 }
4255 gboolean
4256 mono_gc_register_thread (void *baseptr)
4257 {
4258         return mono_thread_info_attach (baseptr) != NULL;
4259 }
4260
4261 /*
4262  * mono_gc_set_stack_end:
4263  *
4264  *   Set the end of the current threads stack to STACK_END. The stack space between 
4265  * STACK_END and the real end of the threads stack will not be scanned during collections.
4266  */
4267 void
4268 mono_gc_set_stack_end (void *stack_end)
4269 {
4270         SgenThreadInfo *info;
4271
4272         LOCK_GC;
4273         info = mono_thread_info_current ();
4274         if (info) {
4275                 g_assert (stack_end < info->stack_end);
4276                 info->stack_end = stack_end;
4277         }
4278         UNLOCK_GC;
4279 }
4280
4281 #if USE_PTHREAD_INTERCEPT
4282
4283
4284 int
4285 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
4286 {
4287         return pthread_create (new_thread, attr, start_routine, arg);
4288 }
4289
4290 int
4291 mono_gc_pthread_join (pthread_t thread, void **retval)
4292 {
4293         return pthread_join (thread, retval);
4294 }
4295
4296 int
4297 mono_gc_pthread_detach (pthread_t thread)
4298 {
4299         return pthread_detach (thread);
4300 }
4301
4302 void
4303 mono_gc_pthread_exit (void *retval) 
4304 {
4305         pthread_exit (retval);
4306 }
4307
4308 #endif /* USE_PTHREAD_INTERCEPT */
4309
4310 /*
4311  * ######################################################################
4312  * ########  Write barriers
4313  * ######################################################################
4314  */
4315
4316 /*
4317  * Note: the write barriers first do the needed GC work and then do the actual store:
4318  * this way the value is visible to the conservative GC scan after the write barrier
4319  * itself. If a GC interrupts the barrier in the middle, value will be kept alive by
4320  * the conservative scan, otherwise by the remembered set scan.
4321  */
4322 void
4323 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
4324 {
4325         HEAVY_STAT (++stat_wbarrier_set_field);
4326         if (ptr_in_nursery (field_ptr)) {
4327                 *(void**)field_ptr = value;
4328                 return;
4329         }
4330         SGEN_LOG (8, "Adding remset at %p", field_ptr);
4331         if (value)
4332                 binary_protocol_wbarrier (field_ptr, value, value->vtable);
4333
4334         remset.wbarrier_set_field (obj, field_ptr, value);
4335 }
4336
4337 void
4338 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
4339 {
4340         HEAVY_STAT (++stat_wbarrier_set_arrayref);
4341         if (ptr_in_nursery (slot_ptr)) {
4342                 *(void**)slot_ptr = value;
4343                 return;
4344         }
4345         SGEN_LOG (8, "Adding remset at %p", slot_ptr);
4346         if (value)
4347                 binary_protocol_wbarrier (slot_ptr, value, value->vtable);
4348
4349         remset.wbarrier_set_arrayref (arr, slot_ptr, value);
4350 }
4351
4352 void
4353 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
4354 {
4355         HEAVY_STAT (++stat_wbarrier_arrayref_copy);
4356         /*This check can be done without taking a lock since dest_ptr array is pinned*/
4357         if (ptr_in_nursery (dest_ptr) || count <= 0) {
4358                 mono_gc_memmove (dest_ptr, src_ptr, count * sizeof (gpointer));
4359                 return;
4360         }
4361
4362 #ifdef SGEN_BINARY_PROTOCOL
4363         {
4364                 int i;
4365                 for (i = 0; i < count; ++i) {
4366                         gpointer dest = (gpointer*)dest_ptr + i;
4367                         gpointer obj = *((gpointer*)src_ptr + i);
4368                         if (obj)
4369                                 binary_protocol_wbarrier (dest, obj, (gpointer)LOAD_VTABLE (obj));
4370                 }
4371         }
4372 #endif
4373
4374         remset.wbarrier_arrayref_copy (dest_ptr, src_ptr, count);
4375 }
4376
4377 static char *found_obj;
4378
4379 static void
4380 find_object_for_ptr_callback (char *obj, size_t size, void *user_data)
4381 {
4382         char *ptr = user_data;
4383
4384         if (ptr >= obj && ptr < obj + size) {
4385                 g_assert (!found_obj);
4386                 found_obj = obj;
4387         }
4388 }
4389
4390 /* for use in the debugger */
4391 char* find_object_for_ptr (char *ptr);
4392 char*
4393 find_object_for_ptr (char *ptr)
4394 {
4395         if (ptr >= nursery_section->data && ptr < nursery_section->end_data) {
4396                 found_obj = NULL;
4397                 sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
4398                                 find_object_for_ptr_callback, ptr, TRUE);
4399                 if (found_obj)
4400                         return found_obj;
4401         }
4402
4403         found_obj = NULL;
4404         sgen_los_iterate_objects (find_object_for_ptr_callback, ptr);
4405         if (found_obj)
4406                 return found_obj;
4407
4408         /*
4409          * Very inefficient, but this is debugging code, supposed to
4410          * be called from gdb, so we don't care.
4411          */
4412         found_obj = NULL;
4413         major_collector.iterate_objects (TRUE, TRUE, find_object_for_ptr_callback, ptr);
4414         return found_obj;
4415 }
4416
4417 void
4418 mono_gc_wbarrier_generic_nostore (gpointer ptr)
4419 {
4420         gpointer obj;
4421
4422         HEAVY_STAT (++stat_wbarrier_generic_store);
4423
4424 #ifdef XDOMAIN_CHECKS_IN_WBARRIER
4425         /* FIXME: ptr_in_heap must be called with the GC lock held */
4426         if (xdomain_checks && *(MonoObject**)ptr && ptr_in_heap (ptr)) {
4427                 char *start = find_object_for_ptr (ptr);
4428                 MonoObject *value = *(MonoObject**)ptr;
4429                 LOCK_GC;
4430                 g_assert (start);
4431                 if (start) {
4432                         MonoObject *obj = (MonoObject*)start;
4433                         if (obj->vtable->domain != value->vtable->domain)
4434                                 g_assert (is_xdomain_ref_allowed (ptr, start, obj->vtable->domain));
4435                 }
4436                 UNLOCK_GC;
4437         }
4438 #endif
4439
4440         obj = *(gpointer*)ptr;
4441         if (obj)
4442                 binary_protocol_wbarrier (ptr, obj, (gpointer)LOAD_VTABLE (obj));
4443
4444         if (ptr_in_nursery (ptr) || ptr_on_stack (ptr)) {
4445                 SGEN_LOG (8, "Skipping remset at %p", ptr);
4446                 return;
4447         }
4448
4449         /*
4450          * We need to record old->old pointer locations for the
4451          * concurrent collector.
4452          */
4453         if (!ptr_in_nursery (obj) && !concurrent_collection_in_progress) {
4454                 SGEN_LOG (8, "Skipping remset at %p", ptr);
4455                 return;
4456         }
4457
4458         SGEN_LOG (8, "Adding remset at %p", ptr);
4459
4460         remset.wbarrier_generic_nostore (ptr);
4461 }
4462
4463 void
4464 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
4465 {
4466         SGEN_LOG (8, "Wbarrier store at %p to %p (%s)", ptr, value, value ? safe_name (value) : "null");
4467         *(void**)ptr = value;
4468         if (ptr_in_nursery (value))
4469                 mono_gc_wbarrier_generic_nostore (ptr);
4470         sgen_dummy_use (value);
4471 }
4472
4473 void mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
4474 {
4475         mword *dest = _dest;
4476         mword *src = _src;
4477
4478         while (size) {
4479                 if (bitmap & 0x1)
4480                         mono_gc_wbarrier_generic_store (dest, (MonoObject*)*src);
4481                 else
4482                         *dest = *src;
4483                 ++src;
4484                 ++dest;
4485                 size -= SIZEOF_VOID_P;
4486                 bitmap >>= 1;
4487         }
4488 }
4489
4490 #ifdef SGEN_BINARY_PROTOCOL
4491 #undef HANDLE_PTR
4492 #define HANDLE_PTR(ptr,obj) do {                                        \
4493                 gpointer o = *(gpointer*)(ptr);                         \
4494                 if ((o)) {                                              \
4495                         gpointer d = ((char*)dest) + ((char*)(ptr) - (char*)(obj)); \
4496                         binary_protocol_wbarrier (d, o, (gpointer) LOAD_VTABLE (o)); \
4497                 }                                                       \
4498         } while (0)
4499
4500 static void
4501 scan_object_for_binary_protocol_copy_wbarrier (gpointer dest, char *start, mword desc)
4502 {
4503 #define SCAN_OBJECT_NOVTABLE
4504 #include "sgen-scan-object.h"
4505 }
4506 #endif
4507
4508 void
4509 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
4510 {
4511         HEAVY_STAT (++stat_wbarrier_value_copy);
4512         g_assert (klass->valuetype);
4513
4514         SGEN_LOG (8, "Adding value remset at %p, count %d, descr %p for class %s (%p)", dest, count, klass->gc_descr, klass->name, klass);
4515
4516         if (ptr_in_nursery (dest) || ptr_on_stack (dest) || !SGEN_CLASS_HAS_REFERENCES (klass)) {
4517                 size_t element_size = mono_class_value_size (klass, NULL);
4518                 size_t size = count * element_size;
4519                 mono_gc_memmove (dest, src, size);              
4520                 return;
4521         }
4522
4523 #ifdef SGEN_BINARY_PROTOCOL
4524         {
4525                 size_t element_size = mono_class_value_size (klass, NULL);
4526                 int i;
4527                 for (i = 0; i < count; ++i) {
4528                         scan_object_for_binary_protocol_copy_wbarrier ((char*)dest + i * element_size,
4529                                         (char*)src + i * element_size - sizeof (MonoObject),
4530                                         (mword) klass->gc_descr);
4531                 }
4532         }
4533 #endif
4534
4535         remset.wbarrier_value_copy (dest, src, count, klass);
4536 }
4537
4538 /**
4539  * mono_gc_wbarrier_object_copy:
4540  *
4541  * Write barrier to call when obj is the result of a clone or copy of an object.
4542  */
4543 void
4544 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
4545 {
4546         int size;
4547
4548         HEAVY_STAT (++stat_wbarrier_object_copy);
4549
4550         if (ptr_in_nursery (obj) || ptr_on_stack (obj)) {
4551                 size = mono_object_class (obj)->instance_size;
4552                 mono_gc_memmove ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
4553                                 size - sizeof (MonoObject));
4554                 return; 
4555         }
4556
4557 #ifdef SGEN_BINARY_PROTOCOL
4558         scan_object_for_binary_protocol_copy_wbarrier (obj, (char*)src, (mword) src->vtable->gc_descr);
4559 #endif
4560
4561         remset.wbarrier_object_copy (obj, src);
4562 }
4563
4564
4565 /*
4566  * ######################################################################
4567  * ########  Other mono public interface functions.
4568  * ######################################################################
4569  */
4570
4571 #define REFS_SIZE 128
4572 typedef struct {
4573         void *data;
4574         MonoGCReferences callback;
4575         int flags;
4576         int count;
4577         int called;
4578         MonoObject *refs [REFS_SIZE];
4579         uintptr_t offsets [REFS_SIZE];
4580 } HeapWalkInfo;
4581
4582 #undef HANDLE_PTR
4583 #define HANDLE_PTR(ptr,obj)     do {    \
4584                 if (*(ptr)) {   \
4585                         if (hwi->count == REFS_SIZE) {  \
4586                                 hwi->callback ((MonoObject*)start, mono_object_class (start), hwi->called? 0: size, hwi->count, hwi->refs, hwi->offsets, hwi->data);    \
4587                                 hwi->count = 0; \
4588                                 hwi->called = 1;        \
4589                         }       \
4590                         hwi->offsets [hwi->count] = (char*)(ptr)-(char*)start;  \
4591                         hwi->refs [hwi->count++] = *(ptr);      \
4592                 }       \
4593         } while (0)
4594
4595 static void
4596 collect_references (HeapWalkInfo *hwi, char *start, size_t size)
4597 {
4598 #include "sgen-scan-object.h"
4599 }
4600
4601 static void
4602 walk_references (char *start, size_t size, void *data)
4603 {
4604         HeapWalkInfo *hwi = data;
4605         hwi->called = 0;
4606         hwi->count = 0;
4607         collect_references (hwi, start, size);
4608         if (hwi->count || !hwi->called)
4609                 hwi->callback ((MonoObject*)start, mono_object_class (start), hwi->called? 0: size, hwi->count, hwi->refs, hwi->offsets, hwi->data);
4610 }
4611
4612 /**
4613  * mono_gc_walk_heap:
4614  * @flags: flags for future use
4615  * @callback: a function pointer called for each object in the heap
4616  * @data: a user data pointer that is passed to callback
4617  *
4618  * This function can be used to iterate over all the live objects in the heap:
4619  * for each object, @callback is invoked, providing info about the object's
4620  * location in memory, its class, its size and the objects it references.
4621  * For each referenced object it's offset from the object address is
4622  * reported in the offsets array.
4623  * The object references may be buffered, so the callback may be invoked
4624  * multiple times for the same object: in all but the first call, the size
4625  * argument will be zero.
4626  * Note that this function can be only called in the #MONO_GC_EVENT_PRE_START_WORLD
4627  * profiler event handler.
4628  *
4629  * Returns: a non-zero value if the GC doesn't support heap walking
4630  */
4631 int
4632 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
4633 {
4634         HeapWalkInfo hwi;
4635
4636         hwi.flags = flags;
4637         hwi.callback = callback;
4638         hwi.data = data;
4639
4640         sgen_clear_nursery_fragments ();
4641         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data, walk_references, &hwi, FALSE);
4642
4643         major_collector.iterate_objects (TRUE, TRUE, walk_references, &hwi);
4644         sgen_los_iterate_objects (walk_references, &hwi);
4645
4646         return 0;
4647 }
4648
4649 void
4650 mono_gc_collect (int generation)
4651 {
4652         LOCK_GC;
4653         if (generation > 1)
4654                 generation = 1;
4655         sgen_perform_collection (0, generation, "user request", TRUE);
4656         UNLOCK_GC;
4657 }
4658
4659 int
4660 mono_gc_max_generation (void)
4661 {
4662         return 1;
4663 }
4664
4665 int
4666 mono_gc_collection_count (int generation)
4667 {
4668         if (generation == 0)
4669                 return stat_minor_gcs;
4670         return stat_major_gcs;
4671 }
4672
4673 int64_t
4674 mono_gc_get_used_size (void)
4675 {
4676         gint64 tot = 0;
4677         LOCK_GC;
4678         tot = los_memory_usage;
4679         tot += nursery_section->next_data - nursery_section->data;
4680         tot += major_collector.get_used_size ();
4681         /* FIXME: account for pinned objects */
4682         UNLOCK_GC;
4683         return tot;
4684 }
4685
4686 void
4687 mono_gc_disable (void)
4688 {
4689         LOCK_GC;
4690         gc_disabled++;
4691         UNLOCK_GC;
4692 }
4693
4694 void
4695 mono_gc_enable (void)
4696 {
4697         LOCK_GC;
4698         gc_disabled--;
4699         UNLOCK_GC;
4700 }
4701
4702 int
4703 mono_gc_get_los_limit (void)
4704 {
4705         return MAX_SMALL_OBJ_SIZE;
4706 }
4707
4708 gboolean
4709 mono_gc_user_markers_supported (void)
4710 {
4711         return TRUE;
4712 }
4713
4714 gboolean
4715 mono_object_is_alive (MonoObject* o)
4716 {
4717         return TRUE;
4718 }
4719
4720 int
4721 mono_gc_get_generation (MonoObject *obj)
4722 {
4723         if (ptr_in_nursery (obj))
4724                 return 0;
4725         return 1;
4726 }
4727
4728 void
4729 mono_gc_enable_events (void)
4730 {
4731 }
4732
4733 void
4734 mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
4735 {
4736         sgen_register_disappearing_link (obj, link_addr, track, FALSE);
4737 }
4738
4739 void
4740 mono_gc_weak_link_remove (void **link_addr, gboolean track)
4741 {
4742         sgen_register_disappearing_link (NULL, link_addr, track, FALSE);
4743 }
4744
4745 MonoObject*
4746 mono_gc_weak_link_get (void **link_addr)
4747 {
4748         /*
4749          * We must only load *link_addr once because it might change
4750          * under our feet, and REVEAL_POINTER (NULL) results in an
4751          * invalid reference.
4752          */
4753         void *ptr = *link_addr;
4754         if (!ptr)
4755                 return NULL;
4756
4757         /*
4758          * During the second bridge processing step the world is
4759          * running again.  That step processes all weak links once
4760          * more to null those that refer to dead objects.  Before that
4761          * is completed, those links must not be followed, so we
4762          * conservatively wait for bridge processing when any weak
4763          * link is dereferenced.
4764          */
4765         if (G_UNLIKELY (bridge_processing_in_progress))
4766                 mono_gc_wait_for_bridge_processing ();
4767
4768         return (MonoObject*) REVEAL_POINTER (ptr);
4769 }
4770
4771 gboolean
4772 mono_gc_ephemeron_array_add (MonoObject *obj)
4773 {
4774         EphemeronLinkNode *node;
4775
4776         LOCK_GC;
4777
4778         node = sgen_alloc_internal (INTERNAL_MEM_EPHEMERON_LINK);
4779         if (!node) {
4780                 UNLOCK_GC;
4781                 return FALSE;
4782         }
4783         node->array = (char*)obj;
4784         node->next = ephemeron_list;
4785         ephemeron_list = node;
4786
4787         SGEN_LOG (5, "Registered ephemeron array %p", obj);
4788
4789         UNLOCK_GC;
4790         return TRUE;
4791 }
4792
4793 void*
4794 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
4795 {
4796         void *result;
4797         LOCK_INTERRUPTION;
4798         result = func (data);
4799         UNLOCK_INTERRUPTION;
4800         return result;
4801 }
4802
4803 gboolean
4804 mono_gc_is_gc_thread (void)
4805 {
4806         gboolean result;
4807         LOCK_GC;
4808         result = mono_thread_info_current () != NULL;
4809         UNLOCK_GC;
4810         return result;
4811 }
4812
4813 static gboolean
4814 is_critical_method (MonoMethod *method)
4815 {
4816         return mono_runtime_is_critical_method (method) || sgen_is_critical_method (method);
4817 }
4818         
4819 void
4820 mono_gc_base_init (void)
4821 {
4822         MonoThreadInfoCallbacks cb;
4823         char *env;
4824         char **opts, **ptr;
4825         char *major_collector_opt = NULL;
4826         char *minor_collector_opt = NULL;
4827         glong max_heap = 0;
4828         glong soft_limit = 0;
4829         int num_workers;
4830         int result;
4831         int dummy;
4832         gboolean debug_print_allowance = FALSE;
4833         double allowance_ratio = 0, save_target = 0;
4834         gboolean have_split_nursery = FALSE;
4835         gboolean cement_enabled = TRUE;
4836
4837         do {
4838                 result = InterlockedCompareExchange (&gc_initialized, -1, 0);
4839                 switch (result) {
4840                 case 1:
4841                         /* already inited */
4842                         return;
4843                 case -1:
4844                         /* being inited by another thread */
4845                         g_usleep (1000);
4846                         break;
4847                 case 0:
4848                         /* we will init it */
4849                         break;
4850                 default:
4851                         g_assert_not_reached ();
4852                 }
4853         } while (result != 0);
4854
4855         LOCK_INIT (gc_mutex);
4856
4857         pagesize = mono_pagesize ();
4858         gc_debug_file = stderr;
4859
4860         cb.thread_register = sgen_thread_register;
4861         cb.thread_unregister = sgen_thread_unregister;
4862         cb.thread_attach = sgen_thread_attach;
4863         cb.mono_method_is_critical = (gpointer)is_critical_method;
4864 #ifndef HOST_WIN32
4865         cb.mono_gc_pthread_create = (gpointer)mono_gc_pthread_create;
4866 #endif
4867
4868         mono_threads_init (&cb, sizeof (SgenThreadInfo));
4869
4870         LOCK_INIT (sgen_interruption_mutex);
4871         LOCK_INIT (pin_queue_mutex);
4872
4873         init_user_copy_or_mark_key ();
4874
4875         if ((env = getenv ("MONO_GC_PARAMS"))) {
4876                 opts = g_strsplit (env, ",", -1);
4877                 for (ptr = opts; *ptr; ++ptr) {
4878                         char *opt = *ptr;
4879                         if (g_str_has_prefix (opt, "major=")) {
4880                                 opt = strchr (opt, '=') + 1;
4881                                 major_collector_opt = g_strdup (opt);
4882                         } else if (g_str_has_prefix (opt, "minor=")) {
4883                                 opt = strchr (opt, '=') + 1;
4884                                 minor_collector_opt = g_strdup (opt);
4885                         }
4886                 }
4887         } else {
4888                 opts = NULL;
4889         }
4890
4891         init_stats ();
4892         sgen_init_internal_allocator ();
4893         sgen_init_nursery_allocator ();
4894
4895         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_SECTION, SGEN_SIZEOF_GC_MEM_SECTION);
4896         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_FINALIZE_READY_ENTRY, sizeof (FinalizeReadyEntry));
4897         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_GRAY_QUEUE, sizeof (GrayQueueSection));
4898         g_assert (sizeof (GenericStoreRememberedSet) == sizeof (gpointer) * STORE_REMSET_BUFFER_SIZE);
4899         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_STORE_REMSET, sizeof (GenericStoreRememberedSet));
4900         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_EPHEMERON_LINK, sizeof (EphemeronLinkNode));
4901
4902 #ifndef HAVE_KW_THREAD
4903         mono_native_tls_alloc (&thread_info_key, NULL);
4904 #endif
4905
4906         /*
4907          * This needs to happen before any internal allocations because
4908          * it inits the small id which is required for hazard pointer
4909          * operations.
4910          */
4911         sgen_os_init ();
4912
4913         mono_thread_info_attach (&dummy);
4914
4915         if (!minor_collector_opt) {
4916                 sgen_simple_nursery_init (&sgen_minor_collector);
4917         } else {
4918                 if (!strcmp (minor_collector_opt, "simple")) {
4919                         sgen_simple_nursery_init (&sgen_minor_collector);
4920                 } else if (!strcmp (minor_collector_opt, "split")) {
4921                         sgen_split_nursery_init (&sgen_minor_collector);
4922                         have_split_nursery = TRUE;
4923                 } else {
4924                         fprintf (stderr, "Unknown minor collector `%s'.\n", minor_collector_opt);
4925                         exit (1);
4926                 }
4927         }
4928
4929         if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep")) {
4930                 sgen_marksweep_init (&major_collector);
4931         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-fixed")) {
4932                 sgen_marksweep_fixed_init (&major_collector);
4933         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-par")) {
4934                 sgen_marksweep_par_init (&major_collector);
4935         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-fixed-par")) {
4936                 sgen_marksweep_fixed_par_init (&major_collector);
4937         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-conc")) {
4938                 sgen_marksweep_conc_init (&major_collector);
4939         } else {
4940                 fprintf (stderr, "Unknown major collector `%s'.\n", major_collector_opt);
4941                 exit (1);
4942         }
4943
4944 #ifdef SGEN_HAVE_CARDTABLE
4945         use_cardtable = major_collector.supports_cardtable;
4946 #else
4947         use_cardtable = FALSE;
4948 #endif
4949
4950         num_workers = mono_cpu_count ();
4951         g_assert (num_workers > 0);
4952         if (num_workers > 16)
4953                 num_workers = 16;
4954
4955         ///* Keep this the default for now */
4956         /* Precise marking is broken on all supported targets. Disable until fixed. */
4957         conservative_stack_mark = TRUE;
4958
4959         sgen_nursery_size = DEFAULT_NURSERY_SIZE;
4960
4961         if (opts) {
4962                 for (ptr = opts; *ptr; ++ptr) {
4963                         char *opt = *ptr;
4964                         if (g_str_has_prefix (opt, "major="))
4965                                 continue;
4966                         if (g_str_has_prefix (opt, "minor="))
4967                                 continue;
4968                         if (g_str_has_prefix (opt, "wbarrier=")) {
4969                                 opt = strchr (opt, '=') + 1;
4970                                 if (strcmp (opt, "remset") == 0) {
4971                                         if (major_collector.is_concurrent) {
4972                                                 fprintf (stderr, "The concurrent collector does not support the SSB write barrier.\n");
4973                                                 exit (1);
4974                                         }
4975                                         use_cardtable = FALSE;
4976                                 } else if (strcmp (opt, "cardtable") == 0) {
4977                                         if (!use_cardtable) {
4978                                                 if (major_collector.supports_cardtable)
4979                                                         fprintf (stderr, "The cardtable write barrier is not supported on this platform.\n");
4980                                                 else
4981                                                         fprintf (stderr, "The major collector does not support the cardtable write barrier.\n");
4982                                                 exit (1);
4983                                         }
4984                                 } else {
4985                                         fprintf (stderr, "wbarrier must either be `remset' or `cardtable'.");
4986                                         exit (1);
4987                                 }
4988                                 continue;
4989                         }
4990                         if (g_str_has_prefix (opt, "max-heap-size=")) {
4991                                 opt = strchr (opt, '=') + 1;
4992                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
4993                                         if ((max_heap & (mono_pagesize () - 1))) {
4994                                                 fprintf (stderr, "max-heap-size size must be a multiple of %d.\n", mono_pagesize ());
4995                                                 exit (1);
4996                                         }
4997                                 } else {
4998                                         fprintf (stderr, "max-heap-size must be an integer.\n");
4999                                         exit (1);
5000                                 }
5001                                 continue;
5002                         }
5003                         if (g_str_has_prefix (opt, "soft-heap-limit=")) {
5004                                 opt = strchr (opt, '=') + 1;
5005                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &soft_limit)) {
5006                                         if (soft_limit <= 0) {
5007                                                 fprintf (stderr, "soft-heap-limit must be positive.\n");
5008                                                 exit (1);
5009                                         }
5010                                 } else {
5011                                         fprintf (stderr, "soft-heap-limit must be an integer.\n");
5012                                         exit (1);
5013                                 }
5014                                 continue;
5015                         }
5016                         if (g_str_has_prefix (opt, "workers=")) {
5017                                 long val;
5018                                 char *endptr;
5019                                 if (!major_collector.is_parallel) {
5020                                         fprintf (stderr, "The workers= option can only be used for parallel collectors.");
5021                                         exit (1);
5022                                 }
5023                                 opt = strchr (opt, '=') + 1;
5024                                 val = strtol (opt, &endptr, 10);
5025                                 if (!*opt || *endptr) {
5026                                         fprintf (stderr, "Cannot parse the workers= option value.");
5027                                         exit (1);
5028                                 }
5029                                 if (val <= 0 || val > 16) {
5030                                         fprintf (stderr, "The number of workers must be in the range 1 to 16.");
5031                                         exit (1);
5032                                 }
5033                                 num_workers = (int)val;
5034                                 continue;
5035                         }
5036                         if (g_str_has_prefix (opt, "stack-mark=")) {
5037                                 opt = strchr (opt, '=') + 1;
5038                                 if (!strcmp (opt, "precise")) {
5039                                         conservative_stack_mark = FALSE;
5040                                 } else if (!strcmp (opt, "conservative")) {
5041                                         conservative_stack_mark = TRUE;
5042                                 } else {
5043                                         fprintf (stderr, "Invalid value '%s' for stack-mark= option, possible values are: 'precise', 'conservative'.\n", opt);
5044                                         exit (1);
5045                                 }
5046                                 continue;
5047                         }
5048                         if (g_str_has_prefix (opt, "bridge=")) {
5049                                 opt = strchr (opt, '=') + 1;
5050                                 sgen_register_test_bridge_callbacks (g_strdup (opt));
5051                                 continue;
5052                         }
5053 #ifdef USER_CONFIG
5054                         if (g_str_has_prefix (opt, "nursery-size=")) {
5055                                 long val;
5056                                 opt = strchr (opt, '=') + 1;
5057                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &val)) {
5058                                         sgen_nursery_size = val;
5059 #ifdef SGEN_ALIGN_NURSERY
5060                                         if ((val & (val - 1))) {
5061                                                 fprintf (stderr, "The nursery size must be a power of two.\n");
5062                                                 exit (1);
5063                                         }
5064
5065                                         if (val < SGEN_MAX_NURSERY_WASTE) {
5066                                                 fprintf (stderr, "The nursery size must be at least %d bytes.\n", SGEN_MAX_NURSERY_WASTE);
5067                                                 exit (1);
5068                                         }
5069
5070                                         sgen_nursery_bits = 0;
5071                                         while (1 << (++ sgen_nursery_bits) != sgen_nursery_size)
5072                                                 ;
5073 #endif
5074                                 } else {
5075                                         fprintf (stderr, "nursery-size must be an integer.\n");
5076                                         exit (1);
5077                                 }
5078                                 continue;
5079                         }
5080 #endif
5081                         if (g_str_has_prefix (opt, "save-target-ratio=")) {
5082                                 char *endptr;
5083                                 opt = strchr (opt, '=') + 1;
5084                                 save_target = strtod (opt, &endptr);
5085                                 if (endptr == opt) {
5086                                         fprintf (stderr, "save-target-ratio must be a number.");
5087                                         exit (1);
5088                                 }
5089                                 if (save_target < SGEN_MIN_SAVE_TARGET_RATIO || save_target > SGEN_MAX_SAVE_TARGET_RATIO) {
5090                                         fprintf (stderr, "save-target-ratio must be between %.2f - %.2f.", SGEN_MIN_SAVE_TARGET_RATIO, SGEN_MAX_SAVE_TARGET_RATIO);
5091                                         exit (1);
5092                                 }
5093                                 continue;
5094                         }
5095                         if (g_str_has_prefix (opt, "default-allowance-ratio=")) {
5096                                 char *endptr;
5097                                 opt = strchr (opt, '=') + 1;
5098
5099                                 allowance_ratio = strtod (opt, &endptr);
5100                                 if (endptr == opt) {
5101                                         fprintf (stderr, "save-target-ratio must be a number.");
5102                                         exit (1);
5103                                 }
5104                                 if (allowance_ratio < SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO || allowance_ratio > SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO) {
5105                                         fprintf (stderr, "default-allowance-ratio must be between %.2f - %.2f.", SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO, SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO);
5106                                         exit (1);
5107                                 }
5108                                 continue;
5109                         }
5110
5111                         if (!strcmp (opt, "cementing")) {
5112                                 cement_enabled = TRUE;
5113                                 continue;
5114                         }
5115                         if (!strcmp (opt, "no-cementing")) {
5116                                 cement_enabled = FALSE;
5117                                 continue;
5118                         }
5119
5120                         if (major_collector.handle_gc_param && major_collector.handle_gc_param (opt))
5121                                 continue;
5122
5123                         if (sgen_minor_collector.handle_gc_param && sgen_minor_collector.handle_gc_param (opt))
5124                                 continue;
5125
5126                         fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
5127                         fprintf (stderr, "  max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
5128                         fprintf (stderr, "  soft-heap-limit=n (where N is an integer, possibly with a k, m or a g suffix)\n");
5129                         fprintf (stderr, "  nursery-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
5130                         fprintf (stderr, "  major=COLLECTOR (where COLLECTOR is `marksweep', `marksweep-par', 'marksweep-fixed' or 'marksweep-fixed-par')\n");
5131                         fprintf (stderr, "  minor=COLLECTOR (where COLLECTOR is `simple' or `split')\n");
5132                         fprintf (stderr, "  wbarrier=WBARRIER (where WBARRIER is `remset' or `cardtable')\n");
5133                         fprintf (stderr, "  stack-mark=MARK-METHOD (where MARK-METHOD is 'precise' or 'conservative')\n");
5134                         fprintf (stderr, "  [no-]cementing\n");
5135                         if (major_collector.print_gc_param_usage)
5136                                 major_collector.print_gc_param_usage ();
5137                         if (sgen_minor_collector.print_gc_param_usage)
5138                                 sgen_minor_collector.print_gc_param_usage ();
5139                         fprintf (stderr, " Experimental options:\n");
5140                         fprintf (stderr, "  save-target-ratio=R (where R must be between %.2f - %.2f).\n", SGEN_MIN_SAVE_TARGET_RATIO, SGEN_MAX_SAVE_TARGET_RATIO);
5141                         fprintf (stderr, "  default-allowance-ratio=R (where R must be between %.2f - %.2f).\n", SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO, SGEN_MAX_ALLOWANCE_NURSERY_SIZE_RATIO);
5142                         exit (1);
5143                 }
5144                 g_strfreev (opts);
5145         }
5146
5147         if (major_collector.is_parallel)
5148                 sgen_workers_init (num_workers);
5149         else if (major_collector.is_concurrent)
5150                 sgen_workers_init (1);
5151
5152         if (major_collector_opt)
5153                 g_free (major_collector_opt);
5154
5155         if (minor_collector_opt)
5156                 g_free (minor_collector_opt);
5157
5158         alloc_nursery ();
5159
5160         sgen_cement_init (cement_enabled);
5161
5162         if ((env = getenv ("MONO_GC_DEBUG"))) {
5163                 opts = g_strsplit (env, ",", -1);
5164                 for (ptr = opts; ptr && *ptr; ptr ++) {
5165                         char *opt = *ptr;
5166                         if (opt [0] >= '0' && opt [0] <= '9') {
5167                                 gc_debug_level = atoi (opt);
5168                                 opt++;
5169                                 if (opt [0] == ':')
5170                                         opt++;
5171                                 if (opt [0]) {
5172 #ifdef HOST_WIN32
5173                                         char *rf = g_strdup_printf ("%s.%d", opt, GetCurrentProcessId ());
5174 #else
5175                                         char *rf = g_strdup_printf ("%s.%d", opt, getpid ());
5176 #endif
5177                                         gc_debug_file = fopen (rf, "wb");
5178                                         if (!gc_debug_file)
5179                                                 gc_debug_file = stderr;
5180                                         g_free (rf);
5181                                 }
5182                         } else if (!strcmp (opt, "print-allowance")) {
5183                                 debug_print_allowance = TRUE;
5184                         } else if (!strcmp (opt, "print-pinning")) {
5185                                 do_pin_stats = TRUE;
5186                         } else if (!strcmp (opt, "verify-before-allocs")) {
5187                                 verify_before_allocs = 1;
5188                                 has_per_allocation_action = TRUE;
5189                         } else if (g_str_has_prefix (opt, "verify-before-allocs=")) {
5190                                 char *arg = strchr (opt, '=') + 1;
5191                                 verify_before_allocs = atoi (arg);
5192                                 has_per_allocation_action = TRUE;
5193                         } else if (!strcmp (opt, "collect-before-allocs")) {
5194                                 collect_before_allocs = 1;
5195                                 has_per_allocation_action = TRUE;
5196                         } else if (g_str_has_prefix (opt, "collect-before-allocs=")) {
5197                                 char *arg = strchr (opt, '=') + 1;
5198                                 has_per_allocation_action = TRUE;
5199                                 collect_before_allocs = atoi (arg);
5200                         } else if (!strcmp (opt, "verify-before-collections")) {
5201                                 whole_heap_check_before_collection = TRUE;
5202                         } else if (!strcmp (opt, "check-at-minor-collections")) {
5203                                 consistency_check_at_minor_collection = TRUE;
5204                                 nursery_clear_policy = CLEAR_AT_GC;
5205                         } else if (!strcmp (opt, "check-mark-bits")) {
5206                                 check_mark_bits_after_major_collection = TRUE;
5207                         } else if (!strcmp (opt, "check-nursery-pinned")) {
5208                                 check_nursery_objects_pinned = TRUE;
5209                         } else if (!strcmp (opt, "xdomain-checks")) {
5210                                 xdomain_checks = TRUE;
5211                         } else if (!strcmp (opt, "clear-at-gc")) {
5212                                 nursery_clear_policy = CLEAR_AT_GC;
5213                         } else if (!strcmp (opt, "clear-nursery-at-gc")) {
5214                                 nursery_clear_policy = CLEAR_AT_GC;
5215                         } else if (!strcmp (opt, "check-scan-starts")) {
5216                                 do_scan_starts_check = TRUE;
5217                         } else if (!strcmp (opt, "verify-nursery-at-minor-gc")) {
5218                                 do_verify_nursery = TRUE;
5219                         } else if (!strcmp (opt, "check-concurrent")) {
5220                                 if (!major_collector.is_concurrent) {
5221                                         fprintf (stderr, "Error: check-concurrent only world with concurrent major collectors.\n");
5222                                         exit (1);
5223                                 }
5224                                 do_concurrent_checks = TRUE;
5225                         } else if (!strcmp (opt, "dump-nursery-at-minor-gc")) {
5226                                 do_dump_nursery_content = TRUE;
5227                         } else if (!strcmp (opt, "no-managed-allocator")) {
5228                                 sgen_set_use_managed_allocator (FALSE);
5229                         } else if (!strcmp (opt, "disable-minor")) {
5230                                 disable_minor_collections = TRUE;
5231                         } else if (!strcmp (opt, "disable-major")) {
5232                                 disable_major_collections = TRUE;
5233                         } else if (g_str_has_prefix (opt, "heap-dump=")) {
5234                                 char *filename = strchr (opt, '=') + 1;
5235                                 nursery_clear_policy = CLEAR_AT_GC;
5236                                 heap_dump_file = fopen (filename, "w");
5237                                 if (heap_dump_file) {
5238                                         fprintf (heap_dump_file, "<sgen-dump>\n");
5239                                         do_pin_stats = TRUE;
5240                                 }
5241 #ifdef SGEN_BINARY_PROTOCOL
5242                         } else if (g_str_has_prefix (opt, "binary-protocol=")) {
5243                                 char *filename = strchr (opt, '=') + 1;
5244                                 binary_protocol_init (filename);
5245                                 if (use_cardtable)
5246                                         fprintf (stderr, "Warning: Cardtable write barriers will not be binary-protocolled.\n");
5247 #endif
5248                         } else {
5249                                 fprintf (stderr, "Invalid format for the MONO_GC_DEBUG env variable: '%s'\n", env);
5250                                 fprintf (stderr, "The format is: MONO_GC_DEBUG=[l[:filename]|<option>]+ where l is a debug level 0-9.\n");
5251                                 fprintf (stderr, "Valid options are:\n");
5252                                 fprintf (stderr, "  collect-before-allocs[=<n>]\n");
5253                                 fprintf (stderr, "  verify-before-allocs[=<n>]\n");
5254                                 fprintf (stderr, "  check-at-minor-collections\n");
5255                                 fprintf (stderr, "  check-mark-bits\n");
5256                                 fprintf (stderr, "  check-nursery-pinned\n");
5257                                 fprintf (stderr, "  verify-before-collections\n");
5258                                 fprintf (stderr, "  verify-nursery-at-minor-gc\n");
5259                                 fprintf (stderr, "  dump-nursery-at-minor-gc\n");
5260                                 fprintf (stderr, "  disable-minor\n");
5261                                 fprintf (stderr, "  disable-major\n");
5262                                 fprintf (stderr, "  xdomain-checks\n");
5263                                 fprintf (stderr, "  check-concurrent\n");
5264                                 fprintf (stderr, "  clear-at-gc\n");
5265                                 fprintf (stderr, "  clear-nursery-at-gc\n");
5266                                 fprintf (stderr, "  check-scan-starts\n");
5267                                 fprintf (stderr, "  no-managed-allocator\n");
5268                                 fprintf (stderr, "  print-allowance\n");
5269                                 fprintf (stderr, "  print-pinning\n");
5270                                 fprintf (stderr, "  heap-dump=<filename>\n");
5271 #ifdef SGEN_BINARY_PROTOCOL
5272                                 fprintf (stderr, "  binary-protocol=<filename>\n");
5273 #endif
5274                                 exit (1);
5275                         }
5276                 }
5277                 g_strfreev (opts);
5278         }
5279
5280         if (major_collector.is_parallel) {
5281                 if (heap_dump_file) {
5282                         fprintf (stderr, "Error: Cannot do heap dump with the parallel collector.\n");
5283                         exit (1);
5284                 }
5285                 if (do_pin_stats) {
5286                         fprintf (stderr, "Error: Cannot gather pinning statistics with the parallel collector.\n");
5287                         exit (1);
5288                 }
5289         }
5290
5291         if (major_collector.post_param_init)
5292                 major_collector.post_param_init (&major_collector);
5293
5294         sgen_memgov_init (max_heap, soft_limit, debug_print_allowance, allowance_ratio, save_target);
5295
5296         memset (&remset, 0, sizeof (remset));
5297
5298 #ifdef SGEN_HAVE_CARDTABLE
5299         if (use_cardtable)
5300                 sgen_card_table_init (&remset);
5301         else
5302 #endif
5303                 sgen_ssb_init (&remset);
5304
5305         if (remset.register_thread)
5306                 remset.register_thread (mono_thread_info_current ());
5307
5308         gc_initialized = 1;
5309 }
5310
5311 const char *
5312 mono_gc_get_gc_name (void)
5313 {
5314         return "sgen";
5315 }
5316
5317 static MonoMethod *write_barrier_method;
5318
5319 gboolean
5320 sgen_is_critical_method (MonoMethod *method)
5321 {
5322         return (method == write_barrier_method || sgen_is_managed_allocator (method));
5323 }
5324
5325 gboolean
5326 sgen_has_critical_method (void)
5327 {
5328         return write_barrier_method || sgen_has_managed_allocator ();
5329 }
5330
5331 static void
5332 emit_nursery_check (MonoMethodBuilder *mb, int *nursery_check_return_labels)
5333 {
5334         memset (nursery_check_return_labels, 0, sizeof (int) * 3);
5335 #ifdef SGEN_ALIGN_NURSERY
5336         // if (ptr_in_nursery (ptr)) return;
5337         /*
5338          * Masking out the bits might be faster, but we would have to use 64 bit
5339          * immediates, which might be slower.
5340          */
5341         mono_mb_emit_ldarg (mb, 0);
5342         mono_mb_emit_icon (mb, DEFAULT_NURSERY_BITS);
5343         mono_mb_emit_byte (mb, CEE_SHR_UN);
5344         mono_mb_emit_icon (mb, (mword)sgen_get_nursery_start () >> DEFAULT_NURSERY_BITS);
5345         nursery_check_return_labels [0] = mono_mb_emit_branch (mb, CEE_BEQ);
5346
5347         if (!major_collector.is_concurrent) {
5348                 // if (!ptr_in_nursery (*ptr)) return;
5349                 mono_mb_emit_ldarg (mb, 0);
5350                 mono_mb_emit_byte (mb, CEE_LDIND_I);
5351                 mono_mb_emit_icon (mb, DEFAULT_NURSERY_BITS);
5352                 mono_mb_emit_byte (mb, CEE_SHR_UN);
5353                 mono_mb_emit_icon (mb, (mword)sgen_get_nursery_start () >> DEFAULT_NURSERY_BITS);
5354                 nursery_check_return_labels [1] = mono_mb_emit_branch (mb, CEE_BNE_UN);
5355         }
5356 #else
5357         int label_continue1, label_continue2;
5358         int dereferenced_var;
5359
5360         // if (ptr < (sgen_get_nursery_start ())) goto continue;
5361         mono_mb_emit_ldarg (mb, 0);
5362         mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_start ());
5363         label_continue_1 = mono_mb_emit_branch (mb, CEE_BLT);
5364
5365         // if (ptr >= sgen_get_nursery_end ())) goto continue;
5366         mono_mb_emit_ldarg (mb, 0);
5367         mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_end ());
5368         label_continue_2 = mono_mb_emit_branch (mb, CEE_BGE);
5369
5370         // Otherwise return
5371         nursery_check_return_labels [0] = mono_mb_emit_branch (mb, CEE_BR);
5372
5373         // continue:
5374         mono_mb_patch_branch (mb, label_continue_1);
5375         mono_mb_patch_branch (mb, label_continue_2);
5376
5377         // Dereference and store in local var
5378         dereferenced_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5379         mono_mb_emit_ldarg (mb, 0);
5380         mono_mb_emit_byte (mb, CEE_LDIND_I);
5381         mono_mb_emit_stloc (mb, dereferenced_var);
5382
5383         if (!major_collector.is_concurrent) {
5384                 // if (*ptr < sgen_get_nursery_start ()) return;
5385                 mono_mb_emit_ldloc (mb, dereferenced_var);
5386                 mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_start ());
5387                 nursery_check_return_labels [1] = mono_mb_emit_branch (mb, CEE_BLT);
5388
5389                 // if (*ptr >= sgen_get_nursery_end ()) return;
5390                 mono_mb_emit_ldloc (mb, dereferenced_var);
5391                 mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_end ());
5392                 nursery_check_return_labels [2] = mono_mb_emit_branch (mb, CEE_BGE);
5393         }
5394 #endif  
5395 }
5396
5397 MonoMethod*
5398 mono_gc_get_write_barrier (void)
5399 {
5400         MonoMethod *res;
5401         MonoMethodBuilder *mb;
5402         MonoMethodSignature *sig;
5403 #ifdef MANAGED_WBARRIER
5404         int i, nursery_check_labels [3];
5405         int label_no_wb_3, label_no_wb_4, label_need_wb, label_slow_path;
5406         int buffer_var, buffer_index_var, dummy_var;
5407
5408 #ifdef HAVE_KW_THREAD
5409         int stack_end_offset = -1, store_remset_buffer_offset = -1;
5410         int store_remset_buffer_index_offset = -1, store_remset_buffer_index_addr_offset = -1;
5411
5412         MONO_THREAD_VAR_OFFSET (stack_end, stack_end_offset);
5413         g_assert (stack_end_offset != -1);
5414         MONO_THREAD_VAR_OFFSET (store_remset_buffer, store_remset_buffer_offset);
5415         g_assert (store_remset_buffer_offset != -1);
5416         MONO_THREAD_VAR_OFFSET (store_remset_buffer_index, store_remset_buffer_index_offset);
5417         g_assert (store_remset_buffer_index_offset != -1);
5418         MONO_THREAD_VAR_OFFSET (store_remset_buffer_index_addr, store_remset_buffer_index_addr_offset);
5419         g_assert (store_remset_buffer_index_addr_offset != -1);
5420 #endif
5421 #endif
5422
5423         // FIXME: Maybe create a separate version for ctors (the branch would be
5424         // correctly predicted more times)
5425         if (write_barrier_method)
5426                 return write_barrier_method;
5427
5428         /* Create the IL version of mono_gc_barrier_generic_store () */
5429         sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5430         sig->ret = &mono_defaults.void_class->byval_arg;
5431         sig->params [0] = &mono_defaults.int_class->byval_arg;
5432
5433         mb = mono_mb_new (mono_defaults.object_class, "wbarrier", MONO_WRAPPER_WRITE_BARRIER);
5434
5435 #ifdef MANAGED_WBARRIER
5436         if (use_cardtable) {
5437                 emit_nursery_check (mb, nursery_check_labels);
5438                 /*
5439                 addr = sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK)
5440                 *addr = 1;
5441
5442                 sgen_cardtable: 
5443                         LDC_PTR sgen_cardtable
5444
5445                 address >> CARD_BITS
5446                         LDARG_0
5447                         LDC_I4 CARD_BITS
5448                         SHR_UN
5449                 if (SGEN_HAVE_OVERLAPPING_CARDS) {
5450                         LDC_PTR card_table_mask
5451                         AND
5452                 }
5453                 AND
5454                 ldc_i4_1
5455                 stind_i1
5456                 */
5457                 mono_mb_emit_ptr (mb, sgen_cardtable);
5458                 mono_mb_emit_ldarg (mb, 0);
5459                 mono_mb_emit_icon (mb, CARD_BITS);
5460                 mono_mb_emit_byte (mb, CEE_SHR_UN);
5461 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
5462                 mono_mb_emit_ptr (mb, (gpointer)CARD_MASK);
5463                 mono_mb_emit_byte (mb, CEE_AND);
5464 #endif
5465                 mono_mb_emit_byte (mb, CEE_ADD);
5466                 mono_mb_emit_icon (mb, 1);
5467                 mono_mb_emit_byte (mb, CEE_STIND_I1);
5468
5469                 // return;
5470                 for (i = 0; i < 3; ++i) {
5471                         if (nursery_check_labels [i])
5472                                 mono_mb_patch_branch (mb, nursery_check_labels [i]);
5473                 }               
5474                 mono_mb_emit_byte (mb, CEE_RET);
5475         } else if (mono_runtime_has_tls_get ()) {
5476                 emit_nursery_check (mb, nursery_check_labels);
5477
5478                 // if (ptr >= stack_end) goto need_wb;
5479                 mono_mb_emit_ldarg (mb, 0);
5480                 EMIT_TLS_ACCESS (mb, stack_end, stack_end_offset);
5481                 label_need_wb = mono_mb_emit_branch (mb, CEE_BGE_UN);
5482
5483                 // if (ptr >= stack_start) return;
5484                 dummy_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5485                 mono_mb_emit_ldarg (mb, 0);
5486                 mono_mb_emit_ldloc_addr (mb, dummy_var);
5487                 label_no_wb_3 = mono_mb_emit_branch (mb, CEE_BGE_UN);
5488
5489                 // need_wb:
5490                 mono_mb_patch_branch (mb, label_need_wb);
5491
5492                 // buffer = STORE_REMSET_BUFFER;
5493                 buffer_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5494                 EMIT_TLS_ACCESS (mb, store_remset_buffer, store_remset_buffer_offset);
5495                 mono_mb_emit_stloc (mb, buffer_var);
5496
5497                 // buffer_index = STORE_REMSET_BUFFER_INDEX;
5498                 buffer_index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5499                 EMIT_TLS_ACCESS (mb, store_remset_buffer_index, store_remset_buffer_index_offset);
5500                 mono_mb_emit_stloc (mb, buffer_index_var);
5501
5502                 // if (buffer [buffer_index] == ptr) return;
5503                 mono_mb_emit_ldloc (mb, buffer_var);
5504                 mono_mb_emit_ldloc (mb, buffer_index_var);
5505                 g_assert (sizeof (gpointer) == 4 || sizeof (gpointer) == 8);
5506                 mono_mb_emit_icon (mb, sizeof (gpointer) == 4 ? 2 : 3);
5507                 mono_mb_emit_byte (mb, CEE_SHL);
5508                 mono_mb_emit_byte (mb, CEE_ADD);
5509                 mono_mb_emit_byte (mb, CEE_LDIND_I);
5510                 mono_mb_emit_ldarg (mb, 0);
5511                 label_no_wb_4 = mono_mb_emit_branch (mb, CEE_BEQ);
5512
5513                 // ++buffer_index;
5514                 mono_mb_emit_ldloc (mb, buffer_index_var);
5515                 mono_mb_emit_icon (mb, 1);
5516                 mono_mb_emit_byte (mb, CEE_ADD);
5517                 mono_mb_emit_stloc (mb, buffer_index_var);
5518
5519                 // if (buffer_index >= STORE_REMSET_BUFFER_SIZE) goto slow_path;
5520                 mono_mb_emit_ldloc (mb, buffer_index_var);
5521                 mono_mb_emit_icon (mb, STORE_REMSET_BUFFER_SIZE);
5522                 label_slow_path = mono_mb_emit_branch (mb, CEE_BGE);
5523
5524                 // buffer [buffer_index] = ptr;
5525                 mono_mb_emit_ldloc (mb, buffer_var);
5526                 mono_mb_emit_ldloc (mb, buffer_index_var);
5527                 g_assert (sizeof (gpointer) == 4 || sizeof (gpointer) == 8);
5528                 mono_mb_emit_icon (mb, sizeof (gpointer) == 4 ? 2 : 3);
5529                 mono_mb_emit_byte (mb, CEE_SHL);
5530                 mono_mb_emit_byte (mb, CEE_ADD);
5531                 mono_mb_emit_ldarg (mb, 0);
5532                 mono_mb_emit_byte (mb, CEE_STIND_I);
5533
5534                 // STORE_REMSET_BUFFER_INDEX = buffer_index;
5535                 EMIT_TLS_ACCESS (mb, store_remset_buffer_index_addr, store_remset_buffer_index_addr_offset);
5536                 mono_mb_emit_ldloc (mb, buffer_index_var);
5537                 mono_mb_emit_byte (mb, CEE_STIND_I);
5538
5539                 // return;
5540                 for (i = 0; i < 3; ++i) {
5541                         if (nursery_check_labels [i])
5542                                 mono_mb_patch_branch (mb, nursery_check_labels [i]);
5543                 }
5544                 mono_mb_patch_branch (mb, label_no_wb_3);
5545                 mono_mb_patch_branch (mb, label_no_wb_4);
5546                 mono_mb_emit_byte (mb, CEE_RET);
5547
5548                 // slow path
5549                 mono_mb_patch_branch (mb, label_slow_path);
5550
5551                 mono_mb_emit_ldarg (mb, 0);
5552                 mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_nostore);
5553                 mono_mb_emit_byte (mb, CEE_RET);
5554         } else
5555 #endif
5556         {
5557                 mono_mb_emit_ldarg (mb, 0);
5558                 mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_nostore);
5559                 mono_mb_emit_byte (mb, CEE_RET);
5560         }
5561
5562         res = mono_mb_create_method (mb, sig, 16);
5563         mono_mb_free (mb);
5564
5565         mono_loader_lock ();
5566         if (write_barrier_method) {
5567                 /* Already created */
5568                 mono_free_method (res);
5569         } else {
5570                 /* double-checked locking */
5571                 mono_memory_barrier ();
5572                 write_barrier_method = res;
5573         }
5574         mono_loader_unlock ();
5575
5576         return write_barrier_method;
5577 }
5578
5579 char*
5580 mono_gc_get_description (void)
5581 {
5582         return g_strdup ("sgen");
5583 }
5584
5585 void
5586 mono_gc_set_desktop_mode (void)
5587 {
5588 }
5589
5590 gboolean
5591 mono_gc_is_moving (void)
5592 {
5593         return TRUE;
5594 }
5595
5596 gboolean
5597 mono_gc_is_disabled (void)
5598 {
5599         return FALSE;
5600 }
5601
5602 #ifdef HOST_WIN32
5603 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved)
5604 {
5605         return TRUE;
5606 }
5607 #endif
5608
5609 NurseryClearPolicy
5610 sgen_get_nursery_clear_policy (void)
5611 {
5612         return nursery_clear_policy;
5613 }
5614
5615 MonoVTable*
5616 sgen_get_array_fill_vtable (void)
5617 {
5618         if (!array_fill_vtable) {
5619                 static MonoClass klass;
5620                 static MonoVTable vtable;
5621                 gsize bmap;
5622
5623                 MonoDomain *domain = mono_get_root_domain ();
5624                 g_assert (domain);
5625
5626                 klass.element_class = mono_defaults.byte_class;
5627                 klass.rank = 1;
5628                 klass.instance_size = sizeof (MonoArray);
5629                 klass.sizes.element_size = 1;
5630                 klass.name = "array_filler_type";
5631
5632                 vtable.klass = &klass;
5633                 bmap = 0;
5634                 vtable.gc_descr = mono_gc_make_descr_for_array (TRUE, &bmap, 0, 1);
5635                 vtable.rank = 1;
5636
5637                 array_fill_vtable = &vtable;
5638         }
5639         return array_fill_vtable;
5640 }
5641
5642 void
5643 sgen_gc_lock (void)
5644 {
5645         LOCK_GC;
5646 }
5647
5648 void
5649 sgen_gc_unlock (void)
5650 {
5651         UNLOCK_GC;
5652 }
5653
5654 void
5655 sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
5656 {
5657         major_collector.iterate_live_block_ranges (callback);
5658 }
5659
5660 void
5661 sgen_major_collector_scan_card_table (SgenGrayQueue *queue)
5662 {
5663         major_collector.scan_card_table (FALSE, queue);
5664 }
5665
5666 SgenMajorCollector*
5667 sgen_get_major_collector (void)
5668 {
5669         return &major_collector;
5670 }
5671
5672 void mono_gc_set_skip_thread (gboolean skip)
5673 {
5674         SgenThreadInfo *info = mono_thread_info_current ();
5675
5676         LOCK_GC;
5677         info->gc_disabled = skip;
5678         UNLOCK_GC;
5679 }
5680
5681 SgenRemeberedSet*
5682 sgen_get_remset (void)
5683 {
5684         return &remset;
5685 }
5686
5687 guint
5688 mono_gc_get_vtable_bits (MonoClass *class)
5689 {
5690         if (sgen_need_bridge_processing () && sgen_is_bridge_class (class))
5691                 return SGEN_GC_BIT_BRIDGE_OBJECT;
5692         return 0;
5693 }
5694
5695 void
5696 mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size)
5697 {
5698         // FIXME:
5699 }
5700
5701
5702 void
5703 sgen_check_whole_heap_stw (void)
5704 {
5705         sgen_stop_world (0);
5706         sgen_clear_nursery_fragments ();
5707         sgen_check_whole_heap ();
5708         sgen_restart_world (0, NULL);
5709 }
5710
5711 void
5712 sgen_gc_event_moves (void)
5713 {
5714         if (moved_objects_idx) {
5715                 mono_profiler_gc_moves (moved_objects, moved_objects_idx);
5716                 moved_objects_idx = 0;
5717         }
5718 }
5719
5720 #endif /* HAVE_SGEN_GC */