bitMap.cpp 18.3 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

# include "incls/_precompiled.incl"
# include "incls/_bitMap.cpp.incl"


29 30 31 32
BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
  _map(map), _size(size_in_bits)
{
  assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
D
duke 已提交
33 34 35 36
  assert(size_in_bits >= 0, "just checking");
}


37 38 39 40 41
BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
  _map(NULL), _size(0)
{
  assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
  resize(size_in_bits, in_resource_area);
D
duke 已提交
42 43
}

44
void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
D
duke 已提交
45
  assert(size_in_bits >= 0, "just checking");
46 47 48
  idx_t old_size_in_words = size_in_words();
  bm_word_t* old_map = map();

D
duke 已提交
49
  _size = size_in_bits;
50 51 52 53 54 55 56 57 58
  idx_t new_size_in_words = size_in_words();
  if (in_resource_area) {
    _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
  } else {
    if (old_map != NULL) FREE_C_HEAP_ARRAY(bm_word_t, _map);
    _map = NEW_C_HEAP_ARRAY(bm_word_t, new_size_in_words);
  }
  Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
                       MIN2(old_size_in_words, new_size_in_words));
D
duke 已提交
59 60 61 62 63 64 65 66 67
  if (new_size_in_words > old_size_in_words) {
    clear_range_of_words(old_size_in_words, size_in_words());
  }
}

void BitMap::set_range_within_word(idx_t beg, idx_t end) {
  // With a valid range (beg <= end), this test ensures that end != 0, as
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  if (beg != end) {
68
    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
D
duke 已提交
69 70 71 72 73 74 75 76
    *word_addr(beg) |= ~mask;
  }
}

void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
  // With a valid range (beg <= end), this test ensures that end != 0, as
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  if (beg != end) {
77
    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
D
duke 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    *word_addr(beg) &= mask;
  }
}

void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
  assert(value == 0 || value == 1, "0 for clear, 1 for set");
  // With a valid range (beg <= end), this test ensures that end != 0, as
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  if (beg != end) {
    intptr_t* pw  = (intptr_t*)word_addr(beg);
    intptr_t  w   = *pw;
    intptr_t  mr  = (intptr_t)inverted_bit_mask_for_range(beg, end);
    intptr_t  nw  = value ? (w | ~mr) : (w & mr);
    while (true) {
      intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
      if (res == w) break;
      w  = *pw;
      nw = value ? (w | ~mr) : (w & mr);
    }
  }
}

void BitMap::set_range(idx_t beg, idx_t end) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  if (beg_full_word < end_full_word) {
    // The range includes at least one full word.
    set_range_within_word(beg, bit_index(beg_full_word));
    set_range_of_words(beg_full_word, end_full_word);
    set_range_within_word(bit_index(end_full_word), end);
  } else {
    // The range spans at most 2 partial words.
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
    set_range_within_word(beg, boundary);
    set_range_within_word(boundary, end);
  }
}

void BitMap::clear_range(idx_t beg, idx_t end) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  if (beg_full_word < end_full_word) {
    // The range includes at least one full word.
    clear_range_within_word(beg, bit_index(beg_full_word));
    clear_range_of_words(beg_full_word, end_full_word);
    clear_range_within_word(bit_index(end_full_word), end);
  } else {
    // The range spans at most 2 partial words.
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
    clear_range_within_word(beg, boundary);
    clear_range_within_word(boundary, end);
  }
}

void BitMap::set_large_range(idx_t beg, idx_t end) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  assert(end_full_word - beg_full_word >= 32,
         "the range must include at least 32 bytes");

  // The range includes at least one full word.
  set_range_within_word(beg, bit_index(beg_full_word));
  set_large_range_of_words(beg_full_word, end_full_word);
  set_range_within_word(bit_index(end_full_word), end);
}

void BitMap::clear_large_range(idx_t beg, idx_t end) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  assert(end_full_word - beg_full_word >= 32,
         "the range must include at least 32 bytes");

  // The range includes at least one full word.
  clear_range_within_word(beg, bit_index(beg_full_word));
  clear_large_range_of_words(beg_full_word, end_full_word);
  clear_range_within_word(bit_index(end_full_word), end);
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
void BitMap::mostly_disjoint_range_union(BitMap* from_bitmap,
                                         idx_t   from_start_index,
                                         idx_t   to_start_index,
                                         size_t  word_num) {
  // Ensure that the parameters are correct.
  // These shouldn't be that expensive to check, hence I left them as
  // guarantees.
  guarantee(from_bitmap->bit_in_word(from_start_index) == 0,
            "it should be aligned on a word boundary");
  guarantee(bit_in_word(to_start_index) == 0,
            "it should be aligned on a word boundary");
  guarantee(word_num >= 2, "word_num should be at least 2");

  intptr_t* from = (intptr_t*) from_bitmap->word_addr(from_start_index);
  intptr_t* to   = (intptr_t*) word_addr(to_start_index);

  if (*from != 0) {
    // if it's 0, then there's no point in doing the CAS
    while (true) {
      intptr_t old_value = *to;
      intptr_t new_value = old_value | *from;
      intptr_t res       = Atomic::cmpxchg_ptr(new_value, to, old_value);
      if (res == old_value) break;
    }
  }
  ++from;
  ++to;

  for (size_t i = 0; i < word_num - 2; ++i) {
    if (*from != 0) {
      // if it's 0, then there's no point in doing the CAS
      assert(*to == 0, "nobody else should be writing here");
      intptr_t new_value = *from;
      *to = new_value;
    }

    ++from;
    ++to;
  }

  if (*from != 0) {
    // if it's 0, then there's no point in doing the CAS
    while (true) {
      intptr_t old_value = *to;
      intptr_t new_value = old_value | *from;
      intptr_t res       = Atomic::cmpxchg_ptr(new_value, to, old_value);
      if (res == old_value) break;
    }
  }

  // the -1 is because we didn't advance them after the final CAS
  assert(from ==
           (intptr_t*) from_bitmap->word_addr(from_start_index) + word_num - 1,
            "invariant");
  assert(to == (intptr_t*) word_addr(to_start_index) + word_num - 1,
            "invariant");
}

D
duke 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
void BitMap::at_put(idx_t offset, bool value) {
  if (value) {
    set_bit(offset);
  } else {
    clear_bit(offset);
  }
}

// Return true to indicate that this thread changed
// the bit, false to indicate that someone else did.
// In either case, the requested bit is in the
// requested state some time during the period that
// this thread is executing this call. More importantly,
// if no other thread is executing an action to
// change the requested bit to a state other than
// the one that this thread is trying to set it to,
// then the the bit is in the expected state
// at exit from this method. However, rather than
// make such a strong assertion here, based on
// assuming such constrained use (which though true
// today, could change in the future to service some
// funky parallel algorithm), we encourage callers
// to do such verification, as and when appropriate.
bool BitMap::par_at_put(idx_t bit, bool value) {
  return value ? par_set_bit(bit) : par_clear_bit(bit);
}

void BitMap::at_put_grow(idx_t offset, bool value) {
  if (offset >= size()) {
    resize(2 * MAX2(size(), offset));
  }
  at_put(offset, value);
}

void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
  if (value) {
    set_range(start_offset, end_offset);
  } else {
    clear_range(start_offset, end_offset);
  }
}

void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  if (beg_full_word < end_full_word) {
    // The range includes at least one full word.
    par_put_range_within_word(beg, bit_index(beg_full_word), value);
    if (value) {
      set_range_of_words(beg_full_word, end_full_word);
    } else {
      clear_range_of_words(beg_full_word, end_full_word);
    }
    par_put_range_within_word(bit_index(end_full_word), end, value);
  } else {
    // The range spans at most 2 partial words.
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
    par_put_range_within_word(beg, boundary, value);
    par_put_range_within_word(boundary, end, value);
  }

}

void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
  if (value) {
    set_large_range(beg, end);
  } else {
    clear_large_range(beg, end);
  }
}

void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
  verify_range(beg, end);

  idx_t beg_full_word = word_index_round_up(beg);
  idx_t end_full_word = word_index(end);

  assert(end_full_word - beg_full_word >= 32,
         "the range must include at least 32 bytes");

  // The range includes at least one full word.
  par_put_range_within_word(beg, bit_index(beg_full_word), value);
  if (value) {
    set_large_range_of_words(beg_full_word, end_full_word);
  } else {
    clear_large_range_of_words(beg_full_word, end_full_word);
  }
  par_put_range_within_word(bit_index(end_full_word), end, value);
}

bool BitMap::contains(const BitMap other) const {
  assert(size() == other.size(), "must have same size");
321 322
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
323 324
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size_in_words(); index++) {
325
    bm_word_t word_union = dest_map[index] | other_map[index];
D
duke 已提交
326 327 328 329 330 331 332 333 334
    // If this has more bits set than dest_map[index], then other is not a
    // subset.
    if (word_union != dest_map[index]) return false;
  }
  return true;
}

bool BitMap::intersects(const BitMap other) const {
  assert(size() == other.size(), "must have same size");
335 336
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
337 338 339 340 341 342 343 344 345 346
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size_in_words(); index++) {
    if ((dest_map[index] & other_map[index]) != 0) return true;
  }
  // Otherwise, no intersection.
  return false;
}

void BitMap::set_union(BitMap other) {
  assert(size() == other.size(), "must have same size");
347 348
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
349 350 351 352 353 354 355 356 357
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size_in_words(); index++) {
    dest_map[index] = dest_map[index] | other_map[index];
  }
}


void BitMap::set_difference(BitMap other) {
  assert(size() == other.size(), "must have same size");
358 359
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
360 361 362 363 364 365 366 367 368
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size_in_words(); index++) {
    dest_map[index] = dest_map[index] & ~(other_map[index]);
  }
}


void BitMap::set_intersection(BitMap other) {
  assert(size() == other.size(), "must have same size");
369 370
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
371 372 373 374 375 376 377
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
    dest_map[index]  = dest_map[index] & other_map[index];
  }
}


378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
  assert(other.size() >= offset, "offset not in range");
  assert(other.size() - offset >= size(), "other not large enough");
  // XXX Ideally, we would remove this restriction.
  guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
            "Only handle aligned cases so far.");
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
  idx_t offset_word_ind = word_index(offset);
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
    dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
  }
}

D
duke 已提交
393 394 395
bool BitMap::set_union_with_result(BitMap other) {
  assert(size() == other.size(), "must have same size");
  bool changed = false;
396 397
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
    idx_t temp = map(index) | other_map[index];
    changed = changed || (temp != map(index));
    map()[index] = temp;
  }
  return changed;
}


bool BitMap::set_difference_with_result(BitMap other) {
  assert(size() == other.size(), "must have same size");
  bool changed = false;
411 412
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
413 414
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
415
    bm_word_t temp = dest_map[index] & ~(other_map[index]);
D
duke 已提交
416 417 418 419 420 421 422 423 424 425
    changed = changed || (temp != dest_map[index]);
    dest_map[index] = temp;
  }
  return changed;
}


bool BitMap::set_intersection_with_result(BitMap other) {
  assert(size() == other.size(), "must have same size");
  bool changed = false;
426 427
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
428 429
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
430 431
    bm_word_t orig = dest_map[index];
    bm_word_t temp = orig & other_map[index];
D
duke 已提交
432 433 434 435 436 437 438 439 440
    changed = changed || (temp != orig);
    dest_map[index]  = temp;
  }
  return changed;
}


void BitMap::set_from(BitMap other) {
  assert(size() == other.size(), "must have same size");
441 442
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
443 444 445 446 447 448 449 450 451
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
    dest_map[index] = other_map[index];
  }
}


bool BitMap::is_same(BitMap other) {
  assert(size() == other.size(), "must have same size");
452 453
  bm_word_t* dest_map = map();
  bm_word_t* other_map = other.map();
D
duke 已提交
454 455 456 457 458 459 460 461
  idx_t size = size_in_words();
  for (idx_t index = 0; index < size; index++) {
    if (dest_map[index] != other_map[index]) return false;
  }
  return true;
}

bool BitMap::is_full() const {
462
  bm_word_t* word = map();
D
duke 已提交
463 464
  idx_t rest = size();
  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
465
    if (*word != (bm_word_t) AllBits) return false;
D
duke 已提交
466 467
    word++;
  }
468
  return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
D
duke 已提交
469 470 471 472
}


bool BitMap::is_empty() const {
473
  bm_word_t* word = map();
D
duke 已提交
474 475
  idx_t rest = size();
  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
476
    if (*word != (bm_word_t) NoBits) return false;
D
duke 已提交
477 478
    word++;
  }
479
  return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
D
duke 已提交
480 481 482 483 484 485 486 487 488 489
}

void BitMap::clear_large() {
  clear_large_range_of_words(0, size_in_words());
}

// Note that if the closure itself modifies the bitmap
// then modifications in and to the left of the _bit_ being
// currently sampled will not be seen. Note also that the
// interval [leftOffset, rightOffset) is right open.
490
bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
D
duke 已提交
491 492 493 494 495 496 497 498
  verify_range(leftOffset, rightOffset);

  idx_t startIndex = word_index(leftOffset);
  idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
  for (idx_t index = startIndex, offset = leftOffset;
       offset < rightOffset && index < endIndex;
       offset = (++index) << LogBitsPerWord) {
    idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
499
    for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
D
duke 已提交
500
      if (rest & 1) {
501
        if (!blk->do_bit(offset)) return false;
D
duke 已提交
502 503 504 505 506 507 508
        //  resample at each closure application
        // (see, for instance, CMS bug 4525989)
        rest = map(index) >> (offset & (BitsPerWord -1));
      }
      rest = rest >> 1;
    }
  }
509
  return true;
D
duke 已提交
510 511
}

512
BitMap::idx_t* BitMap::_pop_count_table = NULL;
D
duke 已提交
513

514 515 516 517 518
void BitMap::init_pop_count_table() {
  if (_pop_count_table == NULL) {
    BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256);
    for (uint i = 0; i < 256; i++) {
      table[i] = num_set_bits(i);
D
duke 已提交
519
    }
520 521 522 523 524 525 526

    intptr_t res = Atomic::cmpxchg_ptr((intptr_t)  table,
                                       (intptr_t*) &_pop_count_table,
                                       (intptr_t)  NULL_WORD);
    if (res != NULL_WORD) {
      guarantee( _pop_count_table == (void*) res, "invariant" );
      FREE_C_HEAP_ARRAY(bm_word_t, table);
D
duke 已提交
527 528 529 530
    }
  }
}

531 532
BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
  idx_t bits = 0;
D
duke 已提交
533

534 535 536 537 538 539
  while (w != 0) {
    while ((w & 1) == 0) {
      w >>= 1;
    }
    bits++;
    w >>= 1;
D
duke 已提交
540
  }
541 542
  return bits;
}
D
duke 已提交
543

544 545 546 547
BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
  assert(_pop_count_table != NULL, "precondition");
  return _pop_count_table[c];
}
D
duke 已提交
548

549 550 551 552 553 554 555 556 557
BitMap::idx_t BitMap::count_one_bits() const {
  init_pop_count_table(); // If necessary.
  idx_t sum = 0;
  typedef unsigned char uchar;
  for (idx_t i = 0; i < size_in_words(); i++) {
    bm_word_t w = map()[i];
    for (size_t j = 0; j < sizeof(bm_word_t); j++) {
      sum += num_set_bits_from_table(uchar(w & 255));
      w >>= 8;
D
duke 已提交
558 559
    }
  }
560
  return sum;
D
duke 已提交
561 562
}

563

D
duke 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
#ifndef PRODUCT

void BitMap::print_on(outputStream* st) const {
  tty->print("Bitmap(%d):", size());
  for (idx_t index = 0; index < size(); index++) {
    tty->print("%c", at(index) ? '1' : '0');
  }
  tty->cr();
}

#endif


577
BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
D
duke 已提交
578 579 580 581 582 583 584 585 586 587 588
  : _bits_per_slot(bits_per_slot)
  , _map(map, size_in_slots * bits_per_slot)
{
}


BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
  : _bits_per_slot(bits_per_slot)
  , _map(size_in_slots * bits_per_slot)
{
}