Merge pull request #475 from pruiz/xamarin-bug-7408
[mono.git] / mono / metadata / sgen-cardtable.h
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef __MONO_SGEN_CARD_TABLE_INLINES_H__
25 #define __MONO_SGEN_CARD_TABLE_INLINES_H__
26
27 #define SGEN_HAVE_CARDTABLE     1
28
29 #ifdef SGEN_HAVE_CARDTABLE
30
31 void sgen_card_table_reset_region (mword start, mword end) MONO_INTERNAL;
32 void* sgen_card_table_align_pointer (void *ptr) MONO_INTERNAL;
33 void sgen_card_table_mark_range (mword address, mword size) MONO_INTERNAL;
34 void sgen_cardtable_scan_object (char *obj, mword obj_size, guint8 *cards,
35                 gboolean mod_union, SgenGrayQueue *queue) MONO_INTERNAL;
36
37 gboolean sgen_card_table_get_card_data (guint8 *dest, mword address, mword cards) MONO_INTERNAL;
38
39 void sgen_card_table_init (SgenRemeberedSet *remset) MONO_INTERNAL;
40
41 /*How many bytes a single card covers*/
42 #define CARD_BITS 9
43
44 /* How many bits of the address space is covered by the card table.
45  * If this value is smaller than the number of address bits, card aliasing is required.
46  */
47 #define CARD_TABLE_BITS 32
48
49 #define CARD_SIZE_IN_BYTES (1 << CARD_BITS)
50 #define CARD_COUNT_BITS (CARD_TABLE_BITS - CARD_BITS)
51 #define CARD_COUNT_IN_BYTES (1 << CARD_COUNT_BITS)
52 #define CARD_MASK ((1 << CARD_COUNT_BITS) - 1)
53
54 #if SIZEOF_VOID_P * 8 > CARD_TABLE_BITS
55 #define SGEN_HAVE_OVERLAPPING_CARDS     1
56 #endif
57
58 extern guint8 *sgen_cardtable MONO_INTERNAL;
59
60
61 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
62
63 static inline guint8*
64 sgen_card_table_get_card_address (mword address)
65 {
66         return sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK);
67 }
68
69 extern guint8 *sgen_shadow_cardtable MONO_INTERNAL;
70
71 static inline guint8*
72 sgen_card_table_get_shadow_card_address (mword address)
73 {
74         return sgen_shadow_cardtable + ((address >> CARD_BITS) & CARD_MASK);
75 }
76
77 static inline gboolean
78 sgen_card_table_card_begin_scanning (mword address)
79 {
80         return *sgen_card_table_get_shadow_card_address (address) != 0;
81 }
82
83 static inline void
84 sgen_card_table_prepare_card_for_scanning (guint8 *card)
85 {
86 }
87
88 #define sgen_card_table_get_card_scan_address sgen_card_table_get_shadow_card_address
89
90 #else
91
92 static inline guint8*
93 sgen_card_table_get_card_address (mword address)
94 {
95         return sgen_cardtable + (address >> CARD_BITS);
96 }
97
98 static inline gboolean
99 sgen_card_table_card_begin_scanning (mword address)
100 {
101         guint8 *card = sgen_card_table_get_card_address (address);
102         gboolean res = *card;
103         *card = 0;
104         return res;
105 }
106
107 static inline void
108 sgen_card_table_prepare_card_for_scanning (guint8 *card)
109 {
110         *card = 0;
111 }
112
113 #define sgen_card_table_get_card_scan_address sgen_card_table_get_card_address
114
115 #endif
116
117 static inline gboolean
118 sgen_card_table_address_is_marked (mword address)
119 {
120         return *sgen_card_table_get_card_address (address) != 0;
121 }
122
123 static inline void
124 sgen_card_table_mark_address (mword address)
125 {
126         *sgen_card_table_get_card_address (address) = 1;
127 }
128
129 #else /*if SGEN_HAVE_CARDTABLE */
130
131 static inline void
132 sgen_card_table_mark_address (mword address)
133 {
134         g_assert_not_reached ();
135 }
136
137 static inline void
138 sgen_card_table_mark_range (mword address, mword size)
139 {
140         g_assert_not_reached ();
141 }
142
143 #define sgen_card_table_address_is_marked(p)    FALSE
144 #define sgen_card_table_init(p)
145
146 guint8*
147 mono_gc_get_card_table (int *shift_bits, gpointer *mask)
148 {
149         return NULL;
150 }
151
152 #endif
153
154 #endif