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