src/share/vm/utilities/bitMap.inline.hpp

Wed, 24 Feb 2010 07:00:33 -0800

author
jmasa
date
Wed, 24 Feb 2010 07:00:33 -0800
changeset 1719
5f1f51edaff6
parent 1279
bd02caa94611
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6928081: G1: rename parameters common with CMS
Summary: Rename marking stack sizing flags to be common between G1 and CMS
Reviewed-by: ysr, tonyp

duke@435 1 /*
xdono@1279 2 * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
jcoomes@1244 25 #ifdef ASSERT
jcoomes@1244 26 inline void BitMap::verify_index(idx_t index) const {
jcoomes@1244 27 assert(index < _size, "BitMap index out of bounds");
jcoomes@1244 28 }
jcoomes@1244 29
jcoomes@1244 30 inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
jcoomes@1244 31 assert(beg_index <= end_index, "BitMap range error");
jcoomes@1244 32 // Note that [0,0) and [size,size) are both valid ranges.
jcoomes@1244 33 if (end_index != _size) verify_index(end_index);
jcoomes@1244 34 }
jcoomes@1244 35 #endif // #ifdef ASSERT
ysr@777 36
ysr@777 37 inline void BitMap::set_bit(idx_t bit) {
ysr@777 38 verify_index(bit);
ysr@777 39 *word_addr(bit) |= bit_mask(bit);
ysr@777 40 }
ysr@777 41
ysr@777 42 inline void BitMap::clear_bit(idx_t bit) {
ysr@777 43 verify_index(bit);
ysr@777 44 *word_addr(bit) &= ~bit_mask(bit);
ysr@777 45 }
ysr@777 46
duke@435 47 inline bool BitMap::par_set_bit(idx_t bit) {
duke@435 48 verify_index(bit);
duke@435 49 volatile idx_t* const addr = word_addr(bit);
duke@435 50 const idx_t mask = bit_mask(bit);
duke@435 51 idx_t old_val = *addr;
duke@435 52
duke@435 53 do {
duke@435 54 const idx_t new_val = old_val | mask;
duke@435 55 if (new_val == old_val) {
duke@435 56 return false; // Someone else beat us to it.
duke@435 57 }
duke@435 58 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 59 (volatile void*) addr,
duke@435 60 (void*) old_val);
duke@435 61 if (cur_val == old_val) {
duke@435 62 return true; // Success.
duke@435 63 }
duke@435 64 old_val = cur_val; // The value changed, try again.
duke@435 65 } while (true);
duke@435 66 }
duke@435 67
duke@435 68 inline bool BitMap::par_clear_bit(idx_t bit) {
duke@435 69 verify_index(bit);
duke@435 70 volatile idx_t* const addr = word_addr(bit);
duke@435 71 const idx_t mask = ~bit_mask(bit);
duke@435 72 idx_t old_val = *addr;
duke@435 73
duke@435 74 do {
duke@435 75 const idx_t new_val = old_val & mask;
duke@435 76 if (new_val == old_val) {
duke@435 77 return false; // Someone else beat us to it.
duke@435 78 }
duke@435 79 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 80 (volatile void*) addr,
duke@435 81 (void*) old_val);
duke@435 82 if (cur_val == old_val) {
duke@435 83 return true; // Success.
duke@435 84 }
duke@435 85 old_val = cur_val; // The value changed, try again.
duke@435 86 } while (true);
duke@435 87 }
duke@435 88
ysr@777 89 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 90 if (hint == small_range && end - beg == 1) {
ysr@777 91 set_bit(beg);
ysr@777 92 } else {
ysr@777 93 if (hint == large_range) {
ysr@777 94 set_large_range(beg, end);
ysr@777 95 } else {
ysr@777 96 set_range(beg, end);
ysr@777 97 }
ysr@777 98 }
ysr@777 99 }
ysr@777 100
ysr@777 101 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 102 if (hint == small_range && end - beg == 1) {
ysr@777 103 clear_bit(beg);
ysr@777 104 } else {
ysr@777 105 if (hint == large_range) {
ysr@777 106 clear_large_range(beg, end);
ysr@777 107 } else {
ysr@777 108 clear_range(beg, end);
ysr@777 109 }
ysr@777 110 }
ysr@777 111 }
ysr@777 112
ysr@777 113 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 114 if (hint == small_range && end - beg == 1) {
ysr@777 115 par_at_put(beg, true);
ysr@777 116 } else {
ysr@777 117 if (hint == large_range) {
ysr@777 118 par_at_put_large_range(beg, end, true);
ysr@777 119 } else {
ysr@777 120 par_at_put_range(beg, end, true);
ysr@777 121 }
ysr@777 122 }
ysr@777 123 }
ysr@777 124
ysr@777 125 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
ysr@777 126 bm_word_t* map = _map;
ysr@777 127 for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;
ysr@777 128 }
ysr@777 129
ysr@777 130
ysr@777 131 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
ysr@777 132 bm_word_t* map = _map;
ysr@777 133 for (idx_t i = beg; i < end; ++i) map[i] = 0;
ysr@777 134 }
ysr@777 135
ysr@777 136
ysr@777 137 inline void BitMap::clear() {
ysr@777 138 clear_range_of_words(0, size_in_words());
ysr@777 139 }
ysr@777 140
ysr@777 141
ysr@777 142 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 143 if (hint == small_range && end - beg == 1) {
ysr@777 144 par_at_put(beg, false);
ysr@777 145 } else {
ysr@777 146 if (hint == large_range) {
ysr@777 147 par_at_put_large_range(beg, end, false);
ysr@777 148 } else {
ysr@777 149 par_at_put_range(beg, end, false);
ysr@777 150 }
ysr@777 151 }
ysr@777 152 }
ysr@777 153
duke@435 154 inline BitMap::idx_t
ysr@777 155 BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const {
ysr@777 156 assert(l_offset <= size(), "BitMap index out of bounds");
ysr@777 157 assert(r_offset <= size(), "BitMap index out of bounds");
ysr@777 158 assert(l_offset <= r_offset, "l_offset > r_offset ?");
duke@435 159
ysr@777 160 if (l_offset == r_offset) {
ysr@777 161 return l_offset;
duke@435 162 }
ysr@777 163 idx_t index = word_index(l_offset);
ysr@777 164 idx_t r_index = word_index(r_offset-1) + 1;
ysr@777 165 idx_t res_offset = l_offset;
duke@435 166
duke@435 167 // check bits including and to the _left_ of offset's position
ysr@777 168 idx_t pos = bit_in_word(res_offset);
ysr@777 169 idx_t res = map(index) >> pos;
ysr@777 170 if (res != (uintptr_t)NoBits) {
duke@435 171 // find the position of the 1-bit
ysr@777 172 for (; !(res & 1); res_offset++) {
duke@435 173 res = res >> 1;
duke@435 174 }
ysr@777 175 assert(res_offset >= l_offset &&
ysr@777 176 res_offset < r_offset, "just checking");
ysr@777 177 return MIN2(res_offset, r_offset);
duke@435 178 }
duke@435 179 // skip over all word length 0-bit runs
duke@435 180 for (index++; index < r_index; index++) {
duke@435 181 res = map(index);
ysr@777 182 if (res != (uintptr_t)NoBits) {
duke@435 183 // found a 1, return the offset
ysr@777 184 for (res_offset = bit_index(index); !(res & 1); res_offset++) {
duke@435 185 res = res >> 1;
duke@435 186 }
duke@435 187 assert(res & 1, "tautology; see loop condition");
ysr@777 188 assert(res_offset >= l_offset, "just checking");
ysr@777 189 return MIN2(res_offset, r_offset);
duke@435 190 }
duke@435 191 }
ysr@777 192 return r_offset;
duke@435 193 }
ysr@777 194
ysr@777 195 inline BitMap::idx_t
ysr@777 196 BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const {
ysr@777 197 assert(l_offset <= size(), "BitMap index out of bounds");
ysr@777 198 assert(r_offset <= size(), "BitMap index out of bounds");
ysr@777 199 assert(l_offset <= r_offset, "l_offset > r_offset ?");
ysr@777 200
ysr@777 201 if (l_offset == r_offset) {
ysr@777 202 return l_offset;
ysr@777 203 }
ysr@777 204 idx_t index = word_index(l_offset);
ysr@777 205 idx_t r_index = word_index(r_offset-1) + 1;
ysr@777 206 idx_t res_offset = l_offset;
ysr@777 207
ysr@777 208 // check bits including and to the _left_ of offset's position
ysr@777 209 idx_t pos = res_offset & (BitsPerWord - 1);
ysr@777 210 idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
ysr@777 211
ysr@777 212 if (res != (uintptr_t)AllBits) {
ysr@777 213 // find the position of the 0-bit
ysr@777 214 for (; res & 1; res_offset++) {
ysr@777 215 res = res >> 1;
ysr@777 216 }
ysr@777 217 assert(res_offset >= l_offset, "just checking");
ysr@777 218 return MIN2(res_offset, r_offset);
ysr@777 219 }
ysr@777 220 // skip over all word length 1-bit runs
ysr@777 221 for (index++; index < r_index; index++) {
ysr@777 222 res = map(index);
ysr@777 223 if (res != (uintptr_t)AllBits) {
ysr@777 224 // found a 0, return the offset
ysr@777 225 for (res_offset = index << LogBitsPerWord; res & 1;
ysr@777 226 res_offset++) {
ysr@777 227 res = res >> 1;
ysr@777 228 }
ysr@777 229 assert(!(res & 1), "tautology; see loop condition");
ysr@777 230 assert(res_offset >= l_offset, "just checking");
ysr@777 231 return MIN2(res_offset, r_offset);
ysr@777 232 }
ysr@777 233 }
ysr@777 234 return r_offset;
ysr@777 235 }
ysr@777 236
ysr@777 237 inline BitMap::idx_t
ysr@777 238 BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset,
ysr@777 239 idx_t r_offset) const
ysr@777 240 {
ysr@777 241 verify_range(l_offset, r_offset);
ysr@777 242 assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned");
ysr@777 243
ysr@777 244 if (l_offset == r_offset) {
ysr@777 245 return l_offset;
ysr@777 246 }
ysr@777 247 idx_t index = word_index(l_offset);
ysr@777 248 idx_t r_index = word_index(r_offset);
ysr@777 249 idx_t res_offset = l_offset;
ysr@777 250
ysr@777 251 // check bits including and to the _left_ of offset's position
ysr@777 252 idx_t res = map(index) >> bit_in_word(res_offset);
ysr@777 253 if (res != (uintptr_t)NoBits) {
ysr@777 254 // find the position of the 1-bit
ysr@777 255 for (; !(res & 1); res_offset++) {
ysr@777 256 res = res >> 1;
ysr@777 257 }
ysr@777 258 assert(res_offset >= l_offset &&
ysr@777 259 res_offset < r_offset, "just checking");
ysr@777 260 return res_offset;
ysr@777 261 }
ysr@777 262 // skip over all word length 0-bit runs
ysr@777 263 for (index++; index < r_index; index++) {
ysr@777 264 res = map(index);
ysr@777 265 if (res != (uintptr_t)NoBits) {
ysr@777 266 // found a 1, return the offset
ysr@777 267 for (res_offset = bit_index(index); !(res & 1); res_offset++) {
ysr@777 268 res = res >> 1;
ysr@777 269 }
ysr@777 270 assert(res & 1, "tautology; see loop condition");
ysr@777 271 assert(res_offset >= l_offset && res_offset < r_offset, "just checking");
ysr@777 272 return res_offset;
ysr@777 273 }
ysr@777 274 }
ysr@777 275 return r_offset;
ysr@777 276 }
ysr@777 277
ysr@777 278
ysr@777 279 // Returns a bit mask for a range of bits [beg, end) within a single word. Each
ysr@777 280 // bit in the mask is 0 if the bit is in the range, 1 if not in the range. The
ysr@777 281 // returned mask can be used directly to clear the range, or inverted to set the
ysr@777 282 // range. Note: end must not be 0.
ysr@777 283 inline BitMap::bm_word_t
ysr@777 284 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
ysr@777 285 assert(end != 0, "does not work when end == 0");
ysr@777 286 assert(beg == end || word_index(beg) == word_index(end - 1),
ysr@777 287 "must be a single-word range");
ysr@777 288 bm_word_t mask = bit_mask(beg) - 1; // low (right) bits
ysr@777 289 if (bit_in_word(end) != 0) {
ysr@777 290 mask |= ~(bit_mask(end) - 1); // high (left) bits
ysr@777 291 }
ysr@777 292 return mask;
ysr@777 293 }
ysr@777 294
ysr@777 295 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
ysr@777 296 memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
ysr@777 297 }
ysr@777 298
ysr@777 299 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
ysr@777 300 memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
ysr@777 301 }
ysr@777 302
ysr@777 303 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
ysr@777 304 idx_t bit_rounded_up = bit + (BitsPerWord - 1);
ysr@777 305 // Check for integer arithmetic overflow.
ysr@777 306 return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
ysr@777 307 }
ysr@777 308
ysr@777 309 inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
ysr@777 310 idx_t r_offset) const {
ysr@777 311 return get_next_one_offset_inline(l_offset, r_offset);
ysr@777 312 }
ysr@777 313
ysr@777 314 inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
ysr@777 315 idx_t r_offset) const {
ysr@777 316 return get_next_zero_offset_inline(l_offset, r_offset);
ysr@777 317 }
ysr@777 318
ysr@777 319 inline void BitMap2D::clear() {
ysr@777 320 _map.clear();
ysr@777 321 }

mercurial