src/share/vm/utilities/bitMap.cpp

Tue, 12 Feb 2019 11:58:44 +0100

author
shade
date
Tue, 12 Feb 2019 11:58:44 +0100
changeset 9616
faa71d8b8ab5
parent 6680
78bbf4d43a14
child 9637
eef07cd490d4
permissions
-rw-r--r--

8211926: Catastrophic size_t underflow in BitMap::*_large methods
Reviewed-by: kbarrett, stuefe

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "memory/allocation.inline.hpp"
stefank@2314 27 #include "utilities/bitMap.inline.hpp"
stefank@2314 28 #include "utilities/copy.hpp"
stefank@2314 29 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 30 # include "os_linux.inline.hpp"
stefank@2314 31 #endif
stefank@2314 32 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 33 # include "os_solaris.inline.hpp"
stefank@2314 34 #endif
stefank@2314 35 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 36 # include "os_windows.inline.hpp"
stefank@2314 37 #endif
goetz@6461 38 #ifdef TARGET_OS_FAMILY_aix
goetz@6461 39 # include "os_aix.inline.hpp"
goetz@6461 40 #endif
never@3156 41 #ifdef TARGET_OS_FAMILY_bsd
never@3156 42 # include "os_bsd.inline.hpp"
never@3156 43 #endif
duke@435 44
duke@435 45
ysr@777 46 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
brutisso@5278 47 _map(map), _size(size_in_bits), _map_allocator(false)
ysr@777 48 {
ysr@777 49 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
duke@435 50 assert(size_in_bits >= 0, "just checking");
duke@435 51 }
duke@435 52
duke@435 53
ysr@777 54 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
brutisso@5278 55 _map(NULL), _size(0), _map_allocator(false)
ysr@777 56 {
ysr@777 57 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
ysr@777 58 resize(size_in_bits, in_resource_area);
duke@435 59 }
duke@435 60
ysr@777 61 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
duke@435 62 assert(size_in_bits >= 0, "just checking");
ysr@777 63 idx_t old_size_in_words = size_in_words();
ysr@777 64 bm_word_t* old_map = map();
ysr@777 65
duke@435 66 _size = size_in_bits;
ysr@777 67 idx_t new_size_in_words = size_in_words();
ysr@777 68 if (in_resource_area) {
ysr@777 69 _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
ysr@777 70 } else {
brutisso@5278 71 if (old_map != NULL) {
brutisso@5278 72 _map_allocator.free();
brutisso@5278 73 }
brutisso@5278 74 _map = _map_allocator.allocate(new_size_in_words);
ysr@777 75 }
ysr@777 76 Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
ysr@777 77 MIN2(old_size_in_words, new_size_in_words));
duke@435 78 if (new_size_in_words > old_size_in_words) {
duke@435 79 clear_range_of_words(old_size_in_words, size_in_words());
duke@435 80 }
duke@435 81 }
duke@435 82
duke@435 83 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
duke@435 84 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 85 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 86 if (beg != end) {
ysr@777 87 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
duke@435 88 *word_addr(beg) |= ~mask;
duke@435 89 }
duke@435 90 }
duke@435 91
duke@435 92 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
duke@435 93 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 94 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 95 if (beg != end) {
ysr@777 96 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
duke@435 97 *word_addr(beg) &= mask;
duke@435 98 }
duke@435 99 }
duke@435 100
duke@435 101 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
duke@435 102 assert(value == 0 || value == 1, "0 for clear, 1 for set");
duke@435 103 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 104 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 105 if (beg != end) {
duke@435 106 intptr_t* pw = (intptr_t*)word_addr(beg);
duke@435 107 intptr_t w = *pw;
duke@435 108 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
duke@435 109 intptr_t nw = value ? (w | ~mr) : (w & mr);
duke@435 110 while (true) {
duke@435 111 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
duke@435 112 if (res == w) break;
tschatzl@6304 113 w = res;
duke@435 114 nw = value ? (w | ~mr) : (w & mr);
duke@435 115 }
duke@435 116 }
duke@435 117 }
duke@435 118
duke@435 119 void BitMap::set_range(idx_t beg, idx_t end) {
duke@435 120 verify_range(beg, end);
duke@435 121
duke@435 122 idx_t beg_full_word = word_index_round_up(beg);
duke@435 123 idx_t end_full_word = word_index(end);
duke@435 124
duke@435 125 if (beg_full_word < end_full_word) {
duke@435 126 // The range includes at least one full word.
duke@435 127 set_range_within_word(beg, bit_index(beg_full_word));
duke@435 128 set_range_of_words(beg_full_word, end_full_word);
duke@435 129 set_range_within_word(bit_index(end_full_word), end);
duke@435 130 } else {
duke@435 131 // The range spans at most 2 partial words.
duke@435 132 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 133 set_range_within_word(beg, boundary);
duke@435 134 set_range_within_word(boundary, end);
duke@435 135 }
duke@435 136 }
duke@435 137
duke@435 138 void BitMap::clear_range(idx_t beg, idx_t end) {
duke@435 139 verify_range(beg, end);
duke@435 140
duke@435 141 idx_t beg_full_word = word_index_round_up(beg);
duke@435 142 idx_t end_full_word = word_index(end);
duke@435 143
duke@435 144 if (beg_full_word < end_full_word) {
duke@435 145 // The range includes at least one full word.
duke@435 146 clear_range_within_word(beg, bit_index(beg_full_word));
duke@435 147 clear_range_of_words(beg_full_word, end_full_word);
duke@435 148 clear_range_within_word(bit_index(end_full_word), end);
duke@435 149 } else {
duke@435 150 // The range spans at most 2 partial words.
duke@435 151 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 152 clear_range_within_word(beg, boundary);
duke@435 153 clear_range_within_word(boundary, end);
duke@435 154 }
duke@435 155 }
duke@435 156
shade@9616 157 bool BitMap::is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word) {
shade@9616 158 // There is little point to call large version on small ranges.
shade@9616 159 // Need to check carefully, keeping potential idx_t underflow in mind.
shade@9616 160 // The threshold should be at least one word.
shade@9616 161 STATIC_ASSERT(small_range_words >= 1);
shade@9616 162 return (beg_full_word + small_range_words >= end_full_word);
shade@9616 163 }
shade@9616 164
duke@435 165 void BitMap::set_large_range(idx_t beg, idx_t end) {
duke@435 166 verify_range(beg, end);
duke@435 167
duke@435 168 idx_t beg_full_word = word_index_round_up(beg);
duke@435 169 idx_t end_full_word = word_index(end);
duke@435 170
shade@9616 171 if (is_small_range_of_words(beg_full_word, end_full_word)) {
shade@9616 172 set_range(beg, end);
shade@9616 173 return;
shade@9616 174 }
duke@435 175
duke@435 176 // The range includes at least one full word.
duke@435 177 set_range_within_word(beg, bit_index(beg_full_word));
duke@435 178 set_large_range_of_words(beg_full_word, end_full_word);
duke@435 179 set_range_within_word(bit_index(end_full_word), end);
duke@435 180 }
duke@435 181
duke@435 182 void BitMap::clear_large_range(idx_t beg, idx_t end) {
duke@435 183 verify_range(beg, end);
duke@435 184
duke@435 185 idx_t beg_full_word = word_index_round_up(beg);
duke@435 186 idx_t end_full_word = word_index(end);
duke@435 187
shade@9616 188 if (is_small_range_of_words(beg_full_word, end_full_word)) {
shade@9616 189 clear_range(beg, end);
shade@9616 190 return;
shade@9616 191 }
duke@435 192
duke@435 193 // The range includes at least one full word.
duke@435 194 clear_range_within_word(beg, bit_index(beg_full_word));
duke@435 195 clear_large_range_of_words(beg_full_word, end_full_word);
duke@435 196 clear_range_within_word(bit_index(end_full_word), end);
duke@435 197 }
duke@435 198
duke@435 199 void BitMap::at_put(idx_t offset, bool value) {
duke@435 200 if (value) {
duke@435 201 set_bit(offset);
duke@435 202 } else {
duke@435 203 clear_bit(offset);
duke@435 204 }
duke@435 205 }
duke@435 206
duke@435 207 // Return true to indicate that this thread changed
duke@435 208 // the bit, false to indicate that someone else did.
duke@435 209 // In either case, the requested bit is in the
duke@435 210 // requested state some time during the period that
duke@435 211 // this thread is executing this call. More importantly,
duke@435 212 // if no other thread is executing an action to
duke@435 213 // change the requested bit to a state other than
duke@435 214 // the one that this thread is trying to set it to,
duke@435 215 // then the the bit is in the expected state
duke@435 216 // at exit from this method. However, rather than
duke@435 217 // make such a strong assertion here, based on
duke@435 218 // assuming such constrained use (which though true
duke@435 219 // today, could change in the future to service some
duke@435 220 // funky parallel algorithm), we encourage callers
duke@435 221 // to do such verification, as and when appropriate.
duke@435 222 bool BitMap::par_at_put(idx_t bit, bool value) {
duke@435 223 return value ? par_set_bit(bit) : par_clear_bit(bit);
duke@435 224 }
duke@435 225
duke@435 226 void BitMap::at_put_grow(idx_t offset, bool value) {
duke@435 227 if (offset >= size()) {
duke@435 228 resize(2 * MAX2(size(), offset));
duke@435 229 }
duke@435 230 at_put(offset, value);
duke@435 231 }
duke@435 232
duke@435 233 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
duke@435 234 if (value) {
duke@435 235 set_range(start_offset, end_offset);
duke@435 236 } else {
duke@435 237 clear_range(start_offset, end_offset);
duke@435 238 }
duke@435 239 }
duke@435 240
duke@435 241 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
duke@435 242 verify_range(beg, end);
duke@435 243
duke@435 244 idx_t beg_full_word = word_index_round_up(beg);
duke@435 245 idx_t end_full_word = word_index(end);
duke@435 246
duke@435 247 if (beg_full_word < end_full_word) {
duke@435 248 // The range includes at least one full word.
duke@435 249 par_put_range_within_word(beg, bit_index(beg_full_word), value);
duke@435 250 if (value) {
duke@435 251 set_range_of_words(beg_full_word, end_full_word);
duke@435 252 } else {
duke@435 253 clear_range_of_words(beg_full_word, end_full_word);
duke@435 254 }
duke@435 255 par_put_range_within_word(bit_index(end_full_word), end, value);
duke@435 256 } else {
duke@435 257 // The range spans at most 2 partial words.
duke@435 258 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 259 par_put_range_within_word(beg, boundary, value);
duke@435 260 par_put_range_within_word(boundary, end, value);
duke@435 261 }
duke@435 262
duke@435 263 }
duke@435 264
duke@435 265 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
duke@435 266 if (value) {
duke@435 267 set_large_range(beg, end);
duke@435 268 } else {
duke@435 269 clear_large_range(beg, end);
duke@435 270 }
duke@435 271 }
duke@435 272
duke@435 273 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
duke@435 274 verify_range(beg, end);
duke@435 275
duke@435 276 idx_t beg_full_word = word_index_round_up(beg);
duke@435 277 idx_t end_full_word = word_index(end);
duke@435 278
shade@9616 279 if (is_small_range_of_words(beg_full_word, end_full_word)) {
shade@9616 280 par_at_put_range(beg, end, value);
shade@9616 281 return;
shade@9616 282 }
duke@435 283
duke@435 284 // The range includes at least one full word.
duke@435 285 par_put_range_within_word(beg, bit_index(beg_full_word), value);
duke@435 286 if (value) {
duke@435 287 set_large_range_of_words(beg_full_word, end_full_word);
duke@435 288 } else {
duke@435 289 clear_large_range_of_words(beg_full_word, end_full_word);
duke@435 290 }
duke@435 291 par_put_range_within_word(bit_index(end_full_word), end, value);
duke@435 292 }
duke@435 293
duke@435 294 bool BitMap::contains(const BitMap other) const {
duke@435 295 assert(size() == other.size(), "must have same size");
ysr@777 296 bm_word_t* dest_map = map();
ysr@777 297 bm_word_t* other_map = other.map();
duke@435 298 idx_t size = size_in_words();
duke@435 299 for (idx_t index = 0; index < size_in_words(); index++) {
ysr@777 300 bm_word_t word_union = dest_map[index] | other_map[index];
duke@435 301 // If this has more bits set than dest_map[index], then other is not a
duke@435 302 // subset.
duke@435 303 if (word_union != dest_map[index]) return false;
duke@435 304 }
duke@435 305 return true;
duke@435 306 }
duke@435 307
duke@435 308 bool BitMap::intersects(const BitMap other) const {
duke@435 309 assert(size() == other.size(), "must have same size");
ysr@777 310 bm_word_t* dest_map = map();
ysr@777 311 bm_word_t* other_map = other.map();
duke@435 312 idx_t size = size_in_words();
duke@435 313 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 314 if ((dest_map[index] & other_map[index]) != 0) return true;
duke@435 315 }
duke@435 316 // Otherwise, no intersection.
duke@435 317 return false;
duke@435 318 }
duke@435 319
duke@435 320 void BitMap::set_union(BitMap other) {
duke@435 321 assert(size() == other.size(), "must have same size");
ysr@777 322 bm_word_t* dest_map = map();
ysr@777 323 bm_word_t* other_map = other.map();
duke@435 324 idx_t size = size_in_words();
duke@435 325 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 326 dest_map[index] = dest_map[index] | other_map[index];
duke@435 327 }
duke@435 328 }
duke@435 329
duke@435 330
duke@435 331 void BitMap::set_difference(BitMap other) {
duke@435 332 assert(size() == other.size(), "must have same size");
ysr@777 333 bm_word_t* dest_map = map();
ysr@777 334 bm_word_t* other_map = other.map();
duke@435 335 idx_t size = size_in_words();
duke@435 336 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 337 dest_map[index] = dest_map[index] & ~(other_map[index]);
duke@435 338 }
duke@435 339 }
duke@435 340
duke@435 341
duke@435 342 void BitMap::set_intersection(BitMap other) {
duke@435 343 assert(size() == other.size(), "must have same size");
ysr@777 344 bm_word_t* dest_map = map();
ysr@777 345 bm_word_t* other_map = other.map();
duke@435 346 idx_t size = size_in_words();
duke@435 347 for (idx_t index = 0; index < size; index++) {
duke@435 348 dest_map[index] = dest_map[index] & other_map[index];
duke@435 349 }
duke@435 350 }
duke@435 351
duke@435 352
ysr@777 353 void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
ysr@777 354 assert(other.size() >= offset, "offset not in range");
ysr@777 355 assert(other.size() - offset >= size(), "other not large enough");
ysr@777 356 // XXX Ideally, we would remove this restriction.
ysr@777 357 guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
ysr@777 358 "Only handle aligned cases so far.");
ysr@777 359 bm_word_t* dest_map = map();
ysr@777 360 bm_word_t* other_map = other.map();
ysr@777 361 idx_t offset_word_ind = word_index(offset);
ysr@777 362 idx_t size = size_in_words();
ysr@777 363 for (idx_t index = 0; index < size; index++) {
ysr@777 364 dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
ysr@777 365 }
ysr@777 366 }
ysr@777 367
duke@435 368 bool BitMap::set_union_with_result(BitMap other) {
duke@435 369 assert(size() == other.size(), "must have same size");
duke@435 370 bool changed = false;
ysr@777 371 bm_word_t* dest_map = map();
ysr@777 372 bm_word_t* other_map = other.map();
duke@435 373 idx_t size = size_in_words();
duke@435 374 for (idx_t index = 0; index < size; index++) {
duke@435 375 idx_t temp = map(index) | other_map[index];
duke@435 376 changed = changed || (temp != map(index));
duke@435 377 map()[index] = temp;
duke@435 378 }
duke@435 379 return changed;
duke@435 380 }
duke@435 381
duke@435 382
duke@435 383 bool BitMap::set_difference_with_result(BitMap other) {
duke@435 384 assert(size() == other.size(), "must have same size");
duke@435 385 bool changed = false;
ysr@777 386 bm_word_t* dest_map = map();
ysr@777 387 bm_word_t* other_map = other.map();
duke@435 388 idx_t size = size_in_words();
duke@435 389 for (idx_t index = 0; index < size; index++) {
ysr@777 390 bm_word_t temp = dest_map[index] & ~(other_map[index]);
duke@435 391 changed = changed || (temp != dest_map[index]);
duke@435 392 dest_map[index] = temp;
duke@435 393 }
duke@435 394 return changed;
duke@435 395 }
duke@435 396
duke@435 397
duke@435 398 bool BitMap::set_intersection_with_result(BitMap other) {
duke@435 399 assert(size() == other.size(), "must have same size");
duke@435 400 bool changed = false;
ysr@777 401 bm_word_t* dest_map = map();
ysr@777 402 bm_word_t* other_map = other.map();
duke@435 403 idx_t size = size_in_words();
duke@435 404 for (idx_t index = 0; index < size; index++) {
ysr@777 405 bm_word_t orig = dest_map[index];
ysr@777 406 bm_word_t temp = orig & other_map[index];
duke@435 407 changed = changed || (temp != orig);
duke@435 408 dest_map[index] = temp;
duke@435 409 }
duke@435 410 return changed;
duke@435 411 }
duke@435 412
duke@435 413
duke@435 414 void BitMap::set_from(BitMap other) {
duke@435 415 assert(size() == other.size(), "must have same size");
ysr@777 416 bm_word_t* dest_map = map();
ysr@777 417 bm_word_t* other_map = other.map();
duke@435 418 idx_t size = size_in_words();
duke@435 419 for (idx_t index = 0; index < size; index++) {
duke@435 420 dest_map[index] = other_map[index];
duke@435 421 }
duke@435 422 }
duke@435 423
duke@435 424
duke@435 425 bool BitMap::is_same(BitMap other) {
duke@435 426 assert(size() == other.size(), "must have same size");
ysr@777 427 bm_word_t* dest_map = map();
ysr@777 428 bm_word_t* other_map = other.map();
duke@435 429 idx_t size = size_in_words();
duke@435 430 for (idx_t index = 0; index < size; index++) {
duke@435 431 if (dest_map[index] != other_map[index]) return false;
duke@435 432 }
duke@435 433 return true;
duke@435 434 }
duke@435 435
duke@435 436 bool BitMap::is_full() const {
ysr@777 437 bm_word_t* word = map();
duke@435 438 idx_t rest = size();
duke@435 439 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
ysr@777 440 if (*word != (bm_word_t) AllBits) return false;
duke@435 441 word++;
duke@435 442 }
ysr@777 443 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
duke@435 444 }
duke@435 445
duke@435 446
duke@435 447 bool BitMap::is_empty() const {
ysr@777 448 bm_word_t* word = map();
duke@435 449 idx_t rest = size();
duke@435 450 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
ysr@777 451 if (*word != (bm_word_t) NoBits) return false;
duke@435 452 word++;
duke@435 453 }
ysr@777 454 return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
duke@435 455 }
duke@435 456
duke@435 457 void BitMap::clear_large() {
duke@435 458 clear_large_range_of_words(0, size_in_words());
duke@435 459 }
duke@435 460
duke@435 461 // Note that if the closure itself modifies the bitmap
duke@435 462 // then modifications in and to the left of the _bit_ being
duke@435 463 // currently sampled will not be seen. Note also that the
duke@435 464 // interval [leftOffset, rightOffset) is right open.
ysr@777 465 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
duke@435 466 verify_range(leftOffset, rightOffset);
duke@435 467
duke@435 468 idx_t startIndex = word_index(leftOffset);
duke@435 469 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
duke@435 470 for (idx_t index = startIndex, offset = leftOffset;
duke@435 471 offset < rightOffset && index < endIndex;
duke@435 472 offset = (++index) << LogBitsPerWord) {
duke@435 473 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
ysr@777 474 for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
duke@435 475 if (rest & 1) {
ysr@777 476 if (!blk->do_bit(offset)) return false;
duke@435 477 // resample at each closure application
duke@435 478 // (see, for instance, CMS bug 4525989)
duke@435 479 rest = map(index) >> (offset & (BitsPerWord -1));
duke@435 480 }
duke@435 481 rest = rest >> 1;
duke@435 482 }
duke@435 483 }
ysr@777 484 return true;
duke@435 485 }
duke@435 486
ysr@777 487 BitMap::idx_t* BitMap::_pop_count_table = NULL;
duke@435 488
ysr@777 489 void BitMap::init_pop_count_table() {
ysr@777 490 if (_pop_count_table == NULL) {
zgu@3900 491 BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
ysr@777 492 for (uint i = 0; i < 256; i++) {
ysr@777 493 table[i] = num_set_bits(i);
ysr@777 494 }
duke@435 495
ysr@777 496 intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table,
ysr@777 497 (intptr_t*) &_pop_count_table,
ysr@777 498 (intptr_t) NULL_WORD);
ysr@777 499 if (res != NULL_WORD) {
ysr@777 500 guarantee( _pop_count_table == (void*) res, "invariant" );
zgu@3900 501 FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
duke@435 502 }
duke@435 503 }
duke@435 504 }
duke@435 505
ysr@777 506 BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
ysr@777 507 idx_t bits = 0;
duke@435 508
ysr@777 509 while (w != 0) {
ysr@777 510 while ((w & 1) == 0) {
ysr@777 511 w >>= 1;
ysr@777 512 }
ysr@777 513 bits++;
ysr@777 514 w >>= 1;
duke@435 515 }
ysr@777 516 return bits;
ysr@777 517 }
duke@435 518
ysr@777 519 BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
ysr@777 520 assert(_pop_count_table != NULL, "precondition");
ysr@777 521 return _pop_count_table[c];
ysr@777 522 }
duke@435 523
ysr@777 524 BitMap::idx_t BitMap::count_one_bits() const {
ysr@777 525 init_pop_count_table(); // If necessary.
ysr@777 526 idx_t sum = 0;
ysr@777 527 typedef unsigned char uchar;
ysr@777 528 for (idx_t i = 0; i < size_in_words(); i++) {
ysr@777 529 bm_word_t w = map()[i];
ysr@777 530 for (size_t j = 0; j < sizeof(bm_word_t); j++) {
ysr@777 531 sum += num_set_bits_from_table(uchar(w & 255));
ysr@777 532 w >>= 8;
duke@435 533 }
duke@435 534 }
ysr@777 535 return sum;
duke@435 536 }
duke@435 537
stefank@4904 538 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
stefank@4904 539 st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
drchase@6680 540 prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte)));
stefank@4904 541 }
ysr@777 542
duke@435 543 #ifndef PRODUCT
duke@435 544
duke@435 545 void BitMap::print_on(outputStream* st) const {
drchase@6680 546 tty->print("Bitmap(" SIZE_FORMAT "):", size());
duke@435 547 for (idx_t index = 0; index < size(); index++) {
duke@435 548 tty->print("%c", at(index) ? '1' : '0');
duke@435 549 }
duke@435 550 tty->cr();
duke@435 551 }
duke@435 552
duke@435 553 #endif
duke@435 554
duke@435 555
ysr@777 556 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
duke@435 557 : _bits_per_slot(bits_per_slot)
duke@435 558 , _map(map, size_in_slots * bits_per_slot)
duke@435 559 {
duke@435 560 }
duke@435 561
duke@435 562
duke@435 563 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
duke@435 564 : _bits_per_slot(bits_per_slot)
duke@435 565 , _map(size_in_slots * bits_per_slot)
duke@435 566 {
duke@435 567 }

mercurial