Handle OOM on string intern icall.
[mono.git] / mono / metadata / sgen-marksweep.c
index 6f48216962c65f70dc133af2f0cef8e46858eb2b..14958a07211de3600414c5b4e18186c07a2f5024 100644 (file)
@@ -1,6 +1,54 @@
+/*
+ * sgen-marksweep.c: Simple generational GC.
+ *
+ * Author:
+ *     Mark Probst <mark.probst@gmail.com>
+ *
+ * Copyright 2009-2010 Novell, Inc.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_SGEN_GC
+
 #include <math.h>
 
+#include "utils/mono-counters.h"
+#include "metadata/object-internals.h"
+#include "metadata/profiler-private.h"
+
+#include "metadata/sgen-gc.h"
+#include "metadata/sgen-protocol.h"
+#include "metadata/sgen-cardtable.h"
+#include "metadata/gc-internal.h"
+
+#define DEBUG(l,x)
+
 #define MS_BLOCK_SIZE  (16*1024)
+#define MS_BLOCK_SIZE_SHIFT    14
+#define MAJOR_SECTION_SIZE     MS_BLOCK_SIZE
+#define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
+
+#ifdef FIXED_HEAP
+#define MS_DEFAULT_HEAP_NUM_BLOCKS     (32 * 1024) /* 512 MB */
+#endif
 
 /*
  * Don't allocate single blocks, but alloc a contingent of this many
  * of a block is the MSBlockHeader, then opional padding, then come
  * the objects, so this must be >= sizeof (MSBlockHeader).
  */
+#ifdef FIXED_HEAP
+#define MS_BLOCK_SKIP  0
+#else
 #define MS_BLOCK_SKIP  16
+#endif
 
 #define MS_BLOCK_FREE  (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
 
-#define MS_NUM_MARK_WORDS(c)   (((c) + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
+#define MS_NUM_MARK_WORDS      ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
 
-#if MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
-#error MAX_SMALL_OBJ_SIZE must be at most (MS_BLOCK_SIZE - MS_BLOCK_SKIP) / 2
+#if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
+#error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
 #endif
 
 typedef struct _MSBlockInfo MSBlockInfo;
 struct _MSBlockInfo {
        int obj_size;
+       int obj_size_index;
        gboolean pinned;
        gboolean has_references;
+#ifndef SGEN_PARALLEL_MARK
+       gboolean has_pinned;    /* means cannot evacuate */
+       gboolean is_to_space;
+#endif
+#ifdef FIXED_HEAP
+       gboolean used;
+#else
+       MSBlockInfo *next;
+#endif
        char *block;
        void **free_list;
        MSBlockInfo *next_free;
-       MSBlockInfo *next;
-       int pin_queue_start;
-       int pin_queue_end;
-       mword mark_words [1];
+       void **pin_queue_start;
+       int pin_queue_num_entries;
+       mword mark_words [MS_NUM_MARK_WORDS];
 };
 
-#define MS_BLOCK_OBJ(b,i)      ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
+#ifdef FIXED_HEAP
+static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
+
+#define ms_heap_start  nursery_end
+static char *ms_heap_end;
 
+#define MS_PTR_IN_SMALL_MAJOR_HEAP(p)  ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
+
+/* array of all all block infos in the system */
+static MSBlockInfo *block_infos;
+#endif
+
+#define MS_BLOCK_OBJ(b,i)              ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
+#define MS_BLOCK_DATA_FOR_OBJ(o)       ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
+
+#ifdef FIXED_HEAP
+#define MS_BLOCK_FOR_OBJ(o)            (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
+#else
 typedef struct {
        MSBlockInfo *info;
 } MSBlockHeader;
 
-#define MS_BLOCK_FOR_OBJ(o)    (((MSBlockHeader*)((mword)(o) & ~(MS_BLOCK_SIZE-1)))->info)
+#define MS_BLOCK_FOR_OBJ(o)            (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
+#endif
 
 #define MS_BLOCK_OBJ_INDEX(o,b)        (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
 
-#define MS_CALC_MARK_BIT(w,b,i)        do {            \
-               if (sizeof (mword) == 4) {      \
-                       (w) = (i) >> 5;         \
-                       (b) = (i) & 31;         \
-               } else {                        \
-                       (w) = (i) >> 6;         \
-                       (b) = (i) & 63;         \
-               }                               \
+#define MS_CALC_MARK_BIT(w,b,o)        do {                            \
+               int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
+               if (sizeof (mword) == 4) {                              \
+                       (w) = i >> 5;                                   \
+                       (b) = i & 31;                                   \
+               } else {                                                \
+                       (w) = i >> 6;                                   \
+                       (b) = i & 63;                                   \
+               }                                                       \
        } while (0)
 
 #define MS_MARK_BIT(bl,w,b)    ((bl)->mark_words [(w)] & (1L << (b)))
 #define MS_SET_MARK_BIT(bl,w,b)        ((bl)->mark_words [(w)] |= (1L << (b)))
+#define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b) do {                    \
+               mword __old = (bl)->mark_words [(w)];                   \
+               mword __bitmask = 1L << (b);                            \
+               if (__old & __bitmask) {                                \
+                       was_marked = TRUE;                              \
+                       break;                                          \
+               }                                                       \
+               if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)],   \
+                                               (gpointer)(__old | __bitmask), \
+                                               (gpointer)__old) ==     \
+                               (gpointer)__old) {                      \
+                       was_marked = FALSE;                             \
+                       break;                                          \
+               }                                                       \
+       } while (1)
 
 #define MS_OBJ_ALLOCED(o,b)    (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
 
 #define MS_BLOCK_OBJ_SIZE_FACTOR       (sqrt (2.0))
 
-#define MIN_MINOR_COLLECTION_SECTION_ALLOWANCE (DEFAULT_NURSERY_SIZE * 3 / MS_BLOCK_SIZE)
-
-static int minor_collection_section_allowance;
-static int minor_collection_sections_alloced = 0;
-
 /*
  * This way we can lookup block object size indexes for sizes up to
  * 256 bytes with a single load.
@@ -84,20 +173,63 @@ static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
 
 #define MS_BLOCK_TYPE_MAX      4
 
-/* all blocks in the system */
+#ifdef SGEN_PARALLEL_MARK
+static LOCK_DECLARE (ms_block_list_mutex);
+#define LOCK_MS_BLOCK_LIST pthread_mutex_lock (&ms_block_list_mutex)
+#define UNLOCK_MS_BLOCK_LIST pthread_mutex_unlock (&ms_block_list_mutex)
+#else
+#define LOCK_MS_BLOCK_LIST
+#define UNLOCK_MS_BLOCK_LIST
+#endif
+
+/* we get this at init */
+static int nursery_bits;
+static char *nursery_start;
+static char *nursery_end;
+
+#ifndef SGEN_PARALLEL_MARK
+static gboolean *evacuate_block_obj_sizes;
+static float evacuation_threshold = 0.666;
+#endif
+
+#define ptr_in_nursery(p)      (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
+
+#ifdef FIXED_HEAP
+/* non-allocated block free-list */
+static MSBlockInfo *empty_blocks = NULL;
+#else
+/* non-allocated block free-list */
+static void *empty_blocks = NULL;
+/* all allocated blocks in the system */
 static MSBlockInfo *all_blocks;
+static int num_empty_blocks = 0;
+#endif
+
+#ifdef FIXED_HEAP
+#define FOREACH_BLOCK(bl)      {                                       \
+               int __block_i;                                          \
+               for (__block_i = 0; __block_i < ms_heap_num_blocks; ++__block_i) { \
+                       (bl) = &block_infos [__block_i];                \
+                       if (!(bl)->used) continue;
+#define END_FOREACH_BLOCK      }}
+#else
+#define FOREACH_BLOCK(bl)      for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
+#define END_FOREACH_BLOCK      }
+#endif
+
 static int num_major_sections = 0;
 /* one free block list for each block object size */
 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
 
-static long stat_major_blocks_alloced = 0;
-static long stat_major_blocks_freed = 0;
+static long long stat_major_blocks_alloced = 0;
+static long long stat_major_blocks_freed = 0;
+static long long stat_major_objects_evacuated = 0;
 
 static int
 ms_find_block_obj_size_index (int size)
 {
        int i;
-       DEBUG (9, g_assert (size <= MAX_SMALL_OBJ_SIZE));
+       DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
        for (i = 0; i < num_block_obj_sizes; ++i)
                if (block_obj_sizes [i] >= size)
                        return i;
@@ -111,56 +243,152 @@ ms_find_block_obj_size_index (int size)
         fast_block_obj_size_indexes [((s)+7)>>3] :             \
         ms_find_block_obj_size_index ((s)))
 
-void *empty_blocks = NULL;
-int num_empty_blocks = 0;
+#ifdef FIXED_HEAP
+static void*
+major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
+{
+       char *heap_start;
+       mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
+       mword alloc_size = nursery_size + major_heap_size;
+       int i;
+
+       g_assert (ms_heap_num_blocks > 0);
+       g_assert (nursery_size % MS_BLOCK_SIZE == 0);
+       if (nursery_align)
+               g_assert (nursery_align % MS_BLOCK_SIZE == 0);
+
+       nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
+       nursery_end = heap_start = nursery_start + nursery_size;
+       nursery_bits = the_nursery_bits;
+
+       ms_heap_end = heap_start + major_heap_size;
+
+       block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
+
+       for (i = 0; i < ms_heap_num_blocks; ++i) {
+               block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
+               if (i < ms_heap_num_blocks - 1)
+                       block_infos [i].next_free = &block_infos [i + 1];
+               else
+                       block_infos [i].next_free = NULL;
+       }
+
+       empty_blocks = &block_infos [0];
 
+       return nursery_start;
+}
+#else
+static void*
+major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
+{
+       if (nursery_align)
+               nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
+       else
+               nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
+
+       nursery_end = nursery_start + nursery_size;
+       nursery_bits = the_nursery_bits;
+
+       return nursery_start;
+}
+#endif
+
+#ifdef FIXED_HEAP
+static MSBlockInfo*
+ms_get_empty_block (void)
+{
+       MSBlockInfo *block;
+
+       g_assert (empty_blocks);
+
+       block = empty_blocks;
+       empty_blocks = empty_blocks->next_free;
+
+       block->used = TRUE;
+
+       mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
+
+       return block;
+}
+
+static void
+ms_free_block (MSBlockInfo *block)
+{
+       block->next_free = empty_blocks;
+       empty_blocks = block;
+       block->used = FALSE;
+       mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
+}
+#else
 static void*
 ms_get_empty_block (void)
 {
        char *p;
        int i;
-       void *block;
+       void *block, *empty, *next;
 
+ retry:
        if (!empty_blocks) {
-               g_assert (num_empty_blocks == 0);
-
-               p = get_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
+               p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
 
                for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
                        block = p;
-                       *(void**)block = empty_blocks;
-                       empty_blocks = block;
+                       /*
+                        * We do the free list update one after the
+                        * other so that other threads can use the new
+                        * blocks as quickly as possible.
+                        */
+                       do {
+                               empty = empty_blocks;
+                               *(void**)block = empty;
+                       } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
                        p += MS_BLOCK_SIZE;
                }
 
-               num_empty_blocks += MS_BLOCK_ALLOC_NUM;
-       }
+               SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
 
-       g_assert (empty_blocks);
+               stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
+       }
 
-       block = empty_blocks;
-       empty_blocks = *(void**)empty_blocks;
+       do {
+               empty = empty_blocks;
+               if (!empty)
+                       goto retry;
+               block = empty;
+               next = *(void**)block;
+       } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
 
-       --num_empty_blocks;
+       SGEN_ATOMIC_ADD (num_empty_blocks, -1);
 
        *(void**)block = NULL;
 
        g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
 
+       mono_sgen_update_heap_boundaries ((mword)block, (mword)block + MS_BLOCK_SIZE);
+
        return block;
 }
 
 static void
 ms_free_block (void *block)
 {
+       void *empty;
+
+       mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
        memset (block, 0, MS_BLOCK_SIZE);
 
-       *(void**)block = empty_blocks;
-       empty_blocks = block;
+       do {
+               empty = empty_blocks;
+               *(void**)block = empty;
+       } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
 
-       ++num_empty_blocks;
+       SGEN_ATOMIC_ADD (num_empty_blocks, 1);
 }
+#endif
+
+//#define MARKSWEEP_CONSISTENCY_CHECK
 
+#ifdef MARKSWEEP_CONSISTENCY_CHECK
 static void
 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
 {
@@ -174,15 +402,33 @@ check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
                   one free slot */
                g_assert (block->free_list);
 
+#ifdef FIXED_HEAP
+               /* the block must not be in the empty_blocks list */
+               for (b = empty_blocks; b; b = b->next_free)
+                       g_assert (b != block);
+#else
                /* the block must be in the all_blocks list */
                for (b = all_blocks; b; b = b->next) {
                        if (b == block)
                                break;
                }
                g_assert (b == block);
+#endif
        }
 }
 
+static void
+check_empty_blocks (void)
+{
+#ifndef FIXED_HEAP
+       void *p;
+       int i = 0;
+       for (p = empty_blocks; p; p = *(void**)p)
+               ++i;
+       g_assert (i == num_empty_blocks);
+#endif
+}
+
 static void
 consistency_check (void)
 {
@@ -190,14 +436,15 @@ consistency_check (void)
        int i;
 
        /* check all blocks */
-       for (block = all_blocks; block; block = block->next) {
+       FOREACH_BLOCK (block) {
                int count = MS_BLOCK_FREE / block->obj_size;
-               int num_mark_words = MS_NUM_MARK_WORDS (count);
                int num_free = 0;
                void **free;
 
+#ifndef FIXED_HEAP
                /* check block header */
                g_assert (((MSBlockHeader*)block->block)->info == block);
+#endif
 
                /* count number of free slots */
                for (i = 0; i < count; ++i) {
@@ -214,9 +461,9 @@ consistency_check (void)
                g_assert (num_free == 0);
 
                /* check all mark words are zero */
-               for (i = 0; i < num_mark_words; ++i)
+               for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
                        g_assert (block->mark_words [i] == 0);
-       }
+       } END_FOREACH_BLOCK;
 
        /* check free blocks */
        for (i = 0; i < num_block_obj_sizes; ++i) {
@@ -224,50 +471,72 @@ consistency_check (void)
                for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
                        check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
        }
+
+       check_empty_blocks ();
 }
+#endif
 
-static void
+static gboolean
 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
 {
        int size = block_obj_sizes [size_index];
        int count = MS_BLOCK_FREE / size;
-       int num_mark_words = MS_NUM_MARK_WORDS (count);
-       int block_info_size = sizeof (MSBlockInfo) + sizeof (mword) * (num_mark_words - 1);
-       MSBlockInfo *info = get_internal_mem (block_info_size, INTERNAL_MEM_MS_BLOCK_INFO);
+       MSBlockInfo *info;
+#ifndef FIXED_HEAP
        MSBlockHeader *header;
+#endif
        MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
        char *obj_start;
        int i;
 
+       if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
+               return FALSE;
+
+#ifdef FIXED_HEAP
+       info = ms_get_empty_block ();
+#else
+       info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
+#endif
+
        DEBUG (9, g_assert (count >= 2));
 
        info->obj_size = size;
+       info->obj_size_index = size_index;
        info->pinned = pinned;
        info->has_references = has_references;
+#ifndef SGEN_PARALLEL_MARK
+       info->has_pinned = pinned;
+       info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
+#endif
+#ifndef FIXED_HEAP
        info->block = ms_get_empty_block ();
 
        header = (MSBlockHeader*) info->block;
        header->info = info;
+#endif
 
        /* build free list */
        obj_start = info->block + MS_BLOCK_SKIP;
        info->free_list = (void**)obj_start;
-       /* we're skipping the last one - it's already NULL */
+       /* we're skipping the last one - it must be nulled */
        for (i = 0; i < count - 1; ++i) {
                char *next_obj_start = obj_start + size;
                *(void**)obj_start = next_obj_start;
                obj_start = next_obj_start;
        }
+       /* the last one */
+       *(void**)obj_start = NULL;
 
        info->next_free = free_blocks [size_index];
        free_blocks [size_index] = info;
 
+#ifndef FIXED_HEAP
        info->next = all_blocks;
        all_blocks = info;
+#endif
 
        ++num_major_sections;
-
-       ++stat_major_blocks_alloced;
+       return TRUE;
 }
 
 static gboolean
@@ -285,8 +554,16 @@ alloc_obj (int size, gboolean pinned, gboolean has_references)
        MSBlockInfo *block;
        void *obj;
 
-       if (!free_blocks [size_index])
-               ms_alloc_block (size_index, pinned, has_references);
+       /* FIXME: try to do this without locking */
+
+       LOCK_MS_BLOCK_LIST;
+
+       if (!free_blocks [size_index]) {
+               if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) {
+                       UNLOCK_MS_BLOCK_LIST;
+                       return NULL;
+               }
+       }
 
        block = free_blocks [size_index];
        DEBUG (9, g_assert (block));
@@ -300,6 +577,8 @@ alloc_obj (int size, gboolean pinned, gboolean has_references)
                block->next_free = NULL;
        }
 
+       UNLOCK_MS_BLOCK_LIST;
+
        /*
         * FIXME: This should not be necessary because it'll be
         * overwritten by the vtable immediately.
@@ -310,19 +589,17 @@ alloc_obj (int size, gboolean pinned, gboolean has_references)
 }
 
 static void*
-ms_alloc_obj (int size, gboolean has_references)
+major_alloc_object (int size, gboolean has_references)
 {
        return alloc_obj (size, FALSE, has_references);
 }
 
-/* FIXME: inline fast path */
-#define MAJOR_GET_COPY_OBJECT_SPACE(dest, size, refs) do {     \
-               (dest) = ms_alloc_obj ((size), (refs));         \
-       } while (0)
-
 /*
  * We're not freeing the block if it's empty.  We leave that work for
  * the next major collection.
+ *
+ * This is just called from the domain clearing code, which runs in a
+ * single thread and has the GC lock, so we don't need an extra lock.
  */
 static void
 free_object (char *obj, size_t size, gboolean pinned)
@@ -331,7 +608,7 @@ free_object (char *obj, size_t size, gboolean pinned)
        int word, bit;
        DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
        DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
-       MS_CALC_MARK_BIT (word, bit, MS_BLOCK_OBJ_INDEX (obj, block));
+       MS_CALC_MARK_BIT (word, bit, obj);
        DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
        if (!block->free_list) {
                MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
@@ -351,11 +628,19 @@ major_free_non_pinned_object (char *obj, size_t size)
        free_object (obj, size, FALSE);
 }
 
-/* size is a multiple of ALLOC_ALIGN */
+/* size is a multiple of SGEN_ALLOC_ALIGN */
 static void*
 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
 {
-       return alloc_obj (size, TRUE, has_references);
+        void *res = alloc_obj (size, TRUE, has_references);
+        /*If we failed to alloc memory, we better try releasing memory
+         *as pinned alloc is requested by the runtime.
+         */
+        if (!res) {
+                sgen_collect_major_no_lock ("pinned alloc failure");
+                res = alloc_obj (size, TRUE, has_references);
+        }
+        return res;
 }
 
 static void
@@ -368,12 +653,18 @@ free_pinned_object (char *obj, size_t size)
  * size is already rounded up and we hold the GC lock.
  */
 static void*
-alloc_degraded (MonoVTable *vtable, size_t size)
+major_alloc_degraded (MonoVTable *vtable, size_t size)
 {
-       void *obj = alloc_obj (size, FALSE, vtable->klass->has_references);
-       *(MonoVTable**)obj = vtable;
-       HEAVY_STAT (++stat_objects_alloced_degraded);
-       HEAVY_STAT (stat_bytes_alloced_degraded += size);
+       void *obj;
+       int old_num_sections = num_major_sections;
+       obj = alloc_obj (size, FALSE, vtable->klass->has_references);
+       if (G_LIKELY (obj)) {
+               *(MonoVTable**)obj = vtable;
+               HEAVY_STAT (++stat_objects_alloced_degraded);
+               HEAVY_STAT (stat_bytes_alloced_degraded += size);
+               g_assert (num_major_sections >= old_num_sections);
+               mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
+       }
        return obj;
 }
 
@@ -389,23 +680,29 @@ major_is_object_live (char *obj)
 {
        MSBlockInfo *block;
        int word, bit;
+#ifndef FIXED_HEAP
        mword objsize;
+#endif
 
        if (ptr_in_nursery (obj))
                return FALSE;
 
-       objsize = safe_object_get_size ((MonoObject*)obj);
-       objsize += ALLOC_ALIGN - 1;
-       objsize &= ~(ALLOC_ALIGN - 1);
+#ifdef FIXED_HEAP
+       /* LOS */
+       if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
+               return FALSE;
+#else
+       objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
 
        /* LOS */
-       if (objsize > MAX_SMALL_OBJ_SIZE)
+       if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
                return FALSE;
+#endif
 
        /* now we know it's in a major block */
        block = MS_BLOCK_FOR_OBJ (obj);
        DEBUG (9, g_assert (!block->pinned));
-       MS_CALC_MARK_BIT (word, bit, MS_BLOCK_OBJ_INDEX (obj, block));
+       MS_CALC_MARK_BIT (word, bit, obj);
        return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
 }
 
@@ -420,7 +717,7 @@ major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallba
 {
        MSBlockInfo *block;
 
-       for (block = all_blocks; block; block = block->next) {
+       FOREACH_BLOCK (block) {
                int count = MS_BLOCK_FREE / block->obj_size;
                int i;
 
@@ -434,17 +731,44 @@ major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallba
                        if (MS_OBJ_ALLOCED (obj, block))
                                callback ((char*)obj, block->obj_size, data);
                }
-       }
+       } END_FOREACH_BLOCK;
 }
 
-#define major_check_scan_starts()
+static void
+major_check_scan_starts (void)
+{
+}
 
 static void
-major_dump_heap (void)
+major_dump_heap (FILE *heap_dump_file)
 {
        MSBlockInfo *block;
+       int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
+       int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
+       int i;
+
+       for (i = 0; i < num_block_obj_sizes; ++i)
+               slots_available [i] = slots_used [i] = 0;
+
+       FOREACH_BLOCK (block) {
+               int index = ms_find_block_obj_size_index (block->obj_size);
+               int count = MS_BLOCK_FREE / block->obj_size;
+
+               slots_available [index] += count;
+               for (i = 0; i < count; ++i) {
+                       if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
+                               ++slots_used [index];
+               }
+       } END_FOREACH_BLOCK;
+
+       fprintf (heap_dump_file, "<occupancies>\n");
+       for (i = 0; i < num_block_obj_sizes; ++i) {
+               fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
+                               block_obj_sizes [i], slots_available [i], slots_used [i]);
+       }
+       fprintf (heap_dump_file, "</occupancies>\n");
 
-       for (block = all_blocks; block; block = block->next) {
+       FOREACH_BLOCK (block) {
                int count = MS_BLOCK_FREE / block->obj_size;
                int i;
                int start = -1;
@@ -457,48 +781,63 @@ major_dump_heap (void)
                                        start = i;
                        } else {
                                if (start >= 0) {
-                                       dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
+                                       mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
                                        start = -1;
                                }
                        }
                }
 
                fprintf (heap_dump_file, "</section>\n");
-       }
+       } END_FOREACH_BLOCK;
 }
 
-#define MS_MARK_INDEX_IN_BLOCK_AND_ENQUEUE_CHECKED(obj,block,index) do { \
+#define LOAD_VTABLE    SGEN_LOAD_VTABLE
+
+#define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {       \
                int __word, __bit;                                      \
-               MS_CALC_MARK_BIT (__word, __bit, (index));              \
-               DEBUG (9, g_assert ((obj) == MS_BLOCK_OBJ ((block), (index)))); \
+               MS_CALC_MARK_BIT (__word, __bit, (obj));                \
                if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
                        MS_SET_MARK_BIT ((block), __word, __bit);       \
                        if ((block)->has_references)                    \
-                               GRAY_OBJECT_ENQUEUE ((obj));            \
-                       binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), safe_object_get_size ((MonoObject*)(obj))); \
+                               GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
+                       binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
                }                                                       \
        } while (0)
-#define MS_MARK_INDEX_IN_BLOCK_AND_ENQUEUE(obj,block,index) do {       \
+#define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {               \
                int __word, __bit;                                      \
-               MS_CALC_MARK_BIT (__word, __bit, (index));              \
-               DEBUG (9, g_assert ((obj) == MS_BLOCK_OBJ ((block), (index)))); \
+               MS_CALC_MARK_BIT (__word, __bit, (obj));                \
                DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
                if (!MS_MARK_BIT ((block), __word, __bit)) {            \
                        MS_SET_MARK_BIT ((block), __word, __bit);       \
                        if ((block)->has_references)                    \
-                               GRAY_OBJECT_ENQUEUE ((obj));            \
-                       binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), safe_object_get_size ((MonoObject*)(obj))); \
+                               GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
+                       binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
+               }                                                       \
+       } while (0)
+#define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {           \
+               int __word, __bit;                                      \
+               gboolean __was_marked;                                  \
+               DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
+               MS_CALC_MARK_BIT (__word, __bit, (obj));                \
+               MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
+               if (!__was_marked) {                                    \
+                       if ((block)->has_references)                    \
+                               GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
+                       binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
                }                                                       \
        } while (0)
 
+#include "sgen-major-copy-object.h"
+
+#ifdef SGEN_PARALLEL_MARK
 static void
-major_copy_or_mark_object (void **ptr)
+major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
 {
        void *obj = *ptr;
+       mword vtable_word = *(mword*)obj;
+       MonoVTable *vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
        mword objsize;
        MSBlockInfo *block;
-       int index;
-       int count;
 
        HEAVY_STAT (++stat_copy_object_called_major);
 
@@ -507,18 +846,130 @@ major_copy_or_mark_object (void **ptr)
 
        if (ptr_in_nursery (obj)) {
                int word, bit;
-               char *forwarded;
+               gboolean has_references;
+               void *destination;
+
+               if (vtable_word & SGEN_FORWARDED_BIT) {
+                       *ptr = (void*)vt;
+                       return;
+               }
+
+               if (vtable_word & SGEN_PINNED_BIT)
+                       return;
 
-               if ((forwarded = object_is_forwarded (obj))) {
+               HEAVY_STAT (++stat_objects_copied_major);
+
+               objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
+               has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
+
+               destination = major_alloc_object (objsize, has_references);
+               if (G_UNLIKELY (!destination)) {
+                       do {
+                               if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
+                                       mono_sgen_pin_object (obj, queue);
+                                       break;
+                               }
+
+                               vtable_word = *(mword*)obj;
+                               /*someone else forwarded it, update the pointer and bail out*/
+                               if (vtable_word & SGEN_FORWARDED_BIT) {
+                                       *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
+                                       break;
+                               }
+
+                               /*someone pinned it, nothing to do.*/
+                               if (vtable_word & SGEN_PINNED_BIT)
+                                       break;
+                       } while (TRUE);
+                       return;
+               }
+
+               if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
+                       gboolean was_marked;
+
+                       par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
+                       obj = destination;
+                       *ptr = obj;
+
+                       /*
+                        * FIXME: If we make major_alloc_object() give
+                        * us the block info, too, we won't have to
+                        * re-fetch it here.
+                        */
+                       block = MS_BLOCK_FOR_OBJ (obj);
+                       MS_CALC_MARK_BIT (word, bit, obj);
+                       DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
+                       MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
+               } else {
+                       /*
+                        * FIXME: We have allocated destination, but
+                        * we cannot use it.  Give it back to the
+                        * allocator.
+                        */
+                       *(void**)destination = NULL;
+
+                       vtable_word = *(mword*)obj;
+                       g_assert (vtable_word & SGEN_FORWARDED_BIT);
+
+                       obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
+
+                       *ptr = obj;
+               }
+       } else {
+#ifdef FIXED_HEAP
+               if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
+#else
+               objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
+
+               if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
+#endif
+               {
+                       block = MS_BLOCK_FOR_OBJ (obj);
+                       MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
+               } else {
+                       if (vtable_word & SGEN_PINNED_BIT)
+                               return;
+                       binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
+                       if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
+                               if (SGEN_VTABLE_HAS_REFERENCES (vt))
+                                       GRAY_OBJECT_ENQUEUE (queue, obj);
+                       } else {
+                               g_assert (SGEN_OBJECT_IS_PINNED (obj));
+                       }
+               }
+       }
+}
+#else
+static void
+major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
+{
+       void *obj = *ptr;
+       MSBlockInfo *block;
+
+       HEAVY_STAT (++stat_copy_object_called_major);
+
+       DEBUG (9, g_assert (obj));
+       DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
+
+       if (ptr_in_nursery (obj)) {
+               int word, bit;
+               char *forwarded, *old_obj;
+
+               if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
                        *ptr = forwarded;
                        return;
                }
-               if (object_is_pinned (obj))
+               if (SGEN_OBJECT_IS_PINNED (obj))
                        return;
 
                HEAVY_STAT (++stat_objects_copied_major);
 
-               obj = copy_object_no_checks (obj);
+       do_copy_object:
+               old_obj = obj;
+               obj = copy_object_no_checks (obj, queue);
+               if (G_UNLIKELY (old_obj == obj)) {
+                       return;
+               }
                *ptr = obj;
 
                /*
@@ -528,48 +979,72 @@ major_copy_or_mark_object (void **ptr)
                 * re-fetch it.
                 */
                block = MS_BLOCK_FOR_OBJ (obj);
-               index = MS_BLOCK_OBJ_INDEX (obj, block);
-               DEBUG (9, g_assert (obj == MS_BLOCK_OBJ (block, index)));
-               MS_CALC_MARK_BIT (word, bit, (index));
+               MS_CALC_MARK_BIT (word, bit, obj);
                DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
                MS_SET_MARK_BIT (block, word, bit);
-               return;
-       }
+       } else {
+               char *forwarded;
+               if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
+                       *ptr = forwarded;
+                       return;
+               }
 
-       objsize = safe_object_get_size ((MonoObject*)obj);
-       objsize += ALLOC_ALIGN - 1;
-       objsize &= ~(ALLOC_ALIGN - 1);
+#ifdef FIXED_HEAP
+               if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
+#else
+               mword objsize;
 
-       if (objsize > MAX_SMALL_OBJ_SIZE) {
-               if (object_is_pinned (obj))
-                       return;
-               binary_protocol_pin (obj, (gpointer)LOAD_VTABLE (obj), safe_object_get_size ((MonoObject*)obj));
-               pin_object (obj);
-               /* FIXME: only enqueue if object has references */
-               GRAY_OBJECT_ENQUEUE (obj);
-               return;
-       }
+               objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
 
-       block = MS_BLOCK_FOR_OBJ (obj);
-       index = MS_BLOCK_OBJ_INDEX (obj, block);
-       count = MS_BLOCK_FREE / block->obj_size;
-       DEBUG (9, g_assert (index >= 0 && index < count));
-       MS_MARK_INDEX_IN_BLOCK_AND_ENQUEUE (obj, block, index);
+               if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
+#endif
+               {
+                       int size_index;
+
+                       block = MS_BLOCK_FOR_OBJ (obj);
+                       size_index = block->obj_size_index;
+
+                       if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
+                               if (block->is_to_space)
+                                       return;
+                               HEAVY_STAT (++stat_major_objects_evacuated);
+                               goto do_copy_object;
+                       } else {
+                               MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
+                       }
+               } else {
+                       if (SGEN_OBJECT_IS_PINNED (obj))
+                               return;
+                       binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
+                       SGEN_PIN_OBJECT (obj);
+                       /* FIXME: only enqueue if object has references */
+                       GRAY_OBJECT_ENQUEUE (queue, obj);
+               }
+       }
 }
+#endif
+
+#include "sgen-major-scan-object.h"
 
 static void
-mark_pinned_objects_in_block (MSBlockInfo *block)
+mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
 {
        int i;
        int last_index = -1;
-       int count = MS_BLOCK_FREE / block->obj_size;
 
-       for (i = block->pin_queue_start; i < block->pin_queue_end; ++i) {
-               int index = MS_BLOCK_OBJ_INDEX (pin_queue [i], block);
-               DEBUG (9, g_assert (index >= 0 && index < count));
+       if (!block->pin_queue_num_entries)
+               return;
+
+#ifndef SGEN_PARALLEL_MARK
+       block->has_pinned = TRUE;
+#endif
+
+       for (i = 0; i < block->pin_queue_num_entries; ++i) {
+               int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
+               DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
                if (index == last_index)
                        continue;
-               MS_MARK_INDEX_IN_BLOCK_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, index);
+               MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
                last_index = index;
        }
 }
@@ -577,54 +1052,87 @@ mark_pinned_objects_in_block (MSBlockInfo *block)
 static void
 major_sweep (void)
 {
-       MSBlockInfo *empty_blocks = NULL;
-       MSBlockInfo **iter;
        int i;
+#ifdef FIXED_HEAP
+       int j;
+#else
+       MSBlockInfo **iter;
+#endif
+#ifndef SGEN_PARALLEL_MARK
+       /* statistics for evacuation */
+       int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
+       int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
+       int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
+
+       for (i = 0; i < num_block_obj_sizes; ++i)
+               slots_available [i] = slots_used [i] = num_blocks [i] = 0;
+#endif
+
+       /* clear all the free lists */
+       for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
+               MSBlockInfo **free_blocks = free_block_lists [i];
+               int j;
+               for (j = 0; j < num_block_obj_sizes; ++j)
+                       free_blocks [j] = NULL;
+       }
 
        /* traverse all blocks, free and zero unmarked objects */
+#ifdef FIXED_HEAP
+       for (j = 0; j < ms_heap_num_blocks; ++j) {
+               MSBlockInfo *block = &block_infos [j];
+#else
        iter = &all_blocks;
        while (*iter) {
                MSBlockInfo *block = *iter;
-               int count = MS_BLOCK_FREE / block->obj_size;
+#endif
+               int count;
                gboolean have_live = FALSE;
-               int word_index = 0;
-               int obj_index = 0;
+               gboolean has_pinned;
+               int obj_index;
+               int obj_size_index;
+
+#ifdef FIXED_HEAP
+               if (!block->used)
+                       continue;
+#endif
+
+               obj_size_index = block->obj_size_index;
+
+#ifndef SGEN_PARALLEL_MARK
+               has_pinned = block->has_pinned;
+               block->has_pinned = block->pinned;
+
+               block->is_to_space = FALSE;
+#endif
 
+               count = MS_BLOCK_FREE / block->obj_size;
                block->free_list = NULL;
 
-               do {
-                       mword word = block->mark_words [word_index];
+               for (obj_index = 0; obj_index < count; ++obj_index) {
+                       int word, bit;
+                       void *obj = MS_BLOCK_OBJ (block, obj_index);
 
-                       if (word == (mword)-1L) {
-                               obj_index += 8 * sizeof (mword);
+                       MS_CALC_MARK_BIT (word, bit, obj);
+                       if (MS_MARK_BIT (block, word, bit)) {
+                               DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
                                have_live = TRUE;
+#ifndef SGEN_PARALLEL_MARK
+                               if (!has_pinned)
+                                       ++slots_used [obj_size_index];
+#endif
                        } else {
-                               int num_bits = MIN (count - obj_index, 8 * sizeof (mword));
-                               for (i = 0; i < num_bits; ++i) {
-                                       void *obj = MS_BLOCK_OBJ (block, obj_index);
-                                       if (word & 1) {
-                                               DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
-                                               have_live = TRUE;
-                                       } else {
-                                               /* an unmarked object */
-                                               void *obj = MS_BLOCK_OBJ (block, obj_index);
-                                               if (MS_OBJ_ALLOCED (obj, block)) {
-                                                       binary_protocol_empty (obj, block->obj_size);
-                                                       memset (obj, 0, block->obj_size);
-                                               }
-                                               *(void**)obj = block->free_list;
-                                               block->free_list = obj;
-                                       }
-                                       word >>= 1;
-                                       ++obj_index;
+                               /* an unmarked object */
+                               if (MS_OBJ_ALLOCED (obj, block)) {
+                                       binary_protocol_empty (obj, block->obj_size);
+                                       memset (obj, 0, block->obj_size);
                                }
+                               *(void**)obj = block->free_list;
+                               block->free_list = obj;
                        }
+               }
 
-                       /* reset mark bits */
-                       block->mark_words [word_index] = 0;
-
-                       ++word_index;
-               } while (obj_index < count);
+               /* reset mark bits */
+               memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
 
                /*
                 * FIXME: reverse free list so that it's in address
@@ -632,54 +1140,59 @@ major_sweep (void)
                 */
 
                if (have_live) {
+#ifndef SGEN_PARALLEL_MARK
+                       if (!has_pinned) {
+                               ++num_blocks [obj_size_index];
+                               slots_available [obj_size_index] += count;
+                       }
+#endif
+
+#ifndef FIXED_HEAP
                        iter = &block->next;
-                       continue;
-               }
+#endif
 
-               /*
-                * blocks without live objects are removed from the
-                * block list and marked with obj_size = -1
-                */
-               *iter = block->next;
-               block->next = empty_blocks;
-               empty_blocks = block;
+                       /*
+                        * If there are free slots in the block, add
+                        * the block to the corresponding free list.
+                        */
+                       if (block->free_list) {
+                               MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
+                               int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
+                               block->next_free = free_blocks [index];
+                               free_blocks [index] = block;
+                       }
+               } else {
+                       /*
+                        * Blocks without live objects are removed from the
+                        * block list and freed.
+                        */
+#ifdef FIXED_HEAP
+                       ms_free_block (block);
+#else
+                       *iter = block->next;
+
+                       ms_free_block (block->block);
+                       mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
+#endif
 
-               block->obj_size = -1;
+                       --num_major_sections;
+               }
        }
 
-       /* if there are no blocks to be freed, we're done */
-       if (!empty_blocks)
-               return;
-
-       /* go through all free lists and remove the blocks to be freed */
+#ifndef SGEN_PARALLEL_MARK
        for (i = 0; i < num_block_obj_sizes; ++i) {
-               int j;
-               for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j) {
-                       MSBlockInfo **free_blocks = free_block_lists [j];
-                       iter = &(free_blocks [i]);
-                       while (*iter) {
-                               MSBlockInfo *block = *iter;
-                               if (block->obj_size < 0)
-                                       *iter = block->next_free;
-                               else
-                                       iter = &block->next_free;
-                       }
+               float usage = (float)slots_used [i] / (float)slots_available [i];
+               if (num_blocks [i] > 5 && usage < evacuation_threshold) {
+                       evacuate_block_obj_sizes [i] = TRUE;
+                       /*
+                       g_print ("slot size %d - %d of %d used\n",
+                                       block_obj_sizes [i], slots_used [i], slots_available [i]);
+                       */
+               } else {
+                       evacuate_block_obj_sizes [i] = FALSE;
                }
        }
-
-       /* now free the blocks */
-       while (empty_blocks) {
-               MSBlockInfo *next = empty_blocks->next;
-
-               ms_free_block (empty_blocks->block);
-               free_internal_mem (empty_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
-
-               empty_blocks = next;
-
-               --num_major_sections;
-
-               ++stat_major_blocks_freed;
-       }
+#endif
 }
 
 static int count_pinned_ref;
@@ -709,7 +1222,7 @@ count_pinned_callback (char *obj, size_t size, void *data)
                ++count_pinned_nonref;
 }
 
-static void
+static void __attribute__ ((unused))
 count_ref_nonref_objs (void)
 {
        int total;
@@ -730,221 +1243,6 @@ count_ref_nonref_objs (void)
                        (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
 }
 
-static void
-major_do_collection (const char *reason)
-{
-       LOSObject *bigobj, *prevbo;
-       MSBlockInfo *block;
-       TV_DECLARE (all_atv);
-       TV_DECLARE (all_btv);
-       TV_DECLARE (atv);
-       TV_DECLARE (btv);
-       /* FIXME: only use these values for the precise scan
-        * note that to_space pointers should be excluded anyway...
-        */
-       char *heap_start = NULL;
-       char *heap_end = (char*)-1;
-       int old_num_major_sections = num_major_sections;
-       int num_major_sections_saved, save_target, allowance_target;
-
-       //count_ref_nonref_objs ();
-       //consistency_check ();
-
-       init_stats ();
-       binary_protocol_collection (GENERATION_OLD);
-       check_scan_starts ();
-       gray_object_queue_init ();
-
-       degraded_mode = 0;
-       DEBUG (1, fprintf (gc_debug_file, "Start major collection %d\n", num_major_gcs));
-       num_major_gcs++;
-       mono_stats.major_gc_count ++;
-
-       /* world must be stopped already */
-       TV_GETTIME (all_atv);
-       TV_GETTIME (atv);
-
-       /* Pinning depends on this */
-       clear_nursery_fragments (nursery_next);
-
-       TV_GETTIME (btv);
-       time_major_pre_collection_fragment_clear += TV_ELAPSED_MS (atv, btv);
-
-       if (xdomain_checks)
-               check_for_xdomain_refs ();
-
-       nursery_section->next_data = nursery_real_end;
-       /* we should also coalesce scanning from sections close to each other
-        * and deal with pointers outside of the sections later.
-        */
-       /* The remsets are not useful for a major collection */
-       clear_remsets ();
-
-       TV_GETTIME (atv);
-       init_pinning ();
-       DEBUG (6, fprintf (gc_debug_file, "Collecting pinned addresses\n"));
-       pin_from_roots ((void*)lowest_heap_address, (void*)highest_heap_address);
-       optimize_pin_queue (0);
-
-       /*
-        * pin_queue now contains all candidate pointers, sorted and
-        * uniqued.  We must do two passes now to figure out which
-        * objects are pinned.
-        *
-        * The first is to find within the pin_queue the area for each
-        * section.  This requires that the pin_queue be sorted.  We
-        * also process the LOS objects and pinned chunks here.
-        *
-        * The second, destructive, pass is to reduce the section
-        * areas to pointers to the actually pinned objects.
-        */
-       DEBUG (6, fprintf (gc_debug_file, "Pinning from sections\n"));
-       /* first pass for the sections */
-       find_section_pin_queue_start_end (nursery_section);
-       for (block = all_blocks; block; block = block->next) {
-               find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
-                               &block->pin_queue_start, &block->pin_queue_end);
-       }
-       /* identify possible pointers to the insize of large objects */
-       DEBUG (6, fprintf (gc_debug_file, "Pinning from large objects\n"));
-       for (bigobj = los_object_list; bigobj; bigobj = bigobj->next) {
-               int start, end;
-               find_optimized_pin_queue_area (bigobj->data, (char*)bigobj->data + bigobj->size, &start, &end);
-               if (start != end) {
-                       pin_object (bigobj->data);
-                       /* FIXME: only enqueue if object has references */
-                       GRAY_OBJECT_ENQUEUE (bigobj->data);
-                       if (heap_dump_file)
-                               pin_stats_register_object ((char*) bigobj->data, safe_object_get_size ((MonoObject*) bigobj->data));
-                       DEBUG (6, fprintf (gc_debug_file, "Marked large object %p (%s) size: %zd from roots\n", bigobj->data, safe_name (bigobj->data), bigobj->size));
-               }
-       }
-       /* second pass for the sections */
-       pin_objects_in_section (nursery_section);
-       for (block = all_blocks; block; block = block->next)
-               mark_pinned_objects_in_block (block);
-
-       TV_GETTIME (btv);
-       time_major_pinning += TV_ELAPSED_MS (atv, btv);
-       DEBUG (2, fprintf (gc_debug_file, "Finding pinned pointers: %d in %d usecs\n", next_pin_slot, TV_ELAPSED (atv, btv)));
-       DEBUG (4, fprintf (gc_debug_file, "Start scan with %d pinned objects\n", next_pin_slot));
-
-       drain_gray_stack ();
-
-       TV_GETTIME (atv);
-       time_major_scan_pinned += TV_ELAPSED_MS (btv, atv);
-
-       /* registered roots, this includes static fields */
-       scan_from_registered_roots (major_copy_or_mark_object, heap_start, heap_end, ROOT_TYPE_NORMAL);
-       scan_from_registered_roots (major_copy_or_mark_object, heap_start, heap_end, ROOT_TYPE_WBARRIER);
-       TV_GETTIME (btv);
-       time_major_scan_registered_roots += TV_ELAPSED_MS (atv, btv);
-
-       /* Threads */
-       /* FIXME: This is the wrong place for this, because it does
-          pinning */
-       scan_thread_data (heap_start, heap_end, TRUE);
-       TV_GETTIME (atv);
-       time_major_scan_thread_data += TV_ELAPSED_MS (btv, atv);
-
-       TV_GETTIME (btv);
-       time_major_scan_alloc_pinned += TV_ELAPSED_MS (atv, btv);
-
-       /* scan the list of objects ready for finalization */
-       scan_finalizer_entries (major_copy_or_mark_object, fin_ready_list);
-       scan_finalizer_entries (major_copy_or_mark_object, critical_fin_list);
-       TV_GETTIME (atv);
-       time_major_scan_finalized += TV_ELAPSED_MS (btv, atv);
-       DEBUG (2, fprintf (gc_debug_file, "Root scan: %d usecs\n", TV_ELAPSED (btv, atv)));
-
-       TV_GETTIME (btv);
-       time_major_scan_big_objects += TV_ELAPSED_MS (atv, btv);
-
-       /* all the objects in the heap */
-       finish_gray_stack (heap_start, heap_end, GENERATION_OLD);
-       TV_GETTIME (atv);
-       time_major_finish_gray_stack += TV_ELAPSED_MS (btv, atv);
-
-       /* sweep the big objects list */
-       prevbo = NULL;
-       for (bigobj = los_object_list; bigobj;) {
-               if (object_is_pinned (bigobj->data)) {
-                       unpin_object (bigobj->data);
-               } else {
-                       LOSObject *to_free;
-                       /* not referenced anywhere, so we can free it */
-                       if (prevbo)
-                               prevbo->next = bigobj->next;
-                       else
-                               los_object_list = bigobj->next;
-                       to_free = bigobj;
-                       bigobj = bigobj->next;
-                       free_large_object (to_free);
-                       continue;
-               }
-               prevbo = bigobj;
-               bigobj = bigobj->next;
-       }
-
-       major_sweep ();
-
-       TV_GETTIME (btv);
-       time_major_sweep += TV_ELAPSED_MS (atv, btv);
-
-       /* walk the pin_queue, build up the fragment list of free memory, unmark
-        * pinned objects as we go, memzero() the empty fragments so they are ready for the
-        * next allocations.
-        */
-       build_nursery_fragments (nursery_section->pin_queue_start, nursery_section->pin_queue_end);
-
-       TV_GETTIME (atv);
-       time_major_fragment_creation += TV_ELAPSED_MS (btv, atv);
-
-       TV_GETTIME (all_btv);
-       mono_stats.major_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
-
-       if (heap_dump_file)
-               dump_heap ("major", num_major_gcs - 1, reason);
-
-       /* prepare the pin queue for the next collection */
-       next_pin_slot = 0;
-       if (fin_ready_list || critical_fin_list) {
-               DEBUG (4, fprintf (gc_debug_file, "Finalizer-thread wakeup: ready %d\n", num_ready_finalizers));
-               mono_gc_finalize_notify ();
-       }
-       pin_stats_reset ();
-
-       g_assert (gray_object_queue_is_empty ());
-
-       num_major_sections_saved = MAX (old_num_major_sections - num_major_sections, 1);
-
-       save_target = num_major_sections / 2;
-       /*
-        * We aim to allow the allocation of as many sections as is
-        * necessary to reclaim save_target sections in the next
-        * collection.  We assume the collection pattern won't change.
-        * In the last cycle, we had num_major_sections_saved for
-        * minor_collection_sections_alloced.  Assuming things won't
-        * change, this must be the same ratio as save_target for
-        * allowance_target, i.e.
-        *
-        *    num_major_sections_saved            save_target
-        * --------------------------------- == ----------------
-        * minor_collection_sections_alloced    allowance_target
-        *
-        * hence:
-        */
-       allowance_target = save_target * minor_collection_sections_alloced / num_major_sections_saved;
-
-       minor_collection_section_allowance = MAX (MIN (allowance_target, num_major_sections), MIN_MINOR_COLLECTION_SECTION_ALLOWANCE);
-
-       minor_collection_sections_alloced = 0;
-
-       check_scan_starts ();
-
-       //consistency_check ();
-}
-
 static int
 ms_calculate_block_obj_sizes (double factor, int *arr)
 {
@@ -954,7 +1252,7 @@ ms_calculate_block_obj_sizes (double factor, int *arr)
 
        do {
                int target_count = ceil (MS_BLOCK_FREE / target_size);
-               int size = MIN ((MS_BLOCK_FREE / target_count) & ~(ALLOC_ALIGN - 1), MAX_SMALL_OBJ_SIZE);
+               int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
 
                if (size != last_size) {
                        if (arr)
@@ -964,69 +1262,103 @@ ms_calculate_block_obj_sizes (double factor, int *arr)
                }
 
                target_size *= factor;
-       } while (last_size < MAX_SMALL_OBJ_SIZE);
+       } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
 
        return num_sizes;
 }
 
+/* only valid during minor collections */
+static int old_num_major_sections;
+
 static void
-major_init (void)
+major_start_nursery_collection (void)
 {
+#ifdef MARKSWEEP_CONSISTENCY_CHECK
+       consistency_check ();
+#endif
+
+       old_num_major_sections = num_major_sections;
+}
+
+static void
+major_finish_nursery_collection (void)
+{
+#ifdef MARKSWEEP_CONSISTENCY_CHECK
+       consistency_check ();
+#endif
+       mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
+}
+
+static void
+major_start_major_collection (void)
+{
+#ifndef SGEN_PARALLEL_MARK
        int i;
 
-       num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
-       block_obj_sizes = get_internal_mem (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
-       ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
+       /* clear the free lists */
+       for (i = 0; i < num_block_obj_sizes; ++i) {
+               if (!evacuate_block_obj_sizes [i])
+                       continue;
 
-       /*
-       {
-               int i;
-               g_print ("block object sizes:\n");
-               for (i = 0; i < num_block_obj_sizes; ++i)
-                       g_print ("%d\n", block_obj_sizes [i]);
+               free_block_lists [0][i] = NULL;
+               free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
        }
-       */
+#endif
+}
 
-       for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
-               free_block_lists [i] = get_internal_mem (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+static void
+major_finish_major_collection (void)
+{
+#ifndef FIXED_HEAP
+       int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
 
-       for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
-               fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
-       for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
-               g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
+       /*
+        * FIXME: We don't free blocks on 32 bit platforms because it
+        * can lead to address space fragmentation, since we're
+        * allocating blocks in larger contingents.
+        */
+       if (sizeof (mword) < 8)
+               return;
 
-       mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
-       mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
+       while (num_empty_blocks > section_reserve) {
+               void *next = *(void**)empty_blocks;
+               mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
+               empty_blocks = next;
+               /*
+                * Needs not be atomic because this is running
+                * single-threaded.
+                */
+               --num_empty_blocks;
 
-       minor_collection_section_allowance = MIN_MINOR_COLLECTION_SECTION_ALLOWANCE;
+               ++stat_major_blocks_freed;
+       }
+#endif
 }
 
-/* only valid during minor collections */
-static int old_num_major_sections;
-
 static void
-major_start_nursery_collection (void)
+major_find_pin_queue_start_ends (SgenGrayQueue *queue)
 {
-       //consistency_check ();
+       MSBlockInfo *block;
 
-       old_num_major_sections = num_major_sections;
+       FOREACH_BLOCK (block) {
+               block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
+                               &block->pin_queue_num_entries);
+       } END_FOREACH_BLOCK;
 }
 
 static void
-major_finish_nursery_collection (void)
+major_pin_objects (SgenGrayQueue *queue)
 {
-       int sections_alloced;
-
-       //consistency_check ();
+       MSBlockInfo *block;
 
-       sections_alloced = num_major_sections - old_num_major_sections;
-       minor_collection_sections_alloced += sections_alloced;
+       FOREACH_BLOCK (block) {
+               mark_pinned_objects_in_block (block, queue);
+       } END_FOREACH_BLOCK;
 }
 
-static gboolean
-major_need_major_collection (void)
+static void
+major_init_to_space (void)
 {
-       return minor_collection_sections_alloced > minor_collection_section_allowance;
 }
 
 static void
@@ -1041,13 +1373,311 @@ major_get_used_size (void)
        gint64 size = 0;
        MSBlockInfo *block;
 
-       for (block = all_blocks; block; block = block->next) {
+       FOREACH_BLOCK (block) {
                int count = MS_BLOCK_FREE / block->obj_size;
                void **iter;
                size += count * block->obj_size;
                for (iter = block->free_list; iter; iter = (void**)*iter)
                        size -= block->obj_size;
-       }
+       } END_FOREACH_BLOCK;
 
        return size;
 }
+
+static int
+get_num_major_sections (void)
+{
+       return num_major_sections;
+}
+
+static gboolean
+major_handle_gc_param (const char *opt)
+{
+#ifdef FIXED_HEAP
+       if (g_str_has_prefix (opt, "major-heap-size=")) {
+               const char *arg = strchr (opt, '=') + 1;
+               glong size;
+               if (!mono_gc_parse_environment_string_extract_number (arg, &size))
+                       return FALSE;
+               ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
+               g_assert (ms_heap_num_blocks > 0);
+               return TRUE;
+       } else
+#endif
+#ifndef SGEN_PARALLEL_MARK
+       if (g_str_has_prefix (opt, "evacuation-threshold=")) {
+               const char *arg = strchr (opt, '=') + 1;
+               int percentage = atoi (arg);
+               if (percentage < 0 || percentage > 100) {
+                       fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
+                       exit (1);
+               }
+               evacuation_threshold = (float)percentage / 100.0;
+               return TRUE;
+       }
+#endif
+
+       return FALSE;
+}
+
+static void
+major_print_gc_param_usage (void)
+{
+       fprintf (stderr,
+                       ""
+#ifdef FIXED_HEAP
+                       "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
+#endif
+#ifndef SGEN_PARALLEL_MARK
+                       "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
+#endif
+                       );
+}
+
+#ifdef SGEN_HAVE_CARDTABLE
+static void
+major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
+{
+       MSBlockInfo *block;
+
+       FOREACH_BLOCK (block) {
+               if (block->has_references)
+                       callback ((mword)block->block, MS_BLOCK_SIZE);
+       } END_FOREACH_BLOCK;
+}
+
+#define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
+/*
+ * MS blocks are 16K aligned.
+ * Cardtables are 4K aligned, at least.
+ * This means that the cardtable of a given block is 32 bytes aligned.
+ */
+static guint8*
+initial_skip_card (guint8 *card_data)
+{
+       mword *cards = (mword*)card_data;
+       mword card;
+       int i;
+       for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
+               card = cards [i];
+               if (card)
+                       break;
+       }
+
+       if (i == CARD_WORDS_PER_BLOCK)
+               return card_data + CARDS_PER_BLOCK;
+
+#if defined(__i386__) && defined(__GNUC__)
+       return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
+#elif defined(__x86_64__) && defined(__GNUC__)
+       return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
+#else
+       for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
+               if (card_data [i])
+                       return &card_data [i];
+       }
+       return card_data;
+#endif
+}
+
+
+static G_GNUC_UNUSED guint8*
+skip_card (guint8 *card_data, guint8 *card_data_end)
+{
+       while (card_data < card_data_end && !*card_data)
+               ++card_data;
+       return card_data;
+}
+
+#define MS_BLOCK_OBJ_INDEX_FAST(o,b,os)        (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
+#define MS_BLOCK_OBJ_FAST(b,os,i)                      ((b) + MS_BLOCK_SKIP + (os) * (i))
+#define MS_OBJ_ALLOCED_FAST(o,b)               (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
+
+static void
+major_scan_card_table (SgenGrayQueue *queue)
+{
+       MSBlockInfo *block;
+
+       FOREACH_BLOCK (block) {
+               int block_obj_size;
+               char *block_start;
+
+               if (!block->has_references)
+                       continue;
+
+               block_obj_size = block->obj_size;
+               block_start = block->block;
+
+               if (block_obj_size >= CARD_SIZE_IN_BYTES) {
+                       guint8 *cards;
+#ifndef SGEN_HAVE_OVERLAPPING_CARDS
+                       guint8 cards_data [CARDS_PER_BLOCK];
+#endif
+                       char *obj, *end, *base;
+
+                       /*We can avoid the extra copy since the remark cardtable was cleaned before */
+#ifdef SGEN_HAVE_OVERLAPPING_CARDS
+                       cards = sgen_card_table_get_card_scan_address ((mword)block_start);
+#else
+                       cards = cards_data;
+                       if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
+                               continue;
+#endif
+
+                       obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
+                       end = block_start + MS_BLOCK_SIZE;
+                       base = sgen_card_table_align_pointer (obj);
+
+                       while (obj < end) {
+                               if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
+                                       int card_offset = (obj - base) >> CARD_BITS;
+                                       sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
+                               }
+                               obj += block_obj_size;
+                       }
+               } else {
+                       guint8 *card_data, *card_base;
+                       guint8 *card_data_end;
+
+                       /*
+                        * This is safe in face of card aliasing for the following reason:
+                        *
+                        * Major blocks are 16k aligned, or 32 cards aligned.
+                        * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
+                        * sizes, they won't overflow the cardtable overlap modulus.
+                        */
+                       card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
+                       card_data_end = card_data + CARDS_PER_BLOCK;
+
+                       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)) {
+                               int index;
+                               int idx = card_data - card_base;
+                               char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
+                               char *end = start + CARD_SIZE_IN_BYTES;
+                               char *obj;
+
+                               if (!*card_data)
+                                       continue;
+                               sgen_card_table_prepare_card_for_scanning (card_data);
+
+                               if (idx == 0)
+                                       index = 0;
+                               else
+                                       index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
+
+                               obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
+                               while (obj < end) {
+                                       if (MS_OBJ_ALLOCED_FAST (obj, block_start))
+                                               minor_scan_object (obj, queue);
+                                       obj += block_obj_size;
+                               }
+                       }
+               }
+       } END_FOREACH_BLOCK;
+}
+#endif
+
+void
+#ifdef SGEN_PARALLEL_MARK
+#ifdef FIXED_HEAP
+mono_sgen_marksweep_fixed_par_init
+#else
+mono_sgen_marksweep_par_init
+#endif
+#else
+#ifdef FIXED_HEAP
+mono_sgen_marksweep_fixed_init
+#else
+mono_sgen_marksweep_init
+#endif
+#endif
+       (SgenMajorCollector *collector)
+{
+       int i;
+
+#ifndef FIXED_HEAP
+       mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
+#endif
+
+       num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
+       block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+       ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
+
+#ifndef SGEN_PARALLEL_MARK
+       evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+       for (i = 0; i < num_block_obj_sizes; ++i)
+               evacuate_block_obj_sizes [i] = FALSE;
+#endif
+
+       /*
+       {
+               int i;
+               g_print ("block object sizes:\n");
+               for (i = 0; i < num_block_obj_sizes; ++i)
+                       g_print ("%d\n", block_obj_sizes [i]);
+       }
+       */
+
+       for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
+               free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+
+       for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
+               fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
+       for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
+               g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
+
+       LOCK_INIT (ms_block_list_mutex);
+
+       mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
+       mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
+       mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
+
+       collector->section_size = MAJOR_SECTION_SIZE;
+#ifdef SGEN_PARALLEL_MARK
+       collector->is_parallel = TRUE;
+#else
+       collector->is_parallel = FALSE;
+#endif
+       collector->supports_cardtable = TRUE;
+
+       collector->alloc_heap = major_alloc_heap;
+       collector->is_object_live = major_is_object_live;
+       collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
+       collector->alloc_degraded = major_alloc_degraded;
+       collector->copy_or_mark_object = major_copy_or_mark_object;
+       collector->alloc_object = major_alloc_object;
+       collector->free_pinned_object = free_pinned_object;
+       collector->iterate_objects = major_iterate_objects;
+       collector->free_non_pinned_object = major_free_non_pinned_object;
+       collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
+       collector->pin_objects = major_pin_objects;
+#ifdef SGEN_HAVE_CARDTABLE
+       collector->scan_card_table = major_scan_card_table;
+       collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
+#endif
+       collector->init_to_space = major_init_to_space;
+       collector->sweep = major_sweep;
+       collector->check_scan_starts = major_check_scan_starts;
+       collector->dump_heap = major_dump_heap;
+       collector->get_used_size = major_get_used_size;
+       collector->start_nursery_collection = major_start_nursery_collection;
+       collector->finish_nursery_collection = major_finish_nursery_collection;
+       collector->start_major_collection = major_start_major_collection;
+       collector->finish_major_collection = major_finish_major_collection;
+       collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
+       collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
+       collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
+       collector->get_num_major_sections = get_num_major_sections;
+       collector->handle_gc_param = major_handle_gc_param;
+       collector->print_gc_param_usage = major_print_gc_param_usage;
+
+       FILL_COLLECTOR_COPY_OBJECT (collector);
+       FILL_COLLECTOR_SCAN_OBJECT (collector);
+
+
+       /*cardtable requires major pages to be 8 cards aligned*/
+       g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
+
+}
+
+#endif