author | xdono |
Tue, 28 Jul 2009 12:12:40 -0700 | |
changeset 3261 | c7d5aae8d3f7 |
parent 2998 | b501bd305780 |
child 5547 | f4b087cbb361 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
3261 | 2 |
* Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. |
1 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
2998
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
25 |
#ifdef ASSERT |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
26 |
inline void BitMap::verify_index(idx_t index) const { |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
27 |
assert(index < _size, "BitMap index out of bounds"); |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
28 |
} |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
29 |
|
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
30 |
inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const { |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
31 |
assert(beg_index <= end_index, "BitMap range error"); |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
32 |
// Note that [0,0) and [size,size) are both valid ranges. |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
33 |
if (end_index != _size) verify_index(end_index); |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
34 |
} |
b501bd305780
6849716: BitMap - performance regression introduced with G1
jcoomes
parents:
1374
diff
changeset
|
35 |
#endif // #ifdef ASSERT |
1374 | 36 |
|
37 |
inline void BitMap::set_bit(idx_t bit) { |
|
38 |
verify_index(bit); |
|
39 |
*word_addr(bit) |= bit_mask(bit); |
|
40 |
} |
|
41 |
||
42 |
inline void BitMap::clear_bit(idx_t bit) { |
|
43 |
verify_index(bit); |
|
44 |
*word_addr(bit) &= ~bit_mask(bit); |
|
45 |
} |
|
46 |
||
1 | 47 |
inline bool BitMap::par_set_bit(idx_t bit) { |
48 |
verify_index(bit); |
|
49 |
volatile idx_t* const addr = word_addr(bit); |
|
50 |
const idx_t mask = bit_mask(bit); |
|
51 |
idx_t old_val = *addr; |
|
52 |
||
53 |
do { |
|
54 |
const idx_t new_val = old_val | mask; |
|
55 |
if (new_val == old_val) { |
|
56 |
return false; // Someone else beat us to it. |
|
57 |
} |
|
58 |
const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val, |
|
59 |
(volatile void*) addr, |
|
60 |
(void*) old_val); |
|
61 |
if (cur_val == old_val) { |
|
62 |
return true; // Success. |
|
63 |
} |
|
64 |
old_val = cur_val; // The value changed, try again. |
|
65 |
} while (true); |
|
66 |
} |
|
67 |
||
68 |
inline bool BitMap::par_clear_bit(idx_t bit) { |
|
69 |
verify_index(bit); |
|
70 |
volatile idx_t* const addr = word_addr(bit); |
|
71 |
const idx_t mask = ~bit_mask(bit); |
|
72 |
idx_t old_val = *addr; |
|
73 |
||
74 |
do { |
|
75 |
const idx_t new_val = old_val & mask; |
|
76 |
if (new_val == old_val) { |
|
77 |
return false; // Someone else beat us to it. |
|
78 |
} |
|
79 |
const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val, |
|
80 |
(volatile void*) addr, |
|
81 |
(void*) old_val); |
|
82 |
if (cur_val == old_val) { |
|
83 |
return true; // Success. |
|
84 |
} |
|
85 |
old_val = cur_val; // The value changed, try again. |
|
86 |
} while (true); |
|
87 |
} |
|
88 |
||
1374 | 89 |
inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
90 |
if (hint == small_range && end - beg == 1) { |
|
91 |
set_bit(beg); |
|
92 |
} else { |
|
93 |
if (hint == large_range) { |
|
94 |
set_large_range(beg, end); |
|
95 |
} else { |
|
96 |
set_range(beg, end); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
102 |
if (hint == small_range && end - beg == 1) { |
|
103 |
clear_bit(beg); |
|
104 |
} else { |
|
105 |
if (hint == large_range) { |
|
106 |
clear_large_range(beg, end); |
|
107 |
} else { |
|
108 |
clear_range(beg, end); |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
114 |
if (hint == small_range && end - beg == 1) { |
|
115 |
par_at_put(beg, true); |
|
116 |
} else { |
|
117 |
if (hint == large_range) { |
|
118 |
par_at_put_large_range(beg, end, true); |
|
119 |
} else { |
|
120 |
par_at_put_range(beg, end, true); |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
1 | 124 |
|
1374 | 125 |
inline void BitMap::set_range_of_words(idx_t beg, idx_t end) { |
126 |
bm_word_t* map = _map; |
|
127 |
for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) { |
|
132 |
bm_word_t* map = _map; |
|
133 |
for (idx_t i = beg; i < end; ++i) map[i] = 0; |
|
134 |
} |
|
135 |
||
136 |
||
137 |
inline void BitMap::clear() { |
|
138 |
clear_range_of_words(0, size_in_words()); |
|
139 |
} |
|
140 |
||
1 | 141 |
|
1374 | 142 |
inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
143 |
if (hint == small_range && end - beg == 1) { |
|
144 |
par_at_put(beg, false); |
|
145 |
} else { |
|
146 |
if (hint == large_range) { |
|
147 |
par_at_put_large_range(beg, end, false); |
|
148 |
} else { |
|
149 |
par_at_put_range(beg, end, false); |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
inline BitMap::idx_t |
|
155 |
BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const { |
|
156 |
assert(l_offset <= size(), "BitMap index out of bounds"); |
|
157 |
assert(r_offset <= size(), "BitMap index out of bounds"); |
|
158 |
assert(l_offset <= r_offset, "l_offset > r_offset ?"); |
|
159 |
||
160 |
if (l_offset == r_offset) { |
|
161 |
return l_offset; |
|
162 |
} |
|
163 |
idx_t index = word_index(l_offset); |
|
164 |
idx_t r_index = word_index(r_offset-1) + 1; |
|
165 |
idx_t res_offset = l_offset; |
|
1 | 166 |
|
167 |
// check bits including and to the _left_ of offset's position |
|
1374 | 168 |
idx_t pos = bit_in_word(res_offset); |
169 |
idx_t res = map(index) >> pos; |
|
170 |
if (res != (uintptr_t)NoBits) { |
|
1 | 171 |
// find the position of the 1-bit |
1374 | 172 |
for (; !(res & 1); res_offset++) { |
1 | 173 |
res = res >> 1; |
174 |
} |
|
1374 | 175 |
assert(res_offset >= l_offset && |
176 |
res_offset < r_offset, "just checking"); |
|
177 |
return MIN2(res_offset, r_offset); |
|
1 | 178 |
} |
179 |
// skip over all word length 0-bit runs |
|
180 |
for (index++; index < r_index; index++) { |
|
181 |
res = map(index); |
|
1374 | 182 |
if (res != (uintptr_t)NoBits) { |
1 | 183 |
// found a 1, return the offset |
1374 | 184 |
for (res_offset = bit_index(index); !(res & 1); res_offset++) { |
1 | 185 |
res = res >> 1; |
186 |
} |
|
187 |
assert(res & 1, "tautology; see loop condition"); |
|
1374 | 188 |
assert(res_offset >= l_offset, "just checking"); |
189 |
return MIN2(res_offset, r_offset); |
|
190 |
} |
|
191 |
} |
|
192 |
return r_offset; |
|
193 |
} |
|
194 |
||
195 |
inline BitMap::idx_t |
|
196 |
BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const { |
|
197 |
assert(l_offset <= size(), "BitMap index out of bounds"); |
|
198 |
assert(r_offset <= size(), "BitMap index out of bounds"); |
|
199 |
assert(l_offset <= r_offset, "l_offset > r_offset ?"); |
|
200 |
||
201 |
if (l_offset == r_offset) { |
|
202 |
return l_offset; |
|
203 |
} |
|
204 |
idx_t index = word_index(l_offset); |
|
205 |
idx_t r_index = word_index(r_offset-1) + 1; |
|
206 |
idx_t res_offset = l_offset; |
|
207 |
||
208 |
// check bits including and to the _left_ of offset's position |
|
209 |
idx_t pos = res_offset & (BitsPerWord - 1); |
|
210 |
idx_t res = (map(index) >> pos) | left_n_bits((int)pos); |
|
211 |
||
212 |
if (res != (uintptr_t)AllBits) { |
|
213 |
// find the position of the 0-bit |
|
214 |
for (; res & 1; res_offset++) { |
|
215 |
res = res >> 1; |
|
216 |
} |
|
217 |
assert(res_offset >= l_offset, "just checking"); |
|
218 |
return MIN2(res_offset, r_offset); |
|
219 |
} |
|
220 |
// skip over all word length 1-bit runs |
|
221 |
for (index++; index < r_index; index++) { |
|
222 |
res = map(index); |
|
223 |
if (res != (uintptr_t)AllBits) { |
|
224 |
// found a 0, return the offset |
|
225 |
for (res_offset = index << LogBitsPerWord; res & 1; |
|
226 |
res_offset++) { |
|
227 |
res = res >> 1; |
|
228 |
} |
|
229 |
assert(!(res & 1), "tautology; see loop condition"); |
|
230 |
assert(res_offset >= l_offset, "just checking"); |
|
231 |
return MIN2(res_offset, r_offset); |
|
1 | 232 |
} |
233 |
} |
|
1374 | 234 |
return r_offset; |
235 |
} |
|
236 |
||
237 |
inline BitMap::idx_t |
|
238 |
BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset, |
|
239 |
idx_t r_offset) const |
|
240 |
{ |
|
241 |
verify_range(l_offset, r_offset); |
|
242 |
assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned"); |
|
243 |
||
244 |
if (l_offset == r_offset) { |
|
245 |
return l_offset; |
|
246 |
} |
|
247 |
idx_t index = word_index(l_offset); |
|
248 |
idx_t r_index = word_index(r_offset); |
|
249 |
idx_t res_offset = l_offset; |
|
250 |
||
251 |
// check bits including and to the _left_ of offset's position |
|
252 |
idx_t res = map(index) >> bit_in_word(res_offset); |
|
253 |
if (res != (uintptr_t)NoBits) { |
|
254 |
// find the position of the 1-bit |
|
255 |
for (; !(res & 1); res_offset++) { |
|
256 |
res = res >> 1; |
|
257 |
} |
|
258 |
assert(res_offset >= l_offset && |
|
259 |
res_offset < r_offset, "just checking"); |
|
260 |
return res_offset; |
|
261 |
} |
|
262 |
// skip over all word length 0-bit runs |
|
263 |
for (index++; index < r_index; index++) { |
|
264 |
res = map(index); |
|
265 |
if (res != (uintptr_t)NoBits) { |
|
266 |
// found a 1, return the offset |
|
267 |
for (res_offset = bit_index(index); !(res & 1); res_offset++) { |
|
268 |
res = res >> 1; |
|
269 |
} |
|
270 |
assert(res & 1, "tautology; see loop condition"); |
|
271 |
assert(res_offset >= l_offset && res_offset < r_offset, "just checking"); |
|
272 |
return res_offset; |
|
273 |
} |
|
274 |
} |
|
275 |
return r_offset; |
|
1 | 276 |
} |
1374 | 277 |
|
278 |
||
279 |
// Returns a bit mask for a range of bits [beg, end) within a single word. Each |
|
280 |
// bit in the mask is 0 if the bit is in the range, 1 if not in the range. The |
|
281 |
// returned mask can be used directly to clear the range, or inverted to set the |
|
282 |
// range. Note: end must not be 0. |
|
283 |
inline BitMap::bm_word_t |
|
284 |
BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const { |
|
285 |
assert(end != 0, "does not work when end == 0"); |
|
286 |
assert(beg == end || word_index(beg) == word_index(end - 1), |
|
287 |
"must be a single-word range"); |
|
288 |
bm_word_t mask = bit_mask(beg) - 1; // low (right) bits |
|
289 |
if (bit_in_word(end) != 0) { |
|
290 |
mask |= ~(bit_mask(end) - 1); // high (left) bits |
|
291 |
} |
|
292 |
return mask; |
|
293 |
} |
|
294 |
||
295 |
inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) { |
|
296 |
memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t)); |
|
297 |
} |
|
298 |
||
299 |
inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) { |
|
300 |
memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t)); |
|
301 |
} |
|
302 |
||
303 |
inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const { |
|
304 |
idx_t bit_rounded_up = bit + (BitsPerWord - 1); |
|
305 |
// Check for integer arithmetic overflow. |
|
306 |
return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words(); |
|
307 |
} |
|
308 |
||
309 |
inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset, |
|
310 |
idx_t r_offset) const { |
|
311 |
return get_next_one_offset_inline(l_offset, r_offset); |
|
312 |
} |
|
313 |
||
314 |
inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset, |
|
315 |
idx_t r_offset) const { |
|
316 |
return get_next_zero_offset_inline(l_offset, r_offset); |
|
317 |
} |
|
318 |
||
319 |
inline void BitMap2D::clear() { |
|
320 |
_map.clear(); |
|
321 |
} |