Renamed target AssignProjectConfigurations to AssignProjectConfiguration (without...
[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         info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD); /*FIXME WHY??? */
568         info->swept = 1;
569 #ifndef FIXED_HEAP
570         info->block = ms_get_empty_block ();
571
572         header = (MSBlockHeader*) info->block;
573         header->info = info;
574 #endif
575 #ifdef SGEN_HAVE_CONCURRENT_MARK
576         info->cardtable_mod_union = NULL;
577 #endif
578
579         update_heap_boundaries_for_block (info);
580
581         /* build free list */
582         obj_start = info->block + MS_BLOCK_SKIP;
583         info->free_list = (void**)obj_start;
584         /* we're skipping the last one - it must be nulled */
585         for (i = 0; i < count - 1; ++i) {
586                 char *next_obj_start = obj_start + size;
587                 *(void**)obj_start = next_obj_start;
588                 obj_start = next_obj_start;
589         }
590         /* the last one */
591         *(void**)obj_start = NULL;
592
593 #ifdef SGEN_PARALLEL_MARK
594         do {
595                 next = info->next_free = free_blocks [size_index];
596         } while (SGEN_CAS_PTR ((void**)&free_blocks [size_index], info, next) != next);
597
598         do {
599                 next = info->next = all_blocks;
600         } while (SGEN_CAS_PTR ((void**)&all_blocks, info, next) != next);
601 #else
602         info->next_free = free_blocks [size_index];
603         free_blocks [size_index] = info;
604
605         info->next = all_blocks;
606         all_blocks = info;
607 #endif
608
609         ++num_major_sections;
610         return TRUE;
611 }
612
613 static gboolean
614 obj_is_from_pinned_alloc (char *ptr)
615 {
616         MSBlockInfo *block;
617
618         FOREACH_BLOCK (block) {
619                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
620                         return block->pinned;
621         } END_FOREACH_BLOCK;
622         return FALSE;
623 }
624
625 static void*
626 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
627 {
628         MSBlockInfo *block;
629         void *obj;
630
631         block = free_blocks [size_index];
632         SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
633
634         if (G_UNLIKELY (!block->swept)) {
635                 stat_major_blocks_lazy_swept ++;
636                 sweep_block (block, FALSE);
637         }
638
639         obj = block->free_list;
640         SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
641
642         block->free_list = *(void**)obj;
643         if (!block->free_list) {
644                 free_blocks [size_index] = block->next_free;
645                 block->next_free = NULL;
646         }
647
648         return obj;
649 }
650
651 #ifdef SGEN_PARALLEL_MARK
652 static gboolean
653 try_remove_block_from_free_list (MSBlockInfo *block, MSBlockInfo **free_blocks, int size_index)
654 {
655         /*
656          * No more free slots in the block, so try to free the block.
657          * Don't try again if we don't succeed - another thread will
658          * already have done it.
659          */
660         MSBlockInfo *next_block = block->next_free;
661         if (SGEN_CAS_PTR ((void**)&free_blocks [size_index], next_block, block) == block) {
662                 /*
663                 void *old = SGEN_CAS_PTR ((void**)&block->next_free, NULL, next_block);
664                 g_assert (old == next_block);
665                 */
666                 block->next_free = NULL;
667                 return TRUE;
668         }
669         return FALSE;
670 }
671
672 static void*
673 alloc_obj_par (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
674 {
675         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
676         MSBlockInfo **free_blocks_local = FREE_BLOCKS_LOCAL (pinned, has_references);
677         MSBlockInfo *block;
678         void *obj;
679
680 #ifdef SGEN_HAVE_CONCURRENT_MARK
681         if (concurrent_mark)
682                 g_assert_not_reached ();
683 #endif
684
685         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
686
687         if (free_blocks_local [size_index]) {
688         get_slot:
689                 obj = unlink_slot_from_free_list_uncontested (free_blocks_local, size_index);
690         } else {
691                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
692
693         get_block:
694                 block = free_blocks [size_index];
695                 if (block) {
696                         if (!try_remove_block_from_free_list (block, free_blocks, size_index))
697                                 goto get_block;
698
699                         g_assert (block->next_free == NULL);
700                         g_assert (block->free_list);
701                         block->next_free = free_blocks_local [size_index];
702                         free_blocks_local [size_index] = block;
703
704                         goto get_slot;
705                 } else {
706                         gboolean success;
707
708                         LOCK_MS_BLOCK_LIST;
709                         success = ms_alloc_block (size_index, pinned, has_references);
710                         UNLOCK_MS_BLOCK_LIST;
711
712                         if (G_UNLIKELY (!success))
713                                 return NULL;
714
715                         goto get_block;
716                 }
717         }
718
719         *(MonoVTable**)obj = vtable;
720
721         return obj;
722 }
723
724 static void*
725 major_par_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
726 {
727         return alloc_obj_par (vtable, size, FALSE, has_references);
728 }
729 #endif
730
731 static void*
732 alloc_obj (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
733 {
734         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
735         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
736         void *obj;
737
738 #ifdef SGEN_PARALLEL_MARK
739         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
740
741 #endif
742
743         if (!free_blocks [size_index]) {
744                 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
745                         return NULL;
746         }
747
748         obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
749
750         *(MonoVTable**)obj = vtable;
751
752         return obj;
753 }
754
755 static void*
756 major_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
757 {
758         return alloc_obj (vtable, size, FALSE, has_references);
759 }
760
761 /*
762  * We're not freeing the block if it's empty.  We leave that work for
763  * the next major collection.
764  *
765  * This is just called from the domain clearing code, which runs in a
766  * single thread and has the GC lock, so we don't need an extra lock.
767  */
768 static void
769 free_object (char *obj, size_t size, gboolean pinned)
770 {
771         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
772         int word, bit;
773
774         if (!block->swept)
775                 sweep_block (block, FALSE);
776         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);
777         SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
778         MS_CALC_MARK_BIT (word, bit, obj);
779         SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
780         if (!block->free_list) {
781                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
782                 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
783                 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
784                 block->next_free = free_blocks [size_index];
785                 free_blocks [size_index] = block;
786         }
787         memset (obj, 0, size);
788         *(void**)obj = block->free_list;
789         block->free_list = (void**)obj;
790 }
791
792 static void
793 major_free_non_pinned_object (char *obj, size_t size)
794 {
795         free_object (obj, size, FALSE);
796 }
797
798 /* size is a multiple of SGEN_ALLOC_ALIGN */
799 static void*
800 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
801 {
802         void *res;
803
804         res = alloc_obj (vtable, size, TRUE, has_references);
805          /*If we failed to alloc memory, we better try releasing memory
806           *as pinned alloc is requested by the runtime.
807           */
808          if (!res) {
809                 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
810                 res = alloc_obj (vtable, size, TRUE, has_references);
811          }
812          return res;
813 }
814
815 static void
816 free_pinned_object (char *obj, size_t size)
817 {
818         free_object (obj, size, TRUE);
819 }
820
821 /*
822  * size is already rounded up and we hold the GC lock.
823  */
824 static void*
825 major_alloc_degraded (MonoVTable *vtable, size_t size)
826 {
827         void *obj;
828         int old_num_sections;
829
830         old_num_sections = num_major_sections;
831
832         obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
833         if (G_LIKELY (obj)) {
834                 HEAVY_STAT (++stat_objects_alloced_degraded);
835                 HEAVY_STAT (stat_bytes_alloced_degraded += size);
836                 g_assert (num_major_sections >= old_num_sections);
837                 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
838         }
839         return obj;
840 }
841
842 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj)   FALSE
843
844 /*
845  * obj is some object.  If it's not in the major heap (i.e. if it's in
846  * the nursery or LOS), return FALSE.  Otherwise return whether it's
847  * been marked or copied.
848  */
849 static gboolean
850 major_is_object_live (char *obj)
851 {
852         MSBlockInfo *block;
853         int word, bit;
854 #ifndef FIXED_HEAP
855         mword objsize;
856 #endif
857
858         if (sgen_ptr_in_nursery (obj))
859                 return FALSE;
860
861 #ifdef FIXED_HEAP
862         /* LOS */
863         if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
864                 return FALSE;
865 #else
866         objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
867
868         /* LOS */
869         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
870                 return FALSE;
871 #endif
872
873         /* now we know it's in a major block */
874         block = MS_BLOCK_FOR_OBJ (obj);
875         SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
876         MS_CALC_MARK_BIT (word, bit, obj);
877         return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
878 }
879
880 static gboolean
881 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
882 {
883         MSBlockInfo *block;
884
885         FOREACH_BLOCK (block) {
886                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE) {
887                         int count = MS_BLOCK_FREE / block->obj_size;
888                         int i;
889
890                         *start = NULL;
891                         for (i = 0; i <= count; ++i) {
892                                 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
893                                         *start = MS_BLOCK_OBJ (block, i);
894                                         break;
895                                 }
896                         }
897                         return !block->pinned;
898                 }
899         } END_FOREACH_BLOCK;
900         return FALSE;
901 }
902
903 static void
904 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
905 {
906         MSBlockInfo *block;
907
908         FOREACH_BLOCK (block) {
909                 int count = MS_BLOCK_FREE / block->obj_size;
910                 int i;
911
912                 if (block->pinned && !pinned)
913                         continue;
914                 if (!block->pinned && !non_pinned)
915                         continue;
916                 if (lazy_sweep)
917                         sweep_block (block, FALSE);
918
919                 for (i = 0; i < count; ++i) {
920                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
921                         if (MS_OBJ_ALLOCED (obj, block))
922                                 callback ((char*)obj, block->obj_size, data);
923                 }
924         } END_FOREACH_BLOCK;
925 }
926
927 static gboolean
928 major_is_valid_object (char *object)
929 {
930         MSBlockInfo *block;
931
932         FOREACH_BLOCK (block) {
933                 int idx;
934                 char *obj;
935
936                 if ((block->block > object) || ((block->block + MS_BLOCK_SIZE) <= object))
937                         continue;
938
939                 idx = MS_BLOCK_OBJ_INDEX (object, block);
940                 obj = (char*)MS_BLOCK_OBJ (block, idx);
941                 if (obj != object)
942                         return FALSE;
943                 return MS_OBJ_ALLOCED (obj, block);
944         } END_FOREACH_BLOCK;
945
946         return FALSE;
947 }
948
949
950 static gboolean
951 major_describe_pointer (char *ptr)
952 {
953         MSBlockInfo *block;
954
955         FOREACH_BLOCK (block) {
956                 int idx;
957                 char *obj;
958                 gboolean live;
959                 MonoVTable *vtable;
960                 int w, b;
961                 gboolean marked;
962
963                 if ((block->block > ptr) || ((block->block + MS_BLOCK_SIZE) <= ptr))
964                         continue;
965
966                 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
967                         block->block, block->obj_size, block->pinned, block->has_references);
968
969                 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
970                 obj = (char*)MS_BLOCK_OBJ (block, idx);
971                 live = MS_OBJ_ALLOCED (obj, block);
972                 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
973
974                 MS_CALC_MARK_BIT (w, b, obj);
975                 marked = MS_MARK_BIT (block, w, b);
976
977                 if (obj == ptr) {
978                         SGEN_LOG (0, "\t(");
979                         if (live)
980                                 SGEN_LOG (0, "object");
981                         else
982                                 SGEN_LOG (0, "dead-object");
983                 } else {
984                         if (live)
985                                 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
986                         else
987                                 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
988                 }
989
990                 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
991
992                 return TRUE;
993         } END_FOREACH_BLOCK;
994
995         return FALSE;
996 }
997
998 static void
999 major_check_scan_starts (void)
1000 {
1001 }
1002
1003 static void
1004 major_dump_heap (FILE *heap_dump_file)
1005 {
1006         MSBlockInfo *block;
1007         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1008         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1009         int i;
1010
1011         for (i = 0; i < num_block_obj_sizes; ++i)
1012                 slots_available [i] = slots_used [i] = 0;
1013
1014         FOREACH_BLOCK (block) {
1015                 int index = ms_find_block_obj_size_index (block->obj_size);
1016                 int count = MS_BLOCK_FREE / block->obj_size;
1017
1018                 slots_available [index] += count;
1019                 for (i = 0; i < count; ++i) {
1020                         if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
1021                                 ++slots_used [index];
1022                 }
1023         } END_FOREACH_BLOCK;
1024
1025         fprintf (heap_dump_file, "<occupancies>\n");
1026         for (i = 0; i < num_block_obj_sizes; ++i) {
1027                 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
1028                                 block_obj_sizes [i], slots_available [i], slots_used [i]);
1029         }
1030         fprintf (heap_dump_file, "</occupancies>\n");
1031
1032         FOREACH_BLOCK (block) {
1033                 int count = MS_BLOCK_FREE / block->obj_size;
1034                 int i;
1035                 int start = -1;
1036
1037                 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
1038
1039                 for (i = 0; i <= count; ++i) {
1040                         if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
1041                                 if (start < 0)
1042                                         start = i;
1043                         } else {
1044                                 if (start >= 0) {
1045                                         sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
1046                                         start = -1;
1047                                 }
1048                         }
1049                 }
1050
1051                 fprintf (heap_dump_file, "</section>\n");
1052         } END_FOREACH_BLOCK;
1053 }
1054
1055 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
1056
1057 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {        \
1058                 int __word, __bit;                                      \
1059                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1060                 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
1061                         MS_SET_MARK_BIT ((block), __word, __bit);       \
1062                         if ((block)->has_references)                    \
1063                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1064                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1065                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1066                 }                                                       \
1067         } while (0)
1068 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {                \
1069                 int __word, __bit;                                      \
1070                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1071                 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj);       \
1072                 if (!MS_MARK_BIT ((block), __word, __bit)) {            \
1073                         MS_SET_MARK_BIT ((block), __word, __bit);       \
1074                         if ((block)->has_references)                    \
1075                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1076                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1077                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1078                 }                                                       \
1079         } while (0)
1080 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {            \
1081                 int __word, __bit;                                      \
1082                 gboolean __was_marked;                                  \
1083                 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj);       \
1084                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
1085                 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
1086                 if (!__was_marked) {                                    \
1087                         if ((block)->has_references)                    \
1088                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
1089                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1090                         INC_NUM_MAJOR_OBJECTS_MARKED ();                \
1091                 }                                                       \
1092         } while (0)
1093
1094 static void
1095 pin_major_object (char *obj, SgenGrayQueue *queue)
1096 {
1097         MSBlockInfo *block;
1098
1099 #ifdef SGEN_HAVE_CONCURRENT_MARK
1100         if (concurrent_mark)
1101                 g_assert_not_reached ();
1102 #endif
1103
1104         block = MS_BLOCK_FOR_OBJ (obj);
1105         block->has_pinned = TRUE;
1106         MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1107 }
1108
1109 #include "sgen-major-copy-object.h"
1110
1111 #ifdef SGEN_PARALLEL_MARK
1112 static void
1113 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
1114 {
1115         mword objsize;
1116         MSBlockInfo *block;
1117         MonoVTable *vt;
1118
1119         HEAVY_STAT (++stat_copy_object_called_major);
1120
1121         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
1122         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
1123
1124         if (sgen_ptr_in_nursery (obj)) {
1125                 int word, bit;
1126                 gboolean has_references;
1127                 void *destination;
1128                 mword vtable_word = *(mword*)obj;
1129                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1130
1131                 if (vtable_word & SGEN_FORWARDED_BIT) {
1132                         *ptr = (void*)vt;
1133                         return;
1134                 }
1135
1136                 if (vtable_word & SGEN_PINNED_BIT)
1137                         return;
1138
1139                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1140                 if (sgen_nursery_is_to_space (obj))
1141                         return;
1142
1143                 HEAVY_STAT (++stat_objects_copied_major);
1144
1145         do_copy_object:
1146                 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1147                 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
1148
1149                 destination = sgen_minor_collector.par_alloc_for_promotion (vt, obj, objsize, has_references);
1150                 if (G_UNLIKELY (!destination)) {
1151                         if (!sgen_ptr_in_nursery (obj)) {
1152                                 int size_index;
1153                                 block = MS_BLOCK_FOR_OBJ (obj);
1154                                 size_index = block->obj_size_index;
1155                                 evacuate_block_obj_sizes [size_index] = FALSE;
1156                         }
1157
1158                         sgen_parallel_pin_or_update (ptr, obj, vt, queue);
1159                         sgen_set_pinned_from_failed_allocation (objsize);
1160                         return;
1161                 }
1162
1163                 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
1164                         gboolean was_marked;
1165
1166                         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
1167                         obj = destination;
1168                         *ptr = obj;
1169
1170                         /*
1171                          * FIXME: If we make major_alloc_object() give
1172                          * us the block info, too, we won't have to
1173                          * re-fetch it here.
1174                          *
1175                          * FIXME (2): We should rework this to avoid all those nursery checks.
1176                          */
1177                         /*
1178                          * For the split nursery allocator the object
1179                          * might still be in the nursery despite
1180                          * having being promoted, in which case we
1181                          * can't mark it.
1182                          */
1183                         if (!sgen_ptr_in_nursery (obj)) {
1184                                 block = MS_BLOCK_FOR_OBJ (obj);
1185                                 MS_CALC_MARK_BIT (word, bit, obj);
1186                                 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
1187                                 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
1188                         }
1189                 } else {
1190                         /*
1191                          * FIXME: We have allocated destination, but
1192                          * we cannot use it.  Give it back to the
1193                          * allocator.
1194                          */
1195                         *(void**)destination = NULL;
1196
1197                         vtable_word = *(mword*)obj;
1198                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
1199
1200                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1201
1202                         *ptr = obj;
1203
1204                         HEAVY_STAT (++stat_slots_allocated_in_vain);
1205                 }
1206         } else {
1207 #ifdef FIXED_HEAP
1208                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1209 #else
1210                 mword vtable_word = *(mword*)obj;
1211                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1212
1213                 /* see comment in the non-parallel version below */
1214                 if (vtable_word & SGEN_FORWARDED_BIT) {
1215                         *ptr = (void*)vt;
1216                         return;
1217                 }
1218                 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1219
1220                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1221 #endif
1222                 {
1223                         int size_index;
1224
1225                         block = MS_BLOCK_FOR_OBJ (obj);
1226                         size_index = block->obj_size_index;
1227
1228                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1229                                 if (block->is_to_space)
1230                                         return;
1231
1232 #ifdef FIXED_HEAP
1233                                 {
1234                                         mword vtable_word = *(mword*)obj;
1235                                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1236
1237                                         if (vtable_word & SGEN_FORWARDED_BIT) {
1238                                                 *ptr = (void*)vt;
1239                                                 return;
1240                                         }
1241                                 }
1242 #endif
1243
1244                                 HEAVY_STAT (++stat_major_objects_evacuated);
1245                                 goto do_copy_object;
1246                         }
1247
1248                         MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1249                 } else {
1250                         LOSObject *bigobj = sgen_los_header_for_object (obj);
1251                         mword size_word = bigobj->size;
1252 #ifdef FIXED_HEAP
1253                         mword vtable_word = *(mword*)obj;
1254                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1255 #endif
1256                         if (size_word & 1)
1257                                 return;
1258                         binary_protocol_pin (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1259                         if (SGEN_CAS_PTR ((void*)&bigobj->size, (void*)(size_word | 1), (void*)size_word) == (void*)size_word) {
1260                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1261                                         GRAY_OBJECT_ENQUEUE (queue, obj);
1262                         } else {
1263                                 g_assert (sgen_los_object_is_pinned (obj));
1264                         }
1265                 }
1266         }
1267 }
1268 #else
1269 #ifdef SGEN_HAVE_CONCURRENT_MARK
1270 static void
1271 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
1272 {
1273         g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
1274
1275         if (!sgen_ptr_in_nursery (obj)) {
1276 #ifdef FIXED_HEAP
1277                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1278 #else
1279                 mword objsize;
1280
1281                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1282
1283                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1284 #endif
1285                 {
1286                         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1287                         MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1288                 } else {
1289                         if (sgen_los_object_is_pinned (obj))
1290                                 return;
1291
1292 #ifdef ENABLE_DTRACE
1293                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1294                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1295                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1296                         }
1297 #endif
1298
1299                         sgen_los_pin_object (obj);
1300                         /* FIXME: only enqueue if object has references */
1301                         GRAY_OBJECT_ENQUEUE (queue, obj);
1302                         INC_NUM_MAJOR_OBJECTS_MARKED ();
1303                 }
1304         }
1305 }
1306 #endif
1307
1308 static void
1309 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
1310 {
1311         MSBlockInfo *block;
1312
1313         HEAVY_STAT (++stat_copy_object_called_major);
1314
1315         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
1316         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
1317
1318         if (sgen_ptr_in_nursery (obj)) {
1319                 int word, bit;
1320                 char *forwarded, *old_obj;
1321
1322                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1323                         *ptr = forwarded;
1324                         return;
1325                 }
1326                 if (SGEN_OBJECT_IS_PINNED (obj))
1327                         return;
1328
1329                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1330                 if (sgen_nursery_is_to_space (obj))
1331                         return;
1332
1333                 HEAVY_STAT (++stat_objects_copied_major);
1334
1335         do_copy_object:
1336                 old_obj = obj;
1337                 obj = copy_object_no_checks (obj, queue);
1338                 if (G_UNLIKELY (old_obj == obj)) {
1339                         /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1340                         if (!sgen_ptr_in_nursery (obj)) {
1341                                 int size_index;
1342                                 block = MS_BLOCK_FOR_OBJ (obj);
1343                                 size_index = block->obj_size_index;
1344                                 evacuate_block_obj_sizes [size_index] = FALSE;
1345                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1346                         }
1347                         return;
1348                 }
1349                 *ptr = obj;
1350
1351                 /*
1352                  * FIXME: See comment for copy_object_no_checks().  If
1353                  * we have that, we can let the allocation function
1354                  * give us the block info, too, and we won't have to
1355                  * re-fetch it.
1356                  *
1357                  * FIXME (2): We should rework this to avoid all those nursery checks.
1358                  */
1359                 /*
1360                  * For the split nursery allocator the object might
1361                  * still be in the nursery despite having being
1362                  * promoted, in which case we can't mark it.
1363                  */
1364                 if (!sgen_ptr_in_nursery (obj)) {
1365                         block = MS_BLOCK_FOR_OBJ (obj);
1366                         MS_CALC_MARK_BIT (word, bit, obj);
1367                         SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
1368                         MS_SET_MARK_BIT (block, word, bit);
1369                         binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1370                 }
1371         } else {
1372                 char *forwarded;
1373 #ifdef FIXED_HEAP
1374                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1375 #else
1376                 mword objsize;
1377
1378                 /*
1379                  * If we have don't have a fixed heap we cannot know
1380                  * whether an object is in the LOS or in the small
1381                  * object major heap without checking its size.  To do
1382                  * that, however, we need to know that we actually
1383                  * have a valid object, not a forwarding pointer, so
1384                  * we have to do this check first.
1385                  */
1386                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1387                         *ptr = forwarded;
1388                         return;
1389                 }
1390
1391                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1392
1393                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1394 #endif
1395                 {
1396                         int size_index;
1397                         gboolean evacuate;
1398
1399                         block = MS_BLOCK_FOR_OBJ (obj);
1400                         size_index = block->obj_size_index;
1401                         evacuate = evacuate_block_obj_sizes [size_index];
1402
1403 #ifdef FIXED_HEAP
1404                         /*
1405                          * We could also check for !block->has_pinned
1406                          * here, but it would only make an uncommon case
1407                          * faster, namely objects that are in blocks
1408                          * whose slot sizes are evacuated but which have
1409                          * pinned objects.
1410                          */
1411                         if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1412                                 *ptr = forwarded;
1413                                 return;
1414                         }
1415 #endif
1416
1417                         if (evacuate && !block->has_pinned) {
1418                                 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1419                                 if (block->is_to_space)
1420                                         return;
1421                                 HEAVY_STAT (++stat_major_objects_evacuated);
1422                                 goto do_copy_object;
1423                         } else {
1424                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1425                         }
1426                 } else {
1427                         if (sgen_los_object_is_pinned (obj))
1428                                 return;
1429                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1430
1431 #ifdef ENABLE_DTRACE
1432                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1433                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1434                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1435                         }
1436 #endif
1437
1438                         sgen_los_pin_object (obj);
1439                         /* FIXME: only enqueue if object has references */
1440                         GRAY_OBJECT_ENQUEUE (queue, obj);
1441                 }
1442         }
1443 }
1444 #endif
1445
1446 static void
1447 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1448 {
1449         major_copy_or_mark_object (ptr, *ptr, queue);
1450 }
1451
1452 #ifdef SGEN_HAVE_CONCURRENT_MARK
1453 static void
1454 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1455 {
1456         major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1457 }
1458
1459 static long long
1460 major_get_and_reset_num_major_objects_marked (void)
1461 {
1462 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1463         long long num = num_major_objects_marked;
1464         num_major_objects_marked = 0;
1465         return num;
1466 #else
1467         return 0;
1468 #endif
1469 }
1470 #endif
1471
1472 #include "sgen-major-scan-object.h"
1473
1474 #ifdef SGEN_HAVE_CONCURRENT_MARK
1475 #define SCAN_FOR_CONCURRENT_MARK
1476 #include "sgen-major-scan-object.h"
1477 #undef SCAN_FOR_CONCURRENT_MARK
1478 #endif
1479
1480 static void
1481 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1482 {
1483         int i;
1484         int last_index = -1;
1485
1486         if (!block->pin_queue_num_entries)
1487                 return;
1488
1489         block->has_pinned = TRUE;
1490
1491         for (i = 0; i < block->pin_queue_num_entries; ++i) {
1492                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1493                 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);
1494                 if (index == last_index)
1495                         continue;
1496                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1497                 last_index = index;
1498         }
1499 }
1500
1501 static inline void
1502 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1503 {
1504         int obj_index;
1505
1506         for (obj_index = 0; obj_index < count; ++obj_index) {
1507                 int word, bit;
1508                 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1509
1510                 MS_CALC_MARK_BIT (word, bit, obj);
1511                 if (MS_MARK_BIT (block, word, bit)) {
1512                         SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1513                 } else {
1514                         /* an unmarked object */
1515                         if (MS_OBJ_ALLOCED (obj, block)) {
1516                                 /*
1517                                  * FIXME: Merge consecutive
1518                                  * slots for lower reporting
1519                                  * overhead.  Maybe memset
1520                                  * will also benefit?
1521                                  */
1522                                 binary_protocol_empty (obj, obj_size);
1523                                 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1524                                 memset (obj, 0, obj_size);
1525                         }
1526                         *(void**)obj = block->free_list;
1527                         block->free_list = obj;
1528                 }
1529         }
1530 }
1531
1532 /*
1533  * sweep_block:
1534  *
1535  *   Traverse BLOCK, freeing and zeroing unused objects.
1536  */
1537 static void
1538 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1539 {
1540         int count;
1541
1542         if (!during_major_collection)
1543                 g_assert (!sgen_concurrent_collection_in_progress ());
1544
1545         if (block->swept)
1546                 return;
1547
1548         count = MS_BLOCK_FREE / block->obj_size;
1549
1550         block->free_list = NULL;
1551
1552         /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1553         // FIXME: Add more sizes
1554         switch (block->obj_size) {
1555         case 16:
1556                 sweep_block_for_size (block, count, 16);
1557                 break;
1558         default:
1559                 sweep_block_for_size (block, count, block->obj_size);
1560                 break;
1561         }
1562
1563         /* reset mark bits */
1564         memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1565
1566         /*
1567          * FIXME: reverse free list so that it's in address
1568          * order
1569          */
1570
1571         block->swept = 1;
1572 }
1573
1574 static inline int
1575 bitcount (mword d)
1576 {
1577 #if SIZEOF_VOID_P == 8
1578         /* http://www.jjj.de/bitwizardry/bitwizardrypage.html */
1579         d -=  (d>>1) & 0x5555555555555555;
1580         d  = ((d>>2) & 0x3333333333333333) + (d & 0x3333333333333333);
1581         d  = ((d>>4) + d) & 0x0f0f0f0f0f0f0f0f;
1582         d *= 0x0101010101010101;
1583         return d >> 56;
1584 #else
1585         /* http://aggregate.org/MAGIC/ */
1586         d -= ((d >> 1) & 0x55555555);
1587         d = (((d >> 2) & 0x33333333) + (d & 0x33333333));
1588         d = (((d >> 4) + d) & 0x0f0f0f0f);
1589         d += (d >> 8);
1590         d += (d >> 16);
1591         return (d & 0x0000003f);
1592 #endif
1593 }
1594
1595 static void
1596 ms_sweep (void)
1597 {
1598         int i;
1599         MSBlockInfo **iter;
1600
1601         /* statistics for evacuation */
1602         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1603         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1604         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1605
1606 #ifdef SGEN_HAVE_CONCURRENT_MARK
1607         mword total_evacuate_heap = 0;
1608         mword total_evacuate_saved = 0;
1609 #endif
1610
1611         for (i = 0; i < num_block_obj_sizes; ++i)
1612                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1613
1614         /* clear all the free lists */
1615         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1616                 MSBlockInfo **free_blocks = free_block_lists [i];
1617                 int j;
1618                 for (j = 0; j < num_block_obj_sizes; ++j)
1619                         free_blocks [j] = NULL;
1620         }
1621
1622         /* traverse all blocks, free and zero unmarked objects */
1623         iter = &all_blocks;
1624         while (*iter) {
1625                 MSBlockInfo *block = *iter;
1626                 int count;
1627                 gboolean have_live = FALSE;
1628                 gboolean has_pinned;
1629                 gboolean have_free = FALSE;
1630                 int obj_size_index;
1631                 int nused = 0;
1632
1633                 obj_size_index = block->obj_size_index;
1634
1635                 has_pinned = block->has_pinned;
1636                 block->has_pinned = block->pinned;
1637
1638                 block->is_to_space = FALSE;
1639                 block->swept = 0;
1640
1641                 count = MS_BLOCK_FREE / block->obj_size;
1642
1643 #ifdef SGEN_HAVE_CONCURRENT_MARK
1644                 if (block->cardtable_mod_union) {
1645                         sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1646                         block->cardtable_mod_union = NULL;
1647                 }
1648 #endif
1649
1650                 /* Count marked objects in the block */
1651                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1652                         nused += bitcount (block->mark_words [i]);
1653                 }
1654                 if (nused) {
1655                         have_live = TRUE;
1656                 }
1657                 if (nused < count)
1658                         have_free = TRUE;
1659
1660                 if (!lazy_sweep)
1661                         sweep_block (block, TRUE);
1662
1663                 if (have_live) {
1664                         if (!has_pinned) {
1665                                 ++num_blocks [obj_size_index];
1666                                 slots_used [obj_size_index] += nused;
1667                                 slots_available [obj_size_index] += count;
1668                         }
1669
1670                         iter = &block->next;
1671
1672                         /*
1673                          * If there are free slots in the block, add
1674                          * the block to the corresponding free list.
1675                          */
1676                         if (have_free) {
1677                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1678                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1679                                 block->next_free = free_blocks [index];
1680                                 free_blocks [index] = block;
1681                         }
1682
1683                         update_heap_boundaries_for_block (block);
1684                 } else {
1685                         /*
1686                          * Blocks without live objects are removed from the
1687                          * block list and freed.
1688                          */
1689                         *iter = block->next;
1690
1691 #ifdef FIXED_HEAP
1692                         ms_free_block (block);
1693 #else
1694                         ms_free_block (block->block);
1695
1696                         sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1697 #endif
1698
1699                         --num_major_sections;
1700                 }
1701         }
1702
1703         for (i = 0; i < num_block_obj_sizes; ++i) {
1704                 float usage = (float)slots_used [i] / (float)slots_available [i];
1705                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1706                         evacuate_block_obj_sizes [i] = TRUE;
1707                         /*
1708                         g_print ("slot size %d - %d of %d used\n",
1709                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1710                         */
1711                 } else {
1712                         evacuate_block_obj_sizes [i] = FALSE;
1713                 }
1714 #ifdef SGEN_HAVE_CONCURRENT_MARK
1715                 {
1716                         mword total_bytes = block_obj_sizes [i] * slots_available [i];
1717                         total_evacuate_heap += total_bytes;
1718                         if (evacuate_block_obj_sizes [i])
1719                                 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1720                 }
1721 #endif
1722         }
1723
1724 #ifdef SGEN_HAVE_CONCURRENT_MARK
1725         want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1726 #endif
1727
1728         have_swept = TRUE;
1729 }
1730
1731 static void
1732 major_sweep (void)
1733 {
1734         ms_sweep ();
1735 }
1736
1737 static int count_pinned_ref;
1738 static int count_pinned_nonref;
1739 static int count_nonpinned_ref;
1740 static int count_nonpinned_nonref;
1741
1742 static void
1743 count_nonpinned_callback (char *obj, size_t size, void *data)
1744 {
1745         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1746
1747         if (vtable->klass->has_references)
1748                 ++count_nonpinned_ref;
1749         else
1750                 ++count_nonpinned_nonref;
1751 }
1752
1753 static void
1754 count_pinned_callback (char *obj, size_t size, void *data)
1755 {
1756         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1757
1758         if (vtable->klass->has_references)
1759                 ++count_pinned_ref;
1760         else
1761                 ++count_pinned_nonref;
1762 }
1763
1764 static G_GNUC_UNUSED void
1765 count_ref_nonref_objs (void)
1766 {
1767         int total;
1768
1769         count_pinned_ref = 0;
1770         count_pinned_nonref = 0;
1771         count_nonpinned_ref = 0;
1772         count_nonpinned_nonref = 0;
1773
1774         major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1775         major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1776
1777         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1778
1779         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1780                         count_pinned_ref, count_nonpinned_ref,
1781                         count_pinned_nonref, count_nonpinned_nonref,
1782                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1783 }
1784
1785 static int
1786 ms_calculate_block_obj_sizes (double factor, int *arr)
1787 {
1788         double target_size = sizeof (MonoObject);
1789         int num_sizes = 0;
1790         int last_size = 0;
1791
1792         do {
1793                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1794                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1795
1796                 if (size != last_size) {
1797                         if (arr)
1798                                 arr [num_sizes] = size;
1799                         ++num_sizes;
1800                         last_size = size;
1801                 }
1802
1803                 target_size *= factor;
1804         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1805
1806         return num_sizes;
1807 }
1808
1809 /* only valid during minor collections */
1810 static int old_num_major_sections;
1811
1812 static void
1813 major_start_nursery_collection (void)
1814 {
1815 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1816         consistency_check ();
1817 #endif
1818
1819         old_num_major_sections = num_major_sections;
1820 }
1821
1822 static void
1823 major_finish_nursery_collection (void)
1824 {
1825 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1826         consistency_check ();
1827 #endif
1828         sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1829 }
1830
1831 static void
1832 major_start_major_collection (void)
1833 {
1834         int i;
1835
1836         /* clear the free lists */
1837         for (i = 0; i < num_block_obj_sizes; ++i) {
1838                 if (!evacuate_block_obj_sizes [i])
1839                         continue;
1840
1841                 free_block_lists [0][i] = NULL;
1842                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1843         }
1844
1845         // Sweep all unswept blocks
1846         if (lazy_sweep) {
1847                 MSBlockInfo **iter;
1848
1849                 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1850
1851                 iter = &all_blocks;
1852                 while (*iter) {
1853                         MSBlockInfo *block = *iter;
1854
1855                         sweep_block (block, TRUE);
1856
1857                         iter = &block->next;
1858                 }
1859
1860                 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1861         }
1862 }
1863
1864 static void
1865 major_finish_major_collection (void)
1866 {
1867 }
1868
1869 #if !defined(FIXED_HEAP) && SIZEOF_VOID_P != 8
1870 static int
1871 compare_pointers (const void *va, const void *vb) {
1872         char *a = *(char**)va, *b = *(char**)vb;
1873         if (a < b)
1874                 return -1;
1875         if (a > b)
1876                 return 1;
1877         return 0;
1878 }
1879 #endif
1880
1881 static void
1882 major_have_computer_minor_collection_allowance (void)
1883 {
1884 #ifndef FIXED_HEAP
1885         int section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1886
1887         g_assert (have_swept);
1888
1889 #if SIZEOF_VOID_P != 8
1890         {
1891                 int i, num_empty_blocks_orig, num_blocks, arr_length;
1892                 void *block;
1893                 void **empty_block_arr;
1894                 void **rebuild_next;
1895
1896                 if (num_empty_blocks <= section_reserve)
1897                         return;
1898                 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1899
1900                 num_empty_blocks_orig = num_empty_blocks;
1901                 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1902                                 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1903                 if (!empty_block_arr)
1904                         goto fallback;
1905
1906                 i = 0;
1907                 for (block = empty_blocks; block; block = *(void**)block)
1908                         empty_block_arr [i++] = block;
1909                 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1910
1911                 qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1912
1913                 /*
1914                  * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1915                  * contiguous ones.  If we do, we free them.  If that's not enough to get to
1916                  * section_reserve, we halve the number of contiguous blocks we're looking
1917                  * for and have another go, until we're done with looking for pairs of
1918                  * blocks, at which point we give up and go to the fallback.
1919                  */
1920                 arr_length = num_empty_blocks_orig;
1921                 num_blocks = MS_BLOCK_ALLOC_NUM;
1922                 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1923                         int first = -1;
1924                         int dest = 0;
1925
1926                         dest = 0;
1927                         for (i = 0; i < arr_length; ++i) {
1928                                 int d = dest;
1929                                 void *block = empty_block_arr [i];
1930                                 SGEN_ASSERT (0, block, "we're not shifting correctly");
1931                                 if (i != dest) {
1932                                         empty_block_arr [dest] = block;
1933                                         /*
1934                                          * This is not strictly necessary, but we're
1935                                          * cautious.
1936                                          */
1937                                         empty_block_arr [i] = NULL;
1938                                 }
1939                                 ++dest;
1940
1941                                 if (first < 0) {
1942                                         first = d;
1943                                         continue;
1944                                 }
1945
1946                                 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1947
1948                                 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1949                                         first = d;
1950                                         continue;
1951                                 }
1952
1953                                 if (d + 1 - first == num_blocks) {
1954                                         /*
1955                                          * We found num_blocks contiguous blocks.  Free them
1956                                          * and null their array entries.  As an optimization
1957                                          * we could, instead of nulling the entries, shift
1958                                          * the following entries over to the left, while
1959                                          * we're iterating.
1960                                          */
1961                                         int j;
1962                                         sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1963                                         for (j = first; j <= d; ++j)
1964                                                 empty_block_arr [j] = NULL;
1965                                         dest = first;
1966                                         first = -1;
1967
1968                                         num_empty_blocks -= num_blocks;
1969
1970                                         stat_major_blocks_freed += num_blocks;
1971                                         if (num_blocks == MS_BLOCK_ALLOC_NUM)
1972                                                 stat_major_blocks_freed_ideal += num_blocks;
1973                                         else
1974                                                 stat_major_blocks_freed_less_ideal += num_blocks;
1975
1976                                 }
1977                         }
1978
1979                         SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1980                         arr_length = dest;
1981                         SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1982
1983                         num_blocks >>= 1;
1984                 }
1985
1986                 /* rebuild empty_blocks free list */
1987                 rebuild_next = (void**)&empty_blocks;
1988                 for (i = 0; i < arr_length; ++i) {
1989                         void *block = empty_block_arr [i];
1990                         SGEN_ASSERT (0, block, "we're missing blocks");
1991                         *rebuild_next = block;
1992                         rebuild_next = (void**)block;
1993                 }
1994                 *rebuild_next = NULL;
1995
1996                 /* free array */
1997                 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1998         }
1999
2000         SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
2001
2002  fallback:
2003         /*
2004          * This is our threshold.  If there's not more empty than used blocks, we won't
2005          * release uncontiguous blocks, in fear of fragmenting the address space.
2006          */
2007         if (num_empty_blocks <= num_major_sections)
2008                 return;
2009 #endif
2010
2011         while (num_empty_blocks > section_reserve) {
2012                 void *next = *(void**)empty_blocks;
2013                 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
2014                 empty_blocks = next;
2015                 /*
2016                  * Needs not be atomic because this is running
2017                  * single-threaded.
2018                  */
2019                 --num_empty_blocks;
2020
2021                 ++stat_major_blocks_freed;
2022 #if SIZEOF_VOID_P != 8
2023                 ++stat_major_blocks_freed_individual;
2024 #endif
2025         }
2026 #endif
2027 }
2028
2029 static void
2030 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
2031 {
2032         MSBlockInfo *block;
2033
2034         FOREACH_BLOCK (block) {
2035                 block->pin_queue_start = sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
2036                                 &block->pin_queue_num_entries);
2037         } END_FOREACH_BLOCK;
2038 }
2039
2040 static void
2041 major_pin_objects (SgenGrayQueue *queue)
2042 {
2043         MSBlockInfo *block;
2044
2045         FOREACH_BLOCK (block) {
2046                 mark_pinned_objects_in_block (block, queue);
2047         } END_FOREACH_BLOCK;
2048 }
2049
2050 static void
2051 major_init_to_space (void)
2052 {
2053 }
2054
2055 static void
2056 major_report_pinned_memory_usage (void)
2057 {
2058         g_assert_not_reached ();
2059 }
2060
2061 static gint64
2062 major_get_used_size (void)
2063 {
2064         gint64 size = 0;
2065         MSBlockInfo *block;
2066
2067         FOREACH_BLOCK (block) {
2068                 int count = MS_BLOCK_FREE / block->obj_size;
2069                 void **iter;
2070                 size += count * block->obj_size;
2071                 for (iter = block->free_list; iter; iter = (void**)*iter)
2072                         size -= block->obj_size;
2073         } END_FOREACH_BLOCK;
2074
2075         return size;
2076 }
2077
2078 static int
2079 get_num_major_sections (void)
2080 {
2081         return num_major_sections;
2082 }
2083
2084 static gboolean
2085 major_handle_gc_param (const char *opt)
2086 {
2087 #ifdef FIXED_HEAP
2088         if (g_str_has_prefix (opt, "major-heap-size=")) {
2089                 const char *arg = strchr (opt, '=') + 1;
2090                 glong size;
2091                 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
2092                         return FALSE;
2093                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
2094                 g_assert (ms_heap_num_blocks > 0);
2095                 return TRUE;
2096         } else
2097 #endif
2098         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
2099                 const char *arg = strchr (opt, '=') + 1;
2100                 int percentage = atoi (arg);
2101                 if (percentage < 0 || percentage > 100) {
2102                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
2103                         exit (1);
2104                 }
2105                 evacuation_threshold = (float)percentage / 100.0;
2106                 return TRUE;
2107         } else if (!strcmp (opt, "lazy-sweep")) {
2108                 lazy_sweep = TRUE;
2109                 return TRUE;
2110         } else if (!strcmp (opt, "no-lazy-sweep")) {
2111                 lazy_sweep = FALSE;
2112                 return TRUE;
2113         }
2114
2115         return FALSE;
2116 }
2117
2118 static void
2119 major_print_gc_param_usage (void)
2120 {
2121         fprintf (stderr,
2122                         ""
2123 #ifdef FIXED_HEAP
2124                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
2125 #endif
2126                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
2127                         "  (no-)lazy-sweep\n"
2128                         );
2129 }
2130
2131 static void
2132 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
2133 {
2134         MSBlockInfo *block;
2135
2136         FOREACH_BLOCK (block) {
2137                 if (block->has_references)
2138                         callback ((mword)block->block, MS_BLOCK_SIZE);
2139         } END_FOREACH_BLOCK;
2140 }
2141
2142 #ifdef HEAVY_STATISTICS
2143 extern long long marked_cards;
2144 extern long long scanned_cards;
2145 extern long long scanned_objects;
2146 extern long long remarked_cards;
2147 #endif
2148
2149 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
2150 /*
2151  * MS blocks are 16K aligned.
2152  * Cardtables are 4K aligned, at least.
2153  * This means that the cardtable of a given block is 32 bytes aligned.
2154  */
2155 static guint8*
2156 initial_skip_card (guint8 *card_data)
2157 {
2158         mword *cards = (mword*)card_data;
2159         mword card;
2160         int i;
2161         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
2162                 card = cards [i];
2163                 if (card)
2164                         break;
2165         }
2166
2167         if (i == CARD_WORDS_PER_BLOCK)
2168                 return card_data + CARDS_PER_BLOCK;
2169
2170 #if defined(__i386__) && defined(__GNUC__)
2171         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
2172 #elif defined(__x86_64__) && defined(__GNUC__)
2173         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
2174 #elif defined(__s390x__) && defined(__GNUC__)
2175         return card_data + i * 8 +  (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
2176 #else
2177         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
2178                 if (card_data [i])
2179                         return &card_data [i];
2180         }
2181         return card_data;
2182 #endif
2183 }
2184
2185
2186 static G_GNUC_UNUSED guint8*
2187 skip_card (guint8 *card_data, guint8 *card_data_end)
2188 {
2189         while (card_data < card_data_end && !*card_data)
2190                 ++card_data;
2191         return card_data;
2192 }
2193
2194 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
2195 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
2196 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
2197
2198 static void
2199 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
2200 {
2201         MSBlockInfo *block;
2202         ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
2203
2204 #ifdef SGEN_HAVE_CONCURRENT_MARK
2205         if (!concurrent_mark)
2206                 g_assert (!mod_union);
2207 #else
2208         g_assert (!mod_union);
2209 #endif
2210
2211         FOREACH_BLOCK (block) {
2212                 int block_obj_size;
2213                 char *block_start;
2214
2215                 if (!block->has_references)
2216                         continue;
2217
2218                 block_obj_size = block->obj_size;
2219                 block_start = block->block;
2220
2221                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
2222                         guint8 *cards;
2223 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
2224                         guint8 cards_data [CARDS_PER_BLOCK];
2225 #endif
2226                         char *obj, *end, *base;
2227
2228                         if (mod_union) {
2229 #ifdef SGEN_HAVE_CONCURRENT_MARK
2230                                 cards = block->cardtable_mod_union;
2231                                 /*
2232                                  * This happens when the nursery
2233                                  * collection that precedes finishing
2234                                  * the concurrent collection allocates
2235                                  * new major blocks.
2236                                  */
2237                                 if (!cards)
2238                                         continue;
2239 #endif
2240                         } else {
2241                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
2242 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
2243                                 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
2244 #else
2245                                 cards = cards_data;
2246                                 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
2247                                         continue;
2248 #endif
2249                         }
2250
2251                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
2252                         end = block_start + MS_BLOCK_SIZE;
2253                         base = sgen_card_table_align_pointer (obj);
2254
2255                         while (obj < end) {
2256                                 int card_offset;
2257
2258                                 if (!block->swept)
2259                                         sweep_block (block, FALSE);
2260
2261                                 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2262                                         goto next_large;
2263
2264                                 if (mod_union) {
2265                                         /* FIXME: do this more efficiently */
2266                                         int w, b;
2267                                         MS_CALC_MARK_BIT (w, b, obj);
2268                                         if (!MS_MARK_BIT (block, w, b))
2269                                                 goto next_large;
2270                                 }
2271
2272                                 card_offset = (obj - base) >> CARD_BITS;
2273                                 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
2274
2275                         next_large:
2276                                 obj += block_obj_size;
2277                         }
2278                 } else {
2279                         guint8 *card_data, *card_base;
2280                         guint8 *card_data_end;
2281
2282                         /*
2283                          * This is safe in face of card aliasing for the following reason:
2284                          *
2285                          * Major blocks are 16k aligned, or 32 cards aligned.
2286                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
2287                          * sizes, they won't overflow the cardtable overlap modulus.
2288                          */
2289                         if (mod_union) {
2290 #ifdef SGEN_HAVE_CONCURRENT_MARK
2291                                 card_data = card_base = block->cardtable_mod_union;
2292                                 /*
2293                                  * This happens when the nursery
2294                                  * collection that precedes finishing
2295                                  * the concurrent collection allocates
2296                                  * new major blocks.
2297                                  */
2298                                 if (!card_data)
2299                                         continue;
2300 #endif
2301                         } else {
2302                                 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
2303                         }
2304                         card_data_end = card_data + CARDS_PER_BLOCK;
2305
2306                         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)) {
2307                                 int index;
2308                                 int idx = card_data - card_base;
2309                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
2310                                 char *end = start + CARD_SIZE_IN_BYTES;
2311                                 char *first_obj, *obj;
2312
2313                                 HEAVY_STAT (++scanned_cards);
2314
2315                                 if (!*card_data)
2316                                         continue;
2317
2318                                 if (!block->swept)
2319                                         sweep_block (block, FALSE);
2320
2321                                 HEAVY_STAT (++marked_cards);
2322
2323                                 sgen_card_table_prepare_card_for_scanning (card_data);
2324
2325                                 if (idx == 0)
2326                                         index = 0;
2327                                 else
2328                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
2329
2330                                 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
2331                                 while (obj < end) {
2332                                         if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2333                                                 goto next_small;
2334
2335                                         if (mod_union) {
2336                                                 /* FIXME: do this more efficiently */
2337                                                 int w, b;
2338                                                 MS_CALC_MARK_BIT (w, b, obj);
2339                                                 if (!MS_MARK_BIT (block, w, b))
2340                                                         goto next_small;
2341                                         }
2342
2343                                         HEAVY_STAT (++scanned_objects);
2344                                         scan_func (obj, queue);
2345                                 next_small:
2346                                         obj += block_obj_size;
2347                                 }
2348                                 HEAVY_STAT (if (*card_data) ++remarked_cards);
2349                                 binary_protocol_card_scan (first_obj, obj - first_obj);
2350                         }
2351                 }
2352         } END_FOREACH_BLOCK;
2353 }
2354
2355 #ifdef SGEN_HAVE_CONCURRENT_MARK
2356 static void
2357 update_cardtable_mod_union (void)
2358 {
2359         MSBlockInfo *block;
2360
2361         FOREACH_BLOCK (block) {
2362                 size_t num_cards;
2363
2364                 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
2365                                 block->block, MS_BLOCK_SIZE, &num_cards);
2366
2367                 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
2368         } END_FOREACH_BLOCK;
2369 }
2370
2371 static guint8*
2372 major_get_cardtable_mod_union_for_object (char *obj)
2373 {
2374         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
2375         return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (block->block)) >> CARD_BITS];
2376 }
2377 #endif
2378
2379 static void
2380 alloc_free_block_lists (MSBlockInfo ***lists)
2381 {
2382         int i;
2383         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2384                 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2385 }
2386
2387 #ifdef SGEN_PARALLEL_MARK
2388 static void*
2389 major_alloc_worker_data (void)
2390 {
2391         /* FIXME: free this when the workers come down */
2392         MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
2393         alloc_free_block_lists (lists);
2394         return lists;
2395 }
2396
2397 static void
2398 major_init_worker_thread (void *data)
2399 {
2400         MSBlockInfo ***lists = data;
2401         int i;
2402
2403         g_assert (lists && lists != free_block_lists);
2404         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2405                 int j;
2406                 for (j = 0; j < num_block_obj_sizes; ++j)
2407                         g_assert (!lists [i][j]);
2408         }
2409
2410 #ifdef HAVE_KW_THREAD
2411         workers_free_block_lists = data;
2412 #else
2413         mono_native_tls_set_value (workers_free_block_lists_key, data);
2414 #endif
2415 }
2416
2417 static void
2418 major_reset_worker_data (void *data)
2419 {
2420         MSBlockInfo ***lists = data;
2421         int i;
2422         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2423                 int j;
2424                 for (j = 0; j < num_block_obj_sizes; ++j)
2425                         lists [i][j] = NULL;
2426         }
2427 }
2428 #endif
2429
2430 #undef pthread_create
2431
2432 static void
2433 post_param_init (SgenMajorCollector *collector)
2434 {
2435         collector->sweeps_lazily = lazy_sweep;
2436 }
2437
2438 #ifdef SGEN_HAVE_CONCURRENT_MARK
2439 static void
2440 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2441 #else // SGEN_HAVE_CONCURRENT_MARK
2442 #ifdef SGEN_PARALLEL_MARK
2443 #ifdef FIXED_HEAP
2444 void
2445 sgen_marksweep_fixed_par_init (SgenMajorCollector *collector)
2446 #else // FIXED_HEAP
2447 void
2448 sgen_marksweep_par_init (SgenMajorCollector *collector)
2449 #endif // FIXED_HEAP
2450 #else // SGEN_PARALLEL_MARK
2451 #ifdef FIXED_HEAP
2452 void
2453 sgen_marksweep_fixed_init (SgenMajorCollector *collector)
2454 #else // FIXED_HEAP
2455 #error unknown configuration
2456 #endif // FIXED_HEAP
2457 #endif // SGEN_PARALLEL_MARK
2458 #endif // SGEN_HAVE_CONCURRENT_MARK
2459 {
2460         int i;
2461
2462 #ifndef FIXED_HEAP
2463         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2464 #endif
2465
2466         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2467         block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2468         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2469
2470         evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2471         for (i = 0; i < num_block_obj_sizes; ++i)
2472                 evacuate_block_obj_sizes [i] = FALSE;
2473
2474         /*
2475         {
2476                 int i;
2477                 g_print ("block object sizes:\n");
2478                 for (i = 0; i < num_block_obj_sizes; ++i)
2479                         g_print ("%d\n", block_obj_sizes [i]);
2480         }
2481         */
2482
2483         alloc_free_block_lists (free_block_lists);
2484
2485         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2486                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2487         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2488                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2489
2490 #ifdef SGEN_PARALLEL_MARK
2491         LOCK_INIT (ms_block_list_mutex);
2492 #endif
2493
2494         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2495         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2496         mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2497         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2498 #if SIZEOF_VOID_P != 8
2499         mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2500         mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2501         mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2502         mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2503 #endif
2504
2505 #ifdef SGEN_PARALLEL_MARK
2506 #ifndef HAVE_KW_THREAD
2507         mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
2508 #endif
2509 #endif
2510
2511         collector->section_size = MAJOR_SECTION_SIZE;
2512 #ifdef SGEN_PARALLEL_MARK
2513         collector->is_parallel = TRUE;
2514         collector->alloc_worker_data = major_alloc_worker_data;
2515         collector->init_worker_thread = major_init_worker_thread;
2516         collector->reset_worker_data = major_reset_worker_data;
2517 #else
2518         collector->is_parallel = FALSE;
2519 #endif
2520 #ifdef SGEN_HAVE_CONCURRENT_MARK
2521         concurrent_mark = is_concurrent;
2522         if (is_concurrent) {
2523                 collector->is_concurrent = TRUE;
2524                 collector->want_synchronous_collection = &want_evacuation;
2525                 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2526         } else
2527 #endif
2528         {
2529                 collector->is_concurrent = FALSE;
2530                 collector->want_synchronous_collection = NULL;
2531         }
2532         collector->supports_cardtable = TRUE;
2533
2534         collector->have_swept = &have_swept;
2535
2536         collector->alloc_heap = major_alloc_heap;
2537         collector->is_object_live = major_is_object_live;
2538         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2539         collector->alloc_degraded = major_alloc_degraded;
2540
2541         collector->alloc_object = major_alloc_object;
2542 #ifdef SGEN_PARALLEL_MARK
2543         collector->par_alloc_object = major_par_alloc_object;
2544 #endif
2545         collector->free_pinned_object = free_pinned_object;
2546         collector->iterate_objects = major_iterate_objects;
2547         collector->free_non_pinned_object = major_free_non_pinned_object;
2548         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2549         collector->pin_objects = major_pin_objects;
2550         collector->pin_major_object = pin_major_object;
2551         collector->scan_card_table = major_scan_card_table;
2552         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2553 #ifdef SGEN_HAVE_CONCURRENT_MARK
2554         if (is_concurrent) {
2555                 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2556                 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2557         }
2558 #endif
2559         collector->init_to_space = major_init_to_space;
2560         collector->sweep = major_sweep;
2561         collector->check_scan_starts = major_check_scan_starts;
2562         collector->dump_heap = major_dump_heap;
2563         collector->get_used_size = major_get_used_size;
2564         collector->start_nursery_collection = major_start_nursery_collection;
2565         collector->finish_nursery_collection = major_finish_nursery_collection;
2566         collector->start_major_collection = major_start_major_collection;
2567         collector->finish_major_collection = major_finish_major_collection;
2568         collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2569         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2570         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2571         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2572         collector->get_num_major_sections = get_num_major_sections;
2573         collector->handle_gc_param = major_handle_gc_param;
2574         collector->print_gc_param_usage = major_print_gc_param_usage;
2575         collector->post_param_init = post_param_init;
2576         collector->is_valid_object = major_is_valid_object;
2577         collector->describe_pointer = major_describe_pointer;
2578
2579         collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2580         collector->major_ops.scan_object = major_scan_object;
2581 #ifdef SGEN_HAVE_CONCURRENT_MARK
2582         if (is_concurrent) {
2583                 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2584                 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2585                 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2586         }
2587 #endif
2588
2589         /*cardtable requires major pages to be 8 cards aligned*/
2590         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2591 }
2592
2593 #ifdef SGEN_HAVE_CONCURRENT_MARK
2594 void
2595 sgen_marksweep_init (SgenMajorCollector *collector)
2596 {
2597         sgen_marksweep_init_internal (collector, FALSE);
2598 }
2599
2600 void
2601 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2602 {
2603         sgen_marksweep_init_internal (collector, TRUE);
2604 }
2605 #endif
2606
2607 #endif