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