Improve a safety check when writing data into StatBuffer
[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                 gboolean mod_union, 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
44 guint8* sgen_get_card_table_configuration (int *shift_bits, gpointer *mask);
45
46 void sgen_card_table_init (SgenRememberedSet *remset);
47
48 /*How many bytes a single card covers*/
49 #define CARD_BITS 9
50
51 /* How many bits of the address space is covered by the card table.
52  * If this value is smaller than the number of address bits, card aliasing is required.
53  */
54 #define CARD_TABLE_BITS 32
55
56 #define CARD_SIZE_IN_BYTES (1 << CARD_BITS)
57 #define CARD_COUNT_BITS (CARD_TABLE_BITS - CARD_BITS)
58 #define CARD_COUNT_IN_BYTES (1 << CARD_COUNT_BITS)
59 #define CARD_MASK ((1 << CARD_COUNT_BITS) - 1)
60
61 #if SIZEOF_VOID_P * 8 > CARD_TABLE_BITS
62 #define SGEN_HAVE_OVERLAPPING_CARDS     1
63 #endif
64
65 extern guint8 *sgen_cardtable;
66
67
68 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
69
70 static inline guint8*
71 sgen_card_table_get_card_address (mword address)
72 {
73         return sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK);
74 }
75
76 extern guint8 *sgen_shadow_cardtable;
77
78 #define SGEN_SHADOW_CARDTABLE_END (sgen_shadow_cardtable + CARD_COUNT_IN_BYTES)
79
80 static inline guint8*
81 sgen_card_table_get_shadow_card_address (mword address)
82 {
83         return sgen_shadow_cardtable + ((address >> CARD_BITS) & CARD_MASK);
84 }
85
86 static inline gboolean
87 sgen_card_table_card_begin_scanning (mword address)
88 {
89         return *sgen_card_table_get_shadow_card_address (address) != 0;
90 }
91
92 static inline void
93 sgen_card_table_prepare_card_for_scanning (guint8 *card)
94 {
95 }
96
97 #define sgen_card_table_get_card_scan_address sgen_card_table_get_shadow_card_address
98
99 #else
100
101 static inline guint8*
102 sgen_card_table_get_card_address (mword address)
103 {
104         return sgen_cardtable + (address >> CARD_BITS);
105 }
106
107 static inline gboolean
108 sgen_card_table_card_begin_scanning (mword address)
109 {
110         guint8 *card = sgen_card_table_get_card_address (address);
111         gboolean res = *card;
112         *card = 0;
113         return res;
114 }
115
116 static inline void
117 sgen_card_table_prepare_card_for_scanning (guint8 *card)
118 {
119         *card = 0;
120 }
121
122 #define sgen_card_table_get_card_scan_address sgen_card_table_get_card_address
123
124 #endif
125
126 static inline gboolean
127 sgen_card_table_address_is_marked (mword address)
128 {
129         return *sgen_card_table_get_card_address (address) != 0;
130 }
131
132 static inline void
133 sgen_card_table_mark_address (mword address)
134 {
135         *sgen_card_table_get_card_address (address) = 1;
136 }
137
138 static inline size_t
139 sgen_card_table_get_card_offset (char *ptr, char *base)
140 {
141         return (ptr - base) >> CARD_BITS;
142 }
143
144 #endif