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