author | sangheki |
Tue, 05 Apr 2016 16:15:53 -0700 | |
changeset 37264 | d9b0f2b53b73 |
parent 37059 | c482915a21aa |
child 37413 | 2f71679d06dd |
permissions | -rw-r--r-- |
1 | 1 |
/* |
37264
d9b0f2b53b73
8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents:
37059
diff
changeset
|
2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
3261
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
3261
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
3261
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "memory/allocation.inline.hpp" |
|
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
27 |
#include "memory/resourceArea.hpp" |
25351
7c198a690050
8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents:
24424
diff
changeset
|
28 |
#include "runtime/atomic.inline.hpp" |
7397 | 29 |
#include "utilities/bitMap.inline.hpp" |
30 |
#include "utilities/copy.hpp" |
|
1 | 31 |
|
37058 | 32 |
STATIC_ASSERT(sizeof(BitMap::bm_word_t) == BytesPerWord); // "Implementation assumption." |
1 | 33 |
|
1374 | 34 |
BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) : |
37057 | 35 |
_map(NULL), _size(0) |
1374 | 36 |
{ |
37 |
resize(size_in_bits, in_resource_area); |
|
1 | 38 |
} |
39 |
||
37059
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
40 |
#ifdef ASSERT |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
41 |
void BitMap::verify_index(idx_t index) const { |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
42 |
assert(index < _size, "BitMap index out of bounds"); |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
43 |
} |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
44 |
|
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
45 |
void BitMap::verify_range(idx_t beg_index, idx_t end_index) const { |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
46 |
assert(beg_index <= end_index, "BitMap range error"); |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
47 |
// Note that [0,0) and [size,size) are both valid ranges. |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
48 |
if (end_index != _size) verify_index(end_index); |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
49 |
} |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
50 |
#endif // #ifdef ASSERT |
c482915a21aa
8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents:
37058
diff
changeset
|
51 |
|
1374 | 52 |
void BitMap::resize(idx_t size_in_bits, bool in_resource_area) { |
53 |
idx_t old_size_in_words = size_in_words(); |
|
54 |
bm_word_t* old_map = map(); |
|
55 |
||
1 | 56 |
_size = size_in_bits; |
1374 | 57 |
idx_t new_size_in_words = size_in_words(); |
58 |
if (in_resource_area) { |
|
59 |
_map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words); |
|
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
60 |
Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map, |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
61 |
MIN2(old_size_in_words, new_size_in_words)); |
1374 | 62 |
} else { |
37057 | 63 |
_map = ArrayAllocator<bm_word_t, mtInternal>::reallocate(old_map, old_size_in_words, new_size_in_words); |
1374 | 64 |
} |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
65 |
|
1 | 66 |
if (new_size_in_words > old_size_in_words) { |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
67 |
clear_range_of_words(old_size_in_words, new_size_in_words); |
1 | 68 |
} |
69 |
} |
|
70 |
||
71 |
void BitMap::set_range_within_word(idx_t beg, idx_t end) { |
|
72 |
// With a valid range (beg <= end), this test ensures that end != 0, as |
|
73 |
// required by inverted_bit_mask_for_range. Also avoids an unnecessary write. |
|
74 |
if (beg != end) { |
|
1374 | 75 |
bm_word_t mask = inverted_bit_mask_for_range(beg, end); |
1 | 76 |
*word_addr(beg) |= ~mask; |
77 |
} |
|
78 |
} |
|
79 |
||
80 |
void BitMap::clear_range_within_word(idx_t beg, idx_t end) { |
|
81 |
// With a valid range (beg <= end), this test ensures that end != 0, as |
|
82 |
// required by inverted_bit_mask_for_range. Also avoids an unnecessary write. |
|
83 |
if (beg != end) { |
|
1374 | 84 |
bm_word_t mask = inverted_bit_mask_for_range(beg, end); |
1 | 85 |
*word_addr(beg) &= mask; |
86 |
} |
|
87 |
} |
|
88 |
||
89 |
void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) { |
|
90 |
assert(value == 0 || value == 1, "0 for clear, 1 for set"); |
|
91 |
// With a valid range (beg <= end), this test ensures that end != 0, as |
|
92 |
// required by inverted_bit_mask_for_range. Also avoids an unnecessary write. |
|
93 |
if (beg != end) { |
|
94 |
intptr_t* pw = (intptr_t*)word_addr(beg); |
|
95 |
intptr_t w = *pw; |
|
96 |
intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end); |
|
97 |
intptr_t nw = value ? (w | ~mr) : (w & mr); |
|
98 |
while (true) { |
|
99 |
intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w); |
|
100 |
if (res == w) break; |
|
22769
516e9b159656
8033545: Missing volatile specifier in Bitmap::par_put_range_within_word
tschatzl
parents:
22234
diff
changeset
|
101 |
w = res; |
1 | 102 |
nw = value ? (w | ~mr) : (w & mr); |
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
void BitMap::set_range(idx_t beg, idx_t end) { |
|
108 |
verify_range(beg, end); |
|
109 |
||
110 |
idx_t beg_full_word = word_index_round_up(beg); |
|
111 |
idx_t end_full_word = word_index(end); |
|
112 |
||
113 |
if (beg_full_word < end_full_word) { |
|
114 |
// The range includes at least one full word. |
|
115 |
set_range_within_word(beg, bit_index(beg_full_word)); |
|
116 |
set_range_of_words(beg_full_word, end_full_word); |
|
117 |
set_range_within_word(bit_index(end_full_word), end); |
|
118 |
} else { |
|
119 |
// The range spans at most 2 partial words. |
|
120 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
121 |
set_range_within_word(beg, boundary); |
|
122 |
set_range_within_word(boundary, end); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
void BitMap::clear_range(idx_t beg, idx_t end) { |
|
127 |
verify_range(beg, end); |
|
128 |
||
129 |
idx_t beg_full_word = word_index_round_up(beg); |
|
130 |
idx_t end_full_word = word_index(end); |
|
131 |
||
132 |
if (beg_full_word < end_full_word) { |
|
133 |
// The range includes at least one full word. |
|
134 |
clear_range_within_word(beg, bit_index(beg_full_word)); |
|
135 |
clear_range_of_words(beg_full_word, end_full_word); |
|
136 |
clear_range_within_word(bit_index(end_full_word), end); |
|
137 |
} else { |
|
138 |
// The range spans at most 2 partial words. |
|
139 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
140 |
clear_range_within_word(beg, boundary); |
|
141 |
clear_range_within_word(boundary, end); |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
void BitMap::set_large_range(idx_t beg, idx_t end) { |
|
146 |
verify_range(beg, end); |
|
147 |
||
148 |
idx_t beg_full_word = word_index_round_up(beg); |
|
149 |
idx_t end_full_word = word_index(end); |
|
150 |
||
151 |
assert(end_full_word - beg_full_word >= 32, |
|
152 |
"the range must include at least 32 bytes"); |
|
153 |
||
154 |
// The range includes at least one full word. |
|
155 |
set_range_within_word(beg, bit_index(beg_full_word)); |
|
156 |
set_large_range_of_words(beg_full_word, end_full_word); |
|
157 |
set_range_within_word(bit_index(end_full_word), end); |
|
158 |
} |
|
159 |
||
160 |
void BitMap::clear_large_range(idx_t beg, idx_t end) { |
|
161 |
verify_range(beg, end); |
|
162 |
||
163 |
idx_t beg_full_word = word_index_round_up(beg); |
|
164 |
idx_t end_full_word = word_index(end); |
|
165 |
||
37264
d9b0f2b53b73
8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents:
37059
diff
changeset
|
166 |
if (end_full_word - beg_full_word < 32) { |
d9b0f2b53b73
8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents:
37059
diff
changeset
|
167 |
clear_range(beg, end); |
d9b0f2b53b73
8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents:
37059
diff
changeset
|
168 |
return; |
d9b0f2b53b73
8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents:
37059
diff
changeset
|
169 |
} |
1 | 170 |
|
171 |
// The range includes at least one full word. |
|
172 |
clear_range_within_word(beg, bit_index(beg_full_word)); |
|
173 |
clear_large_range_of_words(beg_full_word, end_full_word); |
|
174 |
clear_range_within_word(bit_index(end_full_word), end); |
|
175 |
} |
|
176 |
||
177 |
void BitMap::at_put(idx_t offset, bool value) { |
|
178 |
if (value) { |
|
179 |
set_bit(offset); |
|
180 |
} else { |
|
181 |
clear_bit(offset); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
// Return true to indicate that this thread changed |
|
186 |
// the bit, false to indicate that someone else did. |
|
187 |
// In either case, the requested bit is in the |
|
188 |
// requested state some time during the period that |
|
189 |
// this thread is executing this call. More importantly, |
|
190 |
// if no other thread is executing an action to |
|
191 |
// change the requested bit to a state other than |
|
192 |
// the one that this thread is trying to set it to, |
|
193 |
// then the the bit is in the expected state |
|
194 |
// at exit from this method. However, rather than |
|
195 |
// make such a strong assertion here, based on |
|
196 |
// assuming such constrained use (which though true |
|
197 |
// today, could change in the future to service some |
|
198 |
// funky parallel algorithm), we encourage callers |
|
199 |
// to do such verification, as and when appropriate. |
|
200 |
bool BitMap::par_at_put(idx_t bit, bool value) { |
|
201 |
return value ? par_set_bit(bit) : par_clear_bit(bit); |
|
202 |
} |
|
203 |
||
204 |
void BitMap::at_put_grow(idx_t offset, bool value) { |
|
205 |
if (offset >= size()) { |
|
206 |
resize(2 * MAX2(size(), offset)); |
|
207 |
} |
|
208 |
at_put(offset, value); |
|
209 |
} |
|
210 |
||
211 |
void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) { |
|
212 |
if (value) { |
|
213 |
set_range(start_offset, end_offset); |
|
214 |
} else { |
|
215 |
clear_range(start_offset, end_offset); |
|
216 |
} |
|
217 |
} |
|
218 |
||
219 |
void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) { |
|
220 |
verify_range(beg, end); |
|
221 |
||
222 |
idx_t beg_full_word = word_index_round_up(beg); |
|
223 |
idx_t end_full_word = word_index(end); |
|
224 |
||
225 |
if (beg_full_word < end_full_word) { |
|
226 |
// The range includes at least one full word. |
|
227 |
par_put_range_within_word(beg, bit_index(beg_full_word), value); |
|
228 |
if (value) { |
|
229 |
set_range_of_words(beg_full_word, end_full_word); |
|
230 |
} else { |
|
231 |
clear_range_of_words(beg_full_word, end_full_word); |
|
232 |
} |
|
233 |
par_put_range_within_word(bit_index(end_full_word), end, value); |
|
234 |
} else { |
|
235 |
// The range spans at most 2 partial words. |
|
236 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
237 |
par_put_range_within_word(beg, boundary, value); |
|
238 |
par_put_range_within_word(boundary, end, value); |
|
239 |
} |
|
240 |
||
241 |
} |
|
242 |
||
243 |
void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) { |
|
244 |
if (value) { |
|
245 |
set_large_range(beg, end); |
|
246 |
} else { |
|
247 |
clear_large_range(beg, end); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) { |
|
252 |
verify_range(beg, end); |
|
253 |
||
254 |
idx_t beg_full_word = word_index_round_up(beg); |
|
255 |
idx_t end_full_word = word_index(end); |
|
256 |
||
257 |
assert(end_full_word - beg_full_word >= 32, |
|
258 |
"the range must include at least 32 bytes"); |
|
259 |
||
260 |
// The range includes at least one full word. |
|
261 |
par_put_range_within_word(beg, bit_index(beg_full_word), value); |
|
262 |
if (value) { |
|
263 |
set_large_range_of_words(beg_full_word, end_full_word); |
|
264 |
} else { |
|
265 |
clear_large_range_of_words(beg_full_word, end_full_word); |
|
266 |
} |
|
267 |
par_put_range_within_word(bit_index(end_full_word), end, value); |
|
268 |
} |
|
269 |
||
270 |
bool BitMap::contains(const BitMap other) const { |
|
271 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 272 |
bm_word_t* dest_map = map(); |
273 |
bm_word_t* other_map = other.map(); |
|
1 | 274 |
idx_t size = size_in_words(); |
275 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
1374 | 276 |
bm_word_t word_union = dest_map[index] | other_map[index]; |
1 | 277 |
// If this has more bits set than dest_map[index], then other is not a |
278 |
// subset. |
|
279 |
if (word_union != dest_map[index]) return false; |
|
280 |
} |
|
281 |
return true; |
|
282 |
} |
|
283 |
||
284 |
bool BitMap::intersects(const BitMap other) const { |
|
285 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 286 |
bm_word_t* dest_map = map(); |
287 |
bm_word_t* other_map = other.map(); |
|
1 | 288 |
idx_t size = size_in_words(); |
289 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
290 |
if ((dest_map[index] & other_map[index]) != 0) return true; |
|
291 |
} |
|
292 |
// Otherwise, no intersection. |
|
293 |
return false; |
|
294 |
} |
|
295 |
||
296 |
void BitMap::set_union(BitMap other) { |
|
297 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 298 |
bm_word_t* dest_map = map(); |
299 |
bm_word_t* other_map = other.map(); |
|
1 | 300 |
idx_t size = size_in_words(); |
301 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
302 |
dest_map[index] = dest_map[index] | other_map[index]; |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
||
307 |
void BitMap::set_difference(BitMap other) { |
|
308 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 309 |
bm_word_t* dest_map = map(); |
310 |
bm_word_t* other_map = other.map(); |
|
1 | 311 |
idx_t size = size_in_words(); |
312 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
313 |
dest_map[index] = dest_map[index] & ~(other_map[index]); |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
||
318 |
void BitMap::set_intersection(BitMap other) { |
|
319 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 320 |
bm_word_t* dest_map = map(); |
321 |
bm_word_t* other_map = other.map(); |
|
1 | 322 |
idx_t size = size_in_words(); |
323 |
for (idx_t index = 0; index < size; index++) { |
|
324 |
dest_map[index] = dest_map[index] & other_map[index]; |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
||
1374 | 329 |
void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) { |
330 |
assert(other.size() >= offset, "offset not in range"); |
|
331 |
assert(other.size() - offset >= size(), "other not large enough"); |
|
332 |
// XXX Ideally, we would remove this restriction. |
|
333 |
guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0, |
|
334 |
"Only handle aligned cases so far."); |
|
335 |
bm_word_t* dest_map = map(); |
|
336 |
bm_word_t* other_map = other.map(); |
|
337 |
idx_t offset_word_ind = word_index(offset); |
|
338 |
idx_t size = size_in_words(); |
|
339 |
for (idx_t index = 0; index < size; index++) { |
|
340 |
dest_map[index] = dest_map[index] & other_map[offset_word_ind + index]; |
|
341 |
} |
|
342 |
} |
|
343 |
||
1 | 344 |
bool BitMap::set_union_with_result(BitMap other) { |
345 |
assert(size() == other.size(), "must have same size"); |
|
346 |
bool changed = false; |
|
1374 | 347 |
bm_word_t* dest_map = map(); |
348 |
bm_word_t* other_map = other.map(); |
|
1 | 349 |
idx_t size = size_in_words(); |
350 |
for (idx_t index = 0; index < size; index++) { |
|
26937 | 351 |
idx_t temp = dest_map[index] | other_map[index]; |
352 |
changed = changed || (temp != dest_map[index]); |
|
353 |
dest_map[index] = temp; |
|
1 | 354 |
} |
355 |
return changed; |
|
356 |
} |
|
357 |
||
358 |
||
359 |
bool BitMap::set_difference_with_result(BitMap other) { |
|
360 |
assert(size() == other.size(), "must have same size"); |
|
361 |
bool changed = false; |
|
1374 | 362 |
bm_word_t* dest_map = map(); |
363 |
bm_word_t* other_map = other.map(); |
|
1 | 364 |
idx_t size = size_in_words(); |
365 |
for (idx_t index = 0; index < size; index++) { |
|
1374 | 366 |
bm_word_t temp = dest_map[index] & ~(other_map[index]); |
1 | 367 |
changed = changed || (temp != dest_map[index]); |
368 |
dest_map[index] = temp; |
|
369 |
} |
|
370 |
return changed; |
|
371 |
} |
|
372 |
||
373 |
||
374 |
bool BitMap::set_intersection_with_result(BitMap other) { |
|
375 |
assert(size() == other.size(), "must have same size"); |
|
376 |
bool changed = false; |
|
1374 | 377 |
bm_word_t* dest_map = map(); |
378 |
bm_word_t* other_map = other.map(); |
|
1 | 379 |
idx_t size = size_in_words(); |
380 |
for (idx_t index = 0; index < size; index++) { |
|
1374 | 381 |
bm_word_t orig = dest_map[index]; |
382 |
bm_word_t temp = orig & other_map[index]; |
|
1 | 383 |
changed = changed || (temp != orig); |
384 |
dest_map[index] = temp; |
|
385 |
} |
|
386 |
return changed; |
|
387 |
} |
|
388 |
||
389 |
||
390 |
void BitMap::set_from(BitMap other) { |
|
391 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 392 |
bm_word_t* dest_map = map(); |
393 |
bm_word_t* other_map = other.map(); |
|
1 | 394 |
idx_t size = size_in_words(); |
395 |
for (idx_t index = 0; index < size; index++) { |
|
396 |
dest_map[index] = other_map[index]; |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
||
401 |
bool BitMap::is_same(BitMap other) { |
|
402 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 403 |
bm_word_t* dest_map = map(); |
404 |
bm_word_t* other_map = other.map(); |
|
1 | 405 |
idx_t size = size_in_words(); |
406 |
for (idx_t index = 0; index < size; index++) { |
|
407 |
if (dest_map[index] != other_map[index]) return false; |
|
408 |
} |
|
409 |
return true; |
|
410 |
} |
|
411 |
||
412 |
bool BitMap::is_full() const { |
|
1374 | 413 |
bm_word_t* word = map(); |
1 | 414 |
idx_t rest = size(); |
415 |
for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) { |
|
26937 | 416 |
if (*word != ~(bm_word_t)0) return false; |
1 | 417 |
word++; |
418 |
} |
|
26937 | 419 |
return rest == 0 || (*word | ~right_n_bits((int)rest)) == ~(bm_word_t)0; |
1 | 420 |
} |
421 |
||
422 |
||
423 |
bool BitMap::is_empty() const { |
|
1374 | 424 |
bm_word_t* word = map(); |
1 | 425 |
idx_t rest = size(); |
426 |
for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) { |
|
26937 | 427 |
if (*word != 0) return false; |
1 | 428 |
word++; |
429 |
} |
|
26937 | 430 |
return rest == 0 || (*word & right_n_bits((int)rest)) == 0; |
1 | 431 |
} |
432 |
||
433 |
void BitMap::clear_large() { |
|
434 |
clear_large_range_of_words(0, size_in_words()); |
|
435 |
} |
|
436 |
||
437 |
// Note that if the closure itself modifies the bitmap |
|
438 |
// then modifications in and to the left of the _bit_ being |
|
439 |
// currently sampled will not be seen. Note also that the |
|
440 |
// interval [leftOffset, rightOffset) is right open. |
|
1374 | 441 |
bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) { |
1 | 442 |
verify_range(leftOffset, rightOffset); |
443 |
||
444 |
idx_t startIndex = word_index(leftOffset); |
|
445 |
idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words()); |
|
446 |
for (idx_t index = startIndex, offset = leftOffset; |
|
447 |
offset < rightOffset && index < endIndex; |
|
448 |
offset = (++index) << LogBitsPerWord) { |
|
449 |
idx_t rest = map(index) >> (offset & (BitsPerWord - 1)); |
|
26937 | 450 |
for (; offset < rightOffset && rest != 0; offset++) { |
1 | 451 |
if (rest & 1) { |
1374 | 452 |
if (!blk->do_bit(offset)) return false; |
1 | 453 |
// resample at each closure application |
454 |
// (see, for instance, CMS bug 4525989) |
|
455 |
rest = map(index) >> (offset & (BitsPerWord -1)); |
|
456 |
} |
|
457 |
rest = rest >> 1; |
|
458 |
} |
|
459 |
} |
|
1374 | 460 |
return true; |
461 |
} |
|
462 |
||
463 |
BitMap::idx_t* BitMap::_pop_count_table = NULL; |
|
464 |
||
465 |
void BitMap::init_pop_count_table() { |
|
466 |
if (_pop_count_table == NULL) { |
|
13195 | 467 |
BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal); |
1374 | 468 |
for (uint i = 0; i < 256; i++) { |
469 |
table[i] = num_set_bits(i); |
|
470 |
} |
|
471 |
||
472 |
intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table, |
|
473 |
(intptr_t*) &_pop_count_table, |
|
474 |
(intptr_t) NULL_WORD); |
|
475 |
if (res != NULL_WORD) { |
|
476 |
guarantee( _pop_count_table == (void*) res, "invariant" ); |
|
27880
afb974a04396
8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents:
26937
diff
changeset
|
477 |
FREE_C_HEAP_ARRAY(idx_t, table); |
1374 | 478 |
} |
479 |
} |
|
1 | 480 |
} |
481 |
||
1374 | 482 |
BitMap::idx_t BitMap::num_set_bits(bm_word_t w) { |
483 |
idx_t bits = 0; |
|
1 | 484 |
|
1374 | 485 |
while (w != 0) { |
486 |
while ((w & 1) == 0) { |
|
487 |
w >>= 1; |
|
1 | 488 |
} |
1374 | 489 |
bits++; |
490 |
w >>= 1; |
|
1 | 491 |
} |
1374 | 492 |
return bits; |
1 | 493 |
} |
494 |
||
1374 | 495 |
BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) { |
496 |
assert(_pop_count_table != NULL, "precondition"); |
|
497 |
return _pop_count_table[c]; |
|
498 |
} |
|
1 | 499 |
|
1374 | 500 |
BitMap::idx_t BitMap::count_one_bits() const { |
501 |
init_pop_count_table(); // If necessary. |
|
502 |
idx_t sum = 0; |
|
503 |
typedef unsigned char uchar; |
|
504 |
for (idx_t i = 0; i < size_in_words(); i++) { |
|
505 |
bm_word_t w = map()[i]; |
|
506 |
for (size_t j = 0; j < sizeof(bm_word_t); j++) { |
|
507 |
sum += num_set_bits_from_table(uchar(w & 255)); |
|
508 |
w >>= 8; |
|
1 | 509 |
} |
510 |
} |
|
1374 | 511 |
return sum; |
1 | 512 |
} |
513 |
||
16685
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
514 |
void BitMap::print_on_error(outputStream* st, const char* prefix) const { |
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
515 |
st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")", |
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23545
diff
changeset
|
516 |
prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte))); |
16685
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
517 |
} |
1374 | 518 |
|
1 | 519 |
#ifndef PRODUCT |
520 |
||
521 |
void BitMap::print_on(outputStream* st) const { |
|
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23545
diff
changeset
|
522 |
tty->print("Bitmap(" SIZE_FORMAT "):", size()); |
1 | 523 |
for (idx_t index = 0; index < size(); index++) { |
524 |
tty->print("%c", at(index) ? '1' : '0'); |
|
525 |
} |
|
526 |
tty->cr(); |
|
527 |
} |
|
528 |
||
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
529 |
class TestBitMap : public AllStatic { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
530 |
const static BitMap::idx_t BITMAP_SIZE = 1024; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
531 |
static void fillBitMap(BitMap& map) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
532 |
map.set_bit(1); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
533 |
map.set_bit(3); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
534 |
map.set_bit(17); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
535 |
map.set_bit(512); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
536 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
537 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
538 |
static void testResize(bool in_resource_area) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
539 |
{ |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
540 |
BitMap map(0, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
541 |
map.resize(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
542 |
fillBitMap(map); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
543 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
544 |
BitMap map2(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
545 |
fillBitMap(map2); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
546 |
assert(map.is_same(map2), "could be"); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
547 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
548 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
549 |
{ |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
550 |
BitMap map(128, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
551 |
map.resize(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
552 |
fillBitMap(map); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
553 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
554 |
BitMap map2(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
555 |
fillBitMap(map2); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
556 |
assert(map.is_same(map2), "could be"); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
557 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
558 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
559 |
{ |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
560 |
BitMap map(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
561 |
map.resize(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
562 |
fillBitMap(map); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
563 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
564 |
BitMap map2(BITMAP_SIZE, in_resource_area); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
565 |
fillBitMap(map2); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
566 |
assert(map.is_same(map2), "could be"); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
567 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
568 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
569 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
570 |
static void testResizeResource() { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
571 |
ResourceMark rm; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
572 |
testResize(true); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
573 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
574 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
575 |
static void testResizeNonResource() { |
25959 | 576 |
const size_t bitmap_bytes = BITMAP_SIZE / BitsPerByte; |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
577 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
578 |
// Test the default behavior |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
579 |
testResize(false); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
580 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
581 |
{ |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
582 |
// Make sure that AllocatorMallocLimit is larger than our allocation request |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
583 |
// forcing it to call standard malloc() |
25959 | 584 |
SizeTFlagSetting fs(ArrayAllocatorMallocLimit, bitmap_bytes * 4); |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
585 |
testResize(false); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
586 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
587 |
{ |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
588 |
// Make sure that AllocatorMallocLimit is smaller than our allocation request |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
589 |
// forcing it to call mmap() (or equivalent) |
25959 | 590 |
SizeTFlagSetting fs(ArrayAllocatorMallocLimit, bitmap_bytes / 4); |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
591 |
testResize(false); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
592 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
593 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
594 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
595 |
public: |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
596 |
static void test() { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
597 |
testResizeResource(); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
598 |
testResizeNonResource(); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
599 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
600 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
601 |
}; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
602 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
603 |
void TestBitMap_test() { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
604 |
TestBitMap::test(); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
22876
diff
changeset
|
605 |
} |
1 | 606 |
#endif |
607 |
||
608 |
||
1374 | 609 |
BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot) |
1 | 610 |
: _bits_per_slot(bits_per_slot) |
611 |
, _map(map, size_in_slots * bits_per_slot) |
|
612 |
{ |
|
613 |
} |
|
614 |
||
615 |
||
616 |
BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) |
|
617 |
: _bits_per_slot(bits_per_slot) |
|
618 |
, _map(size_in_slots * bits_per_slot) |
|
619 |
{ |
|
620 |
} |