aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_BITMAP_INLINE_HPP aoqi@0: #define SHARE_VM_UTILITIES_BITMAP_INLINE_HPP aoqi@0: aoqi@0: #include "runtime/atomic.hpp" aoqi@0: #include "utilities/bitMap.hpp" aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: inline void BitMap::verify_index(idx_t index) const { aoqi@0: assert(index < _size, "BitMap index out of bounds"); aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const { aoqi@0: assert(beg_index <= end_index, "BitMap range error"); aoqi@0: // Note that [0,0) and [size,size) are both valid ranges. aoqi@0: if (end_index != _size) verify_index(end_index); aoqi@0: } aoqi@0: #endif // #ifdef ASSERT aoqi@0: aoqi@0: inline void BitMap::set_bit(idx_t bit) { aoqi@0: verify_index(bit); aoqi@0: *word_addr(bit) |= bit_mask(bit); aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::clear_bit(idx_t bit) { aoqi@0: verify_index(bit); aoqi@0: *word_addr(bit) &= ~bit_mask(bit); aoqi@0: } aoqi@0: aoqi@0: inline bool BitMap::par_set_bit(idx_t bit) { aoqi@0: verify_index(bit); aoqi@0: volatile bm_word_t* const addr = word_addr(bit); aoqi@0: const bm_word_t mask = bit_mask(bit); aoqi@0: bm_word_t old_val = *addr; aoqi@0: aoqi@0: do { aoqi@0: const bm_word_t new_val = old_val | mask; aoqi@0: if (new_val == old_val) { aoqi@0: return false; // Someone else beat us to it. aoqi@0: } aoqi@0: const bm_word_t cur_val = (bm_word_t) Atomic::cmpxchg_ptr((void*) new_val, aoqi@0: (volatile void*) addr, aoqi@0: (void*) old_val); aoqi@0: if (cur_val == old_val) { aoqi@0: return true; // Success. aoqi@0: } aoqi@0: old_val = cur_val; // The value changed, try again. aoqi@0: } while (true); aoqi@0: } aoqi@0: aoqi@0: inline bool BitMap::par_clear_bit(idx_t bit) { aoqi@0: verify_index(bit); aoqi@0: volatile bm_word_t* const addr = word_addr(bit); aoqi@0: const bm_word_t mask = ~bit_mask(bit); aoqi@0: bm_word_t old_val = *addr; aoqi@0: aoqi@0: do { aoqi@0: const bm_word_t new_val = old_val & mask; aoqi@0: if (new_val == old_val) { aoqi@0: return false; // Someone else beat us to it. aoqi@0: } aoqi@0: const bm_word_t cur_val = (bm_word_t) Atomic::cmpxchg_ptr((void*) new_val, aoqi@0: (volatile void*) addr, aoqi@0: (void*) old_val); aoqi@0: if (cur_val == old_val) { aoqi@0: return true; // Success. aoqi@0: } aoqi@0: old_val = cur_val; // The value changed, try again. aoqi@0: } while (true); aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) { aoqi@0: if (hint == small_range && end - beg == 1) { aoqi@0: set_bit(beg); aoqi@0: } else { aoqi@0: if (hint == large_range) { aoqi@0: set_large_range(beg, end); aoqi@0: } else { aoqi@0: set_range(beg, end); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { aoqi@0: if (hint == small_range && end - beg == 1) { aoqi@0: clear_bit(beg); aoqi@0: } else { aoqi@0: if (hint == large_range) { aoqi@0: clear_large_range(beg, end); aoqi@0: } else { aoqi@0: clear_range(beg, end); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) { aoqi@0: if (hint == small_range && end - beg == 1) { aoqi@0: par_at_put(beg, true); aoqi@0: } else { aoqi@0: if (hint == large_range) { aoqi@0: par_at_put_large_range(beg, end, true); aoqi@0: } else { aoqi@0: par_at_put_range(beg, end, true); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::set_range_of_words(idx_t beg, idx_t end) { aoqi@0: bm_word_t* map = _map; aoqi@0: for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) { aoqi@0: bm_word_t* map = _map; aoqi@0: for (idx_t i = beg; i < end; ++i) map[i] = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline void BitMap::clear() { aoqi@0: clear_range_of_words(0, size_in_words()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { aoqi@0: if (hint == small_range && end - beg == 1) { aoqi@0: par_at_put(beg, false); aoqi@0: } else { aoqi@0: if (hint == large_range) { aoqi@0: par_at_put_large_range(beg, end, false); aoqi@0: } else { aoqi@0: par_at_put_range(beg, end, false); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t aoqi@0: BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const { aoqi@0: assert(l_offset <= size(), "BitMap index out of bounds"); aoqi@0: assert(r_offset <= size(), "BitMap index out of bounds"); aoqi@0: assert(l_offset <= r_offset, "l_offset > r_offset ?"); aoqi@0: aoqi@0: if (l_offset == r_offset) { aoqi@0: return l_offset; aoqi@0: } aoqi@0: idx_t index = word_index(l_offset); aoqi@0: idx_t r_index = word_index(r_offset-1) + 1; aoqi@0: idx_t res_offset = l_offset; aoqi@0: aoqi@0: // check bits including and to the _left_ of offset's position aoqi@0: idx_t pos = bit_in_word(res_offset); aoqi@0: idx_t res = map(index) >> pos; aoqi@0: if (res != (uintptr_t)NoBits) { aoqi@0: // find the position of the 1-bit aoqi@0: for (; !(res & 1); res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // In the following assert, if r_offset is not bitamp word aligned, aoqi@0: // checking that res_offset is strictly less than r_offset is too aoqi@0: // strong and will trip the assert. aoqi@0: // aoqi@0: // Consider the case where l_offset is bit 15 and r_offset is bit 17 aoqi@0: // of the same map word, and where bits [15:16:17:18] == [00:00:00:01]. aoqi@0: // All the bits in the range [l_offset:r_offset) are 0. aoqi@0: // The loop that calculates res_offset, above, would yield the offset aoqi@0: // of bit 18 because it's in the same map word as l_offset and there aoqi@0: // is a set bit in that map word above l_offset (i.e. res != NoBits). aoqi@0: // aoqi@0: // In this case, however, we can assert is that res_offset is strictly aoqi@0: // less than size() since we know that there is at least one set bit aoqi@0: // at an offset above, but in the same map word as, r_offset. aoqi@0: // Otherwise, if r_offset is word aligned then it will not be in the aoqi@0: // same map word as l_offset (unless it equals l_offset). So either aoqi@0: // there won't be a set bit between l_offset and the end of it's map aoqi@0: // word (i.e. res == NoBits), or res_offset will be less than r_offset. aoqi@0: aoqi@0: idx_t limit = is_word_aligned(r_offset) ? r_offset : size(); aoqi@0: assert(res_offset >= l_offset && res_offset < limit, "just checking"); aoqi@0: #endif // ASSERT aoqi@0: return MIN2(res_offset, r_offset); aoqi@0: } aoqi@0: // skip over all word length 0-bit runs aoqi@0: for (index++; index < r_index; index++) { aoqi@0: res = map(index); aoqi@0: if (res != (uintptr_t)NoBits) { aoqi@0: // found a 1, return the offset aoqi@0: for (res_offset = bit_index(index); !(res & 1); res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: assert(res & 1, "tautology; see loop condition"); aoqi@0: assert(res_offset >= l_offset, "just checking"); aoqi@0: return MIN2(res_offset, r_offset); aoqi@0: } aoqi@0: } aoqi@0: return r_offset; aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t aoqi@0: BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const { aoqi@0: assert(l_offset <= size(), "BitMap index out of bounds"); aoqi@0: assert(r_offset <= size(), "BitMap index out of bounds"); aoqi@0: assert(l_offset <= r_offset, "l_offset > r_offset ?"); aoqi@0: aoqi@0: if (l_offset == r_offset) { aoqi@0: return l_offset; aoqi@0: } aoqi@0: idx_t index = word_index(l_offset); aoqi@0: idx_t r_index = word_index(r_offset-1) + 1; aoqi@0: idx_t res_offset = l_offset; aoqi@0: aoqi@0: // check bits including and to the _left_ of offset's position aoqi@0: idx_t pos = res_offset & (BitsPerWord - 1); aoqi@0: idx_t res = (map(index) >> pos) | left_n_bits((int)pos); aoqi@0: aoqi@0: if (res != (uintptr_t)AllBits) { aoqi@0: // find the position of the 0-bit aoqi@0: for (; res & 1; res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: assert(res_offset >= l_offset, "just checking"); aoqi@0: return MIN2(res_offset, r_offset); aoqi@0: } aoqi@0: // skip over all word length 1-bit runs aoqi@0: for (index++; index < r_index; index++) { aoqi@0: res = map(index); aoqi@0: if (res != (uintptr_t)AllBits) { aoqi@0: // found a 0, return the offset aoqi@0: for (res_offset = index << LogBitsPerWord; res & 1; aoqi@0: res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: assert(!(res & 1), "tautology; see loop condition"); aoqi@0: assert(res_offset >= l_offset, "just checking"); aoqi@0: return MIN2(res_offset, r_offset); aoqi@0: } aoqi@0: } aoqi@0: return r_offset; aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t aoqi@0: BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset, aoqi@0: idx_t r_offset) const aoqi@0: { aoqi@0: verify_range(l_offset, r_offset); aoqi@0: assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned"); aoqi@0: aoqi@0: if (l_offset == r_offset) { aoqi@0: return l_offset; aoqi@0: } aoqi@0: idx_t index = word_index(l_offset); aoqi@0: idx_t r_index = word_index(r_offset); aoqi@0: idx_t res_offset = l_offset; aoqi@0: aoqi@0: // check bits including and to the _left_ of offset's position aoqi@0: idx_t res = map(index) >> bit_in_word(res_offset); aoqi@0: if (res != (uintptr_t)NoBits) { aoqi@0: // find the position of the 1-bit aoqi@0: for (; !(res & 1); res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: assert(res_offset >= l_offset && aoqi@0: res_offset < r_offset, "just checking"); aoqi@0: return res_offset; aoqi@0: } aoqi@0: // skip over all word length 0-bit runs aoqi@0: for (index++; index < r_index; index++) { aoqi@0: res = map(index); aoqi@0: if (res != (uintptr_t)NoBits) { aoqi@0: // found a 1, return the offset aoqi@0: for (res_offset = bit_index(index); !(res & 1); res_offset++) { aoqi@0: res = res >> 1; aoqi@0: } aoqi@0: assert(res & 1, "tautology; see loop condition"); aoqi@0: assert(res_offset >= l_offset && res_offset < r_offset, "just checking"); aoqi@0: return res_offset; aoqi@0: } aoqi@0: } aoqi@0: return r_offset; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Returns a bit mask for a range of bits [beg, end) within a single word. Each aoqi@0: // bit in the mask is 0 if the bit is in the range, 1 if not in the range. The aoqi@0: // returned mask can be used directly to clear the range, or inverted to set the aoqi@0: // range. Note: end must not be 0. aoqi@0: inline BitMap::bm_word_t aoqi@0: BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const { aoqi@0: assert(end != 0, "does not work when end == 0"); aoqi@0: assert(beg == end || word_index(beg) == word_index(end - 1), aoqi@0: "must be a single-word range"); aoqi@0: bm_word_t mask = bit_mask(beg) - 1; // low (right) bits aoqi@0: if (bit_in_word(end) != 0) { aoqi@0: mask |= ~(bit_mask(end) - 1); // high (left) bits aoqi@0: } aoqi@0: return mask; aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) { aoqi@0: memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t)); aoqi@0: } aoqi@0: aoqi@0: inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) { aoqi@0: memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t)); aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const { aoqi@0: idx_t bit_rounded_up = bit + (BitsPerWord - 1); aoqi@0: // Check for integer arithmetic overflow. aoqi@0: return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words(); aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset, aoqi@0: idx_t r_offset) const { aoqi@0: return get_next_one_offset_inline(l_offset, r_offset); aoqi@0: } aoqi@0: aoqi@0: inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset, aoqi@0: idx_t r_offset) const { aoqi@0: return get_next_zero_offset_inline(l_offset, r_offset); aoqi@0: } aoqi@0: aoqi@0: inline void BitMap2D::clear() { aoqi@0: _map.clear(); aoqi@0: } aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_BITMAP_INLINE_HPP