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