fde8ac11eff721cd7a961bc2d43a8d8f11ab9cd3
[mono.git] / mono / sgen / 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 /*WARNING: This function returns the number of cards regardless of overflow in case of overlapping cards.*/
28 mword sgen_card_table_number_of_cards_in_range (mword address, mword size);
29
30 void sgen_card_table_reset_region (mword start, mword end);
31 void* sgen_card_table_align_pointer (void *ptr);
32 void sgen_card_table_mark_range (mword address, mword size);
33 void sgen_cardtable_scan_object (GCObject *obj, mword obj_size, guint8 *cards,
34                 ScanCopyContext ctx);
35
36 gboolean sgen_card_table_get_card_data (guint8 *dest, mword address, mword cards);
37
38 guint8* sgen_card_table_alloc_mod_union (char *obj, mword obj_size);
39 void sgen_card_table_free_mod_union (guint8 *mod_union, char *obj, mword obj_size);
40
41 void sgen_card_table_update_mod_union_from_cards (guint8 *dest, guint8 *start_card, size_t num_cards);
42 void sgen_card_table_update_mod_union (guint8 *dest, char *obj, mword obj_size, size_t *out_num_cards);
43 void sgen_card_table_preclean_mod_union (guint8 *cards, guint8 *cards_preclean, size_t num_cards);
44
45 guint8* sgen_get_card_table_configuration (int *shift_bits, gpointer *mask);
46
47 void sgen_card_table_init (SgenRememberedSet *remset);
48
49 /*How many bytes a single card covers*/
50 #define CARD_BITS 9
51
52 /* How many bits of the address space is covered by the card table.
53  * If this value is smaller than the number of address bits, card aliasing is required.
54  */
55 #define CARD_TABLE_BITS 32
56
57 #define CARD_SIZE_IN_BYTES (1 << CARD_BITS)
58 #define CARD_COUNT_BITS (CARD_TABLE_BITS - CARD_BITS)
59 #define CARD_COUNT_IN_BYTES (1 << CARD_COUNT_BITS)
60 #define CARD_MASK ((1 << CARD_COUNT_BITS) - 1)
61
62 #if SIZEOF_VOID_P * 8 > CARD_TABLE_BITS
63 #define SGEN_HAVE_OVERLAPPING_CARDS     1
64 #endif
65
66 extern guint8 *sgen_cardtable;
67
68
69 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
70
71 static inline guint8*
72 sgen_card_table_get_card_address (mword address)
73 {
74         return sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK);
75 }
76
77 extern guint8 *sgen_shadow_cardtable;
78
79 #define SGEN_SHADOW_CARDTABLE_END (sgen_shadow_cardtable + CARD_COUNT_IN_BYTES)
80
81 static inline guint8*
82 sgen_card_table_get_shadow_card_address (mword address)
83 {
84         return sgen_shadow_cardtable + ((address >> CARD_BITS) & CARD_MASK);
85 }
86
87 static inline gboolean
88 sgen_card_table_card_begin_scanning (mword address)
89 {
90         return *sgen_card_table_get_shadow_card_address (address) != 0;
91 }
92
93 static inline void
94 sgen_card_table_prepare_card_for_scanning (guint8 *card)
95 {
96 }
97
98 #define sgen_card_table_get_card_scan_address sgen_card_table_get_shadow_card_address
99
100 #else
101
102 static inline guint8*
103 sgen_card_table_get_card_address (mword address)
104 {
105         return sgen_cardtable + (address >> CARD_BITS);
106 }
107
108 static inline gboolean
109 sgen_card_table_card_begin_scanning (mword address)
110 {
111         guint8 *card = sgen_card_table_get_card_address (address);
112         gboolean res = *card;
113         *card = 0;
114         return res;
115 }
116
117 static inline void
118 sgen_card_table_prepare_card_for_scanning (guint8 *card)
119 {
120         *card = 0;
121 }
122
123 #define sgen_card_table_get_card_scan_address sgen_card_table_get_card_address
124
125 #endif
126
127 static inline gboolean
128 sgen_card_table_address_is_marked (mword address)
129 {
130         return *sgen_card_table_get_card_address (address) != 0;
131 }
132
133 static inline void
134 sgen_card_table_mark_address (mword address)
135 {
136         *sgen_card_table_get_card_address (address) = 1;
137 }
138
139 static inline size_t
140 sgen_card_table_get_card_offset (char *ptr, char *base)
141 {
142         return (ptr - base) >> CARD_BITS;
143 }
144
145 #endif