Changed TryPeek to handle race condition with Dequeue.
[mono.git] / mono / metadata / sgen-marksweep.c
1 /*
2  * sgen-marksweep.c: The Mark & Sweep major collector.
3  *
4  * Author:
5  *      Mark Probst <mark.probst@gmail.com>
6  *
7  * Copyright 2009-2010 Novell, Inc.
8  * Copyright (C) 2012 Xamarin Inc
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License 2.0 as published by the Free Software Foundation;
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License 2.0 along with this library; if not, write to the Free
21  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_SGEN_GC
27
28 #include <math.h>
29 #include <errno.h>
30
31 #include "utils/mono-counters.h"
32 #include "utils/mono-semaphore.h"
33 #include "utils/mono-time.h"
34 #include "metadata/object-internals.h"
35 #include "metadata/profiler-private.h"
36
37 #include "metadata/sgen-gc.h"
38 #include "metadata/sgen-protocol.h"
39 #include "metadata/sgen-cardtable.h"
40 #include "metadata/sgen-memory-governor.h"
41 #include "metadata/sgen-layout-stats.h"
42 #include "metadata/gc-internal.h"
43
44 #if !defined(SGEN_PARALLEL_MARK) && !defined(FIXED_HEAP)
45 #define SGEN_HAVE_CONCURRENT_MARK
46 #endif
47
48 #define MS_BLOCK_SIZE   (16*1024)
49 #define MS_BLOCK_SIZE_SHIFT     14
50 #define MAJOR_SECTION_SIZE      MS_BLOCK_SIZE
51 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
52
53 #ifdef FIXED_HEAP
54 #define MS_DEFAULT_HEAP_NUM_BLOCKS      (32 * 1024) /* 512 MB */
55 #endif
56
57 /*
58  * Don't allocate single blocks, but alloc a contingent of this many
59  * blocks in one swoop.  This must be a power of two.
60  */
61 #define MS_BLOCK_ALLOC_NUM      32
62
63 /*
64  * Number of bytes before the first object in a block.  At the start
65  * of a block is the MSBlockHeader, then opional padding, then come
66  * the objects, so this must be >= sizeof (MSBlockHeader).
67  */
68 #ifdef FIXED_HEAP
69 #define MS_BLOCK_SKIP   0
70 #else
71 #define MS_BLOCK_SKIP   16
72 #endif
73
74 #define MS_BLOCK_FREE   (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
75
76 #define MS_NUM_MARK_WORDS       ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
77
78 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
79 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
80 #endif
81
82 typedef struct _MSBlockInfo MSBlockInfo;
83 struct _MSBlockInfo {
84         int obj_size;
85         int obj_size_index;
86         int pin_queue_num_entries;
87         unsigned int pinned : 1;
88         unsigned int has_references : 1;
89         unsigned int has_pinned : 1;    /* means cannot evacuate */
90         unsigned int is_to_space : 1;
91         unsigned int swept : 1;
92 #ifdef FIXED_HEAP
93         unsigned int used : 1;
94         unsigned int zeroed : 1;
95 #endif
96         MSBlockInfo *next;
97         char *block;
98         void **free_list;
99         MSBlockInfo *next_free;
100         void **pin_queue_start;
101 #ifdef SGEN_HAVE_CONCURRENT_MARK
102         guint8 *cardtable_mod_union;
103 #endif
104         mword mark_words [MS_NUM_MARK_WORDS];
105 };
106
107 #ifdef FIXED_HEAP
108 static mword ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
109
110 static char *ms_heap_start;
111 static char *ms_heap_end;
112
113 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p)   ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
114
115 /* array of all all block infos in the system */
116 static MSBlockInfo *block_infos;
117 #endif
118
119 #define MS_BLOCK_OBJ(b,i)               ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
120 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size)             ((b)->block + MS_BLOCK_SKIP + (obj_size) * (i))
121 #define MS_BLOCK_DATA_FOR_OBJ(o)        ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
122
123 #ifdef FIXED_HEAP
124 #define MS_BLOCK_FOR_OBJ(o)             (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
125 #else
126 typedef struct {
127         MSBlockInfo *info;
128 } MSBlockHeader;
129
130 #define MS_BLOCK_FOR_OBJ(o)             (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
131 #endif
132
133 #define MS_BLOCK_OBJ_INDEX(o,b) (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
134
135 #define MS_CALC_MARK_BIT(w,b,o)         do {                            \
136                 int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
137                 if (sizeof (mword) == 4) {                              \
138                         (w) = i >> 5;                                   \
139                         (b) = i & 31;                                   \
140                 } else {                                                \
141                         (w) = i >> 6;                                   \
142                         (b) = i & 63;                                   \
143                 }                                                       \
144         } while (0)
145
146 #define MS_MARK_BIT(bl,w,b)     ((bl)->mark_words [(w)] & (1L << (b)))
147 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (1L << (b)))
148 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b)  do {                    \
149                 mword __old = (bl)->mark_words [(w)];                   \
150                 mword __bitmask = 1L << (b);                            \
151                 if (__old & __bitmask) {                                \
152                         was_marked = TRUE;                              \
153                         break;                                          \
154                 }                                                       \
155                 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)],   \
156                                                 (gpointer)(__old | __bitmask), \
157                                                 (gpointer)__old) ==     \
158                                 (gpointer)__old) {                      \
159                         was_marked = FALSE;                             \
160                         break;                                          \
161                 }                                                       \
162         } while (1)
163
164 #define MS_OBJ_ALLOCED(o,b)     (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
165
166 #define MS_BLOCK_OBJ_SIZE_FACTOR        (sqrt (2.0))
167
168 /*
169  * This way we can lookup block object size indexes for sizes up to
170  * 256 bytes with a single load.
171  */
172 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES      32
173
174 static int *block_obj_sizes;
175 static int num_block_obj_sizes;
176 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
177
178 #define MS_BLOCK_FLAG_PINNED    1
179 #define MS_BLOCK_FLAG_REFS      2
180
181 #define MS_BLOCK_TYPE_MAX       4
182
183 #ifdef SGEN_PARALLEL_MARK
184 static LOCK_DECLARE (ms_block_list_mutex);
185 #define LOCK_MS_BLOCK_LIST mono_mutex_lock (&ms_block_list_mutex)
186 #define UNLOCK_MS_BLOCK_LIST mono_mutex_unlock (&ms_block_list_mutex)
187 #endif
188
189 static gboolean *evacuate_block_obj_sizes;
190 static float evacuation_threshold = 0.666;
191 #ifdef SGEN_HAVE_CONCURRENT_MARK
192 static float concurrent_evacuation_threshold = 0.666;
193 static gboolean want_evacuation = FALSE;
194 #endif
195
196 static gboolean lazy_sweep = TRUE;
197 static gboolean have_swept;
198
199 #ifdef SGEN_HAVE_CONCURRENT_MARK
200 static gboolean concurrent_mark;
201 #endif
202
203 /* all allocated blocks in the system */
204 static MSBlockInfo *all_blocks;
205
206 #ifdef FIXED_HEAP
207 /* non-allocated block free-list */
208 static MSBlockInfo *empty_blocks = NULL;
209 #else
210 /* non-allocated block free-list */
211 static void *empty_blocks = NULL;
212 static int num_empty_blocks = 0;
213 #endif
214
215 #define FOREACH_BLOCK(bl)       for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
216 #define END_FOREACH_BLOCK       }
217
218 static int num_major_sections = 0;
219 /* one free block list for each block object size */
220 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
221
222 #ifdef SGEN_PARALLEL_MARK
223 #ifdef HAVE_KW_THREAD
224 static __thread MSBlockInfo ***workers_free_block_lists;
225 #else
226 static MonoNativeTlsKey workers_free_block_lists_key;
227 #endif
228 #endif
229
230 static long long stat_major_blocks_alloced = 0;
231 static long long stat_major_blocks_freed = 0;
232 static long long stat_major_blocks_lazy_swept = 0;
233 static long long stat_major_objects_evacuated = 0;
234
235 #if SIZEOF_VOID_P != 8
236 static long long stat_major_blocks_freed_ideal = 0;
237 static long long stat_major_blocks_freed_less_ideal = 0;
238 static long long stat_major_blocks_freed_individual = 0;
239 static long long stat_major_blocks_alloced_less_ideal = 0;
240 #endif
241
242 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
243 static long long num_major_objects_marked = 0;
244 #define INC_NUM_MAJOR_OBJECTS_MARKED()  (++num_major_objects_marked)
245 #else
246 #define INC_NUM_MAJOR_OBJECTS_MARKED()
247 #endif
248
249 static void
250 sweep_block (MSBlockInfo *block, gboolean during_major_collection);
251
252 static int
253 ms_find_block_obj_size_index (int size)
254 {
255         int i;
256         SGEN_ASSERT (9, size <= SGEN_MAX_SMALL_OBJ_SIZE, "size %d is bigger than max small object size %d", size, SGEN_MAX_SMALL_OBJ_SIZE);
257         for (i = 0; i < num_block_obj_sizes; ++i)
258                 if (block_obj_sizes [i] >= size)
259                         return i;
260         g_error ("no object of size %d\n", size);
261 }
262
263 #define FREE_BLOCKS_FROM(lists,p,r)     (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
264 #define FREE_BLOCKS(p,r)                (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
265 #ifdef SGEN_PARALLEL_MARK
266 #ifdef HAVE_KW_THREAD
267 #define FREE_BLOCKS_LOCAL(p,r)          (FREE_BLOCKS_FROM (workers_free_block_lists, (p), (r)))
268 #else
269 #define FREE_BLOCKS_LOCAL(p,r)          (FREE_BLOCKS_FROM (((MSBlockInfo***)(mono_native_tls_get_value (workers_free_block_lists_key))), (p), (r)))
270 #endif
271 #else
272 //#define FREE_BLOCKS_LOCAL(p,r)                (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
273 #endif
274
275 #define MS_BLOCK_OBJ_SIZE_INDEX(s)                              \
276         (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ?      \
277          fast_block_obj_size_indexes [((s)+7)>>3] :             \
278          ms_find_block_obj_size_index ((s)))
279
280 #ifdef FIXED_HEAP
281 static void*
282 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
283 {
284         char *nursery_start;
285         mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
286         mword alloc_size = nursery_size + major_heap_size;
287         mword i;
288
289         g_assert (ms_heap_num_blocks > 0);
290         g_assert (nursery_size % MS_BLOCK_SIZE == 0);
291         if (nursery_align)
292                 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
293
294         nursery_start = sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "heap");
295         ms_heap_start = nursery_start + nursery_size;
296         ms_heap_end = ms_heap_start + major_heap_size;
297
298         block_infos = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO, TRUE);
299
300         for (i = 0; i < ms_heap_num_blocks; ++i) {
301                 block_infos [i].block = ms_heap_start + i * MS_BLOCK_SIZE;
302                 if (i < ms_heap_num_blocks - 1)
303                         block_infos [i].next_free = &block_infos [i + 1];
304                 else
305                         block_infos [i].next_free = NULL;
306                 block_infos [i].zeroed = TRUE;
307         }
308
309         empty_blocks = &block_infos [0];
310
311         return nursery_start;
312 }
313 #else
314 static void*
315 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
316 {
317         char *start;
318         if (nursery_align)
319                 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
320         else
321                 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
322
323         return start;
324 }
325 #endif
326
327 static void
328 update_heap_boundaries_for_block (MSBlockInfo *block)
329 {
330         sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
331 }
332
333 #ifdef FIXED_HEAP
334 static MSBlockInfo*
335 ms_get_empty_block (void)
336 {
337         MSBlockInfo *block;
338
339         g_assert (empty_blocks);
340
341         do {
342                 block = empty_blocks;
343         } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block->next_free, block) != block);
344
345         block->used = TRUE;
346
347         if (!block->zeroed)
348                 memset (block->block, 0, MS_BLOCK_SIZE);
349
350         return block;
351 }
352
353 static void
354 ms_free_block (MSBlockInfo *block)
355 {
356         block->next_free = empty_blocks;
357         empty_blocks = block;
358         block->used = FALSE;
359         block->zeroed = FALSE;
360         sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
361 }
362 #else
363 static void*
364 ms_get_empty_block (void)
365 {
366         char *p;
367         int i;
368         void *block, *empty, *next;
369
370  retry:
371         if (!empty_blocks) {
372                 /*
373                  * We try allocating MS_BLOCK_ALLOC_NUM blocks first.  If that's
374                  * unsuccessful, we halve the number of blocks and try again, until we're at
375                  * 1.  If that doesn't work, either, we assert.
376                  */
377                 int alloc_num = MS_BLOCK_ALLOC_NUM;
378                 for (;;) {
379                         p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
380                                         alloc_num == 1 ? "major heap section" : NULL);
381                         if (p)
382                                 break;
383                         alloc_num >>= 1;
384                 }
385
386                 for (i = 0; i < alloc_num; ++i) {
387                         block = p;
388                         /*
389                          * We do the free list update one after the
390                          * other so that other threads can use the new
391                          * blocks as quickly as possible.
392                          */
393                         do {
394                                 empty = empty_blocks;
395                                 *(void**)block = empty;
396                         } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
397                         p += MS_BLOCK_SIZE;
398                 }
399
400                 SGEN_ATOMIC_ADD (num_empty_blocks, alloc_num);
401
402                 stat_major_blocks_alloced += alloc_num;
403 #if SIZEOF_VOID_P != 8
404                 if (alloc_num != MS_BLOCK_ALLOC_NUM)
405                         stat_major_blocks_alloced_less_ideal += alloc_num;
406 #endif
407         }
408
409         do {
410                 empty = empty_blocks;
411                 if (!empty)
412                         goto retry;
413                 block = empty;
414                 next = *(void**)block;
415         } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
416
417         SGEN_ATOMIC_ADD (num_empty_blocks, -1);
418
419         *(void**)block = NULL;
420
421         g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
422
423         return block;
424 }
425
426 static void
427 ms_free_block (void *block)
428 {
429         void *empty;
430
431         sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
432         memset (block, 0, MS_BLOCK_SIZE);
433
434         do {
435                 empty = empty_blocks;
436                 *(void**)block = empty;
437         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
438
439         SGEN_ATOMIC_ADD (num_empty_blocks, 1);
440 }
441 #endif
442
443 //#define MARKSWEEP_CONSISTENCY_CHECK
444
445 #ifdef MARKSWEEP_CONSISTENCY_CHECK
446 static void
447 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
448 {
449         MSBlockInfo *b;
450
451         for (; block; block = block->next_free) {
452                 g_assert (block->obj_size == size);
453                 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
454
455                 /* blocks in the free lists must have at least
456                    one free slot */
457                 if (block->swept)
458                         g_assert (block->free_list);
459
460 #ifdef FIXED_HEAP
461                 /* the block must not be in the empty_blocks list */
462                 for (b = empty_blocks; b; b = b->next_free)
463                         g_assert (b != block);
464 #endif
465                 /* the block must be in the all_blocks list */
466                 for (b = all_blocks; b; b = b->next) {
467                         if (b == block)
468                                 break;
469                 }
470                 g_assert (b == block);
471         }
472 }
473
474 static void
475 check_empty_blocks (void)
476 {
477 #ifndef FIXED_HEAP
478         void *p;
479         int i = 0;
480         for (p = empty_blocks; p; p = *(void**)p)
481                 ++i;
482         g_assert (i == num_empty_blocks);
483 #endif
484 }
485
486 static void
487 consistency_check (void)
488 {
489         MSBlockInfo *block;
490         int i;
491
492         /* check all blocks */
493         FOREACH_BLOCK (block) {
494                 int count = MS_BLOCK_FREE / block->obj_size;
495                 int num_free = 0;
496                 void **free;
497
498 #ifndef FIXED_HEAP
499                 /* check block header */
500                 g_assert (((MSBlockHeader*)block->block)->info == block);
501 #endif
502
503                 /* count number of free slots */
504                 for (i = 0; i < count; ++i) {
505                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
506                         if (!MS_OBJ_ALLOCED (obj, block))
507                                 ++num_free;
508                 }
509
510                 /* check free list */
511                 for (free = block->free_list; free; free = (void**)*free) {
512                         g_assert (MS_BLOCK_FOR_OBJ (free) == block);
513                         --num_free;
514                 }
515                 g_assert (num_free == 0);
516
517                 /* check all mark words are zero */
518                 if (block->swept) {
519                         for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
520                                 g_assert (block->mark_words [i] == 0);
521                 }
522         } END_FOREACH_BLOCK;
523
524         /* check free blocks */
525         for (i = 0; i < num_block_obj_sizes; ++i) {
526                 int j;
527                 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
528                         check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
529         }
530
531         check_empty_blocks ();
532 }
533 #endif
534
535 static gboolean
536 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
537 {
538         int size = block_obj_sizes [size_index];
539         int count = MS_BLOCK_FREE / size;
540         MSBlockInfo *info;
541 #ifdef SGEN_PARALLEL_MARK
542         MSBlockInfo *next;
543 #endif
544 #ifndef FIXED_HEAP
545         MSBlockHeader *header;
546 #endif
547         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
548         char *obj_start;
549         int i;
550
551         if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
552                 return FALSE;
553
554 #ifdef FIXED_HEAP
555         info = ms_get_empty_block ();
556 #else
557         info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
558 #endif
559
560         SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
561
562         info->obj_size = size;
563         info->obj_size_index = size_index;
564         info->pinned = pinned;
565         info->has_references = has_references;
566         info->has_pinned = pinned;
567         /*
568          * Blocks that are to-space are not evacuated from.  During an major collection
569          * blocks are allocated for two reasons: evacuating objects from the nursery and
570          * evacuating them from major blocks marked for evacuation.  In both cases we don't
571          * want further evacuation.
572          */
573         info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
574         info->swept = 1;
575 #ifndef FIXED_HEAP
576         info->block = ms_get_empty_block ();
577
578         header = (MSBlockHeader*) info->block;
579         header->info = info;
580 #endif
581 #ifdef SGEN_HAVE_CONCURRENT_MARK
582         info->cardtable_mod_union = NULL;
583 #endif
584
585         update_heap_boundaries_for_block (info);
586
587         /* build free list */
588         obj_start = info->block + MS_BLOCK_SKIP;
589         info->free_list = (void**)obj_start;
590         /* we're skipping the last one - it must be nulled */
591         for (i = 0; i < count - 1; ++i) {
592                 char *next_obj_start = obj_start + size;
593                 *(void**)obj_start = next_obj_start;
594                 obj_start = next_obj_start;
595         }
596         /* the last one */
597         *(void**)obj_start = NULL;
598
599 #ifdef SGEN_PARALLEL_MARK
600         do {
601                 next = info->next_free = free_blocks [size_index];
602         } while (SGEN_CAS_PTR ((void**)&free_blocks [size_index], info, next) != next);
603
604         do {
605                 next = info->next = all_blocks;
606         } while (SGEN_CAS_PTR ((void**)&all_blocks, info, next) != next);
607 #else
608         info->next_free = free_blocks [size_index];
609         free_blocks [size_index] = info;
610
611         info->next = all_blocks;
612         all_blocks = info;
613 #endif
614
615         ++num_major_sections;
616         return TRUE;
617 }
618
619 static gboolean
620 obj_is_from_pinned_alloc (char *ptr)
621 {
622         MSBlockInfo *block;
623
624         FOREACH_BLOCK (block) {
625                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
626                         return block->pinned;
627         } END_FOREACH_BLOCK;
628         return FALSE;
629 }
630
631 static void*
632 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
633 {
634         MSBlockInfo *block;
635         void *obj;
636
637         block = free_blocks [size_index];
638         SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
639
640         if (G_UNLIKELY (!block->swept)) {
641                 stat_major_blocks_lazy_swept ++;
642                 sweep_block (block, FALSE);
643         }
644
645         obj = block->free_list;
646         SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
647
648         block->free_list = *(void**)obj;
649         if (!block->free_list) {
650                 free_blocks [size_index] = block->next_free;
651                 block->next_free = NULL;
652         }
653
654         return obj;
655 }
656
657 #ifdef SGEN_PARALLEL_MARK
658 static gboolean
659 try_remove_block_from_free_list (MSBlockInfo *block, MSBlockInfo **free_blocks, int size_index)
660 {
661         /*
662          * No more free slots in the block, so try to free the block.
663          * Don't try again if we don't succeed - another thread will
664          * already have done it.
665          */
666         MSBlockInfo *next_block = block->next_free;
667         if (SGEN_CAS_PTR ((void**)&free_blocks [size_index], next_block, block) == block) {
668                 /*
669                 void *old = SGEN_CAS_PTR ((void**)&block->next_free, NULL, next_block);
670                 g_assert (old == next_block);
671                 */
672                 block->next_free = NULL;
673                 return TRUE;
674         }
675         return FALSE;
676 }
677
678 static void*
679 alloc_obj_par (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
680 {
681         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
682         MSBlockInfo **free_blocks_local = FREE_BLOCKS_LOCAL (pinned, has_references);
683         MSBlockInfo *block;
684         void *obj;
685
686 #ifdef SGEN_HAVE_CONCURRENT_MARK
687         if (concurrent_mark)
688                 g_assert_not_reached ();
689 #endif
690
691         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
692
693         if (free_blocks_local [size_index]) {
694         get_slot:
695                 obj = unlink_slot_from_free_list_uncontested (free_blocks_local, size_index);
696         } else {
697                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
698
699         get_block:
700                 block = free_blocks [size_index];
701                 if (block) {
702                         if (!try_remove_block_from_free_list (block, free_blocks, size_index))
703                                 goto get_block;
704
705                         g_assert (block->next_free == NULL);
706                         g_assert (block->free_list);
707                         block->next_free = free_blocks_local [size_index];
708                         free_blocks_local [size_index] = block;
709
710                         goto get_slot;
711                 } else {
712                         gboolean success;
713
714                         LOCK_MS_BLOCK_LIST;
715                         success = ms_alloc_block (size_index, pinned, has_references);
716                         UNLOCK_MS_BLOCK_LIST;
717
718                         if (G_UNLIKELY (!success))
719                                 return NULL;
720
721                         goto get_block;
722                 }
723         }
724
725         *(MonoVTable**)obj = vtable;
726
727         return obj;
728 }
729
730 static void*
731 major_par_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
732 {
733         return alloc_obj_par (vtable, size, FALSE, has_references);
734 }
735 #endif
736
737 static void*
738 alloc_obj (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
739 {
740         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
741         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
742         void *obj;
743
744 #ifdef SGEN_PARALLEL_MARK
745         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
746
747 #endif
748
749         if (!free_blocks [size_index]) {
750                 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
751                         return NULL;
752         }
753
754         obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
755
756         *(MonoVTable**)obj = vtable;
757
758         return obj;
759 }
760
761 static void*
762 major_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
763 {
764         return alloc_obj (vtable, size, FALSE, has_references);
765 }
766
767 /*
768  * We're not freeing the block if it's empty.  We leave that work for
769  * the next major collection.
770  *
771  * This is just called from the domain clearing code, which runs in a
772  * single thread and has the GC lock, so we don't need an extra lock.
773  */
774 static void
775 free_object (char *obj, size_t size, gboolean pinned)
776 {
777         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
778         int word, bit;
779
780         if (!block->swept)
781                 sweep_block (block, FALSE);
782         SGEN_ASSERT (9, (pinned && block->pinned) || (!pinned && !block->pinned), "free-object pinning mixup object %p pinned %d block %p pinned %d", obj, pinned, block, block->pinned);
783         SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
784         MS_CALC_MARK_BIT (word, bit, obj);
785         SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
786         if (!block->free_list) {
787                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
788                 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
789                 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
790                 block->next_free = free_blocks [size_index];
791                 free_blocks [size_index] = block;
792         }
793         memset (obj, 0, size);
794         *(void**)obj = block->free_list;
795         block->free_list = (void**)obj;
796 }
797
798 static void
799 major_free_non_pinned_object (char *obj, size_t size)
800 {
801         free_object (obj, size, FALSE);
802 }
803
804 /* size is a multiple of SGEN_ALLOC_ALIGN */
805 static void*
806 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
807 {
808         void *res;
809
810         res = alloc_obj (vtable, size, TRUE, has_references);
811          /*If we failed to alloc memory, we better try releasing memory
812           *as pinned alloc is requested by the runtime.
813           */
814          if (!res) {
815                 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
816                 res = alloc_obj (vtable, size, TRUE, has_references);
817          }
818          return res;
819 }
820
821 static void
822 free_pinned_object (char *obj, size_t size)
823 {
824         free_object (obj, size, TRUE);
825 }
826
827 /*
828  * size is already rounded up and we hold the GC lock.
829  */
830 static void*
831 major_alloc_degraded (MonoVTable *vtable, size_t size)
832 {
833         void *obj;
834         int old_num_sections;
835
836         old_num_sections = num_major_sections;
837
838         obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
839         if (G_LIKELY (obj)) {
840                 HEAVY_STAT (++stat_objects_alloced_degraded);
841                 HEAVY_STAT (stat_bytes_alloced_degraded += size);
842                 g_assert (num_major_sections >= old_num_sections);
843                 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
844         }
845         return obj;
846 }
847
848 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj)   FALSE
849
850 /*
851  * obj is some object.  If it's not in the major heap (i.e. if it's in
852  * the nursery or LOS), return FALSE.  Otherwise return whether it's
853  * been marked or copied.
854  */
855 static gboolean
856 major_is_object_live (char *obj)
857 {
858         MSBlockInfo *block;
859         int word, bit;
860 #ifndef FIXED_HEAP
861         mword objsize;
862 #endif
863
864         if (sgen_ptr_in_nursery (obj))
865                 return FALSE;
866
867 #ifdef FIXED_HEAP
868         /* LOS */
869         if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
870                 return FALSE;
871 #else
872         objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
873
874         /* LOS */
875         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
876                 return FALSE;
877 #endif
878
879         /* now we know it's in a major block */
880         block = MS_BLOCK_FOR_OBJ (obj);
881         SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
882         MS_CALC_MARK_BIT (word, bit, obj);
883         return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
884 }
885
886 static gboolean
887 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
888 {
889         MSBlockInfo *block;
890
891         FOREACH_BLOCK (block) {
892                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE) {
893                         int count = MS_BLOCK_FREE / block->obj_size;
894                         int i;
895
896                         *start = NULL;
897                         for (i = 0; i <= count; ++i) {
898                                 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
899                                         *start = MS_BLOCK_OBJ (block, i);
900                                         break;
901                                 }
902                         }
903                         return !block->pinned;
904                 }
905         } END_FOREACH_BLOCK;
906         return FALSE;
907 }
908
909 static void
910 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
911 {
912         gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
913         gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
914         gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
915         MSBlockInfo *block;
916
917         FOREACH_BLOCK (block) {
918                 int count = MS_BLOCK_FREE / block->obj_size;
919                 int i;
920
921                 if (block->pinned && !pinned)
922                         continue;
923                 if (!block->pinned && !non_pinned)
924                         continue;
925                 if (sweep && lazy_sweep) {
926                         sweep_block (block, FALSE);
927                         SGEN_ASSERT (0, block->swept, "Block must be swept after sweeping");
928                 }
929
930                 for (i = 0; i < count; ++i) {
931                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
932                         if (!block->swept) {
933                                 int word, bit;
934                                 MS_CALC_MARK_BIT (word, bit, obj);
935                                 if (!MS_MARK_BIT (block, word, bit))
936                                         continue;
937                         }
938                         if (MS_OBJ_ALLOCED (obj, block))
939                                 callback ((char*)obj, block->obj_size, data);
940                 }
941         } END_FOREACH_BLOCK;
942 }
943
944 static gboolean
945 major_is_valid_object (char *object)
946 {
947         MSBlockInfo *block;
948
949         FOREACH_BLOCK (block) {
950                 int idx;
951                 char *obj;
952
953                 if ((block->block > object) || ((block->block + MS_BLOCK_SIZE) <= object))
954                         continue;
955
956                 idx = MS_BLOCK_OBJ_INDEX (object, block);
957                 obj = (char*)MS_BLOCK_OBJ (block, idx);
958                 if (obj != object)
959                         return FALSE;
960                 return MS_OBJ_ALLOCED (obj, block);
961         } END_FOREACH_BLOCK;
962
963         return FALSE;
964 }
965
966
967 static MonoVTable*
968 major_describe_pointer (char *ptr)
969 {
970         MSBlockInfo *block;
971
972         FOREACH_BLOCK (block) {
973                 int idx;
974                 char *obj;
975                 gboolean live;
976                 MonoVTable *vtable;
977                 int w, b;
978                 gboolean marked;
979
980                 if ((block->block > ptr) || ((block->block + MS_BLOCK_SIZE) <= ptr))
981                         continue;
982
983                 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
984                         block->block, block->obj_size, block->pinned, block->has_references);
985
986                 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
987                 obj = (char*)MS_BLOCK_OBJ (block, idx);
988                 live = MS_OBJ_ALLOCED (obj, block);
989                 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
990
991                 MS_CALC_MARK_BIT (w, b, obj);
992                 marked = MS_MARK_BIT (block, w, b);
993
994                 if (obj == ptr) {
995                         SGEN_LOG (0, "\t(");
996                         if (live)
997                                 SGEN_LOG (0, "object");
998                         else
999                                 SGEN_LOG (0, "dead-object");
1000                 } else {
1001                         if (live)
1002                                 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
1003                         else
1004                                 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
1005                 }
1006
1007                 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
1008
1009                 return vtable;
1010         } END_FOREACH_BLOCK;
1011
1012         return NULL;
1013 }
1014
1015 static void
1016 major_check_scan_starts (void)
1017 {
1018 }
1019
1020 static void
1021 major_dump_heap (FILE *heap_dump_file)
1022 {
1023         MSBlockInfo *block;
1024         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1025         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1026         int i;
1027
1028         for (i = 0; i < num_block_obj_sizes; ++i)
1029                 slots_available [i] = slots_used [i] = 0;
1030
1031         FOREACH_BLOCK (block) {
1032                 int index = ms_find_block_obj_size_index (block->obj_size);
1033                 int count = MS_BLOCK_FREE / block->obj_size;
1034
1035                 slots_available [index] += count;
1036                 for (i = 0; i < count; ++i) {
1037                         if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
1038                                 ++slots_used [index];
1039                 }
1040         } END_FOREACH_BLOCK;
1041
1042         fprintf (heap_dump_file, "<occupancies>\n");
1043         for (i = 0; i < num_block_obj_sizes; ++i) {
1044                 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
1045                                 block_obj_sizes [i], slots_available [i], slots_used [i]);
1046         }
1047         fprintf (heap_dump_file, "</occupancies>\n");
1048
1049         FOREACH_BLOCK (block) {
1050                 int count = MS_BLOCK_FREE / block->obj_size;
1051                 int i;
1052                 int start = -1;
1053
1054                 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
1055
1056                 for (i = 0; i <= count; ++i) {
1057                         if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
1058                                 if (start < 0)
1059                                         start = i;
1060                         } else {
1061                                 if (start >= 0) {
1062                                         sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
1063                                         start = -1;
1064                                 }
1065                         }
1066                 }
1067
1068                 fprintf (heap_dump_file, "</section>\n");
1069         } END_FOREACH_BLOCK;
1070 }
1071
1072 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
1073
1074 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {        \
1075                 int __word, __bit;                                      \
1076                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1077                 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
1078                         MS_SET_MARK_BIT ((block), __word, __bit);       \
1079                         if ((block)->has_references)                    \
1080                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1081                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1082                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1083                 }                                                       \
1084         } while (0)
1085 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {                \
1086                 int __word, __bit;                                      \
1087                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1088                 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj);       \
1089                 if (!MS_MARK_BIT ((block), __word, __bit)) {            \
1090                         MS_SET_MARK_BIT ((block), __word, __bit);       \
1091                         if ((block)->has_references)                    \
1092                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1093                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1094                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1095                 }                                                       \
1096         } while (0)
1097 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {            \
1098                 int __word, __bit;                                      \
1099                 gboolean __was_marked;                                  \
1100                 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj);       \
1101                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1102                 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
1103                 if (!__was_marked) {                                    \
1104                         if ((block)->has_references)                    \
1105                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1106                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1107                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1108                 }                                                       \
1109         } while (0)
1110
1111 static void
1112 pin_major_object (char *obj, SgenGrayQueue *queue)
1113 {
1114         MSBlockInfo *block;
1115
1116 #ifdef SGEN_HAVE_CONCURRENT_MARK
1117         if (concurrent_mark)
1118                 g_assert_not_reached ();
1119 #endif
1120
1121         block = MS_BLOCK_FOR_OBJ (obj);
1122         block->has_pinned = TRUE;
1123         MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1124 }
1125
1126 #include "sgen-major-copy-object.h"
1127
1128 #ifdef SGEN_PARALLEL_MARK
1129 static void
1130 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
1131 {
1132         mword objsize;
1133         MSBlockInfo *block;
1134         MonoVTable *vt;
1135
1136         HEAVY_STAT (++stat_copy_object_called_major);
1137
1138         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
1139         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
1140
1141         if (sgen_ptr_in_nursery (obj)) {
1142                 int word, bit;
1143                 gboolean has_references;
1144                 void *destination;
1145                 mword vtable_word = *(mword*)obj;
1146                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1147
1148                 if (vtable_word & SGEN_FORWARDED_BIT) {
1149                         *ptr = (void*)vt;
1150                         return;
1151                 }
1152
1153                 if (vtable_word & SGEN_PINNED_BIT)
1154                         return;
1155
1156                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1157                 if (sgen_nursery_is_to_space (obj))
1158                         return;
1159
1160                 HEAVY_STAT (++stat_objects_copied_major);
1161
1162         do_copy_object:
1163                 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1164                 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
1165
1166                 destination = sgen_minor_collector.par_alloc_for_promotion (vt, obj, objsize, has_references);
1167                 if (G_UNLIKELY (!destination)) {
1168                         if (!sgen_ptr_in_nursery (obj)) {
1169                                 int size_index;
1170                                 block = MS_BLOCK_FOR_OBJ (obj);
1171                                 size_index = block->obj_size_index;
1172                                 evacuate_block_obj_sizes [size_index] = FALSE;
1173                         }
1174
1175                         sgen_parallel_pin_or_update (ptr, obj, vt, queue);
1176                         sgen_set_pinned_from_failed_allocation (objsize);
1177                         return;
1178                 }
1179
1180                 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
1181                         gboolean was_marked;
1182
1183                         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
1184                         obj = destination;
1185                         *ptr = obj;
1186
1187                         /*
1188                          * FIXME: If we make major_alloc_object() give
1189                          * us the block info, too, we won't have to
1190                          * re-fetch it here.
1191                          *
1192                          * FIXME (2): We should rework this to avoid all those nursery checks.
1193                          */
1194                         /*
1195                          * For the split nursery allocator the object
1196                          * might still be in the nursery despite
1197                          * having being promoted, in which case we
1198                          * can't mark it.
1199                          */
1200                         if (!sgen_ptr_in_nursery (obj)) {
1201                                 block = MS_BLOCK_FOR_OBJ (obj);
1202                                 MS_CALC_MARK_BIT (word, bit, obj);
1203                                 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
1204                                 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
1205                                 binary_protocol_mark (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1206                         }
1207                 } else {
1208                         /*
1209                          * FIXME: We have allocated destination, but
1210                          * we cannot use it.  Give it back to the
1211                          * allocator.
1212                          */
1213                         *(void**)destination = NULL;
1214
1215                         vtable_word = *(mword*)obj;
1216                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
1217
1218                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1219
1220                         *ptr = obj;
1221
1222                         HEAVY_STAT (++stat_slots_allocated_in_vain);
1223                 }
1224         } else {
1225 #ifdef FIXED_HEAP
1226                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1227 #else
1228                 mword vtable_word = *(mword*)obj;
1229                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1230
1231                 /* see comment in the non-parallel version below */
1232                 if (vtable_word & SGEN_FORWARDED_BIT) {
1233                         *ptr = (void*)vt;
1234                         return;
1235                 }
1236                 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1237
1238                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1239 #endif
1240                 {
1241                         int size_index;
1242
1243                         block = MS_BLOCK_FOR_OBJ (obj);
1244                         size_index = block->obj_size_index;
1245
1246                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1247                                 if (block->is_to_space)
1248                                         return;
1249
1250 #ifdef FIXED_HEAP
1251                                 {
1252                                         mword vtable_word = *(mword*)obj;
1253                                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1254
1255                                         if (vtable_word & SGEN_FORWARDED_BIT) {
1256                                                 *ptr = (void*)vt;
1257                                                 return;
1258                                         }
1259                                 }
1260 #endif
1261
1262                                 HEAVY_STAT (++stat_major_objects_evacuated);
1263                                 goto do_copy_object;
1264                         }
1265
1266                         MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1267                 } else {
1268                         LOSObject *bigobj = sgen_los_header_for_object (obj);
1269                         mword size_word = bigobj->size;
1270 #ifdef FIXED_HEAP
1271                         mword vtable_word = *(mword*)obj;
1272                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1273 #endif
1274                         if (size_word & 1)
1275                                 return;
1276                         binary_protocol_pin (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1277                         if (SGEN_CAS_PTR ((void*)&bigobj->size, (void*)(size_word | 1), (void*)size_word) == (void*)size_word) {
1278                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1279                                         GRAY_OBJECT_ENQUEUE (queue, obj);
1280                         } else {
1281                                 g_assert (sgen_los_object_is_pinned (obj));
1282                         }
1283                 }
1284         }
1285 }
1286 #else
1287 #ifdef SGEN_HAVE_CONCURRENT_MARK
1288 static void
1289 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
1290 {
1291         g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
1292
1293         if (!sgen_ptr_in_nursery (obj)) {
1294 #ifdef FIXED_HEAP
1295                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1296 #else
1297                 mword objsize;
1298
1299                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1300
1301                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1302 #endif
1303                 {
1304                         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1305                         MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1306                 } else {
1307                         if (sgen_los_object_is_pinned (obj))
1308                                 return;
1309
1310 #ifdef ENABLE_DTRACE
1311                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1312                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1313                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1314                         }
1315 #endif
1316
1317                         sgen_los_pin_object (obj);
1318                         if (SGEN_OBJECT_HAS_REFERENCES (obj))
1319                                 GRAY_OBJECT_ENQUEUE (queue, obj);
1320                         INC_NUM_MAJOR_OBJECTS_MARKED ();
1321                 }
1322         }
1323 }
1324 #endif
1325
1326 static void
1327 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
1328 {
1329         MSBlockInfo *block;
1330
1331         HEAVY_STAT (++stat_copy_object_called_major);
1332
1333         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
1334         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
1335
1336         if (sgen_ptr_in_nursery (obj)) {
1337                 int word, bit;
1338                 char *forwarded, *old_obj;
1339
1340                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1341                         *ptr = forwarded;
1342                         return;
1343                 }
1344                 if (SGEN_OBJECT_IS_PINNED (obj))
1345                         return;
1346
1347                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1348                 if (sgen_nursery_is_to_space (obj))
1349                         return;
1350
1351                 HEAVY_STAT (++stat_objects_copied_major);
1352
1353         do_copy_object:
1354                 old_obj = obj;
1355                 obj = copy_object_no_checks (obj, queue);
1356                 if (G_UNLIKELY (old_obj == obj)) {
1357                         /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1358                         if (!sgen_ptr_in_nursery (obj)) {
1359                                 int size_index;
1360                                 block = MS_BLOCK_FOR_OBJ (obj);
1361                                 size_index = block->obj_size_index;
1362                                 evacuate_block_obj_sizes [size_index] = FALSE;
1363                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1364                         }
1365                         return;
1366                 }
1367                 *ptr = obj;
1368
1369                 /*
1370                  * FIXME: See comment for copy_object_no_checks().  If
1371                  * we have that, we can let the allocation function
1372                  * give us the block info, too, and we won't have to
1373                  * re-fetch it.
1374                  *
1375                  * FIXME (2): We should rework this to avoid all those nursery checks.
1376                  */
1377                 /*
1378                  * For the split nursery allocator the object might
1379                  * still be in the nursery despite having being
1380                  * promoted, in which case we can't mark it.
1381                  */
1382                 if (!sgen_ptr_in_nursery (obj)) {
1383                         block = MS_BLOCK_FOR_OBJ (obj);
1384                         MS_CALC_MARK_BIT (word, bit, obj);
1385                         SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
1386                         MS_SET_MARK_BIT (block, word, bit);
1387                         binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1388                 }
1389         } else {
1390                 char *forwarded;
1391 #ifdef FIXED_HEAP
1392                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1393 #else
1394                 mword objsize;
1395
1396                 /*
1397                  * If we have don't have a fixed heap we cannot know
1398                  * whether an object is in the LOS or in the small
1399                  * object major heap without checking its size.  To do
1400                  * that, however, we need to know that we actually
1401                  * have a valid object, not a forwarding pointer, so
1402                  * we have to do this check first.
1403                  */
1404                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1405                         *ptr = forwarded;
1406                         return;
1407                 }
1408
1409                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1410
1411                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1412 #endif
1413                 {
1414                         int size_index;
1415                         gboolean evacuate;
1416
1417                         block = MS_BLOCK_FOR_OBJ (obj);
1418                         size_index = block->obj_size_index;
1419                         evacuate = evacuate_block_obj_sizes [size_index];
1420
1421 #ifdef FIXED_HEAP
1422                         /*
1423                          * We could also check for !block->has_pinned
1424                          * here, but it would only make an uncommon case
1425                          * faster, namely objects that are in blocks
1426                          * whose slot sizes are evacuated but which have
1427                          * pinned objects.
1428                          */
1429                         if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1430                                 *ptr = forwarded;
1431                                 return;
1432                         }
1433 #endif
1434
1435                         if (evacuate && !block->has_pinned) {
1436                                 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1437                                 if (block->is_to_space)
1438                                         return;
1439                                 HEAVY_STAT (++stat_major_objects_evacuated);
1440                                 goto do_copy_object;
1441                         } else {
1442                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1443                         }
1444                 } else {
1445                         if (sgen_los_object_is_pinned (obj))
1446                                 return;
1447                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1448
1449 #ifdef ENABLE_DTRACE
1450                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1451                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1452                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1453                         }
1454 #endif
1455
1456                         sgen_los_pin_object (obj);
1457                         if (SGEN_OBJECT_HAS_REFERENCES (obj))
1458                                 GRAY_OBJECT_ENQUEUE (queue, obj);
1459                 }
1460         }
1461 }
1462 #endif
1463
1464 static void
1465 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1466 {
1467         major_copy_or_mark_object (ptr, *ptr, queue);
1468 }
1469
1470 #ifdef SGEN_HAVE_CONCURRENT_MARK
1471 static void
1472 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1473 {
1474         major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1475 }
1476
1477 static long long
1478 major_get_and_reset_num_major_objects_marked (void)
1479 {
1480 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1481         long long num = num_major_objects_marked;
1482         num_major_objects_marked = 0;
1483         return num;
1484 #else
1485         return 0;
1486 #endif
1487 }
1488 #endif
1489
1490 #include "sgen-major-scan-object.h"
1491
1492 #ifdef SGEN_HAVE_CONCURRENT_MARK
1493 #define SCAN_FOR_CONCURRENT_MARK
1494 #include "sgen-major-scan-object.h"
1495 #undef SCAN_FOR_CONCURRENT_MARK
1496 #endif
1497
1498 static void
1499 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1500 {
1501         int i;
1502         int last_index = -1;
1503
1504         if (!block->pin_queue_num_entries)
1505                 return;
1506
1507         block->has_pinned = TRUE;
1508
1509         for (i = 0; i < block->pin_queue_num_entries; ++i) {
1510                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1511                 SGEN_ASSERT (9, index >= 0 && index < MS_BLOCK_FREE / block->obj_size, "invalid object %p index %d max-index %d", block->pin_queue_start [i], index, MS_BLOCK_FREE / block->obj_size);
1512                 if (index == last_index)
1513                         continue;
1514                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1515                 last_index = index;
1516         }
1517 }
1518
1519 static inline void
1520 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1521 {
1522         int obj_index;
1523
1524         for (obj_index = 0; obj_index < count; ++obj_index) {
1525                 int word, bit;
1526                 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1527
1528                 MS_CALC_MARK_BIT (word, bit, obj);
1529                 if (MS_MARK_BIT (block, word, bit)) {
1530                         SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1531                 } else {
1532                         /* an unmarked object */
1533                         if (MS_OBJ_ALLOCED (obj, block)) {
1534                                 /*
1535                                  * FIXME: Merge consecutive
1536                                  * slots for lower reporting
1537                                  * overhead.  Maybe memset
1538                                  * will also benefit?
1539                                  */
1540                                 binary_protocol_empty (obj, obj_size);
1541                                 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1542                                 memset (obj, 0, obj_size);
1543                         }
1544                         *(void**)obj = block->free_list;
1545                         block->free_list = obj;
1546                 }
1547         }
1548 }
1549
1550 /*
1551  * sweep_block:
1552  *
1553  *   Traverse BLOCK, freeing and zeroing unused objects.
1554  */
1555 static void
1556 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1557 {
1558         int count;
1559         void *reversed = NULL;
1560
1561         if (!during_major_collection)
1562                 g_assert (!sgen_concurrent_collection_in_progress ());
1563
1564         if (block->swept)
1565                 return;
1566
1567         count = MS_BLOCK_FREE / block->obj_size;
1568
1569         block->free_list = NULL;
1570
1571         /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1572         // FIXME: Add more sizes
1573         switch (block->obj_size) {
1574         case 16:
1575                 sweep_block_for_size (block, count, 16);
1576                 break;
1577         default:
1578                 sweep_block_for_size (block, count, block->obj_size);
1579                 break;
1580         }
1581
1582         /* reset mark bits */
1583         memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1584
1585         /* Reverse free list so that it's in address order */
1586         reversed = NULL;
1587         while (block->free_list) {
1588                 void *next = *(void**)block->free_list;
1589                 *(void**)block->free_list = reversed;
1590                 reversed = block->free_list;
1591                 block->free_list = next;
1592         }
1593         block->free_list = reversed;
1594
1595         block->swept = 1;
1596 }
1597
1598 static inline int
1599 bitcount (mword d)
1600 {
1601         int count = 0;
1602
1603 #ifdef __GNUC__
1604         if (sizeof (mword) == sizeof (unsigned long))
1605                 count += __builtin_popcountl (d);
1606         else
1607                 count += __builtin_popcount (d);
1608 #else
1609         while (d) {
1610                 count ++;
1611                 d &= (d - 1);
1612         }
1613 #endif
1614         return count;
1615 }
1616
1617 static void
1618 ms_sweep (void)
1619 {
1620         int i;
1621         MSBlockInfo **iter;
1622
1623         /* statistics for evacuation */
1624         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1625         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1626         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1627
1628 #ifdef SGEN_HAVE_CONCURRENT_MARK
1629         mword total_evacuate_heap = 0;
1630         mword total_evacuate_saved = 0;
1631 #endif
1632
1633         for (i = 0; i < num_block_obj_sizes; ++i)
1634                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1635
1636         /* clear all the free lists */
1637         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1638                 MSBlockInfo **free_blocks = free_block_lists [i];
1639                 int j;
1640                 for (j = 0; j < num_block_obj_sizes; ++j)
1641                         free_blocks [j] = NULL;
1642         }
1643
1644         /* traverse all blocks, free and zero unmarked objects */
1645         iter = &all_blocks;
1646         while (*iter) {
1647                 MSBlockInfo *block = *iter;
1648                 int count;
1649                 gboolean have_live = FALSE;
1650                 gboolean has_pinned;
1651                 gboolean have_free = FALSE;
1652                 int obj_size_index;
1653                 int nused = 0;
1654
1655                 obj_size_index = block->obj_size_index;
1656
1657                 has_pinned = block->has_pinned;
1658                 block->has_pinned = block->pinned;
1659
1660                 block->is_to_space = FALSE;
1661                 block->swept = 0;
1662
1663                 count = MS_BLOCK_FREE / block->obj_size;
1664
1665 #ifdef SGEN_HAVE_CONCURRENT_MARK
1666                 if (block->cardtable_mod_union) {
1667                         sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1668                         block->cardtable_mod_union = NULL;
1669                 }
1670 #endif
1671
1672                 /* Count marked objects in the block */
1673                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1674                         nused += bitcount (block->mark_words [i]);
1675                 }
1676                 if (nused) {
1677                         have_live = TRUE;
1678                 }
1679                 if (nused < count)
1680                         have_free = TRUE;
1681
1682                 if (!lazy_sweep)
1683                         sweep_block (block, TRUE);
1684
1685                 if (have_live) {
1686                         if (!has_pinned) {
1687                                 ++num_blocks [obj_size_index];
1688                                 slots_used [obj_size_index] += nused;
1689                                 slots_available [obj_size_index] += count;
1690                         }
1691
1692                         iter = &block->next;
1693
1694                         /*
1695                          * If there are free slots in the block, add
1696                          * the block to the corresponding free list.
1697                          */
1698                         if (have_free) {
1699                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1700                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1701                                 block->next_free = free_blocks [index];
1702                                 free_blocks [index] = block;
1703                         }
1704
1705                         update_heap_boundaries_for_block (block);
1706                 } else {
1707                         /*
1708                          * Blocks without live objects are removed from the
1709                          * block list and freed.
1710                          */
1711                         *iter = block->next;
1712
1713 #ifdef FIXED_HEAP
1714                         ms_free_block (block);
1715 #else
1716                         ms_free_block (block->block);
1717
1718                         sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1719 #endif
1720
1721                         --num_major_sections;
1722                 }
1723         }
1724
1725         for (i = 0; i < num_block_obj_sizes; ++i) {
1726                 float usage = (float)slots_used [i] / (float)slots_available [i];
1727                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1728                         evacuate_block_obj_sizes [i] = TRUE;
1729                         /*
1730                         g_print ("slot size %d - %d of %d used\n",
1731                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1732                         */
1733                 } else {
1734                         evacuate_block_obj_sizes [i] = FALSE;
1735                 }
1736 #ifdef SGEN_HAVE_CONCURRENT_MARK
1737                 {
1738                         mword total_bytes = block_obj_sizes [i] * slots_available [i];
1739                         total_evacuate_heap += total_bytes;
1740                         if (evacuate_block_obj_sizes [i])
1741                                 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1742                 }
1743 #endif
1744         }
1745
1746 #ifdef SGEN_HAVE_CONCURRENT_MARK
1747         want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1748 #endif
1749
1750         have_swept = TRUE;
1751 }
1752
1753 static void
1754 major_sweep (void)
1755 {
1756         ms_sweep ();
1757 }
1758
1759 static int count_pinned_ref;
1760 static int count_pinned_nonref;
1761 static int count_nonpinned_ref;
1762 static int count_nonpinned_nonref;
1763
1764 static void
1765 count_nonpinned_callback (char *obj, size_t size, void *data)
1766 {
1767         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1768
1769         if (vtable->klass->has_references)
1770                 ++count_nonpinned_ref;
1771         else
1772                 ++count_nonpinned_nonref;
1773 }
1774
1775 static void
1776 count_pinned_callback (char *obj, size_t size, void *data)
1777 {
1778         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1779
1780         if (vtable->klass->has_references)
1781                 ++count_pinned_ref;
1782         else
1783                 ++count_pinned_nonref;
1784 }
1785
1786 static G_GNUC_UNUSED void
1787 count_ref_nonref_objs (void)
1788 {
1789         int total;
1790
1791         count_pinned_ref = 0;
1792         count_pinned_nonref = 0;
1793         count_nonpinned_ref = 0;
1794         count_nonpinned_nonref = 0;
1795
1796         major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1797         major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1798
1799         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1800
1801         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1802                         count_pinned_ref, count_nonpinned_ref,
1803                         count_pinned_nonref, count_nonpinned_nonref,
1804                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1805 }
1806
1807 static int
1808 ms_calculate_block_obj_sizes (double factor, int *arr)
1809 {
1810         double target_size = sizeof (MonoObject);
1811         int num_sizes = 0;
1812         int last_size = 0;
1813
1814         do {
1815                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1816                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1817
1818                 if (size != last_size) {
1819                         if (arr)
1820                                 arr [num_sizes] = size;
1821                         ++num_sizes;
1822                         last_size = size;
1823                 }
1824
1825                 target_size *= factor;
1826         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1827
1828         return num_sizes;
1829 }
1830
1831 /* only valid during minor collections */
1832 static int old_num_major_sections;
1833
1834 static void
1835 major_start_nursery_collection (void)
1836 {
1837 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1838         consistency_check ();
1839 #endif
1840
1841         old_num_major_sections = num_major_sections;
1842 }
1843
1844 static void
1845 major_finish_nursery_collection (void)
1846 {
1847 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1848         consistency_check ();
1849 #endif
1850         sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1851 }
1852
1853 static void
1854 major_start_major_collection (void)
1855 {
1856         int i;
1857
1858         /* clear the free lists */
1859         for (i = 0; i < num_block_obj_sizes; ++i) {
1860                 if (!evacuate_block_obj_sizes [i])
1861                         continue;
1862
1863                 free_block_lists [0][i] = NULL;
1864                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1865         }
1866
1867         // Sweep all unswept blocks
1868         if (lazy_sweep) {
1869                 MSBlockInfo **iter;
1870
1871                 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1872
1873                 iter = &all_blocks;
1874                 while (*iter) {
1875                         MSBlockInfo *block = *iter;
1876
1877                         sweep_block (block, TRUE);
1878
1879                         iter = &block->next;
1880                 }
1881
1882                 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1883         }
1884 }
1885
1886 static void
1887 major_finish_major_collection (void)
1888 {
1889 }
1890
1891 #if !defined(FIXED_HEAP) && SIZEOF_VOID_P != 8
1892 static int
1893 compare_pointers (const void *va, const void *vb) {
1894         char *a = *(char**)va, *b = *(char**)vb;
1895         if (a < b)
1896                 return -1;
1897         if (a > b)
1898                 return 1;
1899         return 0;
1900 }
1901 #endif
1902
1903 static void
1904 major_have_computer_minor_collection_allowance (void)
1905 {
1906 #ifndef FIXED_HEAP
1907         int section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1908
1909         g_assert (have_swept);
1910
1911 #if SIZEOF_VOID_P != 8
1912         {
1913                 int i, num_empty_blocks_orig, num_blocks, arr_length;
1914                 void *block;
1915                 void **empty_block_arr;
1916                 void **rebuild_next;
1917
1918 #ifdef TARGET_WIN32
1919                 /*
1920                  * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1921                  * a VirtualAlloc ()-ed block.
1922                  */
1923                 return;
1924 #endif
1925
1926                 if (num_empty_blocks <= section_reserve)
1927                         return;
1928                 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1929
1930                 num_empty_blocks_orig = num_empty_blocks;
1931                 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1932                                 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1933                 if (!empty_block_arr)
1934                         goto fallback;
1935
1936                 i = 0;
1937                 for (block = empty_blocks; block; block = *(void**)block)
1938                         empty_block_arr [i++] = block;
1939                 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1940
1941                 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1942
1943                 /*
1944                  * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1945                  * contiguous ones.  If we do, we free them.  If that's not enough to get to
1946                  * section_reserve, we halve the number of contiguous blocks we're looking
1947                  * for and have another go, until we're done with looking for pairs of
1948                  * blocks, at which point we give up and go to the fallback.
1949                  */
1950                 arr_length = num_empty_blocks_orig;
1951                 num_blocks = MS_BLOCK_ALLOC_NUM;
1952                 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1953                         int first = -1;
1954                         int dest = 0;
1955
1956                         dest = 0;
1957                         for (i = 0; i < arr_length; ++i) {
1958                                 int d = dest;
1959                                 void *block = empty_block_arr [i];
1960                                 SGEN_ASSERT (0, block, "we're not shifting correctly");
1961                                 if (i != dest) {
1962                                         empty_block_arr [dest] = block;
1963                                         /*
1964                                          * This is not strictly necessary, but we're
1965                                          * cautious.
1966                                          */
1967                                         empty_block_arr [i] = NULL;
1968                                 }
1969                                 ++dest;
1970
1971                                 if (first < 0) {
1972                                         first = d;
1973                                         continue;
1974                                 }
1975
1976                                 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1977
1978                                 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1979                                         first = d;
1980                                         continue;
1981                                 }
1982
1983                                 if (d + 1 - first == num_blocks) {
1984                                         /*
1985                                          * We found num_blocks contiguous blocks.  Free them
1986                                          * and null their array entries.  As an optimization
1987                                          * we could, instead of nulling the entries, shift
1988                                          * the following entries over to the left, while
1989                                          * we're iterating.
1990                                          */
1991                                         int j;
1992                                         sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1993                                         for (j = first; j <= d; ++j)
1994                                                 empty_block_arr [j] = NULL;
1995                                         dest = first;
1996                                         first = -1;
1997
1998                                         num_empty_blocks -= num_blocks;
1999
2000                                         stat_major_blocks_freed += num_blocks;
2001                                         if (num_blocks == MS_BLOCK_ALLOC_NUM)
2002                                                 stat_major_blocks_freed_ideal += num_blocks;
2003                                         else
2004                                                 stat_major_blocks_freed_less_ideal += num_blocks;
2005
2006                                 }
2007                         }
2008
2009                         SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
2010                         arr_length = dest;
2011                         SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
2012
2013                         num_blocks >>= 1;
2014                 }
2015
2016                 /* rebuild empty_blocks free list */
2017                 rebuild_next = (void**)&empty_blocks;
2018                 for (i = 0; i < arr_length; ++i) {
2019                         void *block = empty_block_arr [i];
2020                         SGEN_ASSERT (0, block, "we're missing blocks");
2021                         *rebuild_next = block;
2022                         rebuild_next = (void**)block;
2023                 }
2024                 *rebuild_next = NULL;
2025
2026                 /* free array */
2027                 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
2028         }
2029
2030         SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
2031
2032  fallback:
2033         /*
2034          * This is our threshold.  If there's not more empty than used blocks, we won't
2035          * release uncontiguous blocks, in fear of fragmenting the address space.
2036          */
2037         if (num_empty_blocks <= num_major_sections)
2038                 return;
2039 #endif
2040
2041         while (num_empty_blocks > section_reserve) {
2042                 void *next = *(void**)empty_blocks;
2043                 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
2044                 empty_blocks = next;
2045                 /*
2046                  * Needs not be atomic because this is running
2047                  * single-threaded.
2048                  */
2049                 --num_empty_blocks;
2050
2051                 ++stat_major_blocks_freed;
2052 #if SIZEOF_VOID_P != 8
2053                 ++stat_major_blocks_freed_individual;
2054 #endif
2055         }
2056 #endif
2057 }
2058
2059 static void
2060 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
2061 {
2062         MSBlockInfo *block;
2063
2064         FOREACH_BLOCK (block) {
2065                 block->pin_queue_start = sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
2066                                 &block->pin_queue_num_entries);
2067         } END_FOREACH_BLOCK;
2068 }
2069
2070 static void
2071 major_pin_objects (SgenGrayQueue *queue)
2072 {
2073         MSBlockInfo *block;
2074
2075         FOREACH_BLOCK (block) {
2076                 mark_pinned_objects_in_block (block, queue);
2077         } END_FOREACH_BLOCK;
2078 }
2079
2080 static void
2081 major_init_to_space (void)
2082 {
2083 }
2084
2085 static void
2086 major_report_pinned_memory_usage (void)
2087 {
2088         g_assert_not_reached ();
2089 }
2090
2091 static gint64
2092 major_get_used_size (void)
2093 {
2094         gint64 size = 0;
2095         MSBlockInfo *block;
2096
2097         FOREACH_BLOCK (block) {
2098                 int count = MS_BLOCK_FREE / block->obj_size;
2099                 void **iter;
2100                 size += count * block->obj_size;
2101                 for (iter = block->free_list; iter; iter = (void**)*iter)
2102                         size -= block->obj_size;
2103         } END_FOREACH_BLOCK;
2104
2105         return size;
2106 }
2107
2108 static int
2109 get_num_major_sections (void)
2110 {
2111         return num_major_sections;
2112 }
2113
2114 static gboolean
2115 major_handle_gc_param (const char *opt)
2116 {
2117 #ifdef FIXED_HEAP
2118         if (g_str_has_prefix (opt, "major-heap-size=")) {
2119                 const char *arg = strchr (opt, '=') + 1;
2120                 size_t size;
2121                 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
2122                         return FALSE;
2123                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
2124                 g_assert (ms_heap_num_blocks > 0);
2125                 return TRUE;
2126         } else
2127 #endif
2128         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
2129                 const char *arg = strchr (opt, '=') + 1;
2130                 int percentage = atoi (arg);
2131                 if (percentage < 0 || percentage > 100) {
2132                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
2133                         exit (1);
2134                 }
2135                 evacuation_threshold = (float)percentage / 100.0;
2136                 return TRUE;
2137         } else if (!strcmp (opt, "lazy-sweep")) {
2138                 lazy_sweep = TRUE;
2139                 return TRUE;
2140         } else if (!strcmp (opt, "no-lazy-sweep")) {
2141                 lazy_sweep = FALSE;
2142                 return TRUE;
2143         }
2144
2145         return FALSE;
2146 }
2147
2148 static void
2149 major_print_gc_param_usage (void)
2150 {
2151         fprintf (stderr,
2152                         ""
2153 #ifdef FIXED_HEAP
2154                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
2155 #endif
2156                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
2157                         "  (no-)lazy-sweep\n"
2158                         );
2159 }
2160
2161 static void
2162 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
2163 {
2164         MSBlockInfo *block;
2165
2166         FOREACH_BLOCK (block) {
2167                 if (block->has_references)
2168                         callback ((mword)block->block, MS_BLOCK_SIZE);
2169         } END_FOREACH_BLOCK;
2170 }
2171
2172 #ifdef HEAVY_STATISTICS
2173 extern long long marked_cards;
2174 extern long long scanned_cards;
2175 extern long long scanned_objects;
2176 extern long long remarked_cards;
2177 #endif
2178
2179 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
2180 /*
2181  * MS blocks are 16K aligned.
2182  * Cardtables are 4K aligned, at least.
2183  * This means that the cardtable of a given block is 32 bytes aligned.
2184  */
2185 static guint8*
2186 initial_skip_card (guint8 *card_data)
2187 {
2188         mword *cards = (mword*)card_data;
2189         mword card;
2190         int i;
2191         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
2192                 card = cards [i];
2193                 if (card)
2194                         break;
2195         }
2196
2197         if (i == CARD_WORDS_PER_BLOCK)
2198                 return card_data + CARDS_PER_BLOCK;
2199
2200 #if defined(__i386__) && defined(__GNUC__)
2201         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
2202 #elif defined(__x86_64__) && defined(__GNUC__)
2203         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
2204 #elif defined(__s390x__) && defined(__GNUC__)
2205         return card_data + i * 8 +  (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
2206 #else
2207         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
2208                 if (card_data [i])
2209                         return &card_data [i];
2210         }
2211         return card_data;
2212 #endif
2213 }
2214
2215
2216 static G_GNUC_UNUSED guint8*
2217 skip_card (guint8 *card_data, guint8 *card_data_end)
2218 {
2219         while (card_data < card_data_end && !*card_data)
2220                 ++card_data;
2221         return card_data;
2222 }
2223
2224 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
2225 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
2226 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
2227
2228 static void
2229 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
2230 {
2231         MSBlockInfo *block;
2232         ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
2233
2234 #ifdef SGEN_HAVE_CONCURRENT_MARK
2235         if (!concurrent_mark)
2236                 g_assert (!mod_union);
2237 #else
2238         g_assert (!mod_union);
2239 #endif
2240
2241         FOREACH_BLOCK (block) {
2242                 int block_obj_size;
2243                 char *block_start;
2244
2245                 if (!block->has_references)
2246                         continue;
2247
2248                 block_obj_size = block->obj_size;
2249                 block_start = block->block;
2250
2251                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
2252                         guint8 *cards;
2253 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
2254                         guint8 cards_data [CARDS_PER_BLOCK];
2255 #endif
2256                         char *obj, *end, *base;
2257
2258                         if (mod_union) {
2259 #ifdef SGEN_HAVE_CONCURRENT_MARK
2260                                 cards = block->cardtable_mod_union;
2261                                 /*
2262                                  * This happens when the nursery
2263                                  * collection that precedes finishing
2264                                  * the concurrent collection allocates
2265                                  * new major blocks.
2266                                  */
2267                                 if (!cards)
2268                                         continue;
2269 #endif
2270                         } else {
2271                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
2272 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
2273                                 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
2274 #else
2275                                 cards = cards_data;
2276                                 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
2277                                         continue;
2278 #endif
2279                         }
2280
2281                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
2282                         end = block_start + MS_BLOCK_SIZE;
2283                         base = sgen_card_table_align_pointer (obj);
2284
2285                         while (obj < end) {
2286                                 int card_offset;
2287
2288                                 if (!block->swept)
2289                                         sweep_block (block, FALSE);
2290
2291                                 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2292                                         goto next_large;
2293
2294                                 if (mod_union) {
2295                                         /* FIXME: do this more efficiently */
2296                                         int w, b;
2297                                         MS_CALC_MARK_BIT (w, b, obj);
2298                                         if (!MS_MARK_BIT (block, w, b))
2299                                                 goto next_large;
2300                                 }
2301
2302                                 card_offset = (obj - base) >> CARD_BITS;
2303                                 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
2304
2305                         next_large:
2306                                 obj += block_obj_size;
2307                         }
2308                 } else {
2309                         guint8 *card_data, *card_base;
2310                         guint8 *card_data_end;
2311
2312                         /*
2313                          * This is safe in face of card aliasing for the following reason:
2314                          *
2315                          * Major blocks are 16k aligned, or 32 cards aligned.
2316                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
2317                          * sizes, they won't overflow the cardtable overlap modulus.
2318                          */
2319                         if (mod_union) {
2320 #ifdef SGEN_HAVE_CONCURRENT_MARK
2321                                 card_data = card_base = block->cardtable_mod_union;
2322                                 /*
2323                                  * This happens when the nursery
2324                                  * collection that precedes finishing
2325                                  * the concurrent collection allocates
2326                                  * new major blocks.
2327                                  */
2328                                 if (!card_data)
2329                                         continue;
2330 #else
2331                                 g_assert_not_reached ();
2332                                 card_data = NULL;
2333 #endif
2334                         } else {
2335                                 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
2336                         }
2337                         card_data_end = card_data + CARDS_PER_BLOCK;
2338
2339                         for (card_data = initial_skip_card (card_data); card_data < card_data_end; ++card_data) { //card_data = skip_card (card_data + 1, card_data_end)) {
2340                                 int index;
2341                                 int idx = card_data - card_base;
2342                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
2343                                 char *end = start + CARD_SIZE_IN_BYTES;
2344                                 char *first_obj, *obj;
2345
2346                                 HEAVY_STAT (++scanned_cards);
2347
2348                                 if (!*card_data)
2349                                         continue;
2350
2351                                 if (!block->swept)
2352                                         sweep_block (block, FALSE);
2353
2354                                 HEAVY_STAT (++marked_cards);
2355
2356                                 sgen_card_table_prepare_card_for_scanning (card_data);
2357
2358                                 if (idx == 0)
2359                                         index = 0;
2360                                 else
2361                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
2362
2363                                 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
2364                                 while (obj < end) {
2365                                         if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2366                                                 goto next_small;
2367
2368                                         if (mod_union) {
2369                                                 /* FIXME: do this more efficiently */
2370                                                 int w, b;
2371                                                 MS_CALC_MARK_BIT (w, b, obj);
2372                                                 if (!MS_MARK_BIT (block, w, b))
2373                                                         goto next_small;
2374                                         }
2375
2376                                         HEAVY_STAT (++scanned_objects);
2377                                         scan_func (obj, queue);
2378                                 next_small:
2379                                         obj += block_obj_size;
2380                                 }
2381                                 HEAVY_STAT (if (*card_data) ++remarked_cards);
2382                                 binary_protocol_card_scan (first_obj, obj - first_obj);
2383                         }
2384                 }
2385         } END_FOREACH_BLOCK;
2386 }
2387
2388 static void
2389 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
2390 {
2391         MSBlockInfo *block;
2392         long long total_cards = 0;
2393         long long marked_cards = 0;
2394
2395         FOREACH_BLOCK (block) {
2396                 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) block->block);
2397                 int i;
2398
2399                 if (!block->has_references)
2400                         continue;
2401
2402                 total_cards += CARDS_PER_BLOCK;
2403                 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
2404                         if (cards [i])
2405                                 ++marked_cards;
2406                 }
2407         } END_FOREACH_BLOCK;
2408
2409         *num_total_cards = total_cards;
2410         *num_marked_cards = marked_cards;
2411 }
2412
2413 #ifdef SGEN_HAVE_CONCURRENT_MARK
2414 static void
2415 update_cardtable_mod_union (void)
2416 {
2417         MSBlockInfo *block;
2418
2419         FOREACH_BLOCK (block) {
2420                 size_t num_cards;
2421
2422                 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
2423                                 block->block, MS_BLOCK_SIZE, &num_cards);
2424
2425                 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
2426         } END_FOREACH_BLOCK;
2427 }
2428
2429 static guint8*
2430 major_get_cardtable_mod_union_for_object (char *obj)
2431 {
2432         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
2433         return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (block->block)) >> CARD_BITS];
2434 }
2435 #endif
2436
2437 static void
2438 alloc_free_block_lists (MSBlockInfo ***lists)
2439 {
2440         int i;
2441         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2442                 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2443 }
2444
2445 #ifdef SGEN_PARALLEL_MARK
2446 static void*
2447 major_alloc_worker_data (void)
2448 {
2449         /* FIXME: free this when the workers come down */
2450         MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
2451         alloc_free_block_lists (lists);
2452         return lists;
2453 }
2454
2455 static void
2456 major_init_worker_thread (void *data)
2457 {
2458         MSBlockInfo ***lists = data;
2459         int i;
2460
2461         g_assert (lists && lists != free_block_lists);
2462         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2463                 int j;
2464                 for (j = 0; j < num_block_obj_sizes; ++j)
2465                         g_assert (!lists [i][j]);
2466         }
2467
2468 #ifdef HAVE_KW_THREAD
2469         workers_free_block_lists = data;
2470 #else
2471         mono_native_tls_set_value (workers_free_block_lists_key, data);
2472 #endif
2473 }
2474
2475 static void
2476 major_reset_worker_data (void *data)
2477 {
2478         MSBlockInfo ***lists = data;
2479         int i;
2480         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2481                 int j;
2482                 for (j = 0; j < num_block_obj_sizes; ++j)
2483                         lists [i][j] = NULL;
2484         }
2485 }
2486 #endif
2487
2488 #undef pthread_create
2489
2490 static void
2491 post_param_init (SgenMajorCollector *collector)
2492 {
2493         collector->sweeps_lazily = lazy_sweep;
2494 }
2495
2496 #ifdef SGEN_HAVE_CONCURRENT_MARK
2497 static void
2498 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2499 #else // SGEN_HAVE_CONCURRENT_MARK
2500 #ifdef SGEN_PARALLEL_MARK
2501 #ifdef FIXED_HEAP
2502 void
2503 sgen_marksweep_fixed_par_init (SgenMajorCollector *collector)
2504 #else // FIXED_HEAP
2505 void
2506 sgen_marksweep_par_init (SgenMajorCollector *collector)
2507 #endif // FIXED_HEAP
2508 #else // SGEN_PARALLEL_MARK
2509 #ifdef FIXED_HEAP
2510 void
2511 sgen_marksweep_fixed_init (SgenMajorCollector *collector)
2512 #else // FIXED_HEAP
2513 #error unknown configuration
2514 #endif // FIXED_HEAP
2515 #endif // SGEN_PARALLEL_MARK
2516 #endif // SGEN_HAVE_CONCURRENT_MARK
2517 {
2518         int i;
2519
2520 #ifndef FIXED_HEAP
2521         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2522 #endif
2523
2524         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2525         block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2526         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2527
2528         evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2529         for (i = 0; i < num_block_obj_sizes; ++i)
2530                 evacuate_block_obj_sizes [i] = FALSE;
2531
2532         /*
2533         {
2534                 int i;
2535                 g_print ("block object sizes:\n");
2536                 for (i = 0; i < num_block_obj_sizes; ++i)
2537                         g_print ("%d\n", block_obj_sizes [i]);
2538         }
2539         */
2540
2541         alloc_free_block_lists (free_block_lists);
2542
2543         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2544                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2545         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2546                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2547
2548 #ifdef SGEN_PARALLEL_MARK
2549         LOCK_INIT (ms_block_list_mutex);
2550 #endif
2551
2552         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2553         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2554         mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2555         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2556 #if SIZEOF_VOID_P != 8
2557         mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2558         mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2559         mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2560         mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2561 #endif
2562
2563 #ifdef SGEN_PARALLEL_MARK
2564 #ifndef HAVE_KW_THREAD
2565         mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
2566 #endif
2567 #endif
2568
2569         collector->section_size = MAJOR_SECTION_SIZE;
2570 #ifdef SGEN_PARALLEL_MARK
2571         collector->is_parallel = TRUE;
2572         collector->alloc_worker_data = major_alloc_worker_data;
2573         collector->init_worker_thread = major_init_worker_thread;
2574         collector->reset_worker_data = major_reset_worker_data;
2575 #else
2576         collector->is_parallel = FALSE;
2577 #endif
2578 #ifdef SGEN_HAVE_CONCURRENT_MARK
2579         concurrent_mark = is_concurrent;
2580         if (is_concurrent) {
2581                 collector->is_concurrent = TRUE;
2582                 collector->want_synchronous_collection = &want_evacuation;
2583                 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2584         } else
2585 #endif
2586         {
2587                 collector->is_concurrent = FALSE;
2588                 collector->want_synchronous_collection = NULL;
2589         }
2590         collector->supports_cardtable = TRUE;
2591
2592         collector->have_swept = &have_swept;
2593
2594         collector->alloc_heap = major_alloc_heap;
2595         collector->is_object_live = major_is_object_live;
2596         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2597         collector->alloc_degraded = major_alloc_degraded;
2598
2599         collector->alloc_object = major_alloc_object;
2600 #ifdef SGEN_PARALLEL_MARK
2601         collector->par_alloc_object = major_par_alloc_object;
2602 #endif
2603         collector->free_pinned_object = free_pinned_object;
2604         collector->iterate_objects = major_iterate_objects;
2605         collector->free_non_pinned_object = major_free_non_pinned_object;
2606         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2607         collector->pin_objects = major_pin_objects;
2608         collector->pin_major_object = pin_major_object;
2609         collector->scan_card_table = major_scan_card_table;
2610         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2611 #ifdef SGEN_HAVE_CONCURRENT_MARK
2612         if (is_concurrent) {
2613                 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2614                 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2615         }
2616 #endif
2617         collector->init_to_space = major_init_to_space;
2618         collector->sweep = major_sweep;
2619         collector->check_scan_starts = major_check_scan_starts;
2620         collector->dump_heap = major_dump_heap;
2621         collector->get_used_size = major_get_used_size;
2622         collector->start_nursery_collection = major_start_nursery_collection;
2623         collector->finish_nursery_collection = major_finish_nursery_collection;
2624         collector->start_major_collection = major_start_major_collection;
2625         collector->finish_major_collection = major_finish_major_collection;
2626         collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2627         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2628         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2629         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2630         collector->get_num_major_sections = get_num_major_sections;
2631         collector->handle_gc_param = major_handle_gc_param;
2632         collector->print_gc_param_usage = major_print_gc_param_usage;
2633         collector->post_param_init = post_param_init;
2634         collector->is_valid_object = major_is_valid_object;
2635         collector->describe_pointer = major_describe_pointer;
2636         collector->count_cards = major_count_cards;
2637
2638         collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2639         collector->major_ops.scan_object = major_scan_object;
2640 #ifdef SGEN_HAVE_CONCURRENT_MARK
2641         if (is_concurrent) {
2642                 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2643                 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2644                 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2645         }
2646 #endif
2647
2648         /*cardtable requires major pages to be 8 cards aligned*/
2649         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2650 }
2651
2652 #ifdef SGEN_HAVE_CONCURRENT_MARK
2653 void
2654 sgen_marksweep_init (SgenMajorCollector *collector)
2655 {
2656         sgen_marksweep_init_internal (collector, FALSE);
2657 }
2658
2659 void
2660 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2661 {
2662         sgen_marksweep_init_internal (collector, TRUE);
2663 }
2664 #endif
2665
2666 #endif