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

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial