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