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