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