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