Merge branch 'bugfix-main-thread-root'
[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 #include <errno.h>
33
34 #include "utils/mono-counters.h"
35 #include "utils/mono-semaphore.h"
36 #include "utils/mono-time.h"
37 #include "metadata/object-internals.h"
38 #include "metadata/profiler-private.h"
39
40 #include "metadata/sgen-gc.h"
41 #include "metadata/sgen-protocol.h"
42 #include "metadata/sgen-cardtable.h"
43 #include "metadata/gc-internal.h"
44
45 #define MS_BLOCK_SIZE   (16*1024)
46 #define MS_BLOCK_SIZE_SHIFT     14
47 #define MAJOR_SECTION_SIZE      MS_BLOCK_SIZE
48 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
49
50 #ifdef FIXED_HEAP
51 #define MS_DEFAULT_HEAP_NUM_BLOCKS      (32 * 1024) /* 512 MB */
52 #endif
53
54 /*
55  * Don't allocate single blocks, but alloc a contingent of this many
56  * blocks in one swoop.
57  */
58 #define MS_BLOCK_ALLOC_NUM      32
59
60 /*
61  * Number of bytes before the first object in a block.  At the start
62  * of a block is the MSBlockHeader, then opional padding, then come
63  * the objects, so this must be >= sizeof (MSBlockHeader).
64  */
65 #ifdef FIXED_HEAP
66 #define MS_BLOCK_SKIP   0
67 #else
68 #define MS_BLOCK_SKIP   16
69 #endif
70
71 #define MS_BLOCK_FREE   (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
72
73 #define MS_NUM_MARK_WORDS       ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
74
75 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
76 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
77 #endif
78
79 typedef struct _MSBlockInfo MSBlockInfo;
80 struct _MSBlockInfo {
81         int obj_size;
82         int obj_size_index;
83         int pin_queue_num_entries;
84         unsigned int pinned : 1;
85         unsigned int has_references : 1;
86         unsigned int has_pinned : 1;    /* means cannot evacuate */
87         unsigned int is_to_space : 1;
88 #ifdef FIXED_HEAP
89         unsigned int used : 1;
90         unsigned int zeroed : 1;
91 #endif
92         MSBlockInfo *next;
93         char *block;
94         void **free_list;
95         MSBlockInfo *next_free;
96         void **pin_queue_start;
97         mword mark_words [MS_NUM_MARK_WORDS];
98 };
99
100 #ifdef FIXED_HEAP
101 static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
102
103 #define ms_heap_start   nursery_end
104 static char *ms_heap_end;
105
106 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p)   ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
107
108 /* array of all all block infos in the system */
109 static MSBlockInfo *block_infos;
110 #endif
111
112 #define MS_BLOCK_OBJ(b,i)               ((b)->block + MS_BLOCK_SKIP + (b)->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 pthread_mutex_lock (&ms_block_list_mutex)
178 #define UNLOCK_MS_BLOCK_LIST pthread_mutex_unlock (&ms_block_list_mutex)
179 #else
180 #define LOCK_MS_BLOCK_LIST
181 #define UNLOCK_MS_BLOCK_LIST
182 #endif
183
184 /* we get this at init */
185 static int nursery_bits;
186 static char *nursery_start;
187 static char *nursery_end;
188
189 static gboolean *evacuate_block_obj_sizes;
190 static float evacuation_threshold = 0.666;
191
192 static gboolean concurrent_sweep = FALSE;
193 static gboolean have_swept;
194
195 #define ptr_in_nursery(p)       (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
196
197 /* all allocated blocks in the system */
198 static MSBlockInfo *all_blocks;
199
200 #ifdef FIXED_HEAP
201 /* non-allocated block free-list */
202 static MSBlockInfo *empty_blocks = NULL;
203 #else
204 /* non-allocated block free-list */
205 static void *empty_blocks = NULL;
206 static int num_empty_blocks = 0;
207 #endif
208
209 #define FOREACH_BLOCK(bl)       for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
210 #define END_FOREACH_BLOCK       }
211
212 static int num_major_sections = 0;
213 /* one free block list for each block object size */
214 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
215
216 static long long stat_major_blocks_alloced = 0;
217 static long long stat_major_blocks_freed = 0;
218 static long long stat_major_objects_evacuated = 0;
219 static long long stat_time_wait_for_sweep = 0;
220 #ifdef SGEN_PARALLEL_MARK
221 static long long stat_slots_allocated_in_vain = 0;
222 #endif
223
224 static gboolean ms_sweep_in_progress = FALSE;
225 static pthread_t ms_sweep_thread;
226 static MonoSemType ms_sweep_cmd_semaphore;
227 static MonoSemType ms_sweep_done_semaphore;
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_MS (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         DEBUG (9, g_assert (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_assert_not_reached ();
283 }
284
285 #define FREE_BLOCKS(p,r) (free_block_lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
286
287 #define MS_BLOCK_OBJ_SIZE_INDEX(s)                              \
288         (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ?      \
289          fast_block_obj_size_indexes [((s)+7)>>3] :             \
290          ms_find_block_obj_size_index ((s)))
291
292 #ifdef FIXED_HEAP
293 static void*
294 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
295 {
296         char *heap_start;
297         mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
298         mword alloc_size = nursery_size + major_heap_size;
299         int i;
300
301         g_assert (ms_heap_num_blocks > 0);
302         g_assert (nursery_size % MS_BLOCK_SIZE == 0);
303         if (nursery_align)
304                 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
305
306         nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
307         nursery_end = heap_start = nursery_start + nursery_size;
308         nursery_bits = the_nursery_bits;
309
310         ms_heap_end = heap_start + major_heap_size;
311
312         block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
313
314         for (i = 0; i < ms_heap_num_blocks; ++i) {
315                 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
316                 if (i < ms_heap_num_blocks - 1)
317                         block_infos [i].next_free = &block_infos [i + 1];
318                 else
319                         block_infos [i].next_free = NULL;
320                 block_infos [i].zeroed = TRUE;
321         }
322
323         empty_blocks = &block_infos [0];
324
325         return nursery_start;
326 }
327 #else
328 static void*
329 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
330 {
331         if (nursery_align)
332                 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
333         else
334                 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
335
336         nursery_end = nursery_start + nursery_size;
337         nursery_bits = the_nursery_bits;
338
339         return nursery_start;
340 }
341 #endif
342
343 static void
344 update_heap_boundaries_for_block (MSBlockInfo *block)
345 {
346         mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
347 }
348
349 #ifdef FIXED_HEAP
350 static MSBlockInfo*
351 ms_get_empty_block (void)
352 {
353         MSBlockInfo *block;
354
355         g_assert (empty_blocks);
356
357         block = empty_blocks;
358         empty_blocks = empty_blocks->next_free;
359
360         block->used = TRUE;
361
362         if (!block->zeroed)
363                 memset (block->block, 0, MS_BLOCK_SIZE);
364
365         return block;
366 }
367
368 static void
369 ms_free_block (MSBlockInfo *block)
370 {
371         block->next_free = empty_blocks;
372         empty_blocks = block;
373         block->used = FALSE;
374         block->zeroed = FALSE;
375         mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
376 }
377 #else
378 static void*
379 ms_get_empty_block (void)
380 {
381         char *p;
382         int i;
383         void *block, *empty, *next;
384
385  retry:
386         if (!empty_blocks) {
387                 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
388
389                 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
390                         block = p;
391                         /*
392                          * We do the free list update one after the
393                          * other so that other threads can use the new
394                          * blocks as quickly as possible.
395                          */
396                         do {
397                                 empty = empty_blocks;
398                                 *(void**)block = empty;
399                         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
400                         p += MS_BLOCK_SIZE;
401                 }
402
403                 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
404
405                 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
406         }
407
408         do {
409                 empty = empty_blocks;
410                 if (!empty)
411                         goto retry;
412                 block = empty;
413                 next = *(void**)block;
414         } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
415
416         SGEN_ATOMIC_ADD (num_empty_blocks, -1);
417
418         *(void**)block = NULL;
419
420         g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
421
422         return block;
423 }
424
425 static void
426 ms_free_block (void *block)
427 {
428         void *empty;
429
430         mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
431         memset (block, 0, MS_BLOCK_SIZE);
432
433         do {
434                 empty = empty_blocks;
435                 *(void**)block = empty;
436         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
437
438         SGEN_ATOMIC_ADD (num_empty_blocks, 1);
439 }
440 #endif
441
442 //#define MARKSWEEP_CONSISTENCY_CHECK
443
444 #ifdef MARKSWEEP_CONSISTENCY_CHECK
445 static void
446 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
447 {
448         MSBlockInfo *b;
449
450         for (; block; block = block->next_free) {
451                 g_assert (block->obj_size == size);
452                 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
453
454                 /* blocks in the free lists must have at least
455                    one free slot */
456                 g_assert (block->free_list);
457
458 #ifdef FIXED_HEAP
459                 /* the block must not be in the empty_blocks list */
460                 for (b = empty_blocks; b; b = b->next_free)
461                         g_assert (b != block);
462 #endif
463                 /* the block must be in the all_blocks list */
464                 for (b = all_blocks; b; b = b->next) {
465                         if (b == block)
466                                 break;
467                 }
468                 g_assert (b == block);
469         }
470 }
471
472 static void
473 check_empty_blocks (void)
474 {
475 #ifndef FIXED_HEAP
476         void *p;
477         int i = 0;
478         for (p = empty_blocks; p; p = *(void**)p)
479                 ++i;
480         g_assert (i == num_empty_blocks);
481 #endif
482 }
483
484 static void
485 consistency_check (void)
486 {
487         MSBlockInfo *block;
488         int i;
489
490         /* check all blocks */
491         FOREACH_BLOCK (block) {
492                 int count = MS_BLOCK_FREE / block->obj_size;
493                 int num_free = 0;
494                 void **free;
495
496 #ifndef FIXED_HEAP
497                 /* check block header */
498                 g_assert (((MSBlockHeader*)block->block)->info == block);
499 #endif
500
501                 /* count number of free slots */
502                 for (i = 0; i < count; ++i) {
503                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
504                         if (!MS_OBJ_ALLOCED (obj, block))
505                                 ++num_free;
506                 }
507
508                 /* check free list */
509                 for (free = block->free_list; free; free = (void**)*free) {
510                         g_assert (MS_BLOCK_FOR_OBJ (free) == block);
511                         --num_free;
512                 }
513                 g_assert (num_free == 0);
514
515                 /* check all mark words are zero */
516                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
517                         g_assert (block->mark_words [i] == 0);
518         } END_FOREACH_BLOCK;
519
520         /* check free blocks */
521         for (i = 0; i < num_block_obj_sizes; ++i) {
522                 int j;
523                 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
524                         check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
525         }
526
527         check_empty_blocks ();
528 }
529 #endif
530
531 static gboolean
532 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
533 {
534         int size = block_obj_sizes [size_index];
535         int count = MS_BLOCK_FREE / size;
536         MSBlockInfo *info;
537 #ifndef FIXED_HEAP
538         MSBlockHeader *header;
539 #endif
540         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
541         char *obj_start;
542         int i;
543
544         if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
545                 return FALSE;
546
547 #ifdef FIXED_HEAP
548         info = ms_get_empty_block ();
549 #else
550         info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
551 #endif
552
553         DEBUG (9, g_assert (count >= 2));
554
555         info->obj_size = size;
556         info->obj_size_index = size_index;
557         info->pinned = pinned;
558         info->has_references = has_references;
559         info->has_pinned = pinned;
560         info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
561 #ifndef FIXED_HEAP
562         info->block = ms_get_empty_block ();
563
564         header = (MSBlockHeader*) info->block;
565         header->info = info;
566 #endif
567
568         update_heap_boundaries_for_block (info);
569
570         /* build free list */
571         obj_start = info->block + MS_BLOCK_SKIP;
572         info->free_list = (void**)obj_start;
573         /* we're skipping the last one - it must be nulled */
574         for (i = 0; i < count - 1; ++i) {
575                 char *next_obj_start = obj_start + size;
576                 *(void**)obj_start = next_obj_start;
577                 obj_start = next_obj_start;
578         }
579         /* the last one */
580         *(void**)obj_start = NULL;
581
582         info->next_free = free_blocks [size_index];
583         free_blocks [size_index] = info;
584
585         info->next = all_blocks;
586         all_blocks = info;
587
588         ++num_major_sections;
589         return TRUE;
590 }
591
592 static gboolean
593 obj_is_from_pinned_alloc (char *ptr)
594 {
595         MSBlockInfo *block;
596
597         FOREACH_BLOCK (block) {
598                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
599                         return block->pinned;
600         } END_FOREACH_BLOCK;
601         return FALSE;
602 }
603
604 static void*
605 alloc_obj (int size, gboolean pinned, gboolean has_references)
606 {
607         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
608         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
609         MSBlockInfo *block;
610         void *obj;
611
612         /* FIXME: try to do this without locking */
613
614         LOCK_MS_BLOCK_LIST;
615
616         g_assert (!ms_sweep_in_progress);
617
618         if (!free_blocks [size_index]) {
619                 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) {
620                         UNLOCK_MS_BLOCK_LIST;
621                         return NULL;
622                 }
623         }
624
625         block = free_blocks [size_index];
626         DEBUG (9, g_assert (block));
627
628         obj = block->free_list;
629         DEBUG (9, g_assert (obj));
630
631         block->free_list = *(void**)obj;
632         if (!block->free_list) {
633                 free_blocks [size_index] = block->next_free;
634                 block->next_free = NULL;
635         }
636
637         UNLOCK_MS_BLOCK_LIST;
638
639         /*
640          * FIXME: This should not be necessary because it'll be
641          * overwritten by the vtable immediately.
642          */
643         *(void**)obj = NULL;
644
645         return obj;
646 }
647
648 static void*
649 major_alloc_object (int size, gboolean has_references)
650 {
651         return alloc_obj (size, FALSE, has_references);
652 }
653
654 /*
655  * We're not freeing the block if it's empty.  We leave that work for
656  * the next major collection.
657  *
658  * This is just called from the domain clearing code, which runs in a
659  * single thread and has the GC lock, so we don't need an extra lock.
660  */
661 static void
662 free_object (char *obj, size_t size, gboolean pinned)
663 {
664         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
665         int word, bit;
666         DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
667         DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
668         MS_CALC_MARK_BIT (word, bit, obj);
669         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
670         if (!block->free_list) {
671                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
672                 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
673                 DEBUG (9, g_assert (!block->next_free));
674                 block->next_free = free_blocks [size_index];
675                 free_blocks [size_index] = block;
676         }
677         memset (obj, 0, size);
678         *(void**)obj = block->free_list;
679         block->free_list = (void**)obj;
680 }
681
682 static void
683 major_free_non_pinned_object (char *obj, size_t size)
684 {
685         free_object (obj, size, FALSE);
686 }
687
688 /* size is a multiple of SGEN_ALLOC_ALIGN */
689 static void*
690 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
691 {
692         void *res;
693
694         ms_wait_for_sweep_done ();
695
696         res = alloc_obj (size, TRUE, has_references);
697          /*If we failed to alloc memory, we better try releasing memory
698           *as pinned alloc is requested by the runtime.
699           */
700          if (!res) {
701                  sgen_collect_major_no_lock ("pinned alloc failure");
702                  res = alloc_obj (size, TRUE, has_references);
703          }
704          return res;
705 }
706
707 static void
708 free_pinned_object (char *obj, size_t size)
709 {
710         free_object (obj, size, TRUE);
711 }
712
713 /*
714  * size is already rounded up and we hold the GC lock.
715  */
716 static void*
717 major_alloc_degraded (MonoVTable *vtable, size_t size)
718 {
719         void *obj;
720         int old_num_sections;
721
722         ms_wait_for_sweep_done ();
723
724         old_num_sections = num_major_sections;
725
726         obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
727         if (G_LIKELY (obj)) {
728                 *(MonoVTable**)obj = vtable;
729                 HEAVY_STAT (++stat_objects_alloced_degraded);
730                 HEAVY_STAT (stat_bytes_alloced_degraded += size);
731                 g_assert (num_major_sections >= old_num_sections);
732                 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
733         }
734         return obj;
735 }
736
737 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj)   FALSE
738
739 /*
740  * obj is some object.  If it's not in the major heap (i.e. if it's in
741  * the nursery or LOS), return FALSE.  Otherwise return whether it's
742  * been marked or copied.
743  */
744 static gboolean
745 major_is_object_live (char *obj)
746 {
747         MSBlockInfo *block;
748         int word, bit;
749 #ifndef FIXED_HEAP
750         mword objsize;
751 #endif
752
753         if (ptr_in_nursery (obj))
754                 return FALSE;
755
756 #ifdef FIXED_HEAP
757         /* LOS */
758         if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
759                 return FALSE;
760 #else
761         objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
762
763         /* LOS */
764         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
765                 return FALSE;
766 #endif
767
768         /* now we know it's in a major block */
769         block = MS_BLOCK_FOR_OBJ (obj);
770         DEBUG (9, g_assert (!block->pinned));
771         MS_CALC_MARK_BIT (word, bit, obj);
772         return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
773 }
774
775 static gboolean
776 major_ptr_is_in_non_pinned_space (char *ptr)
777 {
778         MSBlockInfo *block;
779
780         FOREACH_BLOCK (block) {
781                 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
782                         return !block->pinned;
783         } END_FOREACH_BLOCK;
784         return FALSE;
785 }
786
787 static void
788 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
789 {
790         MSBlockInfo *block;
791
792         ms_wait_for_sweep_done ();
793
794         FOREACH_BLOCK (block) {
795                 int count = MS_BLOCK_FREE / block->obj_size;
796                 int i;
797
798                 if (block->pinned && !pinned)
799                         continue;
800                 if (!block->pinned && !non_pinned)
801                         continue;
802
803                 for (i = 0; i < count; ++i) {
804                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
805                         if (MS_OBJ_ALLOCED (obj, block))
806                                 callback ((char*)obj, block->obj_size, data);
807                 }
808         } END_FOREACH_BLOCK;
809 }
810
811 static void
812 major_check_scan_starts (void)
813 {
814 }
815
816 static void
817 major_dump_heap (FILE *heap_dump_file)
818 {
819         MSBlockInfo *block;
820         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
821         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
822         int i;
823
824         for (i = 0; i < num_block_obj_sizes; ++i)
825                 slots_available [i] = slots_used [i] = 0;
826
827         FOREACH_BLOCK (block) {
828                 int index = ms_find_block_obj_size_index (block->obj_size);
829                 int count = MS_BLOCK_FREE / block->obj_size;
830
831                 slots_available [index] += count;
832                 for (i = 0; i < count; ++i) {
833                         if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
834                                 ++slots_used [index];
835                 }
836         } END_FOREACH_BLOCK;
837
838         fprintf (heap_dump_file, "<occupancies>\n");
839         for (i = 0; i < num_block_obj_sizes; ++i) {
840                 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
841                                 block_obj_sizes [i], slots_available [i], slots_used [i]);
842         }
843         fprintf (heap_dump_file, "</occupancies>\n");
844
845         FOREACH_BLOCK (block) {
846                 int count = MS_BLOCK_FREE / block->obj_size;
847                 int i;
848                 int start = -1;
849
850                 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
851
852                 for (i = 0; i <= count; ++i) {
853                         if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
854                                 if (start < 0)
855                                         start = i;
856                         } else {
857                                 if (start >= 0) {
858                                         mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
859                                         start = -1;
860                                 }
861                         }
862                 }
863
864                 fprintf (heap_dump_file, "</section>\n");
865         } END_FOREACH_BLOCK;
866 }
867
868 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
869
870 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {        \
871                 int __word, __bit;                                      \
872                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
873                 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
874                         MS_SET_MARK_BIT ((block), __word, __bit);       \
875                         if ((block)->has_references)                    \
876                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
877                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
878                 }                                                       \
879         } while (0)
880 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {                \
881                 int __word, __bit;                                      \
882                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
883                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
884                 if (!MS_MARK_BIT ((block), __word, __bit)) {            \
885                         MS_SET_MARK_BIT ((block), __word, __bit);       \
886                         if ((block)->has_references)                    \
887                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
888                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
889                 }                                                       \
890         } while (0)
891 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {            \
892                 int __word, __bit;                                      \
893                 gboolean __was_marked;                                  \
894                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
895                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
896                 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
897                 if (!__was_marked) {                                    \
898                         if ((block)->has_references)                    \
899                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
900                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
901                 }                                                       \
902         } while (0)
903
904 #include "sgen-major-copy-object.h"
905
906 #ifdef SGEN_PARALLEL_MARK
907 static void
908 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
909 {
910         void *obj = *ptr;
911         mword objsize;
912         MSBlockInfo *block;
913         MonoVTable *vt;
914
915         HEAVY_STAT (++stat_copy_object_called_major);
916
917         DEBUG (9, g_assert (obj));
918         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
919
920         if (ptr_in_nursery (obj)) {
921                 int word, bit;
922                 gboolean has_references;
923                 void *destination;
924                 mword vtable_word = *(mword*)obj;
925                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
926
927                 if (vtable_word & SGEN_FORWARDED_BIT) {
928                         *ptr = (void*)vt;
929                         return;
930                 }
931
932                 if (vtable_word & SGEN_PINNED_BIT)
933                         return;
934
935                 HEAVY_STAT (++stat_objects_copied_major);
936
937         do_copy_object:
938                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
939                 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
940
941                 destination = major_alloc_object (objsize, has_references);
942                 if (G_UNLIKELY (!destination)) {
943                         if (!ptr_in_nursery (obj)) {
944                                 int size_index;
945                                 block = MS_BLOCK_FOR_OBJ (obj);
946                                 size_index = block->obj_size_index;
947                                 evacuate_block_obj_sizes [size_index] = FALSE;
948                         }
949
950                         do {
951                                 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
952                                         mono_sgen_pin_object (obj, queue);
953                                         break;
954                                 }
955
956                                 vtable_word = *(mword*)obj;
957                                 /*someone else forwarded it, update the pointer and bail out*/
958                                 if (vtable_word & SGEN_FORWARDED_BIT) {
959                                         *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
960                                         break;
961                                 }
962
963                                 /*someone pinned it, nothing to do.*/
964                                 if (vtable_word & SGEN_PINNED_BIT)
965                                         break;
966                         } while (TRUE);
967                         return;
968                 }
969
970                 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
971                         gboolean was_marked;
972
973                         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
974                         obj = destination;
975                         *ptr = obj;
976
977                         /*
978                          * FIXME: If we make major_alloc_object() give
979                          * us the block info, too, we won't have to
980                          * re-fetch it here.
981                          */
982                         block = MS_BLOCK_FOR_OBJ (obj);
983                         MS_CALC_MARK_BIT (word, bit, obj);
984                         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
985                         MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
986                 } else {
987                         /*
988                          * FIXME: We have allocated destination, but
989                          * we cannot use it.  Give it back to the
990                          * allocator.
991                          */
992                         *(void**)destination = NULL;
993
994                         vtable_word = *(mword*)obj;
995                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
996
997                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
998
999                         *ptr = obj;
1000
1001                         ++stat_slots_allocated_in_vain;
1002                 }
1003         } else {
1004 #ifdef FIXED_HEAP
1005                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1006 #else
1007                 mword vtable_word = *(mword*)obj;
1008                 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1009
1010                 /* see comment in the non-parallel version below */
1011                 if (vtable_word & SGEN_FORWARDED_BIT) {
1012                         *ptr = (void*)vt;
1013                         return;
1014                 }
1015                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
1016
1017                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1018 #endif
1019                 {
1020                         int size_index;
1021
1022                         block = MS_BLOCK_FOR_OBJ (obj);
1023                         size_index = block->obj_size_index;
1024
1025                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1026                                 if (block->is_to_space)
1027                                         return;
1028
1029 #ifdef FIXED_HEAP
1030                                 {
1031                                         mword vtable_word = *(mword*)obj;
1032                                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1033
1034                                         if (vtable_word & SGEN_FORWARDED_BIT) {
1035                                                 *ptr = (void*)vt;
1036                                                 return;
1037                                         }
1038                                 }
1039 #endif
1040
1041                                 HEAVY_STAT (++stat_major_objects_evacuated);
1042                                 goto do_copy_object;
1043                         }
1044
1045                         MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1046                 } else {
1047 #ifdef FIXED_HEAP
1048                         mword vtable_word = *(mword*)obj;
1049                         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1050 #endif
1051
1052                         if (vtable_word & SGEN_PINNED_BIT)
1053                                 return;
1054                         binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
1055                         if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1056                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1057                                         GRAY_OBJECT_ENQUEUE (queue, obj);
1058                         } else {
1059                                 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1060                         }
1061                 }
1062         }
1063 }
1064 #else
1065 static void
1066 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1067 {
1068         void *obj = *ptr;
1069         MSBlockInfo *block;
1070
1071         HEAVY_STAT (++stat_copy_object_called_major);
1072
1073         DEBUG (9, g_assert (obj));
1074         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1075
1076         if (ptr_in_nursery (obj)) {
1077                 int word, bit;
1078                 char *forwarded, *old_obj;
1079
1080                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1081                         *ptr = forwarded;
1082                         return;
1083                 }
1084                 if (SGEN_OBJECT_IS_PINNED (obj))
1085                         return;
1086
1087                 HEAVY_STAT (++stat_objects_copied_major);
1088
1089         do_copy_object:
1090                 old_obj = obj;
1091                 obj = copy_object_no_checks (obj, queue);
1092                 if (G_UNLIKELY (old_obj == obj)) {
1093                         /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1094                         if (!ptr_in_nursery (obj)) {
1095                                 int size_index;
1096                                 block = MS_BLOCK_FOR_OBJ (obj);
1097                                 size_index = block->obj_size_index;
1098                                 evacuate_block_obj_sizes [size_index] = FALSE;
1099                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1100                         }
1101                         return;
1102                 }
1103                 *ptr = obj;
1104
1105                 /*
1106                  * FIXME: See comment for copy_object_no_checks().  If
1107                  * we have that, we can let the allocation function
1108                  * give us the block info, too, and we won't have to
1109                  * re-fetch it.
1110                  */
1111                 block = MS_BLOCK_FOR_OBJ (obj);
1112                 MS_CALC_MARK_BIT (word, bit, obj);
1113                 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1114                 MS_SET_MARK_BIT (block, word, bit);
1115         } else {
1116                 char *forwarded;
1117 #ifdef FIXED_HEAP
1118                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1119 #else
1120                 mword objsize;
1121
1122                 /*
1123                  * If we have don't have a fixed heap we cannot know
1124                  * whether an object is in the LOS or in the small
1125                  * object major heap without checking its size.  To do
1126                  * that, however, we need to know that we actually
1127                  * have a valid object, not a forwarding pointer, so
1128                  * we have to do this check first.
1129                  */
1130                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1131                         *ptr = forwarded;
1132                         return;
1133                 }
1134
1135                 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1136
1137                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1138 #endif
1139                 {
1140                         int size_index;
1141                         gboolean evacuate;
1142
1143                         block = MS_BLOCK_FOR_OBJ (obj);
1144                         size_index = block->obj_size_index;
1145                         evacuate = evacuate_block_obj_sizes [size_index];
1146
1147 #ifdef FIXED_HEAP
1148                         /*
1149                          * We could also check for !block->has_pinned
1150                          * here, but it would only make an uncommon case
1151                          * faster, namely objects that are in blocks
1152                          * whose slot sizes are evacuated but which have
1153                          * pinned objects.
1154                          */
1155                         if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1156                                 *ptr = forwarded;
1157                                 return;
1158                         }
1159 #endif
1160
1161                         if (evacuate && !block->has_pinned) {
1162                                 if (block->is_to_space)
1163                                         return;
1164                                 HEAVY_STAT (++stat_major_objects_evacuated);
1165                                 goto do_copy_object;
1166                         } else {
1167                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1168                         }
1169                 } else {
1170                         if (SGEN_OBJECT_IS_PINNED (obj))
1171                                 return;
1172                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
1173                         SGEN_PIN_OBJECT (obj);
1174                         /* FIXME: only enqueue if object has references */
1175                         GRAY_OBJECT_ENQUEUE (queue, obj);
1176                 }
1177         }
1178 }
1179 #endif
1180
1181 #include "sgen-major-scan-object.h"
1182
1183 static void
1184 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1185 {
1186         int i;
1187         int last_index = -1;
1188
1189         if (!block->pin_queue_num_entries)
1190                 return;
1191
1192         block->has_pinned = TRUE;
1193
1194         for (i = 0; i < block->pin_queue_num_entries; ++i) {
1195                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1196                 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1197                 if (index == last_index)
1198                         continue;
1199                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1200                 last_index = index;
1201         }
1202 }
1203
1204 static void
1205 ms_sweep (void)
1206 {
1207         int i;
1208         MSBlockInfo **iter;
1209
1210         /* statistics for evacuation */
1211         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1212         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1213         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1214
1215         for (i = 0; i < num_block_obj_sizes; ++i)
1216                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1217
1218         /* clear all the free lists */
1219         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1220                 MSBlockInfo **free_blocks = free_block_lists [i];
1221                 int j;
1222                 for (j = 0; j < num_block_obj_sizes; ++j)
1223                         free_blocks [j] = NULL;
1224         }
1225
1226         /* traverse all blocks, free and zero unmarked objects */
1227         iter = &all_blocks;
1228         while (*iter) {
1229                 MSBlockInfo *block = *iter;
1230                 int count;
1231                 gboolean have_live = FALSE;
1232                 gboolean has_pinned;
1233                 int obj_index;
1234                 int obj_size_index;
1235
1236                 obj_size_index = block->obj_size_index;
1237
1238                 has_pinned = block->has_pinned;
1239                 block->has_pinned = block->pinned;
1240
1241                 block->is_to_space = FALSE;
1242
1243                 count = MS_BLOCK_FREE / block->obj_size;
1244                 block->free_list = NULL;
1245
1246                 for (obj_index = 0; obj_index < count; ++obj_index) {
1247                         int word, bit;
1248                         void *obj = MS_BLOCK_OBJ (block, obj_index);
1249
1250                         MS_CALC_MARK_BIT (word, bit, obj);
1251                         if (MS_MARK_BIT (block, word, bit)) {
1252                                 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1253                                 have_live = TRUE;
1254                                 if (!has_pinned)
1255                                         ++slots_used [obj_size_index];
1256                         } else {
1257                                 /* an unmarked object */
1258                                 if (MS_OBJ_ALLOCED (obj, block)) {
1259                                         binary_protocol_empty (obj, block->obj_size);
1260                                         memset (obj, 0, block->obj_size);
1261                                 }
1262                                 *(void**)obj = block->free_list;
1263                                 block->free_list = obj;
1264                         }
1265                 }
1266
1267                 /* reset mark bits */
1268                 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1269
1270                 /*
1271                  * FIXME: reverse free list so that it's in address
1272                  * order
1273                  */
1274
1275                 if (have_live) {
1276                         if (!has_pinned) {
1277                                 ++num_blocks [obj_size_index];
1278                                 slots_available [obj_size_index] += count;
1279                         }
1280
1281                         iter = &block->next;
1282
1283                         /*
1284                          * If there are free slots in the block, add
1285                          * the block to the corresponding free list.
1286                          */
1287                         if (block->free_list) {
1288                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1289                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1290                                 block->next_free = free_blocks [index];
1291                                 free_blocks [index] = block;
1292                         }
1293
1294                         update_heap_boundaries_for_block (block);
1295                 } else {
1296                         /*
1297                          * Blocks without live objects are removed from the
1298                          * block list and freed.
1299                          */
1300                         *iter = block->next;
1301
1302 #ifdef FIXED_HEAP
1303                         ms_free_block (block);
1304 #else
1305                         ms_free_block (block->block);
1306
1307                         mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1308 #endif
1309
1310                         --num_major_sections;
1311                 }
1312         }
1313
1314         for (i = 0; i < num_block_obj_sizes; ++i) {
1315                 float usage = (float)slots_used [i] / (float)slots_available [i];
1316                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1317                         evacuate_block_obj_sizes [i] = TRUE;
1318                         /*
1319                         g_print ("slot size %d - %d of %d used\n",
1320                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1321                         */
1322                 } else {
1323                         evacuate_block_obj_sizes [i] = FALSE;
1324                 }
1325         }
1326
1327         have_swept = TRUE;
1328 }
1329
1330 static void*
1331 ms_sweep_thread_func (void *dummy)
1332 {
1333         g_assert (concurrent_sweep);
1334
1335         for (;;) {
1336                 int result;
1337
1338                 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1339                         if (errno != EINTR)
1340                                 g_error ("MONO_SEM_WAIT");
1341                 }
1342
1343                 ms_sweep ();
1344
1345                 ms_signal_sweep_done ();
1346         }
1347
1348         return NULL;
1349 }
1350
1351 static void
1352 major_sweep (void)
1353 {
1354         if (concurrent_sweep) {
1355                 g_assert (ms_sweep_thread);
1356                 ms_signal_sweep_command ();
1357         } else {
1358                 ms_sweep ();
1359         }
1360 }
1361
1362 static int count_pinned_ref;
1363 static int count_pinned_nonref;
1364 static int count_nonpinned_ref;
1365 static int count_nonpinned_nonref;
1366
1367 static void
1368 count_nonpinned_callback (char *obj, size_t size, void *data)
1369 {
1370         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1371
1372         if (vtable->klass->has_references)
1373                 ++count_nonpinned_ref;
1374         else
1375                 ++count_nonpinned_nonref;
1376 }
1377
1378 static void
1379 count_pinned_callback (char *obj, size_t size, void *data)
1380 {
1381         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1382
1383         if (vtable->klass->has_references)
1384                 ++count_pinned_ref;
1385         else
1386                 ++count_pinned_nonref;
1387 }
1388
1389 static void __attribute__ ((unused))
1390 count_ref_nonref_objs (void)
1391 {
1392         int total;
1393
1394         count_pinned_ref = 0;
1395         count_pinned_nonref = 0;
1396         count_nonpinned_ref = 0;
1397         count_nonpinned_nonref = 0;
1398
1399         major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1400         major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1401
1402         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1403
1404         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1405                         count_pinned_ref, count_nonpinned_ref,
1406                         count_pinned_nonref, count_nonpinned_nonref,
1407                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1408 }
1409
1410 static int
1411 ms_calculate_block_obj_sizes (double factor, int *arr)
1412 {
1413         double target_size = sizeof (MonoObject);
1414         int num_sizes = 0;
1415         int last_size = 0;
1416
1417         do {
1418                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1419                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1420
1421                 if (size != last_size) {
1422                         if (arr)
1423                                 arr [num_sizes] = size;
1424                         ++num_sizes;
1425                         last_size = size;
1426                 }
1427
1428                 target_size *= factor;
1429         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1430
1431         return num_sizes;
1432 }
1433
1434 /* only valid during minor collections */
1435 static int old_num_major_sections;
1436
1437 static void
1438 major_start_nursery_collection (void)
1439 {
1440         ms_wait_for_sweep_done ();
1441
1442 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1443         consistency_check ();
1444 #endif
1445
1446         old_num_major_sections = num_major_sections;
1447 }
1448
1449 static void
1450 major_finish_nursery_collection (void)
1451 {
1452 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1453         consistency_check ();
1454 #endif
1455         mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1456 }
1457
1458 static void
1459 major_start_major_collection (void)
1460 {
1461         int i;
1462
1463         ms_wait_for_sweep_done ();
1464
1465         /* clear the free lists */
1466         for (i = 0; i < num_block_obj_sizes; ++i) {
1467                 if (!evacuate_block_obj_sizes [i])
1468                         continue;
1469
1470                 free_block_lists [0][i] = NULL;
1471                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1472         }
1473 }
1474
1475 static void
1476 major_finish_major_collection (void)
1477 {
1478 }
1479
1480 static void
1481 major_have_computer_minor_collection_allowance (void)
1482 {
1483 #ifndef FIXED_HEAP
1484         int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1485
1486         g_assert (have_swept);
1487         ms_wait_for_sweep_done ();
1488         g_assert (!ms_sweep_in_progress);
1489
1490         /*
1491          * FIXME: We don't free blocks on 32 bit platforms because it
1492          * can lead to address space fragmentation, since we're
1493          * allocating blocks in larger contingents.
1494          */
1495         if (sizeof (mword) < 8)
1496                 return;
1497
1498         while (num_empty_blocks > section_reserve) {
1499                 void *next = *(void**)empty_blocks;
1500                 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1501                 empty_blocks = next;
1502                 /*
1503                  * Needs not be atomic because this is running
1504                  * single-threaded.
1505                  */
1506                 --num_empty_blocks;
1507
1508                 ++stat_major_blocks_freed;
1509         }
1510 #endif
1511 }
1512
1513 static void
1514 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1515 {
1516         MSBlockInfo *block;
1517
1518         FOREACH_BLOCK (block) {
1519                 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1520                                 &block->pin_queue_num_entries);
1521         } END_FOREACH_BLOCK;
1522 }
1523
1524 static void
1525 major_pin_objects (SgenGrayQueue *queue)
1526 {
1527         MSBlockInfo *block;
1528
1529         FOREACH_BLOCK (block) {
1530                 mark_pinned_objects_in_block (block, queue);
1531         } END_FOREACH_BLOCK;
1532 }
1533
1534 static void
1535 major_init_to_space (void)
1536 {
1537 }
1538
1539 static void
1540 major_report_pinned_memory_usage (void)
1541 {
1542         g_assert_not_reached ();
1543 }
1544
1545 static gint64
1546 major_get_used_size (void)
1547 {
1548         gint64 size = 0;
1549         MSBlockInfo *block;
1550
1551         FOREACH_BLOCK (block) {
1552                 int count = MS_BLOCK_FREE / block->obj_size;
1553                 void **iter;
1554                 size += count * block->obj_size;
1555                 for (iter = block->free_list; iter; iter = (void**)*iter)
1556                         size -= block->obj_size;
1557         } END_FOREACH_BLOCK;
1558
1559         return size;
1560 }
1561
1562 static int
1563 get_num_major_sections (void)
1564 {
1565         return num_major_sections;
1566 }
1567
1568 static gboolean
1569 major_handle_gc_param (const char *opt)
1570 {
1571 #ifdef FIXED_HEAP
1572         if (g_str_has_prefix (opt, "major-heap-size=")) {
1573                 const char *arg = strchr (opt, '=') + 1;
1574                 glong size;
1575                 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1576                         return FALSE;
1577                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1578                 g_assert (ms_heap_num_blocks > 0);
1579                 return TRUE;
1580         } else
1581 #endif
1582         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1583                 const char *arg = strchr (opt, '=') + 1;
1584                 int percentage = atoi (arg);
1585                 if (percentage < 0 || percentage > 100) {
1586                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1587                         exit (1);
1588                 }
1589                 evacuation_threshold = (float)percentage / 100.0;
1590                 return TRUE;
1591         } else if (!strcmp (opt, "concurrent-sweep")) {
1592                 concurrent_sweep = TRUE;
1593                 return TRUE;
1594         } else if (!strcmp (opt, "no-concurrent-sweep")) {
1595                 concurrent_sweep = FALSE;
1596                 return TRUE;
1597         }
1598
1599         return FALSE;
1600 }
1601
1602 static void
1603 major_print_gc_param_usage (void)
1604 {
1605         fprintf (stderr,
1606                         ""
1607 #ifdef FIXED_HEAP
1608                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1609 #endif
1610                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1611                         "  (no-)concurrent-sweep\n"
1612                         );
1613 }
1614
1615 #ifdef SGEN_HAVE_CARDTABLE
1616 static void
1617 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1618 {
1619         MSBlockInfo *block;
1620
1621         FOREACH_BLOCK (block) {
1622                 if (block->has_references)
1623                         callback ((mword)block->block, MS_BLOCK_SIZE);
1624         } END_FOREACH_BLOCK;
1625 }
1626
1627 #ifdef HEAVY_STATISTICS
1628 extern long long marked_cards;
1629 extern long long scanned_cards;
1630 extern long long scanned_objects;
1631
1632 #endif
1633
1634 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1635 /*
1636  * MS blocks are 16K aligned.
1637  * Cardtables are 4K aligned, at least.
1638  * This means that the cardtable of a given block is 32 bytes aligned.
1639  */
1640 static guint8*
1641 initial_skip_card (guint8 *card_data)
1642 {
1643         mword *cards = (mword*)card_data;
1644         mword card;
1645         int i;
1646         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1647                 card = cards [i];
1648                 if (card)
1649                         break;
1650         }
1651
1652         if (i == CARD_WORDS_PER_BLOCK)
1653                 return card_data + CARDS_PER_BLOCK;
1654
1655 #if defined(__i386__) && defined(__GNUC__)
1656         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
1657 #elif defined(__x86_64__) && defined(__GNUC__)
1658         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
1659 #else
1660         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1661                 if (card_data [i])
1662                         return &card_data [i];
1663         }
1664         return card_data;
1665 #endif
1666 }
1667
1668
1669 static G_GNUC_UNUSED guint8*
1670 skip_card (guint8 *card_data, guint8 *card_data_end)
1671 {
1672         while (card_data < card_data_end && !*card_data)
1673                 ++card_data;
1674         return card_data;
1675 }
1676
1677 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1678 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
1679 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1680
1681 static void
1682 major_scan_card_table (SgenGrayQueue *queue)
1683 {
1684         MSBlockInfo *block;
1685
1686         FOREACH_BLOCK (block) {
1687                 int block_obj_size;
1688                 char *block_start;
1689
1690                 if (!block->has_references)
1691                         continue;
1692
1693                 block_obj_size = block->obj_size;
1694                 block_start = block->block;
1695
1696                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1697                         guint8 *cards;
1698 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1699                         guint8 cards_data [CARDS_PER_BLOCK];
1700 #endif
1701                         char *obj, *end, *base;
1702
1703                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
1704 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1705                         cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1706 #else
1707                         cards = cards_data;
1708                         if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1709                                 continue;
1710 #endif
1711
1712                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1713                         end = block_start + MS_BLOCK_SIZE;
1714                         base = sgen_card_table_align_pointer (obj);
1715
1716                         while (obj < end) {
1717                                 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1718                                         int card_offset = (obj - base) >> CARD_BITS;
1719                                         sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1720                                 }
1721                                 obj += block_obj_size;
1722                         }
1723                 } else {
1724                         guint8 *card_data, *card_base;
1725                         guint8 *card_data_end;
1726
1727                         /*
1728                          * This is safe in face of card aliasing for the following reason:
1729                          *
1730                          * Major blocks are 16k aligned, or 32 cards aligned.
1731                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1732                          * sizes, they won't overflow the cardtable overlap modulus.
1733                          */
1734                         card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1735                         card_data_end = card_data + CARDS_PER_BLOCK;
1736
1737                         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)) {
1738                                 int index;
1739                                 int idx = card_data - card_base;
1740                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1741                                 char *end = start + CARD_SIZE_IN_BYTES;
1742                                 char *obj;
1743
1744                                 HEAVY_STAT (++scanned_cards);
1745
1746                                 if (!*card_data)
1747                                         continue;
1748
1749                                 HEAVY_STAT (++marked_cards);
1750
1751                                 sgen_card_table_prepare_card_for_scanning (card_data);
1752
1753                                 if (idx == 0)
1754                                         index = 0;
1755                                 else
1756                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1757
1758                                 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1759                                 while (obj < end) {
1760                                         if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1761                                                 HEAVY_STAT (++scanned_objects);
1762                                                 minor_scan_object (obj, queue);
1763                                         }
1764                                         obj += block_obj_size;
1765                                 }
1766                         }
1767                 }
1768         } END_FOREACH_BLOCK;
1769 }
1770 #endif
1771
1772 static gboolean
1773 major_is_worker_thread (pthread_t thread)
1774 {
1775         if (concurrent_sweep)
1776                 return thread == ms_sweep_thread;
1777         else
1778                 return FALSE;
1779 }
1780
1781 #undef pthread_create
1782
1783 static void
1784 post_param_init (void)
1785 {
1786         if (concurrent_sweep) {
1787                 if (pthread_create (&ms_sweep_thread, NULL, ms_sweep_thread_func, NULL)) {
1788                         fprintf (stderr, "Error: Could not create sweep thread.\n");
1789                         exit (1);
1790                 }
1791         }
1792 }
1793
1794 void
1795 #ifdef SGEN_PARALLEL_MARK
1796 #ifdef FIXED_HEAP
1797 mono_sgen_marksweep_fixed_par_init
1798 #else
1799 mono_sgen_marksweep_par_init
1800 #endif
1801 #else
1802 #ifdef FIXED_HEAP
1803 mono_sgen_marksweep_fixed_init
1804 #else
1805 mono_sgen_marksweep_init
1806 #endif
1807 #endif
1808         (SgenMajorCollector *collector)
1809 {
1810         int i;
1811
1812 #ifndef FIXED_HEAP
1813         mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1814 #endif
1815
1816         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1817         block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1818         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1819
1820         evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1821         for (i = 0; i < num_block_obj_sizes; ++i)
1822                 evacuate_block_obj_sizes [i] = FALSE;
1823
1824         /*
1825         {
1826                 int i;
1827                 g_print ("block object sizes:\n");
1828                 for (i = 0; i < num_block_obj_sizes; ++i)
1829                         g_print ("%d\n", block_obj_sizes [i]);
1830         }
1831         */
1832
1833         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1834                 free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1835
1836         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1837                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1838         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1839                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1840
1841         LOCK_INIT (ms_block_list_mutex);
1842
1843         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
1844         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
1845         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
1846         mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_time_wait_for_sweep);
1847 #ifdef SGEN_PARALLEL_MARK
1848         mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
1849 #endif
1850
1851         /*
1852          * FIXME: These are superfluous if concurrent sweep is
1853          * disabled.  We might want to create them lazily.
1854          */
1855         MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
1856         MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
1857
1858         collector->section_size = MAJOR_SECTION_SIZE;
1859 #ifdef SGEN_PARALLEL_MARK
1860         collector->is_parallel = TRUE;
1861 #else
1862         collector->is_parallel = FALSE;
1863 #endif
1864         collector->supports_cardtable = TRUE;
1865
1866         collector->have_swept = &have_swept;
1867
1868         collector->alloc_heap = major_alloc_heap;
1869         collector->is_object_live = major_is_object_live;
1870         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1871         collector->alloc_degraded = major_alloc_degraded;
1872         collector->copy_or_mark_object = major_copy_or_mark_object;
1873         collector->alloc_object = major_alloc_object;
1874         collector->free_pinned_object = free_pinned_object;
1875         collector->iterate_objects = major_iterate_objects;
1876         collector->free_non_pinned_object = major_free_non_pinned_object;
1877         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1878         collector->pin_objects = major_pin_objects;
1879 #ifdef SGEN_HAVE_CARDTABLE
1880         collector->scan_card_table = major_scan_card_table;
1881         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1882 #endif
1883         collector->init_to_space = major_init_to_space;
1884         collector->sweep = major_sweep;
1885         collector->check_scan_starts = major_check_scan_starts;
1886         collector->dump_heap = major_dump_heap;
1887         collector->get_used_size = major_get_used_size;
1888         collector->start_nursery_collection = major_start_nursery_collection;
1889         collector->finish_nursery_collection = major_finish_nursery_collection;
1890         collector->start_major_collection = major_start_major_collection;
1891         collector->finish_major_collection = major_finish_major_collection;
1892         collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
1893         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1894         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1895         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1896         collector->get_num_major_sections = get_num_major_sections;
1897         collector->handle_gc_param = major_handle_gc_param;
1898         collector->print_gc_param_usage = major_print_gc_param_usage;
1899         collector->is_worker_thread = major_is_worker_thread;
1900         collector->post_param_init = post_param_init;
1901
1902         FILL_COLLECTOR_COPY_OBJECT (collector);
1903         FILL_COLLECTOR_SCAN_OBJECT (collector);
1904
1905 #ifdef SGEN_HAVE_CARDTABLE
1906         /*cardtable requires major pages to be 8 cards aligned*/
1907         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
1908 #endif
1909 }
1910
1911 #endif