[sgen] Don't reallocate mod_union at each major
[mono.git] / mono / sgen / sgen-los.c
1 /*
2  * sgen-los.c: Large objects space.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
8  *
9  * Thread start/stop adapted from Boehm's GC:
10  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
11  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
12  * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
13  * Copyright (c) 2000-2004 by Hewlett-Packard Company.  All rights reserved.
14  * Copyright 2001-2003 Ximian, Inc
15  * Copyright 2003-2010 Novell, Inc.
16  * Copyright (C) 2012 Xamarin Inc
17  *
18  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_SGEN_GC
24
25 #include <string.h>
26
27 #include "mono/sgen/sgen-gc.h"
28 #include "mono/sgen/sgen-protocol.h"
29 #include "mono/sgen/sgen-cardtable.h"
30 #include "mono/sgen/sgen-memory-governor.h"
31 #include "mono/sgen/sgen-client.h"
32
33 #define LOS_SECTION_SIZE        (1024 * 1024)
34
35 /*
36  * This shouldn't be much smaller or larger than MAX_SMALL_OBJ_SIZE.
37  * Must be at least sizeof (LOSSection).
38  */
39 #define LOS_CHUNK_SIZE          4096
40 #define LOS_CHUNK_BITS          12
41
42 /* Largest object that can be allocated in a section. */
43 #define LOS_SECTION_OBJECT_LIMIT        (LOS_SECTION_SIZE - LOS_CHUNK_SIZE - sizeof (LOSObject))
44 //#define LOS_SECTION_OBJECT_LIMIT      0
45 #define LOS_SECTION_NUM_CHUNKS          ((LOS_SECTION_SIZE >> LOS_CHUNK_BITS) - 1)
46
47 #define LOS_SECTION_FOR_OBJ(obj)        ((LOSSection*)((mword)(obj) & ~(mword)(LOS_SECTION_SIZE - 1)))
48 #define LOS_CHUNK_INDEX(obj,section)    (((char*)(obj) - (char*)(section)) >> LOS_CHUNK_BITS)
49
50 #define LOS_NUM_FAST_SIZES              32
51
52 typedef struct _LOSFreeChunks LOSFreeChunks;
53 struct _LOSFreeChunks {
54         LOSFreeChunks *next_size;
55         size_t size;
56 };
57
58 typedef struct _LOSSection LOSSection;
59 struct _LOSSection {
60         LOSSection *next;
61         size_t num_free_chunks;
62         unsigned char *free_chunk_map;
63 };
64
65 /* We allow read only access on the list while sweep is not running */
66 LOSObject *los_object_list = NULL;
67 mword los_memory_usage = 0;
68
69 static LOSSection *los_sections = NULL;
70 static LOSFreeChunks *los_fast_free_lists [LOS_NUM_FAST_SIZES]; /* 0 is for larger sizes */
71 static mword los_num_objects = 0;
72 static int los_num_sections = 0;
73
74 //#define USE_MALLOC
75 //#define LOS_CONSISTENCY_CHECK
76 //#define LOS_DUMMY
77
78 #ifdef LOS_DUMMY
79 #define LOS_SEGMENT_SIZE        (4096 * 1024)
80
81 static char *los_segment = NULL;
82 static int los_segment_index = 0;
83 #endif
84
85 mword
86 sgen_los_object_size (LOSObject *obj)
87 {
88         return obj->size & ~1L;
89 }
90
91 #ifdef LOS_CONSISTENCY_CHECK
92 static void
93 los_consistency_check (void)
94 {
95         LOSSection *section;
96         LOSObject *obj;
97         int i;
98         mword memory_usage = 0;
99
100         for (obj = los_object_list; obj; obj = obj->next) {
101                 mword obj_size = sgen_los_object_size (obj);
102                 char *end = obj->data + obj_size;
103                 int start_index, num_chunks;
104
105                 memory_usage += obj_size;
106
107                 if (obj_size > LOS_SECTION_OBJECT_LIMIT)
108                         continue;
109
110                 section = LOS_SECTION_FOR_OBJ (obj);
111
112                 g_assert (end <= (char*)section + LOS_SECTION_SIZE);
113
114                 start_index = LOS_CHUNK_INDEX (obj, section);
115                 num_chunks = (obj_size + sizeof (LOSObject) + LOS_CHUNK_SIZE - 1) >> LOS_CHUNK_BITS;
116                 for (i = start_index; i < start_index + num_chunks; ++i)
117                         g_assert (!section->free_chunk_map [i]);
118         }
119
120         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
121                 LOSFreeChunks *size_chunks;
122                 for (size_chunks = los_fast_free_lists [i]; size_chunks; size_chunks = size_chunks->next_size) {
123                         LOSSection *section = LOS_SECTION_FOR_OBJ (size_chunks);
124                         int j, num_chunks, start_index;
125
126                         if (i == 0)
127                                 g_assert (size_chunks->size >= LOS_NUM_FAST_SIZES * LOS_CHUNK_SIZE);
128                         else
129                                 g_assert (size_chunks->size == i * LOS_CHUNK_SIZE);
130
131                         num_chunks = size_chunks->size >> LOS_CHUNK_BITS;
132                         start_index = LOS_CHUNK_INDEX (size_chunks, section);
133                         for (j = start_index; j < start_index + num_chunks; ++j)
134                                 g_assert (section->free_chunk_map [j]);
135                 }
136         }
137
138         g_assert (los_memory_usage == memory_usage);
139 }
140 #endif
141
142 static void
143 add_free_chunk (LOSFreeChunks *free_chunks, size_t size)
144 {
145         size_t num_chunks = size >> LOS_CHUNK_BITS;
146
147         free_chunks->size = size;
148
149         if (num_chunks >= LOS_NUM_FAST_SIZES)
150                 num_chunks = 0;
151         free_chunks->next_size = los_fast_free_lists [num_chunks];
152         los_fast_free_lists [num_chunks] = free_chunks;
153 }
154
155 static LOSFreeChunks*
156 get_from_size_list (LOSFreeChunks **list, size_t size)
157 {
158         LOSFreeChunks *free_chunks = NULL;
159         LOSSection *section;
160         size_t i, num_chunks, start_index;
161
162
163         g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
164
165         while (*list) {
166                 free_chunks = *list;
167                 if (free_chunks->size >= size)
168                         break;
169                 list = &(*list)->next_size;
170         }
171
172         if (!*list)
173                 return NULL;
174
175         *list = free_chunks->next_size;
176
177         if (free_chunks->size > size)
178                 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
179
180         num_chunks = size >> LOS_CHUNK_BITS;
181
182         section = LOS_SECTION_FOR_OBJ (free_chunks);
183
184         start_index = LOS_CHUNK_INDEX (free_chunks, section);
185         for (i = start_index; i < start_index + num_chunks; ++i) {
186                 g_assert (section->free_chunk_map [i]);
187                 section->free_chunk_map [i] = 0;
188         }
189
190         section->num_free_chunks -= size >> LOS_CHUNK_BITS;
191         g_assert (section->num_free_chunks >= 0);
192
193         return free_chunks;
194 }
195
196 static LOSObject*
197 randomize_los_object_start (gpointer addr, size_t obj_size, size_t alloced_size, size_t addr_alignment)
198 {
199         size_t offset = 0;
200         if (alloced_size != obj_size) {
201                 /*
202                  * We want to get a random offset between 0 and (alloced_size - obj_size)
203                  * We do a prime multiplication to avoid usage of functions which might not
204                  * be thread/signal safe (like rand ()). We subtract 1 to avoid common
205                  * power by 2 factors.
206                  */
207                 offset = SGEN_ALIGN_DOWN ((((size_t)addr - 1) * 2654435761u) % (alloced_size - obj_size));
208         }
209         SGEN_ASSERT (0, (alloced_size - obj_size) < addr_alignment, "Why are we wasting one entire chunk for a los object ?");
210         /* Randomize the location within the reserved chunks to improve cache performance */
211         return (LOSObject*)((guint8*)addr + offset);
212
213 }
214
215 static LOSObject*
216 get_los_section_memory (size_t size)
217 {
218         LOSSection *section;
219         LOSFreeChunks *free_chunks;
220         size_t num_chunks;
221         size_t obj_size = size;
222
223         size = SGEN_ALIGN_UP_TO (size, LOS_CHUNK_SIZE);
224
225         num_chunks = size >> LOS_CHUNK_BITS;
226
227         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
228         g_assert (num_chunks > 0);
229
230  retry:
231         if (num_chunks >= LOS_NUM_FAST_SIZES) {
232                 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
233         } else {
234                 size_t i;
235                 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
236                         free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
237                         if (free_chunks)
238                                 break;
239                 }
240                 if (!free_chunks)
241                         free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
242         }
243
244         if (free_chunks) {
245                 return randomize_los_object_start (free_chunks, obj_size, size, LOS_CHUNK_SIZE);
246         }
247
248         if (!sgen_memgov_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
249                 return NULL;
250
251         section = (LOSSection *)sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, (SgenAllocFlags)(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE), NULL);
252
253         if (!section)
254                 return NULL;
255
256         free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
257         free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
258         free_chunks->next_size = los_fast_free_lists [0];
259         los_fast_free_lists [0] = free_chunks;
260
261         section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
262
263         section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
264         g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
265         section->free_chunk_map [0] = 0;
266         memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
267
268         section->next = los_sections;
269         los_sections = section;
270
271         ++los_num_sections;
272
273         goto retry;
274 }
275
276 static void
277 free_los_section_memory (LOSObject *obj, size_t size)
278 {
279         LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
280         size_t num_chunks, i, start_index;
281
282         size = SGEN_ALIGN_UP_TO (size, LOS_CHUNK_SIZE);
283
284         num_chunks = size >> LOS_CHUNK_BITS;
285
286         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
287         g_assert (num_chunks > 0);
288
289         section->num_free_chunks += num_chunks;
290         g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
291
292         /*
293          * We could free the LOS section here if it's empty, but we
294          * can't unless we also remove its free chunks from the fast
295          * free lists.  Instead, we do it in los_sweep().
296          */
297
298         start_index = LOS_CHUNK_INDEX (obj, section);
299         for (i = start_index; i < start_index + num_chunks; ++i) {
300                 g_assert (!section->free_chunk_map [i]);
301                 section->free_chunk_map [i] = 1;
302         }
303
304         add_free_chunk ((LOSFreeChunks*)SGEN_ALIGN_DOWN_TO ((mword)obj, LOS_CHUNK_SIZE), size);
305 }
306
307 void
308 sgen_los_free_object (LOSObject *obj)
309 {
310         if (obj->cardtable_mod_union)
311                 sgen_card_table_free_mod_union (obj->cardtable_mod_union, (char*)obj->data, sgen_los_object_size (obj));
312
313 #ifndef LOS_DUMMY
314         mword size = sgen_los_object_size (obj);
315         SGEN_LOG (4, "Freed large object %p, size %lu", obj->data, (unsigned long)size);
316         binary_protocol_empty (obj->data, size);
317
318         los_memory_usage -= size;
319         los_num_objects--;
320
321 #ifdef USE_MALLOC
322         free (obj);
323 #else
324         if (size > LOS_SECTION_OBJECT_LIMIT) {
325                 int pagesize = mono_pagesize ();
326                 size += sizeof (LOSObject);
327                 size = SGEN_ALIGN_UP_TO (size, pagesize);
328                 sgen_free_os_memory ((gpointer)SGEN_ALIGN_DOWN_TO ((mword)obj, pagesize), size, SGEN_ALLOC_HEAP);
329                 sgen_memgov_release_space (size, SPACE_LOS);
330         } else {
331                 free_los_section_memory (obj, size + sizeof (LOSObject));
332 #ifdef LOS_CONSISTENCY_CHECKS
333                 los_consistency_check ();
334 #endif
335         }
336 #endif
337 #endif
338 }
339
340 /*
341  * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
342  * They are currently kept track of with a linked list.
343  * They don't move, so there is no need to pin them during collection
344  * and we avoid the memcpy overhead.
345  */
346 void*
347 sgen_los_alloc_large_inner (GCVTable vtable, size_t size)
348 {
349         LOSObject *obj = NULL;
350         void **vtslot;
351
352         g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
353         g_assert ((size & 1) == 0);
354
355         /*
356          * size + sizeof (LOSObject) <= SSIZE_MAX - (mono_pagesize () - 1)
357          *
358          * therefore:
359          *
360          * size <= SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject)
361          */
362         if (size > SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject))
363                 return NULL;
364
365 #ifdef LOS_DUMMY
366         if (!los_segment)
367                 los_segment = sgen_alloc_os_memory (LOS_SEGMENT_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
368         los_segment_index = ALIGN_UP (los_segment_index);
369
370         obj = (LOSObject*)(los_segment + los_segment_index);
371         los_segment_index += size + sizeof (LOSObject);
372         g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
373 #else
374         sgen_ensure_free_space (size, GENERATION_OLD);
375
376 #ifdef USE_MALLOC
377         obj = malloc (size + sizeof (LOSObject));
378         memset (obj, 0, size + sizeof (LOSObject));
379 #else
380         if (size > LOS_SECTION_OBJECT_LIMIT) {
381                 size_t obj_size = size + sizeof (LOSObject);
382                 int pagesize = mono_pagesize ();
383                 size_t alloc_size = SGEN_ALIGN_UP_TO (obj_size, pagesize);
384                 if (sgen_memgov_try_alloc_space (alloc_size, SPACE_LOS)) {
385                         obj = (LOSObject *)sgen_alloc_os_memory (alloc_size, (SgenAllocFlags)(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE), NULL);
386                         if (obj)
387                                 obj = randomize_los_object_start (obj, obj_size, alloc_size, pagesize);
388                 }
389         } else {
390                 obj = get_los_section_memory (size + sizeof (LOSObject));
391                 if (obj)
392                         memset (obj, 0, size + sizeof (LOSObject));
393         }
394 #endif
395 #endif
396         if (!obj)
397                 return NULL;
398         g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
399         obj->size = size;
400         vtslot = (void**)obj->data;
401         *vtslot = vtable;
402         sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
403         obj->next = los_object_list;
404         /*
405          * We need a memory barrier so we don't expose as head of the los object list
406          * a LOSObject that doesn't have its fields initialized.
407          */
408         mono_memory_write_barrier ();
409         los_object_list = obj;
410         los_memory_usage += size;
411         los_num_objects++;
412         SGEN_LOG (4, "Allocated large object %p, vtable: %p (%s), size: %zd", obj->data, vtable, sgen_client_vtable_get_name (vtable), size);
413         binary_protocol_alloc (obj->data, vtable, size, sgen_client_get_provenance ());
414
415 #ifdef LOS_CONSISTENCY_CHECK
416         los_consistency_check ();
417 #endif
418
419         return obj->data;
420 }
421
422 static void sgen_los_unpin_object (GCObject *data);
423
424 void
425 sgen_los_sweep (void)
426 {
427         LOSObject *bigobj, *prevbo;
428         LOSSection *section, *prev;
429         int i;
430         int num_sections = 0;
431
432         /* sweep the big objects list */
433         prevbo = NULL;
434         for (bigobj = los_object_list; bigobj;) {
435                 SGEN_ASSERT (0, !SGEN_OBJECT_IS_PINNED (bigobj->data), "Who pinned a LOS object?");
436
437                 if (sgen_los_object_is_pinned (bigobj->data)) {
438                         if (bigobj->cardtable_mod_union) {
439                                 mword obj_size = sgen_los_object_size (bigobj);
440                                 mword num_cards = sgen_card_table_number_of_cards_in_range ((mword) bigobj->data, obj_size);
441                                 memset (bigobj->cardtable_mod_union, 0, num_cards);
442                         }
443
444                         sgen_los_unpin_object (bigobj->data);
445                         sgen_update_heap_boundaries ((mword)bigobj->data, (mword)bigobj->data + sgen_los_object_size (bigobj));
446                 } else {
447                         LOSObject *to_free;
448                         /* not referenced anywhere, so we can free it */
449                         if (prevbo)
450                                 prevbo->next = bigobj->next;
451                         else
452                                 los_object_list = bigobj->next;
453                         to_free = bigobj;
454                         bigobj = bigobj->next;
455                         sgen_los_free_object (to_free);
456                         continue;
457                 }
458                 prevbo = bigobj;
459                 bigobj = bigobj->next;
460         }
461
462         /* Try to free memory */
463         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
464                 los_fast_free_lists [i] = NULL;
465
466         prev = NULL;
467         section = los_sections;
468         while (section) {
469                 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
470                         LOSSection *next = section->next;
471                         if (prev)
472                                 prev->next = next;
473                         else
474                                 los_sections = next;
475                         sgen_free_os_memory (section, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP);
476                         sgen_memgov_release_space (LOS_SECTION_SIZE, SPACE_LOS);
477                         section = next;
478                         --los_num_sections;
479                         continue;
480                 }
481
482                 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
483                         if (section->free_chunk_map [i]) {
484                                 int j;
485                                 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
486                                         ;
487                                 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
488                                 i = j - 1;
489                         }
490                 }
491
492                 prev = section;
493                 section = section->next;
494
495                 ++num_sections;
496         }
497
498 #ifdef LOS_CONSISTENCY_CHECK
499         los_consistency_check ();
500 #endif
501
502         /*
503         g_print ("LOS sections: %d  objects: %d  usage: %d\n", num_sections, los_num_objects, los_memory_usage);
504         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
505                 int num_chunks = 0;
506                 LOSFreeChunks *free_chunks;
507                 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
508                         ++num_chunks;
509                 g_print ("  %d: %d\n", i, num_chunks);
510         }
511         */
512
513         g_assert (los_num_sections == num_sections);
514 }
515
516 gboolean
517 sgen_ptr_is_in_los (char *ptr, char **start)
518 {
519         LOSObject *obj;
520
521         *start = NULL;
522         for (obj = los_object_list; obj; obj = obj->next) {
523                 char *end = (char*)obj->data + sgen_los_object_size (obj);
524
525                 if (ptr >= (char*)obj->data && ptr < end) {
526                         *start = (char*)obj->data;
527                         return TRUE;
528                 }
529         }
530         return FALSE;
531 }
532
533 void
534 sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
535 {
536         LOSObject *obj;
537
538         for (obj = los_object_list; obj; obj = obj->next)
539                 cb (obj->data, sgen_los_object_size (obj), user_data);
540 }
541
542 gboolean
543 sgen_los_is_valid_object (char *object)
544 {
545         LOSObject *obj;
546
547         for (obj = los_object_list; obj; obj = obj->next) {
548                 if ((char*)obj->data == object)
549                         return TRUE;
550         }
551         return FALSE;
552 }
553
554 gboolean
555 mono_sgen_los_describe_pointer (char *ptr)
556 {
557         LOSObject *obj;
558
559         for (obj = los_object_list; obj; obj = obj->next) {
560                 const char *los_kind;
561                 mword size;
562                 gboolean pinned;
563
564                 if ((char*)obj->data > ptr || (char*)obj->data + sgen_los_object_size (obj) <= ptr)
565                         continue;
566
567                 size = sgen_los_object_size (obj);
568                 pinned = sgen_los_object_is_pinned (obj->data);
569
570                 if (size > LOS_SECTION_OBJECT_LIMIT)
571                         los_kind = "huge-los-ptr";
572                 else
573                         los_kind = "los-ptr";
574
575                 if ((char*)obj->data == ptr) {
576                         SGEN_LOG (0, "%s (size %d pin %d)\n", los_kind, (int)size, pinned ? 1 : 0);
577                 } else {
578                         SGEN_LOG (0, "%s (interior-ptr offset %zd size %d pin %d)",
579                                         los_kind, ptr - (char*)obj->data, (int)size, pinned ? 1 : 0);
580                 }
581
582                 return TRUE;
583         }
584         return FALSE;
585 }
586
587 void
588 sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
589 {
590         LOSObject *obj;
591         for (obj = los_object_list; obj; obj = obj->next) {
592                 GCVTable vt = SGEN_LOAD_VTABLE (obj->data);
593                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
594                         callback ((mword)obj->data, sgen_los_object_size (obj));
595         }
596 }
597
598 static guint8*
599 get_cardtable_mod_union_for_object (LOSObject *obj)
600 {
601         mword size = sgen_los_object_size (obj);
602         guint8 *mod_union = obj->cardtable_mod_union;
603         guint8 *other;
604         if (mod_union)
605                 return mod_union;
606         mod_union = sgen_card_table_alloc_mod_union ((char*)obj->data, size);
607         other = (guint8 *)SGEN_CAS_PTR ((gpointer*)&obj->cardtable_mod_union, mod_union, NULL);
608         if (!other) {
609                 SGEN_ASSERT (0, obj->cardtable_mod_union == mod_union, "Why did CAS not replace?");
610                 return mod_union;
611         }
612         sgen_card_table_free_mod_union (mod_union, (char*)obj->data, size);
613         return other;
614 }
615
616 void
617 sgen_los_scan_card_table (CardTableScanType scan_type, ScanCopyContext ctx)
618 {
619         LOSObject *obj;
620
621         binary_protocol_los_card_table_scan_start (sgen_timestamp (), scan_type & CARDTABLE_SCAN_MOD_UNION);
622         for (obj = los_object_list; obj; obj = obj->next) {
623                 mword num_cards = 0;
624                 guint8 *cards;
625
626                 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
627                         continue;
628
629                 if (scan_type & CARDTABLE_SCAN_MOD_UNION) {
630                         if (!sgen_los_object_is_pinned (obj->data))
631                                 continue;
632
633                         cards = get_cardtable_mod_union_for_object (obj);
634                         g_assert (cards);
635                         if (scan_type == CARDTABLE_SCAN_MOD_UNION_PRECLEAN) {
636                                 guint8 *cards_preclean;
637                                 mword obj_size = sgen_los_object_size (obj);
638                                 num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj->data, obj_size);
639                                 cards_preclean = (guint8 *)sgen_alloc_internal_dynamic (num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION, TRUE);
640
641                                 sgen_card_table_preclean_mod_union (cards, cards_preclean, num_cards);
642
643                                 cards = cards_preclean;
644                         }
645                 } else {
646                         cards = NULL;
647                 }
648
649                 sgen_cardtable_scan_object (obj->data, sgen_los_object_size (obj), cards, ctx);
650
651                 if (scan_type == CARDTABLE_SCAN_MOD_UNION_PRECLEAN)
652                         sgen_free_internal_dynamic (cards, num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION);
653         }
654         binary_protocol_los_card_table_scan_end (sgen_timestamp (), scan_type & CARDTABLE_SCAN_MOD_UNION);
655 }
656
657 void
658 sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards)
659 {
660         LOSObject *obj;
661         long long total_cards = 0;
662         long long marked_cards = 0;
663
664         for (obj = los_object_list; obj; obj = obj->next) {
665                 int i;
666                 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) obj->data);
667                 guint8 *cards_end = sgen_card_table_get_card_scan_address ((mword) obj->data + sgen_los_object_size (obj) - 1);
668                 mword num_cards = (cards_end - cards) + 1;
669
670                 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
671                         continue;
672
673                 total_cards += num_cards;
674                 for (i = 0; i < num_cards; ++i) {
675                         if (cards [i])
676                                 ++marked_cards;
677                 }
678         }
679
680         *num_total_cards = total_cards;
681         *num_marked_cards = marked_cards;
682 }
683
684 void
685 sgen_los_update_cardtable_mod_union (void)
686 {
687         LOSObject *obj;
688
689         for (obj = los_object_list; obj; obj = obj->next) {
690                 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
691                         continue;
692                 sgen_card_table_update_mod_union (get_cardtable_mod_union_for_object (obj),
693                                 (char*)obj->data, sgen_los_object_size (obj), NULL);
694         }
695 }
696
697 LOSObject*
698 sgen_los_header_for_object (GCObject *data)
699 {
700 #if _MSC_VER
701         return (LOSObject*)((char*)data - (int)(&(((LOSObject*)0)->data)));
702 #else
703         return (LOSObject*)((char*)data - sizeof (LOSObject));
704 #endif
705 }
706
707 void
708 sgen_los_pin_object (GCObject *data)
709 {
710         LOSObject *obj = sgen_los_header_for_object (data);
711         obj->size = obj->size | 1;
712         binary_protocol_pin (data, (gpointer)SGEN_LOAD_VTABLE (data), sgen_safe_object_get_size (data));
713 }
714
715 static void
716 sgen_los_unpin_object (GCObject *data)
717 {
718         LOSObject *obj = sgen_los_header_for_object (data);
719         obj->size = sgen_los_object_size (obj);
720 }
721
722 gboolean
723 sgen_los_object_is_pinned (GCObject *data)
724 {
725         LOSObject *obj = sgen_los_header_for_object (data);
726         return obj->size & 1;
727 }
728
729 void
730 sgen_los_mark_mod_union_card (GCObject *mono_obj, void **ptr)
731 {
732         LOSObject *obj = sgen_los_header_for_object (mono_obj);
733         guint8 *mod_union = get_cardtable_mod_union_for_object (obj);
734         /* The LOSObject structure is not represented within the card space */
735         size_t offset = sgen_card_table_get_card_offset ((char*)ptr, (char*)sgen_card_table_align_pointer((char*)mono_obj));
736         SGEN_ASSERT (0, mod_union, "FIXME: optionally allocate the mod union if it's not here and CAS it in.");
737         mod_union [offset] = 1;
738 }
739
740 #endif /* HAVE_SGEN_GC */