hotspot/src/share/vm/utilities/bitMap.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 1374 4c24294029a9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
BitMap::BitMap(idx_t* map, idx_t size_in_bits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  assert(size_in_bits >= 0, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  _map = map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  _size = size_in_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
BitMap::BitMap(idx_t size_in_bits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  assert(size_in_bits >= 0, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  _size = size_in_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  _map = NEW_RESOURCE_ARRAY(idx_t, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
void BitMap::resize(idx_t size_in_bits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  assert(size_in_bits >= 0, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  size_t old_size_in_words = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  uintptr_t* old_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  _size = size_in_bits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  size_t new_size_in_words = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  _map = NEW_RESOURCE_ARRAY(idx_t, new_size_in_words);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  Copy::disjoint_words((HeapWord*) old_map, (HeapWord*) _map, MIN2(old_size_in_words, new_size_in_words));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  if (new_size_in_words > old_size_in_words) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    clear_range_of_words(old_size_in_words, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// Returns a bit mask for a range of bits [beg, end) within a single word.  Each
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// returned mask can be used directly to clear the range, or inverted to set the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// range.  Note:  end must not be 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
inline BitMap::idx_t
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  assert(end != 0, "does not work when end == 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  assert(beg == end || word_index(beg) == word_index(end - 1),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
         "must be a single-word range");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  idx_t mask = bit_mask(beg) - 1;       // low (right) bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  if (bit_in_word(end) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    mask |= ~(bit_mask(end) - 1);       // high (left) bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  return 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
void BitMap::set_range_within_word(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if (beg != end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    idx_t mask = inverted_bit_mask_for_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    *word_addr(beg) |= ~mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  if (beg != end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    idx_t mask = inverted_bit_mask_for_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    *word_addr(beg) &= mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  assert(value == 0 || value == 1, "0 for clear, 1 for set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  if (beg != end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    intptr_t* pw  = (intptr_t*)word_addr(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    intptr_t  w   = *pw;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    intptr_t  mr  = (intptr_t)inverted_bit_mask_for_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    intptr_t  nw  = value ? (w | ~mr) : (w & mr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      if (res == w) break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      w  = *pw;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      nw = value ? (w | ~mr) : (w & mr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  idx_t bit_rounded_up = bit + (BitsPerWord - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // Check for integer arithmetic overflow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
void BitMap::set_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    set_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    set_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    set_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    set_range_within_word(beg, boundary);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    set_range_within_word(boundary, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void BitMap::clear_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    clear_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    clear_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    clear_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    clear_range_within_word(beg, boundary);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    clear_range_within_word(boundary, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
void BitMap::set_large_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  assert(end_full_word - beg_full_word >= 32,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
         "the range must include at least 32 bytes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  set_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  set_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  set_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
void BitMap::clear_large_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  assert(end_full_word - beg_full_word >= 32,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
         "the range must include at least 32 bytes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  clear_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  clear_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  clear_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
void BitMap::at_put(idx_t offset, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    set_bit(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    clear_bit(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
// Return true to indicate that this thread changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
// the bit, false to indicate that someone else did.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
// In either case, the requested bit is in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
// requested state some time during the period that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
// this thread is executing this call. More importantly,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
// if no other thread is executing an action to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
// change the requested bit to a state other than
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
// the one that this thread is trying to set it to,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
// then the the bit is in the expected state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
// at exit from this method. However, rather than
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
// make such a strong assertion here, based on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
// assuming such constrained use (which though true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
// today, could change in the future to service some
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
// funky parallel algorithm), we encourage callers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
// to do such verification, as and when appropriate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
bool BitMap::par_at_put(idx_t bit, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  return value ? par_set_bit(bit) : par_clear_bit(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
void BitMap::at_put_grow(idx_t offset, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  if (offset >= size()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    resize(2 * MAX2(size(), offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  at_put(offset, value);
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::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    set_range(start_offset, end_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    clear_range(start_offset, end_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    par_put_range_within_word(beg, bit_index(beg_full_word), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      set_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      clear_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    par_put_range_within_word(bit_index(end_full_word), end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    par_put_range_within_word(beg, boundary, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    par_put_range_within_word(boundary, end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  }
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::at_put_large_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    set_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    clear_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  assert(end_full_word - beg_full_word >= 32,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
         "the range must include at least 32 bytes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  par_put_range_within_word(beg, bit_index(beg_full_word), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    set_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    clear_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  par_put_range_within_word(bit_index(end_full_word), end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
bool BitMap::contains(const BitMap other) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  uintptr_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  uintptr_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  for (idx_t index = 0; index < size_in_words(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    uintptr_t word_union = dest_map[index] | other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    // If this has more bits set than dest_map[index], then other is not a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    // subset.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    if (word_union != dest_map[index]) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
bool BitMap::intersects(const BitMap other) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  uintptr_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  uintptr_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  for (idx_t index = 0; index < size_in_words(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    if ((dest_map[index] & other_map[index]) != 0) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  // Otherwise, no intersection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
void BitMap::set_union(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  for (idx_t index = 0; index < size_in_words(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
    dest_map[index] = dest_map[index] | other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
void BitMap::set_difference(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  for (idx_t index = 0; index < size_in_words(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    dest_map[index] = dest_map[index] & ~(other_map[index]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
void BitMap::set_intersection(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    dest_map[index]  = dest_map[index] & other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
bool BitMap::set_union_with_result(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  bool changed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    idx_t temp = map(index) | other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    changed = changed || (temp != map(index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    map()[index] = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
bool BitMap::set_difference_with_result(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  bool changed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    idx_t temp = dest_map[index] & ~(other_map[index]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    changed = changed || (temp != dest_map[index]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    dest_map[index] = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
bool BitMap::set_intersection_with_result(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  bool changed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    idx_t orig = dest_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    idx_t temp = orig & other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    changed = changed || (temp != orig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    dest_map[index]  = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
void BitMap::set_from(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    dest_map[index] = other_map[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
bool BitMap::is_same(BitMap other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  idx_t* dest_map = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  idx_t* other_map = other.map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  idx_t size = size_in_words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  for (idx_t index = 0; index < size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    if (dest_map[index] != other_map[index]) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
bool BitMap::is_full() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  uintptr_t* word = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  idx_t rest = size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    if (*word != (uintptr_t) AllBits) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    word++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  return rest == 0 || (*word | ~right_n_bits((int)rest)) == (uintptr_t) AllBits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
bool BitMap::is_empty() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  uintptr_t* word = map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  idx_t rest = size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    if (*word != (uintptr_t) NoBits) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    word++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  return rest == 0 || (*word & right_n_bits((int)rest)) == (uintptr_t) NoBits;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
void BitMap::clear_large() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  clear_large_range_of_words(0, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
// Note that if the closure itself modifies the bitmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
// then modifications in and to the left of the _bit_ being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
// currently sampled will not be seen. Note also that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
// interval [leftOffset, rightOffset) is right open.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
void BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  verify_range(leftOffset, rightOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  idx_t startIndex = word_index(leftOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  for (idx_t index = startIndex, offset = leftOffset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
       offset < rightOffset && index < endIndex;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
       offset = (++index) << LogBitsPerWord) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
    idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    for (; offset < rightOffset && rest != (uintptr_t)NoBits; offset++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
      if (rest & 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
        blk->do_bit(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
        //  resample at each closure application
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
        // (see, for instance, CMS bug 4525989)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
        rest = map(index) >> (offset & (BitsPerWord -1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
        // XXX debugging: remove
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
        // The following assertion assumes that closure application
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
        // doesn't clear bits (may not be true in general, e.g. G1).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
        assert(rest & 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
               "incorrect shift or closure application can clear bits?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
      rest = rest >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
                                          idx_t r_offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  assert(l_offset <= size(), "BitMap index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  assert(r_offset <= size(), "BitMap index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  assert(l_offset <= r_offset, "l_offset > r_offset ?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  if (l_offset == r_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    return l_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  idx_t   index = word_index(l_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  idx_t r_index = word_index(r_offset-1) + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  idx_t res_offset = l_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  // check bits including and to the _left_ of offset's position
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  idx_t pos = bit_in_word(res_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  idx_t res = map(index) >> pos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  if (res != (uintptr_t)NoBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
    // find the position of the 1-bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
    for (; !(res & 1); res_offset++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
      res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    assert(res_offset >= l_offset, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    return MIN2(res_offset, r_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  // skip over all word length 0-bit runs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  for (index++; index < r_index; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
    res = map(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    if (res != (uintptr_t)NoBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
      // found a 1, return the offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
      for (res_offset = index << LogBitsPerWord; !(res & 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
           res_offset++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
        res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
      assert(res & 1, "tautology; see loop condition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
      assert(res_offset >= l_offset, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
      return MIN2(res_offset, r_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  return r_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
                                           idx_t r_offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  assert(l_offset <= size(), "BitMap index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  assert(r_offset <= size(), "BitMap index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  assert(l_offset <= r_offset, "l_offset > r_offset ?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  if (l_offset == r_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
    return l_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  idx_t   index = word_index(l_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  idx_t r_index = word_index(r_offset-1) + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  idx_t res_offset = l_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  // check bits including and to the _left_ of offset's position
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  idx_t pos = res_offset & (BitsPerWord - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
  if (res != (uintptr_t)AllBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    // find the position of the 0-bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    for (; res & 1; res_offset++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
      res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    assert(res_offset >= l_offset, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    return MIN2(res_offset, r_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  // skip over all word length 1-bit runs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  for (index++; index < r_index; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
    res = map(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    if (res != (uintptr_t)AllBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
      // found a 0, return the offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
      for (res_offset = index << LogBitsPerWord; res & 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
           res_offset++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
        res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
      assert(!(res & 1), "tautology; see loop condition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
      assert(res_offset >= l_offset, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
      return MIN2(res_offset, r_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  return r_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
void BitMap::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
  tty->print("Bitmap(%d):", size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  for (idx_t index = 0; index < size(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
    tty->print("%c", at(index) ? '1' : '0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
BitMap2D::BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
  : _bits_per_slot(bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  , _map(map, size_in_slots * bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  : _bits_per_slot(bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  , _map(size_in_slots * bits_per_slot)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
}