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