author | tschatzl |
Thu, 06 Feb 2014 17:12:10 +0100 | |
changeset 22769 | 516e9b159656 |
parent 22234 | da823d78ad65 |
child 22876 | 57aa8995d43b |
permissions | -rw-r--r-- |
1 | 1 |
/* |
22234
da823d78ad65
8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
mikael
parents:
18092
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, 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" |
|
27 |
#include "utilities/bitMap.inline.hpp" |
|
28 |
#include "utilities/copy.hpp" |
|
29 |
#ifdef TARGET_OS_FAMILY_linux |
|
30 |
# include "os_linux.inline.hpp" |
|
31 |
#endif |
|
32 |
#ifdef TARGET_OS_FAMILY_solaris |
|
33 |
# include "os_solaris.inline.hpp" |
|
34 |
#endif |
|
35 |
#ifdef TARGET_OS_FAMILY_windows |
|
36 |
# include "os_windows.inline.hpp" |
|
37 |
#endif |
|
10565 | 38 |
#ifdef TARGET_OS_FAMILY_bsd |
39 |
# include "os_bsd.inline.hpp" |
|
40 |
#endif |
|
1 | 41 |
|
42 |
||
1374 | 43 |
BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) : |
18092 | 44 |
_map(map), _size(size_in_bits), _map_allocator(false) |
1374 | 45 |
{ |
46 |
assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption."); |
|
1 | 47 |
assert(size_in_bits >= 0, "just checking"); |
48 |
} |
|
49 |
||
50 |
||
1374 | 51 |
BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) : |
18092 | 52 |
_map(NULL), _size(0), _map_allocator(false) |
1374 | 53 |
{ |
54 |
assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption."); |
|
55 |
resize(size_in_bits, in_resource_area); |
|
1 | 56 |
} |
57 |
||
1374 | 58 |
void BitMap::resize(idx_t size_in_bits, bool in_resource_area) { |
1 | 59 |
assert(size_in_bits >= 0, "just checking"); |
1374 | 60 |
idx_t old_size_in_words = size_in_words(); |
61 |
bm_word_t* old_map = map(); |
|
62 |
||
1 | 63 |
_size = size_in_bits; |
1374 | 64 |
idx_t new_size_in_words = size_in_words(); |
65 |
if (in_resource_area) { |
|
66 |
_map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words); |
|
67 |
} else { |
|
18092 | 68 |
if (old_map != NULL) { |
69 |
_map_allocator.free(); |
|
70 |
} |
|
71 |
_map = _map_allocator.allocate(new_size_in_words); |
|
1374 | 72 |
} |
73 |
Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map, |
|
74 |
MIN2(old_size_in_words, new_size_in_words)); |
|
1 | 75 |
if (new_size_in_words > old_size_in_words) { |
76 |
clear_range_of_words(old_size_in_words, size_in_words()); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
void BitMap::set_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::clear_range_within_word(idx_t beg, idx_t end) { |
|
90 |
// With a valid range (beg <= end), this test ensures that end != 0, as |
|
91 |
// required by inverted_bit_mask_for_range. Also avoids an unnecessary write. |
|
92 |
if (beg != end) { |
|
1374 | 93 |
bm_word_t mask = inverted_bit_mask_for_range(beg, end); |
1 | 94 |
*word_addr(beg) &= mask; |
95 |
} |
|
96 |
} |
|
97 |
||
98 |
void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) { |
|
99 |
assert(value == 0 || value == 1, "0 for clear, 1 for set"); |
|
100 |
// With a valid range (beg <= end), this test ensures that end != 0, as |
|
101 |
// required by inverted_bit_mask_for_range. Also avoids an unnecessary write. |
|
102 |
if (beg != end) { |
|
103 |
intptr_t* pw = (intptr_t*)word_addr(beg); |
|
104 |
intptr_t w = *pw; |
|
105 |
intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end); |
|
106 |
intptr_t nw = value ? (w | ~mr) : (w & mr); |
|
107 |
while (true) { |
|
108 |
intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w); |
|
109 |
if (res == w) break; |
|
22769
516e9b159656
8033545: Missing volatile specifier in Bitmap::par_put_range_within_word
tschatzl
parents:
22234
diff
changeset
|
110 |
w = res; |
1 | 111 |
nw = value ? (w | ~mr) : (w & mr); |
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
void BitMap::set_range(idx_t beg, idx_t end) { |
|
117 |
verify_range(beg, end); |
|
118 |
||
119 |
idx_t beg_full_word = word_index_round_up(beg); |
|
120 |
idx_t end_full_word = word_index(end); |
|
121 |
||
122 |
if (beg_full_word < end_full_word) { |
|
123 |
// The range includes at least one full word. |
|
124 |
set_range_within_word(beg, bit_index(beg_full_word)); |
|
125 |
set_range_of_words(beg_full_word, end_full_word); |
|
126 |
set_range_within_word(bit_index(end_full_word), end); |
|
127 |
} else { |
|
128 |
// The range spans at most 2 partial words. |
|
129 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
130 |
set_range_within_word(beg, boundary); |
|
131 |
set_range_within_word(boundary, end); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
void BitMap::clear_range(idx_t beg, idx_t end) { |
|
136 |
verify_range(beg, end); |
|
137 |
||
138 |
idx_t beg_full_word = word_index_round_up(beg); |
|
139 |
idx_t end_full_word = word_index(end); |
|
140 |
||
141 |
if (beg_full_word < end_full_word) { |
|
142 |
// The range includes at least one full word. |
|
143 |
clear_range_within_word(beg, bit_index(beg_full_word)); |
|
144 |
clear_range_of_words(beg_full_word, end_full_word); |
|
145 |
clear_range_within_word(bit_index(end_full_word), end); |
|
146 |
} else { |
|
147 |
// The range spans at most 2 partial words. |
|
148 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
149 |
clear_range_within_word(beg, boundary); |
|
150 |
clear_range_within_word(boundary, end); |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
void BitMap::set_large_range(idx_t beg, idx_t end) { |
|
155 |
verify_range(beg, end); |
|
156 |
||
157 |
idx_t beg_full_word = word_index_round_up(beg); |
|
158 |
idx_t end_full_word = word_index(end); |
|
159 |
||
160 |
assert(end_full_word - beg_full_word >= 32, |
|
161 |
"the range must include at least 32 bytes"); |
|
162 |
||
163 |
// The range includes at least one full word. |
|
164 |
set_range_within_word(beg, bit_index(beg_full_word)); |
|
165 |
set_large_range_of_words(beg_full_word, end_full_word); |
|
166 |
set_range_within_word(bit_index(end_full_word), end); |
|
167 |
} |
|
168 |
||
169 |
void BitMap::clear_large_range(idx_t beg, idx_t end) { |
|
170 |
verify_range(beg, end); |
|
171 |
||
172 |
idx_t beg_full_word = word_index_round_up(beg); |
|
173 |
idx_t end_full_word = word_index(end); |
|
174 |
||
175 |
assert(end_full_word - beg_full_word >= 32, |
|
176 |
"the range must include at least 32 bytes"); |
|
177 |
||
178 |
// The range includes at least one full word. |
|
179 |
clear_range_within_word(beg, bit_index(beg_full_word)); |
|
180 |
clear_large_range_of_words(beg_full_word, end_full_word); |
|
181 |
clear_range_within_word(bit_index(end_full_word), end); |
|
182 |
} |
|
183 |
||
184 |
void BitMap::at_put(idx_t offset, bool value) { |
|
185 |
if (value) { |
|
186 |
set_bit(offset); |
|
187 |
} else { |
|
188 |
clear_bit(offset); |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
// Return true to indicate that this thread changed |
|
193 |
// the bit, false to indicate that someone else did. |
|
194 |
// In either case, the requested bit is in the |
|
195 |
// requested state some time during the period that |
|
196 |
// this thread is executing this call. More importantly, |
|
197 |
// if no other thread is executing an action to |
|
198 |
// change the requested bit to a state other than |
|
199 |
// the one that this thread is trying to set it to, |
|
200 |
// then the the bit is in the expected state |
|
201 |
// at exit from this method. However, rather than |
|
202 |
// make such a strong assertion here, based on |
|
203 |
// assuming such constrained use (which though true |
|
204 |
// today, could change in the future to service some |
|
205 |
// funky parallel algorithm), we encourage callers |
|
206 |
// to do such verification, as and when appropriate. |
|
207 |
bool BitMap::par_at_put(idx_t bit, bool value) { |
|
208 |
return value ? par_set_bit(bit) : par_clear_bit(bit); |
|
209 |
} |
|
210 |
||
211 |
void BitMap::at_put_grow(idx_t offset, bool value) { |
|
212 |
if (offset >= size()) { |
|
213 |
resize(2 * MAX2(size(), offset)); |
|
214 |
} |
|
215 |
at_put(offset, value); |
|
216 |
} |
|
217 |
||
218 |
void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) { |
|
219 |
if (value) { |
|
220 |
set_range(start_offset, end_offset); |
|
221 |
} else { |
|
222 |
clear_range(start_offset, end_offset); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) { |
|
227 |
verify_range(beg, end); |
|
228 |
||
229 |
idx_t beg_full_word = word_index_round_up(beg); |
|
230 |
idx_t end_full_word = word_index(end); |
|
231 |
||
232 |
if (beg_full_word < end_full_word) { |
|
233 |
// The range includes at least one full word. |
|
234 |
par_put_range_within_word(beg, bit_index(beg_full_word), value); |
|
235 |
if (value) { |
|
236 |
set_range_of_words(beg_full_word, end_full_word); |
|
237 |
} else { |
|
238 |
clear_range_of_words(beg_full_word, end_full_word); |
|
239 |
} |
|
240 |
par_put_range_within_word(bit_index(end_full_word), end, value); |
|
241 |
} else { |
|
242 |
// The range spans at most 2 partial words. |
|
243 |
idx_t boundary = MIN2(bit_index(beg_full_word), end); |
|
244 |
par_put_range_within_word(beg, boundary, value); |
|
245 |
par_put_range_within_word(boundary, end, value); |
|
246 |
} |
|
247 |
||
248 |
} |
|
249 |
||
250 |
void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) { |
|
251 |
if (value) { |
|
252 |
set_large_range(beg, end); |
|
253 |
} else { |
|
254 |
clear_large_range(beg, end); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) { |
|
259 |
verify_range(beg, end); |
|
260 |
||
261 |
idx_t beg_full_word = word_index_round_up(beg); |
|
262 |
idx_t end_full_word = word_index(end); |
|
263 |
||
264 |
assert(end_full_word - beg_full_word >= 32, |
|
265 |
"the range must include at least 32 bytes"); |
|
266 |
||
267 |
// The range includes at least one full word. |
|
268 |
par_put_range_within_word(beg, bit_index(beg_full_word), value); |
|
269 |
if (value) { |
|
270 |
set_large_range_of_words(beg_full_word, end_full_word); |
|
271 |
} else { |
|
272 |
clear_large_range_of_words(beg_full_word, end_full_word); |
|
273 |
} |
|
274 |
par_put_range_within_word(bit_index(end_full_word), end, value); |
|
275 |
} |
|
276 |
||
277 |
bool BitMap::contains(const BitMap other) const { |
|
278 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 279 |
bm_word_t* dest_map = map(); |
280 |
bm_word_t* other_map = other.map(); |
|
1 | 281 |
idx_t size = size_in_words(); |
282 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
1374 | 283 |
bm_word_t word_union = dest_map[index] | other_map[index]; |
1 | 284 |
// If this has more bits set than dest_map[index], then other is not a |
285 |
// subset. |
|
286 |
if (word_union != dest_map[index]) return false; |
|
287 |
} |
|
288 |
return true; |
|
289 |
} |
|
290 |
||
291 |
bool BitMap::intersects(const BitMap other) const { |
|
292 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 293 |
bm_word_t* dest_map = map(); |
294 |
bm_word_t* other_map = other.map(); |
|
1 | 295 |
idx_t size = size_in_words(); |
296 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
297 |
if ((dest_map[index] & other_map[index]) != 0) return true; |
|
298 |
} |
|
299 |
// Otherwise, no intersection. |
|
300 |
return false; |
|
301 |
} |
|
302 |
||
303 |
void BitMap::set_union(BitMap other) { |
|
304 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 305 |
bm_word_t* dest_map = map(); |
306 |
bm_word_t* other_map = other.map(); |
|
1 | 307 |
idx_t size = size_in_words(); |
308 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
309 |
dest_map[index] = dest_map[index] | other_map[index]; |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
||
314 |
void BitMap::set_difference(BitMap other) { |
|
315 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 316 |
bm_word_t* dest_map = map(); |
317 |
bm_word_t* other_map = other.map(); |
|
1 | 318 |
idx_t size = size_in_words(); |
319 |
for (idx_t index = 0; index < size_in_words(); index++) { |
|
320 |
dest_map[index] = dest_map[index] & ~(other_map[index]); |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
||
325 |
void BitMap::set_intersection(BitMap other) { |
|
326 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 327 |
bm_word_t* dest_map = map(); |
328 |
bm_word_t* other_map = other.map(); |
|
1 | 329 |
idx_t size = size_in_words(); |
330 |
for (idx_t index = 0; index < size; index++) { |
|
331 |
dest_map[index] = dest_map[index] & other_map[index]; |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
||
1374 | 336 |
void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) { |
337 |
assert(other.size() >= offset, "offset not in range"); |
|
338 |
assert(other.size() - offset >= size(), "other not large enough"); |
|
339 |
// XXX Ideally, we would remove this restriction. |
|
340 |
guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0, |
|
341 |
"Only handle aligned cases so far."); |
|
342 |
bm_word_t* dest_map = map(); |
|
343 |
bm_word_t* other_map = other.map(); |
|
344 |
idx_t offset_word_ind = word_index(offset); |
|
345 |
idx_t size = size_in_words(); |
|
346 |
for (idx_t index = 0; index < size; index++) { |
|
347 |
dest_map[index] = dest_map[index] & other_map[offset_word_ind + index]; |
|
348 |
} |
|
349 |
} |
|
350 |
||
1 | 351 |
bool BitMap::set_union_with_result(BitMap other) { |
352 |
assert(size() == other.size(), "must have same size"); |
|
353 |
bool changed = false; |
|
1374 | 354 |
bm_word_t* dest_map = map(); |
355 |
bm_word_t* other_map = other.map(); |
|
1 | 356 |
idx_t size = size_in_words(); |
357 |
for (idx_t index = 0; index < size; index++) { |
|
358 |
idx_t temp = map(index) | other_map[index]; |
|
359 |
changed = changed || (temp != map(index)); |
|
360 |
map()[index] = temp; |
|
361 |
} |
|
362 |
return changed; |
|
363 |
} |
|
364 |
||
365 |
||
366 |
bool BitMap::set_difference_with_result(BitMap other) { |
|
367 |
assert(size() == other.size(), "must have same size"); |
|
368 |
bool changed = false; |
|
1374 | 369 |
bm_word_t* dest_map = map(); |
370 |
bm_word_t* other_map = other.map(); |
|
1 | 371 |
idx_t size = size_in_words(); |
372 |
for (idx_t index = 0; index < size; index++) { |
|
1374 | 373 |
bm_word_t temp = dest_map[index] & ~(other_map[index]); |
1 | 374 |
changed = changed || (temp != dest_map[index]); |
375 |
dest_map[index] = temp; |
|
376 |
} |
|
377 |
return changed; |
|
378 |
} |
|
379 |
||
380 |
||
381 |
bool BitMap::set_intersection_with_result(BitMap other) { |
|
382 |
assert(size() == other.size(), "must have same size"); |
|
383 |
bool changed = false; |
|
1374 | 384 |
bm_word_t* dest_map = map(); |
385 |
bm_word_t* other_map = other.map(); |
|
1 | 386 |
idx_t size = size_in_words(); |
387 |
for (idx_t index = 0; index < size; index++) { |
|
1374 | 388 |
bm_word_t orig = dest_map[index]; |
389 |
bm_word_t temp = orig & other_map[index]; |
|
1 | 390 |
changed = changed || (temp != orig); |
391 |
dest_map[index] = temp; |
|
392 |
} |
|
393 |
return changed; |
|
394 |
} |
|
395 |
||
396 |
||
397 |
void BitMap::set_from(BitMap other) { |
|
398 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 399 |
bm_word_t* dest_map = map(); |
400 |
bm_word_t* other_map = other.map(); |
|
1 | 401 |
idx_t size = size_in_words(); |
402 |
for (idx_t index = 0; index < size; index++) { |
|
403 |
dest_map[index] = other_map[index]; |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
||
408 |
bool BitMap::is_same(BitMap other) { |
|
409 |
assert(size() == other.size(), "must have same size"); |
|
1374 | 410 |
bm_word_t* dest_map = map(); |
411 |
bm_word_t* other_map = other.map(); |
|
1 | 412 |
idx_t size = size_in_words(); |
413 |
for (idx_t index = 0; index < size; index++) { |
|
414 |
if (dest_map[index] != other_map[index]) return false; |
|
415 |
} |
|
416 |
return true; |
|
417 |
} |
|
418 |
||
419 |
bool BitMap::is_full() const { |
|
1374 | 420 |
bm_word_t* word = map(); |
1 | 421 |
idx_t rest = size(); |
422 |
for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) { |
|
1374 | 423 |
if (*word != (bm_word_t) AllBits) return false; |
1 | 424 |
word++; |
425 |
} |
|
1374 | 426 |
return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits; |
1 | 427 |
} |
428 |
||
429 |
||
430 |
bool BitMap::is_empty() const { |
|
1374 | 431 |
bm_word_t* word = map(); |
1 | 432 |
idx_t rest = size(); |
433 |
for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) { |
|
1374 | 434 |
if (*word != (bm_word_t) NoBits) return false; |
1 | 435 |
word++; |
436 |
} |
|
1374 | 437 |
return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits; |
1 | 438 |
} |
439 |
||
440 |
void BitMap::clear_large() { |
|
441 |
clear_large_range_of_words(0, size_in_words()); |
|
442 |
} |
|
443 |
||
444 |
// Note that if the closure itself modifies the bitmap |
|
445 |
// then modifications in and to the left of the _bit_ being |
|
446 |
// currently sampled will not be seen. Note also that the |
|
447 |
// interval [leftOffset, rightOffset) is right open. |
|
1374 | 448 |
bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) { |
1 | 449 |
verify_range(leftOffset, rightOffset); |
450 |
||
451 |
idx_t startIndex = word_index(leftOffset); |
|
452 |
idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words()); |
|
453 |
for (idx_t index = startIndex, offset = leftOffset; |
|
454 |
offset < rightOffset && index < endIndex; |
|
455 |
offset = (++index) << LogBitsPerWord) { |
|
456 |
idx_t rest = map(index) >> (offset & (BitsPerWord - 1)); |
|
1374 | 457 |
for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) { |
1 | 458 |
if (rest & 1) { |
1374 | 459 |
if (!blk->do_bit(offset)) return false; |
1 | 460 |
// resample at each closure application |
461 |
// (see, for instance, CMS bug 4525989) |
|
462 |
rest = map(index) >> (offset & (BitsPerWord -1)); |
|
463 |
} |
|
464 |
rest = rest >> 1; |
|
465 |
} |
|
466 |
} |
|
1374 | 467 |
return true; |
468 |
} |
|
469 |
||
470 |
BitMap::idx_t* BitMap::_pop_count_table = NULL; |
|
471 |
||
472 |
void BitMap::init_pop_count_table() { |
|
473 |
if (_pop_count_table == NULL) { |
|
13195 | 474 |
BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal); |
1374 | 475 |
for (uint i = 0; i < 256; i++) { |
476 |
table[i] = num_set_bits(i); |
|
477 |
} |
|
478 |
||
479 |
intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table, |
|
480 |
(intptr_t*) &_pop_count_table, |
|
481 |
(intptr_t) NULL_WORD); |
|
482 |
if (res != NULL_WORD) { |
|
483 |
guarantee( _pop_count_table == (void*) res, "invariant" ); |
|
13195 | 484 |
FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal); |
1374 | 485 |
} |
486 |
} |
|
1 | 487 |
} |
488 |
||
1374 | 489 |
BitMap::idx_t BitMap::num_set_bits(bm_word_t w) { |
490 |
idx_t bits = 0; |
|
1 | 491 |
|
1374 | 492 |
while (w != 0) { |
493 |
while ((w & 1) == 0) { |
|
494 |
w >>= 1; |
|
1 | 495 |
} |
1374 | 496 |
bits++; |
497 |
w >>= 1; |
|
1 | 498 |
} |
1374 | 499 |
return bits; |
1 | 500 |
} |
501 |
||
1374 | 502 |
BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) { |
503 |
assert(_pop_count_table != NULL, "precondition"); |
|
504 |
return _pop_count_table[c]; |
|
505 |
} |
|
1 | 506 |
|
1374 | 507 |
BitMap::idx_t BitMap::count_one_bits() const { |
508 |
init_pop_count_table(); // If necessary. |
|
509 |
idx_t sum = 0; |
|
510 |
typedef unsigned char uchar; |
|
511 |
for (idx_t i = 0; i < size_in_words(); i++) { |
|
512 |
bm_word_t w = map()[i]; |
|
513 |
for (size_t j = 0; j < sizeof(bm_word_t); j++) { |
|
514 |
sum += num_set_bits_from_table(uchar(w & 255)); |
|
515 |
w >>= 8; |
|
1 | 516 |
} |
517 |
} |
|
1374 | 518 |
return sum; |
1 | 519 |
} |
520 |
||
16685
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
521 |
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
|
522 |
st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")", |
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
523 |
prefix, map(), (char*)map() + (size() >> LogBitsPerByte)); |
41c34debcde0
8011872: Include Bit Map addresses in the hs_err files
stefank
parents:
13195
diff
changeset
|
524 |
} |
1374 | 525 |
|
1 | 526 |
#ifndef PRODUCT |
527 |
||
528 |
void BitMap::print_on(outputStream* st) const { |
|
529 |
tty->print("Bitmap(%d):", size()); |
|
530 |
for (idx_t index = 0; index < size(); index++) { |
|
531 |
tty->print("%c", at(index) ? '1' : '0'); |
|
532 |
} |
|
533 |
tty->cr(); |
|
534 |
} |
|
535 |
||
536 |
#endif |
|
537 |
||
538 |
||
1374 | 539 |
BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot) |
1 | 540 |
: _bits_per_slot(bits_per_slot) |
541 |
, _map(map, size_in_slots * bits_per_slot) |
|
542 |
{ |
|
543 |
} |
|
544 |
||
545 |
||
546 |
BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) |
|
547 |
: _bits_per_slot(bits_per_slot) |
|
548 |
, _map(size_in_slots * bits_per_slot) |
|
549 |
{ |
|
550 |
} |