[sgen] Don't compile evacuation for parallel mark.
[mono.git] / mono / metadata / sgen-marksweep.c
1 /*
2  * sgen-marksweep.c: Simple generational GC.
3  *
4  * Author:
5  *      Mark Probst <mark.probst@gmail.com>
6  *
7  * Copyright 2009-2010 Novell, Inc.
8  * 
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #ifdef HAVE_SGEN_GC
30
31 #include <math.h>
32
33 #include "utils/mono-counters.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
41
42 #define DEBUG(l,x)
43
44 #define MS_BLOCK_SIZE   (16*1024)
45 #define MS_BLOCK_SIZE_SHIFT     14
46 #define MAJOR_SECTION_SIZE      MS_BLOCK_SIZE
47 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
48
49 #ifdef FIXED_HEAP
50 #define MS_DEFAULT_HEAP_NUM_BLOCKS      (32 * 1024) /* 512 MB */
51 #endif
52
53 /*
54  * Don't allocate single blocks, but alloc a contingent of this many
55  * blocks in one swoop.
56  */
57 #define MS_BLOCK_ALLOC_NUM      32
58
59 /*
60  * Number of bytes before the first object in a block.  At the start
61  * of a block is the MSBlockHeader, then opional padding, then come
62  * the objects, so this must be >= sizeof (MSBlockHeader).
63  */
64 #ifdef FIXED_HEAP
65 #define MS_BLOCK_SKIP   0
66 #else
67 #define MS_BLOCK_SKIP   16
68 #endif
69
70 #define MS_BLOCK_FREE   (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
71
72 #define MS_NUM_MARK_WORDS       ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
73
74 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
75 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
76 #endif
77
78 typedef struct _MSBlockInfo MSBlockInfo;
79 struct _MSBlockInfo {
80         int obj_size;
81         int obj_size_index;
82         gboolean pinned;
83         gboolean has_references;
84 #ifndef SGEN_PARALLEL_MARK
85         gboolean has_pinned;    /* means cannot evacuate */
86         gboolean is_to_space;
87 #endif
88 #ifdef FIXED_HEAP
89         gboolean used;
90 #else
91         MSBlockInfo *next;
92 #endif
93         char *block;
94         void **free_list;
95         MSBlockInfo *next_free;
96         void **pin_queue_start;
97         int pin_queue_num_entries;
98         mword mark_words [MS_NUM_MARK_WORDS];
99 };
100
101 #ifdef FIXED_HEAP
102 static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
103
104 #define ms_heap_start   nursery_end
105 static char *ms_heap_end;
106
107 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p)   ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
108
109 /* array of all all block infos in the system */
110 static MSBlockInfo *block_infos;
111 #endif
112
113 #define MS_BLOCK_OBJ(b,i)               ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
114 #define MS_BLOCK_DATA_FOR_OBJ(o)        ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
115
116 #ifdef FIXED_HEAP
117 #define MS_BLOCK_FOR_OBJ(o)             (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
118 #else
119 typedef struct {
120         MSBlockInfo *info;
121 } MSBlockHeader;
122
123 #define MS_BLOCK_FOR_OBJ(o)             (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
124 #endif
125
126 #define MS_BLOCK_OBJ_INDEX(o,b) (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
127
128 #define MS_CALC_MARK_BIT(w,b,o)         do {                            \
129                 int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
130                 if (sizeof (mword) == 4) {                              \
131                         (w) = i >> 5;                                   \
132                         (b) = i & 31;                                   \
133                 } else {                                                \
134                         (w) = i >> 6;                                   \
135                         (b) = i & 63;                                   \
136                 }                                                       \
137         } while (0)
138
139 #define MS_MARK_BIT(bl,w,b)     ((bl)->mark_words [(w)] & (1L << (b)))
140 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (1L << (b)))
141 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b)  do {                    \
142                 mword __old = (bl)->mark_words [(w)];                   \
143                 mword __bitmask = 1L << (b);                            \
144                 if (__old & __bitmask) {                                \
145                         was_marked = TRUE;                              \
146                         break;                                          \
147                 }                                                       \
148                 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)],   \
149                                                 (gpointer)(__old | __bitmask), \
150                                                 (gpointer)__old) ==     \
151                                 (gpointer)__old) {                      \
152                         was_marked = FALSE;                             \
153                         break;                                          \
154                 }                                                       \
155         } while (1)
156
157 #define MS_OBJ_ALLOCED(o,b)     (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
158
159 #define MS_BLOCK_OBJ_SIZE_FACTOR        (sqrt (2.0))
160
161 /*
162  * This way we can lookup block object size indexes for sizes up to
163  * 256 bytes with a single load.
164  */
165 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES      32
166
167 static int *block_obj_sizes;
168 static int num_block_obj_sizes;
169 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
170
171 #define MS_BLOCK_FLAG_PINNED    1
172 #define MS_BLOCK_FLAG_REFS      2
173
174 #define MS_BLOCK_TYPE_MAX       4
175
176 #ifdef SGEN_PARALLEL_MARK
177 static LOCK_DECLARE (ms_block_list_mutex);
178 #define LOCK_MS_BLOCK_LIST pthread_mutex_lock (&ms_block_list_mutex)
179 #define UNLOCK_MS_BLOCK_LIST pthread_mutex_unlock (&ms_block_list_mutex)
180 #else
181 #define LOCK_MS_BLOCK_LIST
182 #define UNLOCK_MS_BLOCK_LIST
183 #endif
184
185 /* we get this at init */
186 static int nursery_bits;
187 static char *nursery_start;
188 static char *nursery_end;
189
190 #ifndef SGEN_PARALLEL_MARK
191 static gboolean *evacuate_block_obj_sizes;
192 static float evacuation_threshold = 0.666;
193 #endif
194
195 #define ptr_in_nursery(p)       (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
196
197 #ifdef FIXED_HEAP
198 /* non-allocated block free-list */
199 static MSBlockInfo *empty_blocks = NULL;
200 #else
201 /* non-allocated block free-list */
202 static void *empty_blocks = NULL;
203 /* all allocated blocks in the system */
204 static MSBlockInfo *all_blocks;
205 static int num_empty_blocks = 0;
206 #endif
207
208 #ifdef FIXED_HEAP
209 #define FOREACH_BLOCK(bl)       {                                       \
210                 int __block_i;                                          \
211                 for (__block_i = 0; __block_i < ms_heap_num_blocks; ++__block_i) { \
212                         (bl) = &block_infos [__block_i];                \
213                         if (!(bl)->used) continue;
214 #define END_FOREACH_BLOCK       }}
215 #else
216 #define FOREACH_BLOCK(bl)       for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
217 #define END_FOREACH_BLOCK       }
218 #endif
219
220 static int num_major_sections = 0;
221 /* one free block list for each block object size */
222 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
223
224 static long long stat_major_blocks_alloced = 0;
225 static long long stat_major_blocks_freed = 0;
226 static long long stat_major_objects_evacuated = 0;
227
228 static int
229 ms_find_block_obj_size_index (int size)
230 {
231         int i;
232         DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
233         for (i = 0; i < num_block_obj_sizes; ++i)
234                 if (block_obj_sizes [i] >= size)
235                         return i;
236         g_assert_not_reached ();
237 }
238
239 #define FREE_BLOCKS(p,r) (free_block_lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
240
241 #define MS_BLOCK_OBJ_SIZE_INDEX(s)                              \
242         (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ?      \
243          fast_block_obj_size_indexes [((s)+7)>>3] :             \
244          ms_find_block_obj_size_index ((s)))
245
246 #ifdef FIXED_HEAP
247 static void*
248 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
249 {
250         char *heap_start;
251         mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
252         mword alloc_size = nursery_size + major_heap_size;
253         int i;
254
255         g_assert (ms_heap_num_blocks > 0);
256         g_assert (nursery_size % MS_BLOCK_SIZE == 0);
257         if (nursery_align)
258                 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
259
260         nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
261         nursery_end = heap_start = nursery_start + nursery_size;
262         nursery_bits = the_nursery_bits;
263
264         ms_heap_end = heap_start + major_heap_size;
265
266         block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
267
268         for (i = 0; i < ms_heap_num_blocks; ++i) {
269                 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
270                 if (i < ms_heap_num_blocks - 1)
271                         block_infos [i].next_free = &block_infos [i + 1];
272                 else
273                         block_infos [i].next_free = NULL;
274         }
275
276         empty_blocks = &block_infos [0];
277
278         return nursery_start;
279 }
280 #else
281 static void*
282 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
283 {
284         if (nursery_align)
285                 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
286         else
287                 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
288
289         nursery_end = nursery_start + nursery_size;
290         nursery_bits = the_nursery_bits;
291
292         return nursery_start;
293 }
294 #endif
295
296 #ifdef FIXED_HEAP
297 static MSBlockInfo*
298 ms_get_empty_block (void)
299 {
300         MSBlockInfo *block;
301
302         g_assert (empty_blocks);
303
304         block = empty_blocks;
305         empty_blocks = empty_blocks->next_free;
306
307         block->used = TRUE;
308
309         mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
310
311         return block;
312 }
313
314 static void
315 ms_free_block (MSBlockInfo *block)
316 {
317         block->next_free = empty_blocks;
318         empty_blocks = block;
319         block->used = FALSE;
320 }
321 #else
322 static void*
323 ms_get_empty_block (void)
324 {
325         char *p;
326         int i;
327         void *block, *empty, *next;
328
329  retry:
330         if (!empty_blocks) {
331                 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
332
333                 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
334                         block = p;
335                         /*
336                          * We do the free list update one after the
337                          * other so that other threads can use the new
338                          * blocks as quickly as possible.
339                          */
340                         do {
341                                 empty = empty_blocks;
342                                 *(void**)block = empty;
343                         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
344                         p += MS_BLOCK_SIZE;
345                 }
346
347                 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
348
349                 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
350         }
351
352         do {
353                 empty = empty_blocks;
354                 if (!empty)
355                         goto retry;
356                 block = empty;
357                 next = *(void**)block;
358         } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
359
360         SGEN_ATOMIC_ADD (num_empty_blocks, -1);
361
362         *(void**)block = NULL;
363
364         g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
365
366         mono_sgen_update_heap_boundaries ((mword)block, (mword)block + MS_BLOCK_SIZE);
367
368         return block;
369 }
370
371 static void
372 ms_free_block (void *block)
373 {
374         void *empty;
375
376         memset (block, 0, MS_BLOCK_SIZE);
377
378         do {
379                 empty = empty_blocks;
380                 *(void**)block = empty;
381         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
382
383         SGEN_ATOMIC_ADD (num_empty_blocks, 1);
384 }
385 #endif
386
387 //#define MARKSWEEP_CONSISTENCY_CHECK
388
389 #ifdef MARKSWEEP_CONSISTENCY_CHECK
390 static void
391 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
392 {
393         MSBlockInfo *b;
394
395         for (; block; block = block->next_free) {
396                 g_assert (block->obj_size == size);
397                 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
398
399                 /* blocks in the free lists must have at least
400                    one free slot */
401                 g_assert (block->free_list);
402
403 #ifdef FIXED_HEAP
404                 /* the block must not be in the empty_blocks list */
405                 for (b = empty_blocks; b; b = b->next_free)
406                         g_assert (b != block);
407 #else
408                 /* the block must be in the all_blocks list */
409                 for (b = all_blocks; b; b = b->next) {
410                         if (b == block)
411                                 break;
412                 }
413                 g_assert (b == block);
414 #endif
415         }
416 }
417
418 static void
419 check_empty_blocks (void)
420 {
421 #ifndef FIXED_HEAP
422         void *p;
423         int i = 0;
424         for (p = empty_blocks; p; p = *(void**)p)
425                 ++i;
426         g_assert (i == num_empty_blocks);
427 #endif
428 }
429
430 static void
431 consistency_check (void)
432 {
433         MSBlockInfo *block;
434         int i;
435
436         /* check all blocks */
437         FOREACH_BLOCK (block) {
438                 int count = MS_BLOCK_FREE / block->obj_size;
439                 int num_free = 0;
440                 void **free;
441
442 #ifndef FIXED_HEAP
443                 /* check block header */
444                 g_assert (((MSBlockHeader*)block->block)->info == block);
445 #endif
446
447                 /* count number of free slots */
448                 for (i = 0; i < count; ++i) {
449                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
450                         if (!MS_OBJ_ALLOCED (obj, block))
451                                 ++num_free;
452                 }
453
454                 /* check free list */
455                 for (free = block->free_list; free; free = (void**)*free) {
456                         g_assert (MS_BLOCK_FOR_OBJ (free) == block);
457                         --num_free;
458                 }
459                 g_assert (num_free == 0);
460
461                 /* check all mark words are zero */
462                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
463                         g_assert (block->mark_words [i] == 0);
464         } END_FOREACH_BLOCK;
465
466         /* check free blocks */
467         for (i = 0; i < num_block_obj_sizes; ++i) {
468                 int j;
469                 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
470                         check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
471         }
472
473         check_empty_blocks ();
474 }
475 #endif
476
477 static void
478 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
479 {
480         int size = block_obj_sizes [size_index];
481         int count = MS_BLOCK_FREE / size;
482 #ifdef FIXED_HEAP
483         MSBlockInfo *info = ms_get_empty_block ();
484 #else
485         MSBlockInfo *info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
486         MSBlockHeader *header;
487 #endif
488         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
489         char *obj_start;
490         int i;
491
492         DEBUG (9, g_assert (count >= 2));
493
494         info->obj_size = size;
495         info->obj_size_index = size_index;
496         info->pinned = pinned;
497         info->has_references = has_references;
498 #ifndef SGEN_PARALLEL_MARK
499         info->has_pinned = pinned;
500         info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
501 #endif
502 #ifndef FIXED_HEAP
503         info->block = ms_get_empty_block ();
504
505         header = (MSBlockHeader*) info->block;
506         header->info = info;
507 #endif
508
509         /* build free list */
510         obj_start = info->block + MS_BLOCK_SKIP;
511         info->free_list = (void**)obj_start;
512         /* we're skipping the last one - it must be nulled */
513         for (i = 0; i < count - 1; ++i) {
514                 char *next_obj_start = obj_start + size;
515                 *(void**)obj_start = next_obj_start;
516                 obj_start = next_obj_start;
517         }
518         /* the last one */
519         *(void**)obj_start = NULL;
520
521         info->next_free = free_blocks [size_index];
522         free_blocks [size_index] = info;
523
524 #ifndef FIXED_HEAP
525         info->next = all_blocks;
526         all_blocks = info;
527 #endif
528
529         ++num_major_sections;
530 }
531
532 static gboolean
533 obj_is_from_pinned_alloc (char *obj)
534 {
535         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
536         return block->pinned;
537 }
538
539 static void*
540 alloc_obj (int size, gboolean pinned, gboolean has_references)
541 {
542         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
543         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
544         MSBlockInfo *block;
545         void *obj;
546
547         /* FIXME: try to do this without locking */
548
549         LOCK_MS_BLOCK_LIST;
550
551         if (!free_blocks [size_index])
552                 ms_alloc_block (size_index, pinned, has_references);
553
554         block = free_blocks [size_index];
555         DEBUG (9, g_assert (block));
556
557         obj = block->free_list;
558         DEBUG (9, g_assert (obj));
559
560         block->free_list = *(void**)obj;
561         if (!block->free_list) {
562                 free_blocks [size_index] = block->next_free;
563                 block->next_free = NULL;
564         }
565
566         UNLOCK_MS_BLOCK_LIST;
567
568         /*
569          * FIXME: This should not be necessary because it'll be
570          * overwritten by the vtable immediately.
571          */
572         *(void**)obj = NULL;
573
574         return obj;
575 }
576
577 static void*
578 major_alloc_object (int size, gboolean has_references)
579 {
580         return alloc_obj (size, FALSE, has_references);
581 }
582
583 /*
584  * We're not freeing the block if it's empty.  We leave that work for
585  * the next major collection.
586  *
587  * This is just called from the domain clearing code, which runs in a
588  * single thread and has the GC lock, so we don't need an extra lock.
589  */
590 static void
591 free_object (char *obj, size_t size, gboolean pinned)
592 {
593         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
594         int word, bit;
595         DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
596         DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
597         MS_CALC_MARK_BIT (word, bit, obj);
598         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
599         if (!block->free_list) {
600                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
601                 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
602                 DEBUG (9, g_assert (!block->next_free));
603                 block->next_free = free_blocks [size_index];
604                 free_blocks [size_index] = block;
605         }
606         memset (obj, 0, size);
607         *(void**)obj = block->free_list;
608         block->free_list = (void**)obj;
609 }
610
611 static void
612 major_free_non_pinned_object (char *obj, size_t size)
613 {
614         free_object (obj, size, FALSE);
615 }
616
617 /* size is a multiple of SGEN_ALLOC_ALIGN */
618 static void*
619 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
620 {
621         return alloc_obj (size, TRUE, has_references);
622 }
623
624 static void
625 free_pinned_object (char *obj, size_t size)
626 {
627         free_object (obj, size, TRUE);
628 }
629
630 /*
631  * size is already rounded up and we hold the GC lock.
632  */
633 static void*
634 major_alloc_degraded (MonoVTable *vtable, size_t size)
635 {
636         void *obj;
637         int old_num_sections = num_major_sections;
638         obj = alloc_obj (size, FALSE, vtable->klass->has_references);
639         *(MonoVTable**)obj = vtable;
640         HEAVY_STAT (++stat_objects_alloced_degraded);
641         HEAVY_STAT (stat_bytes_alloced_degraded += size);
642         g_assert (num_major_sections >= old_num_sections);
643         mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
644         return obj;
645 }
646
647 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj)   FALSE
648
649 /*
650  * obj is some object.  If it's not in the major heap (i.e. if it's in
651  * the nursery or LOS), return FALSE.  Otherwise return whether it's
652  * been marked or copied.
653  */
654 static gboolean
655 major_is_object_live (char *obj)
656 {
657         MSBlockInfo *block;
658         int word, bit;
659 #ifndef FIXED_HEAP
660         mword objsize;
661 #endif
662
663         if (ptr_in_nursery (obj))
664                 return FALSE;
665
666 #ifdef FIXED_HEAP
667         /* LOS */
668         if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
669                 return FALSE;
670 #else
671         objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
672
673         /* LOS */
674         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
675                 return FALSE;
676 #endif
677
678         /* now we know it's in a major block */
679         block = MS_BLOCK_FOR_OBJ (obj);
680         DEBUG (9, g_assert (!block->pinned));
681         MS_CALC_MARK_BIT (word, bit, obj);
682         return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
683 }
684
685 static gboolean
686 major_ptr_is_in_non_pinned_space (char *ptr)
687 {
688         g_assert_not_reached ();
689 }
690
691 static void
692 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
693 {
694         MSBlockInfo *block;
695
696         FOREACH_BLOCK (block) {
697                 int count = MS_BLOCK_FREE / block->obj_size;
698                 int i;
699
700                 if (block->pinned && !pinned)
701                         continue;
702                 if (!block->pinned && !non_pinned)
703                         continue;
704
705                 for (i = 0; i < count; ++i) {
706                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
707                         if (MS_OBJ_ALLOCED (obj, block))
708                                 callback ((char*)obj, block->obj_size, data);
709                 }
710         } END_FOREACH_BLOCK;
711 }
712
713 static void
714 major_check_scan_starts (void)
715 {
716 }
717
718 static void
719 major_dump_heap (FILE *heap_dump_file)
720 {
721         MSBlockInfo *block;
722         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
723         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
724         int i;
725
726         for (i = 0; i < num_block_obj_sizes; ++i)
727                 slots_available [i] = slots_used [i] = 0;
728
729         FOREACH_BLOCK (block) {
730                 int index = ms_find_block_obj_size_index (block->obj_size);
731                 int count = MS_BLOCK_FREE / block->obj_size;
732
733                 slots_available [index] += count;
734                 for (i = 0; i < count; ++i) {
735                         if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
736                                 ++slots_used [index];
737                 }
738         } END_FOREACH_BLOCK;
739
740         fprintf (heap_dump_file, "<occupancies>\n");
741         for (i = 0; i < num_block_obj_sizes; ++i) {
742                 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
743                                 block_obj_sizes [i], slots_available [i], slots_used [i]);
744         }
745         fprintf (heap_dump_file, "</occupancies>\n");
746
747         FOREACH_BLOCK (block) {
748                 int count = MS_BLOCK_FREE / block->obj_size;
749                 int i;
750                 int start = -1;
751
752                 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
753
754                 for (i = 0; i <= count; ++i) {
755                         if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
756                                 if (start < 0)
757                                         start = i;
758                         } else {
759                                 if (start >= 0) {
760                                         mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
761                                         start = -1;
762                                 }
763                         }
764                 }
765
766                 fprintf (heap_dump_file, "</section>\n");
767         } END_FOREACH_BLOCK;
768 }
769
770 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
771
772 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {        \
773                 int __word, __bit;                                      \
774                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
775                 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
776                         MS_SET_MARK_BIT ((block), __word, __bit);       \
777                         if ((block)->has_references)                    \
778                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
779                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
780                 }                                                       \
781         } while (0)
782 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {                \
783                 int __word, __bit;                                      \
784                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
785                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
786                 if (!MS_MARK_BIT ((block), __word, __bit)) {            \
787                         MS_SET_MARK_BIT ((block), __word, __bit);       \
788                         if ((block)->has_references)                    \
789                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
790                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
791                 }                                                       \
792         } while (0)
793 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {            \
794                 int __word, __bit;                                      \
795                 gboolean __was_marked;                                  \
796                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
797                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
798                 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
799                 if (!__was_marked) {                                    \
800                         if ((block)->has_references)                    \
801                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
802                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
803                 }                                                       \
804         } while (0)
805
806 #include "sgen-major-copy-object.h"
807
808 #ifdef SGEN_PARALLEL_MARK
809 static void
810 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
811 {
812         void *obj = *ptr;
813         mword vtable_word = *(mword*)obj;
814         MonoVTable *vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
815         mword objsize;
816         MSBlockInfo *block;
817
818         HEAVY_STAT (++stat_copy_object_called_major);
819
820         DEBUG (9, g_assert (obj));
821         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
822
823         if (ptr_in_nursery (obj)) {
824                 int word, bit;
825                 gboolean has_references;
826                 void *destination;
827
828                 if (vtable_word & SGEN_FORWARDED_BIT) {
829                         *ptr = (void*)vt;
830                         return;
831                 }
832
833                 if (vtable_word & SGEN_PINNED_BIT)
834                         return;
835
836                 HEAVY_STAT (++stat_objects_copied_major);
837
838                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
839                 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
840
841                 destination = major_alloc_object (objsize, has_references);
842
843                 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
844                         gboolean was_marked;
845
846                         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
847                         obj = destination;
848                         *ptr = obj;
849
850                         /*
851                          * FIXME: If we make major_alloc_object() give
852                          * us the block info, too, we won't have to
853                          * re-fetch it here.
854                          */
855                         block = MS_BLOCK_FOR_OBJ (obj);
856                         MS_CALC_MARK_BIT (word, bit, obj);
857                         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
858                         MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
859                 } else {
860                         /*
861                          * FIXME: We have allocated destination, but
862                          * we cannot use it.  Give it back to the
863                          * allocator.
864                          */
865                         *(void**)destination = NULL;
866
867                         vtable_word = *(mword*)obj;
868                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
869
870                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
871
872                         *ptr = obj;
873                 }
874         } else {
875 #ifdef FIXED_HEAP
876                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
877 #else
878                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
879
880                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
881 #endif
882                 {
883                         block = MS_BLOCK_FOR_OBJ (obj);
884                         MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
885                 } else {
886                         if (vtable_word & SGEN_PINNED_BIT)
887                                 return;
888                         binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
889                         if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
890                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
891                                         GRAY_OBJECT_ENQUEUE (queue, obj);
892                         } else {
893                                 g_assert (SGEN_OBJECT_IS_PINNED (obj));
894                         }
895                 }
896         }
897 }
898 #else
899 static void
900 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
901 {
902         void *obj = *ptr;
903         MSBlockInfo *block;
904
905         HEAVY_STAT (++stat_copy_object_called_major);
906
907         DEBUG (9, g_assert (obj));
908         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
909
910         if (ptr_in_nursery (obj)) {
911                 int word, bit;
912                 char *forwarded;
913
914                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
915                         *ptr = forwarded;
916                         return;
917                 }
918                 if (SGEN_OBJECT_IS_PINNED (obj))
919                         return;
920
921                 HEAVY_STAT (++stat_objects_copied_major);
922
923         do_copy_object:
924                 obj = copy_object_no_checks (obj, queue);
925                 *ptr = obj;
926
927                 /*
928                  * FIXME: See comment for copy_object_no_checks().  If
929                  * we have that, we can let the allocation function
930                  * give us the block info, too, and we won't have to
931                  * re-fetch it.
932                  */
933                 block = MS_BLOCK_FOR_OBJ (obj);
934                 MS_CALC_MARK_BIT (word, bit, obj);
935                 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
936                 MS_SET_MARK_BIT (block, word, bit);
937         } else {
938                 char *forwarded;
939                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
940                         *ptr = forwarded;
941                         return;
942                 }
943
944 #ifdef FIXED_HEAP
945                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
946 #else
947                 mword objsize;
948
949                 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
950
951                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
952 #endif
953                 {
954                         int size_index;
955
956                         block = MS_BLOCK_FOR_OBJ (obj);
957                         size_index = block->obj_size_index;
958
959                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
960                                 if (block->is_to_space)
961                                         return;
962                                 HEAVY_STAT (++stat_major_objects_evacuated);
963                                 goto do_copy_object;
964                         } else {
965                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
966                         }
967                 } else {
968                         if (SGEN_OBJECT_IS_PINNED (obj))
969                                 return;
970                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
971                         SGEN_PIN_OBJECT (obj);
972                         /* FIXME: only enqueue if object has references */
973                         GRAY_OBJECT_ENQUEUE (queue, obj);
974                 }
975         }
976 }
977 #endif
978
979 #include "sgen-major-scan-object.h"
980
981 static void
982 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
983 {
984         int i;
985         int last_index = -1;
986
987         if (!block->pin_queue_num_entries)
988                 return;
989
990 #ifndef SGEN_PARALLEL_MARK
991         block->has_pinned = TRUE;
992 #endif
993
994         for (i = 0; i < block->pin_queue_num_entries; ++i) {
995                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
996                 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
997                 if (index == last_index)
998                         continue;
999                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1000                 last_index = index;
1001         }
1002 }
1003
1004 static void
1005 major_sweep (void)
1006 {
1007         int i;
1008 #ifdef FIXED_HEAP
1009         int j;
1010 #else
1011         MSBlockInfo **iter;
1012 #endif
1013 #ifndef SGEN_PARALLEL_MARK
1014         /* statistics for evacuation */
1015         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1016         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1017         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1018
1019         for (i = 0; i < num_block_obj_sizes; ++i)
1020                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1021 #endif
1022
1023         /* clear all the free lists */
1024         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1025                 MSBlockInfo **free_blocks = free_block_lists [i];
1026                 int j;
1027                 for (j = 0; j < num_block_obj_sizes; ++j)
1028                         free_blocks [j] = NULL;
1029         }
1030
1031         /* traverse all blocks, free and zero unmarked objects */
1032 #ifdef FIXED_HEAP
1033         for (j = 0; j < ms_heap_num_blocks; ++j) {
1034                 MSBlockInfo *block = &block_infos [j];
1035 #else
1036         iter = &all_blocks;
1037         while (*iter) {
1038                 MSBlockInfo *block = *iter;
1039 #endif
1040                 int count;
1041                 gboolean have_live = FALSE;
1042                 gboolean has_pinned;
1043                 int obj_index;
1044                 int obj_size_index;
1045
1046 #ifdef FIXED_HEAP
1047                 if (!block->used)
1048                         continue;
1049 #endif
1050
1051                 obj_size_index = block->obj_size_index;
1052
1053 #ifndef SGEN_PARALLEL_MARK
1054                 has_pinned = block->has_pinned;
1055                 block->has_pinned = block->pinned;
1056
1057                 block->is_to_space = FALSE;
1058 #endif
1059
1060                 count = MS_BLOCK_FREE / block->obj_size;
1061                 block->free_list = NULL;
1062
1063                 for (obj_index = 0; obj_index < count; ++obj_index) {
1064                         int word, bit;
1065                         void *obj = MS_BLOCK_OBJ (block, obj_index);
1066
1067                         MS_CALC_MARK_BIT (word, bit, obj);
1068                         if (MS_MARK_BIT (block, word, bit)) {
1069                                 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1070                                 have_live = TRUE;
1071 #ifndef SGEN_PARALLEL_MARK
1072                                 if (!has_pinned)
1073                                         ++slots_used [obj_size_index];
1074 #endif
1075                         } else {
1076                                 /* an unmarked object */
1077                                 if (MS_OBJ_ALLOCED (obj, block)) {
1078                                         binary_protocol_empty (obj, block->obj_size);
1079                                         memset (obj, 0, block->obj_size);
1080                                 }
1081                                 *(void**)obj = block->free_list;
1082                                 block->free_list = obj;
1083                         }
1084                 }
1085
1086                 /* reset mark bits */
1087                 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1088
1089                 /*
1090                  * FIXME: reverse free list so that it's in address
1091                  * order
1092                  */
1093
1094                 if (have_live) {
1095 #ifndef SGEN_PARALLEL_MARK
1096                         if (!has_pinned) {
1097                                 ++num_blocks [obj_size_index];
1098                                 slots_available [obj_size_index] += count;
1099                         }
1100 #endif
1101
1102 #ifndef FIXED_HEAP
1103                         iter = &block->next;
1104 #endif
1105
1106                         /*
1107                          * If there are free slots in the block, add
1108                          * the block to the corresponding free list.
1109                          */
1110                         if (block->free_list) {
1111                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1112                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1113                                 block->next_free = free_blocks [index];
1114                                 free_blocks [index] = block;
1115                         }
1116                 } else {
1117                         /*
1118                          * Blocks without live objects are removed from the
1119                          * block list and freed.
1120                          */
1121 #ifdef FIXED_HEAP
1122                         ms_free_block (block);
1123 #else
1124                         *iter = block->next;
1125
1126                         ms_free_block (block->block);
1127                         mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1128 #endif
1129
1130                         --num_major_sections;
1131                 }
1132         }
1133
1134 #ifndef SGEN_PARALLEL_MARK
1135         for (i = 0; i < num_block_obj_sizes; ++i) {
1136                 float usage = (float)slots_used [i] / (float)slots_available [i];
1137                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1138                         evacuate_block_obj_sizes [i] = TRUE;
1139                         /*
1140                         g_print ("slot size %d - %d of %d used\n",
1141                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1142                         */
1143                 } else {
1144                         evacuate_block_obj_sizes [i] = FALSE;
1145                 }
1146         }
1147 #endif
1148 }
1149
1150 static int count_pinned_ref;
1151 static int count_pinned_nonref;
1152 static int count_nonpinned_ref;
1153 static int count_nonpinned_nonref;
1154
1155 static void
1156 count_nonpinned_callback (char *obj, size_t size, void *data)
1157 {
1158         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1159
1160         if (vtable->klass->has_references)
1161                 ++count_nonpinned_ref;
1162         else
1163                 ++count_nonpinned_nonref;
1164 }
1165
1166 static void
1167 count_pinned_callback (char *obj, size_t size, void *data)
1168 {
1169         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1170
1171         if (vtable->klass->has_references)
1172                 ++count_pinned_ref;
1173         else
1174                 ++count_pinned_nonref;
1175 }
1176
1177 static void __attribute__ ((unused))
1178 count_ref_nonref_objs (void)
1179 {
1180         int total;
1181
1182         count_pinned_ref = 0;
1183         count_pinned_nonref = 0;
1184         count_nonpinned_ref = 0;
1185         count_nonpinned_nonref = 0;
1186
1187         major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1188         major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1189
1190         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1191
1192         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1193                         count_pinned_ref, count_nonpinned_ref,
1194                         count_pinned_nonref, count_nonpinned_nonref,
1195                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1196 }
1197
1198 static int
1199 ms_calculate_block_obj_sizes (double factor, int *arr)
1200 {
1201         double target_size = sizeof (MonoObject);
1202         int num_sizes = 0;
1203         int last_size = 0;
1204
1205         do {
1206                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1207                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1208
1209                 if (size != last_size) {
1210                         if (arr)
1211                                 arr [num_sizes] = size;
1212                         ++num_sizes;
1213                         last_size = size;
1214                 }
1215
1216                 target_size *= factor;
1217         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1218
1219         return num_sizes;
1220 }
1221
1222 /* only valid during minor collections */
1223 static int old_num_major_sections;
1224
1225 static void
1226 major_start_nursery_collection (void)
1227 {
1228 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1229         consistency_check ();
1230 #endif
1231
1232         old_num_major_sections = num_major_sections;
1233 }
1234
1235 static void
1236 major_finish_nursery_collection (void)
1237 {
1238 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1239         consistency_check ();
1240 #endif
1241         mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1242 }
1243
1244 static void
1245 major_start_major_collection (void)
1246 {
1247 #ifndef SGEN_PARALLEL_MARK
1248         int i;
1249
1250         /* clear the free lists */
1251         for (i = 0; i < num_block_obj_sizes; ++i) {
1252                 if (!evacuate_block_obj_sizes [i])
1253                         continue;
1254
1255                 free_block_lists [0][i] = NULL;
1256                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1257         }
1258 #endif
1259 }
1260
1261 static void
1262 major_finish_major_collection (void)
1263 {
1264 #ifndef FIXED_HEAP
1265         int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1266
1267         /*
1268          * FIXME: We don't free blocks on 32 bit platforms because it
1269          * can lead to address space fragmentation, since we're
1270          * allocating blocks in larger contingents.
1271          */
1272         if (sizeof (mword) < 8)
1273                 return;
1274
1275         while (num_empty_blocks > section_reserve) {
1276                 void *next = *(void**)empty_blocks;
1277                 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1278                 empty_blocks = next;
1279                 /*
1280                  * Needs not be atomic because this is running
1281                  * single-threaded.
1282                  */
1283                 --num_empty_blocks;
1284
1285                 ++stat_major_blocks_freed;
1286         }
1287 #endif
1288 }
1289
1290 static void
1291 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1292 {
1293         MSBlockInfo *block;
1294
1295         FOREACH_BLOCK (block) {
1296                 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1297                                 &block->pin_queue_num_entries);
1298         } END_FOREACH_BLOCK;
1299 }
1300
1301 static void
1302 major_pin_objects (SgenGrayQueue *queue)
1303 {
1304         MSBlockInfo *block;
1305
1306         FOREACH_BLOCK (block) {
1307                 mark_pinned_objects_in_block (block, queue);
1308         } END_FOREACH_BLOCK;
1309 }
1310
1311 static void
1312 major_init_to_space (void)
1313 {
1314 }
1315
1316 static void
1317 major_report_pinned_memory_usage (void)
1318 {
1319         g_assert_not_reached ();
1320 }
1321
1322 static gint64
1323 major_get_used_size (void)
1324 {
1325         gint64 size = 0;
1326         MSBlockInfo *block;
1327
1328         FOREACH_BLOCK (block) {
1329                 int count = MS_BLOCK_FREE / block->obj_size;
1330                 void **iter;
1331                 size += count * block->obj_size;
1332                 for (iter = block->free_list; iter; iter = (void**)*iter)
1333                         size -= block->obj_size;
1334         } END_FOREACH_BLOCK;
1335
1336         return size;
1337 }
1338
1339 static int
1340 get_num_major_sections (void)
1341 {
1342         return num_major_sections;
1343 }
1344
1345 static gboolean
1346 major_handle_gc_param (const char *opt)
1347 {
1348 #ifdef FIXED_HEAP
1349         if (g_str_has_prefix (opt, "major-heap-size=")) {
1350                 const char *arg = strchr (opt, '=') + 1;
1351                 glong size;
1352                 if (!mono_sgen_parse_environment_string_extract_number (arg, &size))
1353                         return FALSE;
1354                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1355                 g_assert (ms_heap_num_blocks > 0);
1356                 return TRUE;
1357         } else
1358 #endif
1359 #ifndef SGEN_PARALLEL_MARK
1360         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1361                 const char *arg = strchr (opt, '=') + 1;
1362                 int percentage = atoi (arg);
1363                 if (percentage < 0 || percentage > 100) {
1364                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1365                         exit (1);
1366                 }
1367                 evacuation_threshold = (float)percentage / 100.0;
1368                 return TRUE;
1369         }
1370 #endif
1371
1372         return FALSE;
1373 }
1374
1375 static void
1376 major_print_gc_param_usage (void)
1377 {
1378         fprintf (stderr,
1379                         ""
1380 #ifdef FIXED_HEAP
1381                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1382 #endif
1383 #ifndef SGEN_PARALLEL_MARK
1384                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1385 #endif
1386                         );
1387 }
1388
1389 #ifdef SGEN_HAVE_CARDTABLE
1390 static void
1391 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1392 {
1393         MSBlockInfo *block;
1394
1395         FOREACH_BLOCK (block) {
1396                 if (block->has_references)
1397                         callback ((mword)block->block, MS_BLOCK_SIZE);
1398         } END_FOREACH_BLOCK;
1399 }
1400
1401 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1402 /*
1403  * MS blocks are 16K aligned.
1404  * Cardtables are 4K aligned, at least.
1405  * This means that the cardtable of a given block is 32 bytes aligned.
1406  */
1407 static guint8*
1408 initial_skip_card (guint8 *card_data)
1409 {
1410         mword *cards = (mword*)card_data;
1411         mword card;
1412         int i;
1413         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1414                 card = cards [i];
1415                 if (card)
1416                         break;
1417         }
1418
1419         if (i == CARD_WORDS_PER_BLOCK)
1420                 return card_data + CARDS_PER_BLOCK;
1421
1422 #if defined(__i386__) && defined(__GNUC__)
1423         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
1424 #elif defined(__x86_64__) && defined(__GNUC__)
1425         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
1426 #else
1427         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1428                 if (card_data [i])
1429                         return &card_data [i];
1430         }
1431         return card_data;
1432 #endif
1433 }
1434
1435
1436 static G_GNUC_UNUSED guint8*
1437 skip_card (guint8 *card_data, guint8 *card_data_end)
1438 {
1439         while (card_data < card_data_end && !*card_data)
1440                 ++card_data;
1441         return card_data;
1442 }
1443
1444 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1445 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
1446 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1447
1448 static void
1449 major_scan_card_table (SgenGrayQueue *queue)
1450 {
1451         MSBlockInfo *block;
1452
1453         FOREACH_BLOCK (block) {
1454                 int block_obj_size;
1455                 char *block_start;
1456
1457                 if (!block->has_references)
1458                         continue;
1459
1460                 block_obj_size = block->obj_size;
1461                 block_start = block->block;
1462
1463                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1464                         guint8 *cards;
1465 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1466                         guint8 cards_data [CARDS_PER_BLOCK];
1467 #endif
1468                         char *obj, *end, *base;
1469
1470                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
1471 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1472                         cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1473 #else
1474                         cards = cards_data;
1475                         if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1476                                 continue;
1477 #endif
1478
1479                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1480                         end = block_start + MS_BLOCK_SIZE;
1481                         base = sgen_card_table_align_pointer (obj);
1482
1483                         while (obj < end) {
1484                                 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1485                                         int card_offset = (obj - base) >> CARD_BITS;
1486                                         sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1487                                 }
1488                                 obj += block_obj_size;
1489                         }
1490                 } else {
1491                         guint8 *card_data, *card_base;
1492                         guint8 *card_data_end;
1493
1494                         /*
1495                          * This is safe in face of card aliasing for the following reason:
1496                          *
1497                          * Major blocks are 16k aligned, or 32 cards aligned.
1498                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1499                          * sizes, they won't overflow the cardtable overlap modulus.
1500                          */
1501                         card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1502                         card_data_end = card_data + CARDS_PER_BLOCK;
1503
1504                         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)) {
1505                                 int index;
1506                                 int idx = card_data - card_base;
1507                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1508                                 char *end = start + CARD_SIZE_IN_BYTES;
1509                                 char *obj;
1510
1511                                 if (!*card_data)
1512                                         continue;
1513                                 sgen_card_table_prepare_card_for_scanning (card_data);
1514
1515                                 if (idx == 0)
1516                                         index = 0;
1517                                 else
1518                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1519
1520                                 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1521                                 while (obj < end) {
1522                                         if (MS_OBJ_ALLOCED_FAST (obj, block_start))
1523                                                 minor_scan_object (obj, queue);
1524                                         obj += block_obj_size;
1525                                 }
1526                         }
1527                 }
1528         } END_FOREACH_BLOCK;
1529 }
1530 #endif
1531
1532 void
1533 #ifdef SGEN_PARALLEL_MARK
1534 #ifdef FIXED_HEAP
1535 mono_sgen_marksweep_fixed_par_init
1536 #else
1537 mono_sgen_marksweep_par_init
1538 #endif
1539 #else
1540 #ifdef FIXED_HEAP
1541 mono_sgen_marksweep_fixed_init
1542 #else
1543 mono_sgen_marksweep_init
1544 #endif
1545 #endif
1546         (SgenMajorCollector *collector)
1547 {
1548         int i;
1549
1550 #ifndef FIXED_HEAP
1551         mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1552 #endif
1553
1554         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1555         block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1556         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1557
1558 #ifndef SGEN_PARALLEL_MARK
1559         evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1560         for (i = 0; i < num_block_obj_sizes; ++i)
1561                 evacuate_block_obj_sizes [i] = FALSE;
1562 #endif
1563
1564         /*
1565         {
1566                 int i;
1567                 g_print ("block object sizes:\n");
1568                 for (i = 0; i < num_block_obj_sizes; ++i)
1569                         g_print ("%d\n", block_obj_sizes [i]);
1570         }
1571         */
1572
1573         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1574                 free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1575
1576         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1577                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1578         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1579                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1580
1581         LOCK_INIT (ms_block_list_mutex);
1582
1583         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
1584         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
1585         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
1586
1587         collector->section_size = MAJOR_SECTION_SIZE;
1588 #ifdef SGEN_PARALLEL_MARK
1589         collector->is_parallel = TRUE;
1590 #else
1591         collector->is_parallel = FALSE;
1592 #endif
1593         collector->supports_cardtable = TRUE;
1594
1595         collector->alloc_heap = major_alloc_heap;
1596         collector->is_object_live = major_is_object_live;
1597         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1598         collector->alloc_degraded = major_alloc_degraded;
1599         collector->copy_or_mark_object = major_copy_or_mark_object;
1600         collector->alloc_object = major_alloc_object;
1601         collector->free_pinned_object = free_pinned_object;
1602         collector->iterate_objects = major_iterate_objects;
1603         collector->free_non_pinned_object = major_free_non_pinned_object;
1604         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1605         collector->pin_objects = major_pin_objects;
1606 #ifdef SGEN_HAVE_CARDTABLE
1607         collector->scan_card_table = major_scan_card_table;
1608         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1609 #endif
1610         collector->init_to_space = major_init_to_space;
1611         collector->sweep = major_sweep;
1612         collector->check_scan_starts = major_check_scan_starts;
1613         collector->dump_heap = major_dump_heap;
1614         collector->get_used_size = major_get_used_size;
1615         collector->start_nursery_collection = major_start_nursery_collection;
1616         collector->finish_nursery_collection = major_finish_nursery_collection;
1617         collector->start_major_collection = major_start_major_collection;
1618         collector->finish_major_collection = major_finish_major_collection;
1619         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1620         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1621         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1622         collector->get_num_major_sections = get_num_major_sections;
1623         collector->handle_gc_param = major_handle_gc_param;
1624         collector->print_gc_param_usage = major_print_gc_param_usage;
1625
1626         FILL_COLLECTOR_COPY_OBJECT (collector);
1627         FILL_COLLECTOR_SCAN_OBJECT (collector);
1628
1629
1630         /*cardtable requires major pages to be 8 cards aligned*/
1631         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
1632
1633 }
1634
1635 #endif