update .sln too.
[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 MonoVTable*
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 vtable;
993         } END_FOREACH_BLOCK;
994
995         return NULL;
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                                 binary_protocol_mark (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1189                         }
1190                 } else {
1191                         /*
1192                          * FIXME: We have allocated destination, but
1193                          * we cannot use it.  Give it back to the
1194                          * allocator.
1195                          */
1196                         *(void**)destination = NULL;
1197
1198                         vtable_word = *(mword*)obj;
1199                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
1200
1201                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1202
1203                         *ptr = obj;
1204
1205                         HEAVY_STAT (++stat_slots_allocated_in_vain);
1206                 }
1207         } else {
1208 #ifdef FIXED_HEAP
1209                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1210 #else
1211                 mword vtable_word = *(mword*)obj;
1212                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1213
1214                 /* see comment in the non-parallel version below */
1215                 if (vtable_word & SGEN_FORWARDED_BIT) {
1216                         *ptr = (void*)vt;
1217                         return;
1218                 }
1219                 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1220
1221                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1222 #endif
1223                 {
1224                         int size_index;
1225
1226                         block = MS_BLOCK_FOR_OBJ (obj);
1227                         size_index = block->obj_size_index;
1228
1229                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1230                                 if (block->is_to_space)
1231                                         return;
1232
1233 #ifdef FIXED_HEAP
1234                                 {
1235                                         mword vtable_word = *(mword*)obj;
1236                                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1237
1238                                         if (vtable_word & SGEN_FORWARDED_BIT) {
1239                                                 *ptr = (void*)vt;
1240                                                 return;
1241                                         }
1242                                 }
1243 #endif
1244
1245                                 HEAVY_STAT (++stat_major_objects_evacuated);
1246                                 goto do_copy_object;
1247                         }
1248
1249                         MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1250                 } else {
1251                         LOSObject *bigobj = sgen_los_header_for_object (obj);
1252                         mword size_word = bigobj->size;
1253 #ifdef FIXED_HEAP
1254                         mword vtable_word = *(mword*)obj;
1255                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1256 #endif
1257                         if (size_word & 1)
1258                                 return;
1259                         binary_protocol_pin (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1260                         if (SGEN_CAS_PTR ((void*)&bigobj->size, (void*)(size_word | 1), (void*)size_word) == (void*)size_word) {
1261                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1262                                         GRAY_OBJECT_ENQUEUE (queue, obj);
1263                         } else {
1264                                 g_assert (sgen_los_object_is_pinned (obj));
1265                         }
1266                 }
1267         }
1268 }
1269 #else
1270 #ifdef SGEN_HAVE_CONCURRENT_MARK
1271 static void
1272 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
1273 {
1274         g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
1275
1276         if (!sgen_ptr_in_nursery (obj)) {
1277 #ifdef FIXED_HEAP
1278                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1279 #else
1280                 mword objsize;
1281
1282                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1283
1284                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1285 #endif
1286                 {
1287                         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1288                         MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1289                 } else {
1290                         if (sgen_los_object_is_pinned (obj))
1291                                 return;
1292
1293 #ifdef ENABLE_DTRACE
1294                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1295                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1296                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1297                         }
1298 #endif
1299
1300                         sgen_los_pin_object (obj);
1301                         /* FIXME: only enqueue if object has references */
1302                         GRAY_OBJECT_ENQUEUE (queue, obj);
1303                         INC_NUM_MAJOR_OBJECTS_MARKED ();
1304                 }
1305         }
1306 }
1307 #endif
1308
1309 static void
1310 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
1311 {
1312         MSBlockInfo *block;
1313
1314         HEAVY_STAT (++stat_copy_object_called_major);
1315
1316         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
1317         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
1318
1319         if (sgen_ptr_in_nursery (obj)) {
1320                 int word, bit;
1321                 char *forwarded, *old_obj;
1322
1323                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1324                         *ptr = forwarded;
1325                         return;
1326                 }
1327                 if (SGEN_OBJECT_IS_PINNED (obj))
1328                         return;
1329
1330                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1331                 if (sgen_nursery_is_to_space (obj))
1332                         return;
1333
1334                 HEAVY_STAT (++stat_objects_copied_major);
1335
1336         do_copy_object:
1337                 old_obj = obj;
1338                 obj = copy_object_no_checks (obj, queue);
1339                 if (G_UNLIKELY (old_obj == obj)) {
1340                         /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1341                         if (!sgen_ptr_in_nursery (obj)) {
1342                                 int size_index;
1343                                 block = MS_BLOCK_FOR_OBJ (obj);
1344                                 size_index = block->obj_size_index;
1345                                 evacuate_block_obj_sizes [size_index] = FALSE;
1346                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1347                         }
1348                         return;
1349                 }
1350                 *ptr = obj;
1351
1352                 /*
1353                  * FIXME: See comment for copy_object_no_checks().  If
1354                  * we have that, we can let the allocation function
1355                  * give us the block info, too, and we won't have to
1356                  * re-fetch it.
1357                  *
1358                  * FIXME (2): We should rework this to avoid all those nursery checks.
1359                  */
1360                 /*
1361                  * For the split nursery allocator the object might
1362                  * still be in the nursery despite having being
1363                  * promoted, in which case we can't mark it.
1364                  */
1365                 if (!sgen_ptr_in_nursery (obj)) {
1366                         block = MS_BLOCK_FOR_OBJ (obj);
1367                         MS_CALC_MARK_BIT (word, bit, obj);
1368                         SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
1369                         MS_SET_MARK_BIT (block, word, bit);
1370                         binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1371                 }
1372         } else {
1373                 char *forwarded;
1374 #ifdef FIXED_HEAP
1375                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1376 #else
1377                 mword objsize;
1378
1379                 /*
1380                  * If we have don't have a fixed heap we cannot know
1381                  * whether an object is in the LOS or in the small
1382                  * object major heap without checking its size.  To do
1383                  * that, however, we need to know that we actually
1384                  * have a valid object, not a forwarding pointer, so
1385                  * we have to do this check first.
1386                  */
1387                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1388                         *ptr = forwarded;
1389                         return;
1390                 }
1391
1392                 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1393
1394                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1395 #endif
1396                 {
1397                         int size_index;
1398                         gboolean evacuate;
1399
1400                         block = MS_BLOCK_FOR_OBJ (obj);
1401                         size_index = block->obj_size_index;
1402                         evacuate = evacuate_block_obj_sizes [size_index];
1403
1404 #ifdef FIXED_HEAP
1405                         /*
1406                          * We could also check for !block->has_pinned
1407                          * here, but it would only make an uncommon case
1408                          * faster, namely objects that are in blocks
1409                          * whose slot sizes are evacuated but which have
1410                          * pinned objects.
1411                          */
1412                         if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1413                                 *ptr = forwarded;
1414                                 return;
1415                         }
1416 #endif
1417
1418                         if (evacuate && !block->has_pinned) {
1419                                 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1420                                 if (block->is_to_space)
1421                                         return;
1422                                 HEAVY_STAT (++stat_major_objects_evacuated);
1423                                 goto do_copy_object;
1424                         } else {
1425                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1426                         }
1427                 } else {
1428                         if (sgen_los_object_is_pinned (obj))
1429                                 return;
1430                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1431
1432 #ifdef ENABLE_DTRACE
1433                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1434                                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1435                                 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1436                         }
1437 #endif
1438
1439                         sgen_los_pin_object (obj);
1440                         /* FIXME: only enqueue if object has references */
1441                         GRAY_OBJECT_ENQUEUE (queue, obj);
1442                 }
1443         }
1444 }
1445 #endif
1446
1447 static void
1448 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1449 {
1450         major_copy_or_mark_object (ptr, *ptr, queue);
1451 }
1452
1453 #ifdef SGEN_HAVE_CONCURRENT_MARK
1454 static void
1455 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1456 {
1457         major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1458 }
1459
1460 static long long
1461 major_get_and_reset_num_major_objects_marked (void)
1462 {
1463 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1464         long long num = num_major_objects_marked;
1465         num_major_objects_marked = 0;
1466         return num;
1467 #else
1468         return 0;
1469 #endif
1470 }
1471 #endif
1472
1473 #include "sgen-major-scan-object.h"
1474
1475 #ifdef SGEN_HAVE_CONCURRENT_MARK
1476 #define SCAN_FOR_CONCURRENT_MARK
1477 #include "sgen-major-scan-object.h"
1478 #undef SCAN_FOR_CONCURRENT_MARK
1479 #endif
1480
1481 static void
1482 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1483 {
1484         int i;
1485         int last_index = -1;
1486
1487         if (!block->pin_queue_num_entries)
1488                 return;
1489
1490         block->has_pinned = TRUE;
1491
1492         for (i = 0; i < block->pin_queue_num_entries; ++i) {
1493                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1494                 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);
1495                 if (index == last_index)
1496                         continue;
1497                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1498                 last_index = index;
1499         }
1500 }
1501
1502 static inline void
1503 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1504 {
1505         int obj_index;
1506
1507         for (obj_index = 0; obj_index < count; ++obj_index) {
1508                 int word, bit;
1509                 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1510
1511                 MS_CALC_MARK_BIT (word, bit, obj);
1512                 if (MS_MARK_BIT (block, word, bit)) {
1513                         SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1514                 } else {
1515                         /* an unmarked object */
1516                         if (MS_OBJ_ALLOCED (obj, block)) {
1517                                 /*
1518                                  * FIXME: Merge consecutive
1519                                  * slots for lower reporting
1520                                  * overhead.  Maybe memset
1521                                  * will also benefit?
1522                                  */
1523                                 binary_protocol_empty (obj, obj_size);
1524                                 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1525                                 memset (obj, 0, obj_size);
1526                         }
1527                         *(void**)obj = block->free_list;
1528                         block->free_list = obj;
1529                 }
1530         }
1531 }
1532
1533 /*
1534  * sweep_block:
1535  *
1536  *   Traverse BLOCK, freeing and zeroing unused objects.
1537  */
1538 static void
1539 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1540 {
1541         int count;
1542
1543         if (!during_major_collection)
1544                 g_assert (!sgen_concurrent_collection_in_progress ());
1545
1546         if (block->swept)
1547                 return;
1548
1549         count = MS_BLOCK_FREE / block->obj_size;
1550
1551         block->free_list = NULL;
1552
1553         /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1554         // FIXME: Add more sizes
1555         switch (block->obj_size) {
1556         case 16:
1557                 sweep_block_for_size (block, count, 16);
1558                 break;
1559         default:
1560                 sweep_block_for_size (block, count, block->obj_size);
1561                 break;
1562         }
1563
1564         /* reset mark bits */
1565         memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1566
1567         /*
1568          * FIXME: reverse free list so that it's in address
1569          * order
1570          */
1571
1572         block->swept = 1;
1573 }
1574
1575 static inline int
1576 bitcount (mword d)
1577 {
1578         int count = 0;
1579
1580 #ifdef __GNUC__
1581         if (sizeof (mword) == sizeof (unsigned long))
1582                 count += __builtin_popcountl (d);
1583         else
1584                 count += __builtin_popcount (d);
1585 #else
1586         while (d) {
1587                 count ++;
1588                 d &= (d - 1);
1589         }
1590 #endif
1591         return count;
1592 }
1593
1594 static void
1595 ms_sweep (void)
1596 {
1597         int i;
1598         MSBlockInfo **iter;
1599
1600         /* statistics for evacuation */
1601         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1602         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1603         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1604
1605 #ifdef SGEN_HAVE_CONCURRENT_MARK
1606         mword total_evacuate_heap = 0;
1607         mword total_evacuate_saved = 0;
1608 #endif
1609
1610         for (i = 0; i < num_block_obj_sizes; ++i)
1611                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1612
1613         /* clear all the free lists */
1614         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1615                 MSBlockInfo **free_blocks = free_block_lists [i];
1616                 int j;
1617                 for (j = 0; j < num_block_obj_sizes; ++j)
1618                         free_blocks [j] = NULL;
1619         }
1620
1621         /* traverse all blocks, free and zero unmarked objects */
1622         iter = &all_blocks;
1623         while (*iter) {
1624                 MSBlockInfo *block = *iter;
1625                 int count;
1626                 gboolean have_live = FALSE;
1627                 gboolean has_pinned;
1628                 gboolean have_free = FALSE;
1629                 int obj_size_index;
1630                 int nused = 0;
1631
1632                 obj_size_index = block->obj_size_index;
1633
1634                 has_pinned = block->has_pinned;
1635                 block->has_pinned = block->pinned;
1636
1637                 block->is_to_space = FALSE;
1638                 block->swept = 0;
1639
1640                 count = MS_BLOCK_FREE / block->obj_size;
1641
1642 #ifdef SGEN_HAVE_CONCURRENT_MARK
1643                 if (block->cardtable_mod_union) {
1644                         sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1645                         block->cardtable_mod_union = NULL;
1646                 }
1647 #endif
1648
1649                 /* Count marked objects in the block */
1650                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1651                         nused += bitcount (block->mark_words [i]);
1652                 }
1653                 if (nused) {
1654                         have_live = TRUE;
1655                 }
1656                 if (nused < count)
1657                         have_free = TRUE;
1658
1659                 if (!lazy_sweep)
1660                         sweep_block (block, TRUE);
1661
1662                 if (have_live) {
1663                         if (!has_pinned) {
1664                                 ++num_blocks [obj_size_index];
1665                                 slots_used [obj_size_index] += nused;
1666                                 slots_available [obj_size_index] += count;
1667                         }
1668
1669                         iter = &block->next;
1670
1671                         /*
1672                          * If there are free slots in the block, add
1673                          * the block to the corresponding free list.
1674                          */
1675                         if (have_free) {
1676                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1677                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1678                                 block->next_free = free_blocks [index];
1679                                 free_blocks [index] = block;
1680                         }
1681
1682                         update_heap_boundaries_for_block (block);
1683                 } else {
1684                         /*
1685                          * Blocks without live objects are removed from the
1686                          * block list and freed.
1687                          */
1688                         *iter = block->next;
1689
1690 #ifdef FIXED_HEAP
1691                         ms_free_block (block);
1692 #else
1693                         ms_free_block (block->block);
1694
1695                         sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1696 #endif
1697
1698                         --num_major_sections;
1699                 }
1700         }
1701
1702         for (i = 0; i < num_block_obj_sizes; ++i) {
1703                 float usage = (float)slots_used [i] / (float)slots_available [i];
1704                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1705                         evacuate_block_obj_sizes [i] = TRUE;
1706                         /*
1707                         g_print ("slot size %d - %d of %d used\n",
1708                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1709                         */
1710                 } else {
1711                         evacuate_block_obj_sizes [i] = FALSE;
1712                 }
1713 #ifdef SGEN_HAVE_CONCURRENT_MARK
1714                 {
1715                         mword total_bytes = block_obj_sizes [i] * slots_available [i];
1716                         total_evacuate_heap += total_bytes;
1717                         if (evacuate_block_obj_sizes [i])
1718                                 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1719                 }
1720 #endif
1721         }
1722
1723 #ifdef SGEN_HAVE_CONCURRENT_MARK
1724         want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1725 #endif
1726
1727         have_swept = TRUE;
1728 }
1729
1730 static void
1731 major_sweep (void)
1732 {
1733         ms_sweep ();
1734 }
1735
1736 static int count_pinned_ref;
1737 static int count_pinned_nonref;
1738 static int count_nonpinned_ref;
1739 static int count_nonpinned_nonref;
1740
1741 static void
1742 count_nonpinned_callback (char *obj, size_t size, void *data)
1743 {
1744         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1745
1746         if (vtable->klass->has_references)
1747                 ++count_nonpinned_ref;
1748         else
1749                 ++count_nonpinned_nonref;
1750 }
1751
1752 static void
1753 count_pinned_callback (char *obj, size_t size, void *data)
1754 {
1755         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1756
1757         if (vtable->klass->has_references)
1758                 ++count_pinned_ref;
1759         else
1760                 ++count_pinned_nonref;
1761 }
1762
1763 static G_GNUC_UNUSED void
1764 count_ref_nonref_objs (void)
1765 {
1766         int total;
1767
1768         count_pinned_ref = 0;
1769         count_pinned_nonref = 0;
1770         count_nonpinned_ref = 0;
1771         count_nonpinned_nonref = 0;
1772
1773         major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1774         major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1775
1776         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1777
1778         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1779                         count_pinned_ref, count_nonpinned_ref,
1780                         count_pinned_nonref, count_nonpinned_nonref,
1781                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1782 }
1783
1784 static int
1785 ms_calculate_block_obj_sizes (double factor, int *arr)
1786 {
1787         double target_size = sizeof (MonoObject);
1788         int num_sizes = 0;
1789         int last_size = 0;
1790
1791         do {
1792                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1793                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1794
1795                 if (size != last_size) {
1796                         if (arr)
1797                                 arr [num_sizes] = size;
1798                         ++num_sizes;
1799                         last_size = size;
1800                 }
1801
1802                 target_size *= factor;
1803         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1804
1805         return num_sizes;
1806 }
1807
1808 /* only valid during minor collections */
1809 static int old_num_major_sections;
1810
1811 static void
1812 major_start_nursery_collection (void)
1813 {
1814 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1815         consistency_check ();
1816 #endif
1817
1818         old_num_major_sections = num_major_sections;
1819 }
1820
1821 static void
1822 major_finish_nursery_collection (void)
1823 {
1824 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1825         consistency_check ();
1826 #endif
1827         sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1828 }
1829
1830 static void
1831 major_start_major_collection (void)
1832 {
1833         int i;
1834
1835         /* clear the free lists */
1836         for (i = 0; i < num_block_obj_sizes; ++i) {
1837                 if (!evacuate_block_obj_sizes [i])
1838                         continue;
1839
1840                 free_block_lists [0][i] = NULL;
1841                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1842         }
1843
1844         // Sweep all unswept blocks
1845         if (lazy_sweep) {
1846                 MSBlockInfo **iter;
1847
1848                 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1849
1850                 iter = &all_blocks;
1851                 while (*iter) {
1852                         MSBlockInfo *block = *iter;
1853
1854                         sweep_block (block, TRUE);
1855
1856                         iter = &block->next;
1857                 }
1858
1859                 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1860         }
1861 }
1862
1863 static void
1864 major_finish_major_collection (void)
1865 {
1866 }
1867
1868 #if !defined(FIXED_HEAP) && SIZEOF_VOID_P != 8
1869 static int
1870 compare_pointers (const void *va, const void *vb) {
1871         char *a = *(char**)va, *b = *(char**)vb;
1872         if (a < b)
1873                 return -1;
1874         if (a > b)
1875                 return 1;
1876         return 0;
1877 }
1878 #endif
1879
1880 static void
1881 major_have_computer_minor_collection_allowance (void)
1882 {
1883 #ifndef FIXED_HEAP
1884         int section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1885
1886         g_assert (have_swept);
1887
1888 #if SIZEOF_VOID_P != 8
1889         {
1890                 int i, num_empty_blocks_orig, num_blocks, arr_length;
1891                 void *block;
1892                 void **empty_block_arr;
1893                 void **rebuild_next;
1894
1895 #ifdef TARGET_WIN32
1896                 /*
1897                  * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1898                  * a VirtualAlloc ()-ed block.
1899                  */
1900                 return;
1901 #endif
1902
1903                 if (num_empty_blocks <= section_reserve)
1904                         return;
1905                 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1906
1907                 num_empty_blocks_orig = num_empty_blocks;
1908                 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1909                                 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1910                 if (!empty_block_arr)
1911                         goto fallback;
1912
1913                 i = 0;
1914                 for (block = empty_blocks; block; block = *(void**)block)
1915                         empty_block_arr [i++] = block;
1916                 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1917
1918                 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1919
1920                 /*
1921                  * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1922                  * contiguous ones.  If we do, we free them.  If that's not enough to get to
1923                  * section_reserve, we halve the number of contiguous blocks we're looking
1924                  * for and have another go, until we're done with looking for pairs of
1925                  * blocks, at which point we give up and go to the fallback.
1926                  */
1927                 arr_length = num_empty_blocks_orig;
1928                 num_blocks = MS_BLOCK_ALLOC_NUM;
1929                 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1930                         int first = -1;
1931                         int dest = 0;
1932
1933                         dest = 0;
1934                         for (i = 0; i < arr_length; ++i) {
1935                                 int d = dest;
1936                                 void *block = empty_block_arr [i];
1937                                 SGEN_ASSERT (0, block, "we're not shifting correctly");
1938                                 if (i != dest) {
1939                                         empty_block_arr [dest] = block;
1940                                         /*
1941                                          * This is not strictly necessary, but we're
1942                                          * cautious.
1943                                          */
1944                                         empty_block_arr [i] = NULL;
1945                                 }
1946                                 ++dest;
1947
1948                                 if (first < 0) {
1949                                         first = d;
1950                                         continue;
1951                                 }
1952
1953                                 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1954
1955                                 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1956                                         first = d;
1957                                         continue;
1958                                 }
1959
1960                                 if (d + 1 - first == num_blocks) {
1961                                         /*
1962                                          * We found num_blocks contiguous blocks.  Free them
1963                                          * and null their array entries.  As an optimization
1964                                          * we could, instead of nulling the entries, shift
1965                                          * the following entries over to the left, while
1966                                          * we're iterating.
1967                                          */
1968                                         int j;
1969                                         sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1970                                         for (j = first; j <= d; ++j)
1971                                                 empty_block_arr [j] = NULL;
1972                                         dest = first;
1973                                         first = -1;
1974
1975                                         num_empty_blocks -= num_blocks;
1976
1977                                         stat_major_blocks_freed += num_blocks;
1978                                         if (num_blocks == MS_BLOCK_ALLOC_NUM)
1979                                                 stat_major_blocks_freed_ideal += num_blocks;
1980                                         else
1981                                                 stat_major_blocks_freed_less_ideal += num_blocks;
1982
1983                                 }
1984                         }
1985
1986                         SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1987                         arr_length = dest;
1988                         SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1989
1990                         num_blocks >>= 1;
1991                 }
1992
1993                 /* rebuild empty_blocks free list */
1994                 rebuild_next = (void**)&empty_blocks;
1995                 for (i = 0; i < arr_length; ++i) {
1996                         void *block = empty_block_arr [i];
1997                         SGEN_ASSERT (0, block, "we're missing blocks");
1998                         *rebuild_next = block;
1999                         rebuild_next = (void**)block;
2000                 }
2001                 *rebuild_next = NULL;
2002
2003                 /* free array */
2004                 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
2005         }
2006
2007         SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
2008
2009  fallback:
2010         /*
2011          * This is our threshold.  If there's not more empty than used blocks, we won't
2012          * release uncontiguous blocks, in fear of fragmenting the address space.
2013          */
2014         if (num_empty_blocks <= num_major_sections)
2015                 return;
2016 #endif
2017
2018         while (num_empty_blocks > section_reserve) {
2019                 void *next = *(void**)empty_blocks;
2020                 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
2021                 empty_blocks = next;
2022                 /*
2023                  * Needs not be atomic because this is running
2024                  * single-threaded.
2025                  */
2026                 --num_empty_blocks;
2027
2028                 ++stat_major_blocks_freed;
2029 #if SIZEOF_VOID_P != 8
2030                 ++stat_major_blocks_freed_individual;
2031 #endif
2032         }
2033 #endif
2034 }
2035
2036 static void
2037 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
2038 {
2039         MSBlockInfo *block;
2040
2041         FOREACH_BLOCK (block) {
2042                 block->pin_queue_start = sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
2043                                 &block->pin_queue_num_entries);
2044         } END_FOREACH_BLOCK;
2045 }
2046
2047 static void
2048 major_pin_objects (SgenGrayQueue *queue)
2049 {
2050         MSBlockInfo *block;
2051
2052         FOREACH_BLOCK (block) {
2053                 mark_pinned_objects_in_block (block, queue);
2054         } END_FOREACH_BLOCK;
2055 }
2056
2057 static void
2058 major_init_to_space (void)
2059 {
2060 }
2061
2062 static void
2063 major_report_pinned_memory_usage (void)
2064 {
2065         g_assert_not_reached ();
2066 }
2067
2068 static gint64
2069 major_get_used_size (void)
2070 {
2071         gint64 size = 0;
2072         MSBlockInfo *block;
2073
2074         FOREACH_BLOCK (block) {
2075                 int count = MS_BLOCK_FREE / block->obj_size;
2076                 void **iter;
2077                 size += count * block->obj_size;
2078                 for (iter = block->free_list; iter; iter = (void**)*iter)
2079                         size -= block->obj_size;
2080         } END_FOREACH_BLOCK;
2081
2082         return size;
2083 }
2084
2085 static int
2086 get_num_major_sections (void)
2087 {
2088         return num_major_sections;
2089 }
2090
2091 static gboolean
2092 major_handle_gc_param (const char *opt)
2093 {
2094 #ifdef FIXED_HEAP
2095         if (g_str_has_prefix (opt, "major-heap-size=")) {
2096                 const char *arg = strchr (opt, '=') + 1;
2097                 glong size;
2098                 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
2099                         return FALSE;
2100                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
2101                 g_assert (ms_heap_num_blocks > 0);
2102                 return TRUE;
2103         } else
2104 #endif
2105         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
2106                 const char *arg = strchr (opt, '=') + 1;
2107                 int percentage = atoi (arg);
2108                 if (percentage < 0 || percentage > 100) {
2109                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
2110                         exit (1);
2111                 }
2112                 evacuation_threshold = (float)percentage / 100.0;
2113                 return TRUE;
2114         } else if (!strcmp (opt, "lazy-sweep")) {
2115                 lazy_sweep = TRUE;
2116                 return TRUE;
2117         } else if (!strcmp (opt, "no-lazy-sweep")) {
2118                 lazy_sweep = FALSE;
2119                 return TRUE;
2120         }
2121
2122         return FALSE;
2123 }
2124
2125 static void
2126 major_print_gc_param_usage (void)
2127 {
2128         fprintf (stderr,
2129                         ""
2130 #ifdef FIXED_HEAP
2131                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
2132 #endif
2133                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
2134                         "  (no-)lazy-sweep\n"
2135                         );
2136 }
2137
2138 static void
2139 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
2140 {
2141         MSBlockInfo *block;
2142
2143         FOREACH_BLOCK (block) {
2144                 if (block->has_references)
2145                         callback ((mword)block->block, MS_BLOCK_SIZE);
2146         } END_FOREACH_BLOCK;
2147 }
2148
2149 #ifdef HEAVY_STATISTICS
2150 extern long long marked_cards;
2151 extern long long scanned_cards;
2152 extern long long scanned_objects;
2153 extern long long remarked_cards;
2154 #endif
2155
2156 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
2157 /*
2158  * MS blocks are 16K aligned.
2159  * Cardtables are 4K aligned, at least.
2160  * This means that the cardtable of a given block is 32 bytes aligned.
2161  */
2162 static guint8*
2163 initial_skip_card (guint8 *card_data)
2164 {
2165         mword *cards = (mword*)card_data;
2166         mword card;
2167         int i;
2168         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
2169                 card = cards [i];
2170                 if (card)
2171                         break;
2172         }
2173
2174         if (i == CARD_WORDS_PER_BLOCK)
2175                 return card_data + CARDS_PER_BLOCK;
2176
2177 #if defined(__i386__) && defined(__GNUC__)
2178         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
2179 #elif defined(__x86_64__) && defined(__GNUC__)
2180         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
2181 #elif defined(__s390x__) && defined(__GNUC__)
2182         return card_data + i * 8 +  (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
2183 #else
2184         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
2185                 if (card_data [i])
2186                         return &card_data [i];
2187         }
2188         return card_data;
2189 #endif
2190 }
2191
2192
2193 static G_GNUC_UNUSED guint8*
2194 skip_card (guint8 *card_data, guint8 *card_data_end)
2195 {
2196         while (card_data < card_data_end && !*card_data)
2197                 ++card_data;
2198         return card_data;
2199 }
2200
2201 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
2202 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
2203 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
2204
2205 static void
2206 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
2207 {
2208         MSBlockInfo *block;
2209         ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
2210
2211 #ifdef SGEN_HAVE_CONCURRENT_MARK
2212         if (!concurrent_mark)
2213                 g_assert (!mod_union);
2214 #else
2215         g_assert (!mod_union);
2216 #endif
2217
2218         FOREACH_BLOCK (block) {
2219                 int block_obj_size;
2220                 char *block_start;
2221
2222                 if (!block->has_references)
2223                         continue;
2224
2225                 block_obj_size = block->obj_size;
2226                 block_start = block->block;
2227
2228                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
2229                         guint8 *cards;
2230 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
2231                         guint8 cards_data [CARDS_PER_BLOCK];
2232 #endif
2233                         char *obj, *end, *base;
2234
2235                         if (mod_union) {
2236 #ifdef SGEN_HAVE_CONCURRENT_MARK
2237                                 cards = block->cardtable_mod_union;
2238                                 /*
2239                                  * This happens when the nursery
2240                                  * collection that precedes finishing
2241                                  * the concurrent collection allocates
2242                                  * new major blocks.
2243                                  */
2244                                 if (!cards)
2245                                         continue;
2246 #endif
2247                         } else {
2248                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
2249 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
2250                                 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
2251 #else
2252                                 cards = cards_data;
2253                                 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
2254                                         continue;
2255 #endif
2256                         }
2257
2258                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
2259                         end = block_start + MS_BLOCK_SIZE;
2260                         base = sgen_card_table_align_pointer (obj);
2261
2262                         while (obj < end) {
2263                                 int card_offset;
2264
2265                                 if (!block->swept)
2266                                         sweep_block (block, FALSE);
2267
2268                                 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2269                                         goto next_large;
2270
2271                                 if (mod_union) {
2272                                         /* FIXME: do this more efficiently */
2273                                         int w, b;
2274                                         MS_CALC_MARK_BIT (w, b, obj);
2275                                         if (!MS_MARK_BIT (block, w, b))
2276                                                 goto next_large;
2277                                 }
2278
2279                                 card_offset = (obj - base) >> CARD_BITS;
2280                                 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
2281
2282                         next_large:
2283                                 obj += block_obj_size;
2284                         }
2285                 } else {
2286                         guint8 *card_data, *card_base;
2287                         guint8 *card_data_end;
2288
2289                         /*
2290                          * This is safe in face of card aliasing for the following reason:
2291                          *
2292                          * Major blocks are 16k aligned, or 32 cards aligned.
2293                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
2294                          * sizes, they won't overflow the cardtable overlap modulus.
2295                          */
2296                         if (mod_union) {
2297 #ifdef SGEN_HAVE_CONCURRENT_MARK
2298                                 card_data = card_base = block->cardtable_mod_union;
2299                                 /*
2300                                  * This happens when the nursery
2301                                  * collection that precedes finishing
2302                                  * the concurrent collection allocates
2303                                  * new major blocks.
2304                                  */
2305                                 if (!card_data)
2306                                         continue;
2307 #else
2308                                 g_assert_not_reached ();
2309                                 card_data = NULL;
2310 #endif
2311                         } else {
2312                                 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
2313                         }
2314                         card_data_end = card_data + CARDS_PER_BLOCK;
2315
2316                         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)) {
2317                                 int index;
2318                                 int idx = card_data - card_base;
2319                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
2320                                 char *end = start + CARD_SIZE_IN_BYTES;
2321                                 char *first_obj, *obj;
2322
2323                                 HEAVY_STAT (++scanned_cards);
2324
2325                                 if (!*card_data)
2326                                         continue;
2327
2328                                 if (!block->swept)
2329                                         sweep_block (block, FALSE);
2330
2331                                 HEAVY_STAT (++marked_cards);
2332
2333                                 sgen_card_table_prepare_card_for_scanning (card_data);
2334
2335                                 if (idx == 0)
2336                                         index = 0;
2337                                 else
2338                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
2339
2340                                 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
2341                                 while (obj < end) {
2342                                         if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
2343                                                 goto next_small;
2344
2345                                         if (mod_union) {
2346                                                 /* FIXME: do this more efficiently */
2347                                                 int w, b;
2348                                                 MS_CALC_MARK_BIT (w, b, obj);
2349                                                 if (!MS_MARK_BIT (block, w, b))
2350                                                         goto next_small;
2351                                         }
2352
2353                                         HEAVY_STAT (++scanned_objects);
2354                                         scan_func (obj, queue);
2355                                 next_small:
2356                                         obj += block_obj_size;
2357                                 }
2358                                 HEAVY_STAT (if (*card_data) ++remarked_cards);
2359                                 binary_protocol_card_scan (first_obj, obj - first_obj);
2360                         }
2361                 }
2362         } END_FOREACH_BLOCK;
2363 }
2364
2365 #ifdef SGEN_HAVE_CONCURRENT_MARK
2366 static void
2367 update_cardtable_mod_union (void)
2368 {
2369         MSBlockInfo *block;
2370
2371         FOREACH_BLOCK (block) {
2372                 size_t num_cards;
2373
2374                 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
2375                                 block->block, MS_BLOCK_SIZE, &num_cards);
2376
2377                 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
2378         } END_FOREACH_BLOCK;
2379 }
2380
2381 static guint8*
2382 major_get_cardtable_mod_union_for_object (char *obj)
2383 {
2384         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
2385         return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (block->block)) >> CARD_BITS];
2386 }
2387 #endif
2388
2389 static void
2390 alloc_free_block_lists (MSBlockInfo ***lists)
2391 {
2392         int i;
2393         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2394                 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2395 }
2396
2397 #ifdef SGEN_PARALLEL_MARK
2398 static void*
2399 major_alloc_worker_data (void)
2400 {
2401         /* FIXME: free this when the workers come down */
2402         MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
2403         alloc_free_block_lists (lists);
2404         return lists;
2405 }
2406
2407 static void
2408 major_init_worker_thread (void *data)
2409 {
2410         MSBlockInfo ***lists = data;
2411         int i;
2412
2413         g_assert (lists && lists != free_block_lists);
2414         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2415                 int j;
2416                 for (j = 0; j < num_block_obj_sizes; ++j)
2417                         g_assert (!lists [i][j]);
2418         }
2419
2420 #ifdef HAVE_KW_THREAD
2421         workers_free_block_lists = data;
2422 #else
2423         mono_native_tls_set_value (workers_free_block_lists_key, data);
2424 #endif
2425 }
2426
2427 static void
2428 major_reset_worker_data (void *data)
2429 {
2430         MSBlockInfo ***lists = data;
2431         int i;
2432         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2433                 int j;
2434                 for (j = 0; j < num_block_obj_sizes; ++j)
2435                         lists [i][j] = NULL;
2436         }
2437 }
2438 #endif
2439
2440 #undef pthread_create
2441
2442 static void
2443 post_param_init (SgenMajorCollector *collector)
2444 {
2445         collector->sweeps_lazily = lazy_sweep;
2446 }
2447
2448 #ifdef SGEN_HAVE_CONCURRENT_MARK
2449 static void
2450 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2451 #else // SGEN_HAVE_CONCURRENT_MARK
2452 #ifdef SGEN_PARALLEL_MARK
2453 #ifdef FIXED_HEAP
2454 void
2455 sgen_marksweep_fixed_par_init (SgenMajorCollector *collector)
2456 #else // FIXED_HEAP
2457 void
2458 sgen_marksweep_par_init (SgenMajorCollector *collector)
2459 #endif // FIXED_HEAP
2460 #else // SGEN_PARALLEL_MARK
2461 #ifdef FIXED_HEAP
2462 void
2463 sgen_marksweep_fixed_init (SgenMajorCollector *collector)
2464 #else // FIXED_HEAP
2465 #error unknown configuration
2466 #endif // FIXED_HEAP
2467 #endif // SGEN_PARALLEL_MARK
2468 #endif // SGEN_HAVE_CONCURRENT_MARK
2469 {
2470         int i;
2471
2472 #ifndef FIXED_HEAP
2473         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2474 #endif
2475
2476         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2477         block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2478         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2479
2480         evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2481         for (i = 0; i < num_block_obj_sizes; ++i)
2482                 evacuate_block_obj_sizes [i] = FALSE;
2483
2484         /*
2485         {
2486                 int i;
2487                 g_print ("block object sizes:\n");
2488                 for (i = 0; i < num_block_obj_sizes; ++i)
2489                         g_print ("%d\n", block_obj_sizes [i]);
2490         }
2491         */
2492
2493         alloc_free_block_lists (free_block_lists);
2494
2495         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2496                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2497         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2498                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2499
2500 #ifdef SGEN_PARALLEL_MARK
2501         LOCK_INIT (ms_block_list_mutex);
2502 #endif
2503
2504         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2505         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2506         mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2507         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2508 #if SIZEOF_VOID_P != 8
2509         mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2510         mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2511         mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2512         mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2513 #endif
2514
2515 #ifdef SGEN_PARALLEL_MARK
2516 #ifndef HAVE_KW_THREAD
2517         mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
2518 #endif
2519 #endif
2520
2521         collector->section_size = MAJOR_SECTION_SIZE;
2522 #ifdef SGEN_PARALLEL_MARK
2523         collector->is_parallel = TRUE;
2524         collector->alloc_worker_data = major_alloc_worker_data;
2525         collector->init_worker_thread = major_init_worker_thread;
2526         collector->reset_worker_data = major_reset_worker_data;
2527 #else
2528         collector->is_parallel = FALSE;
2529 #endif
2530 #ifdef SGEN_HAVE_CONCURRENT_MARK
2531         concurrent_mark = is_concurrent;
2532         if (is_concurrent) {
2533                 collector->is_concurrent = TRUE;
2534                 collector->want_synchronous_collection = &want_evacuation;
2535                 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2536         } else
2537 #endif
2538         {
2539                 collector->is_concurrent = FALSE;
2540                 collector->want_synchronous_collection = NULL;
2541         }
2542         collector->supports_cardtable = TRUE;
2543
2544         collector->have_swept = &have_swept;
2545
2546         collector->alloc_heap = major_alloc_heap;
2547         collector->is_object_live = major_is_object_live;
2548         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2549         collector->alloc_degraded = major_alloc_degraded;
2550
2551         collector->alloc_object = major_alloc_object;
2552 #ifdef SGEN_PARALLEL_MARK
2553         collector->par_alloc_object = major_par_alloc_object;
2554 #endif
2555         collector->free_pinned_object = free_pinned_object;
2556         collector->iterate_objects = major_iterate_objects;
2557         collector->free_non_pinned_object = major_free_non_pinned_object;
2558         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2559         collector->pin_objects = major_pin_objects;
2560         collector->pin_major_object = pin_major_object;
2561         collector->scan_card_table = major_scan_card_table;
2562         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2563 #ifdef SGEN_HAVE_CONCURRENT_MARK
2564         if (is_concurrent) {
2565                 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2566                 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2567         }
2568 #endif
2569         collector->init_to_space = major_init_to_space;
2570         collector->sweep = major_sweep;
2571         collector->check_scan_starts = major_check_scan_starts;
2572         collector->dump_heap = major_dump_heap;
2573         collector->get_used_size = major_get_used_size;
2574         collector->start_nursery_collection = major_start_nursery_collection;
2575         collector->finish_nursery_collection = major_finish_nursery_collection;
2576         collector->start_major_collection = major_start_major_collection;
2577         collector->finish_major_collection = major_finish_major_collection;
2578         collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2579         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2580         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2581         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2582         collector->get_num_major_sections = get_num_major_sections;
2583         collector->handle_gc_param = major_handle_gc_param;
2584         collector->print_gc_param_usage = major_print_gc_param_usage;
2585         collector->post_param_init = post_param_init;
2586         collector->is_valid_object = major_is_valid_object;
2587         collector->describe_pointer = major_describe_pointer;
2588
2589         collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2590         collector->major_ops.scan_object = major_scan_object;
2591 #ifdef SGEN_HAVE_CONCURRENT_MARK
2592         if (is_concurrent) {
2593                 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2594                 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2595                 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2596         }
2597 #endif
2598
2599         /*cardtable requires major pages to be 8 cards aligned*/
2600         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2601 }
2602
2603 #ifdef SGEN_HAVE_CONCURRENT_MARK
2604 void
2605 sgen_marksweep_init (SgenMajorCollector *collector)
2606 {
2607         sgen_marksweep_init_internal (collector, FALSE);
2608 }
2609
2610 void
2611 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2612 {
2613         sgen_marksweep_init_internal (collector, TRUE);
2614 }
2615 #endif
2616
2617 #endif