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