Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/bitMap.inline.hpp
32285 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_UTILITIES_BITMAP_INLINE_HPP25#define SHARE_VM_UTILITIES_BITMAP_INLINE_HPP2627#include "runtime/atomic.inline.hpp"28#include "utilities/bitMap.hpp"2930#ifdef ASSERT31inline void BitMap::verify_index(idx_t index) const {32assert(index < _size, "BitMap index out of bounds");33}3435inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {36assert(beg_index <= end_index, "BitMap range error");37// Note that [0,0) and [size,size) are both valid ranges.38if (end_index != _size) verify_index(end_index);39}40#endif // #ifdef ASSERT4142inline void BitMap::set_bit(idx_t bit) {43verify_index(bit);44*word_addr(bit) |= bit_mask(bit);45}4647inline void BitMap::clear_bit(idx_t bit) {48verify_index(bit);49*word_addr(bit) &= ~bit_mask(bit);50}5152inline bool BitMap::par_set_bit(idx_t bit) {53verify_index(bit);54volatile bm_word_t* const addr = word_addr(bit);55const bm_word_t mask = bit_mask(bit);56bm_word_t old_val = *addr;5758do {59const bm_word_t new_val = old_val | mask;60if (new_val == old_val) {61return false; // Someone else beat us to it.62}63const bm_word_t cur_val = (bm_word_t) Atomic::cmpxchg_ptr((void*) new_val,64(volatile void*) addr,65(void*) old_val);66if (cur_val == old_val) {67return true; // Success.68}69old_val = cur_val; // The value changed, try again.70} while (true);71}7273inline bool BitMap::par_clear_bit(idx_t bit) {74verify_index(bit);75volatile bm_word_t* const addr = word_addr(bit);76const bm_word_t mask = ~bit_mask(bit);77bm_word_t old_val = *addr;7879do {80const bm_word_t new_val = old_val & mask;81if (new_val == old_val) {82return false; // Someone else beat us to it.83}84const bm_word_t cur_val = (bm_word_t) Atomic::cmpxchg_ptr((void*) new_val,85(volatile void*) addr,86(void*) old_val);87if (cur_val == old_val) {88return true; // Success.89}90old_val = cur_val; // The value changed, try again.91} while (true);92}9394inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {95if (hint == small_range && end - beg == 1) {96set_bit(beg);97} else {98if (hint == large_range) {99set_large_range(beg, end);100} else {101set_range(beg, end);102}103}104}105106inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {107if (hint == small_range && end - beg == 1) {108clear_bit(beg);109} else {110if (hint == large_range) {111clear_large_range(beg, end);112} else {113clear_range(beg, end);114}115}116}117118inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {119if (hint == small_range && end - beg == 1) {120par_at_put(beg, true);121} else {122if (hint == large_range) {123par_at_put_large_range(beg, end, true);124} else {125par_at_put_range(beg, end, true);126}127}128}129130inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {131bm_word_t* map = _map;132for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;133}134135136inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {137bm_word_t* map = _map;138for (idx_t i = beg; i < end; ++i) map[i] = 0;139}140141142inline void BitMap::clear() {143clear_range_of_words(0, size_in_words());144}145146147inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {148if (hint == small_range && end - beg == 1) {149par_at_put(beg, false);150} else {151if (hint == large_range) {152par_at_put_large_range(beg, end, false);153} else {154par_at_put_range(beg, end, false);155}156}157}158159inline BitMap::idx_t160BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const {161assert(l_offset <= size(), "BitMap index out of bounds");162assert(r_offset <= size(), "BitMap index out of bounds");163assert(l_offset <= r_offset, "l_offset > r_offset ?");164165if (l_offset == r_offset) {166return l_offset;167}168idx_t index = word_index(l_offset);169idx_t r_index = word_index(r_offset-1) + 1;170idx_t res_offset = l_offset;171172// check bits including and to the _left_ of offset's position173idx_t pos = bit_in_word(res_offset);174idx_t res = map(index) >> pos;175if (res != (uintptr_t)NoBits) {176// find the position of the 1-bit177for (; !(res & 1); res_offset++) {178res = res >> 1;179}180181#ifdef ASSERT182// In the following assert, if r_offset is not bitamp word aligned,183// checking that res_offset is strictly less than r_offset is too184// strong and will trip the assert.185//186// Consider the case where l_offset is bit 15 and r_offset is bit 17187// of the same map word, and where bits [15:16:17:18] == [00:00:00:01].188// All the bits in the range [l_offset:r_offset) are 0.189// The loop that calculates res_offset, above, would yield the offset190// of bit 18 because it's in the same map word as l_offset and there191// is a set bit in that map word above l_offset (i.e. res != NoBits).192//193// In this case, however, we can assert is that res_offset is strictly194// less than size() since we know that there is at least one set bit195// at an offset above, but in the same map word as, r_offset.196// Otherwise, if r_offset is word aligned then it will not be in the197// same map word as l_offset (unless it equals l_offset). So either198// there won't be a set bit between l_offset and the end of it's map199// word (i.e. res == NoBits), or res_offset will be less than r_offset.200201idx_t limit = is_word_aligned(r_offset) ? r_offset : size();202assert(res_offset >= l_offset && res_offset < limit, "just checking");203#endif // ASSERT204return MIN2(res_offset, r_offset);205}206// skip over all word length 0-bit runs207for (index++; index < r_index; index++) {208res = map(index);209if (res != (uintptr_t)NoBits) {210// found a 1, return the offset211for (res_offset = bit_index(index); !(res & 1); res_offset++) {212res = res >> 1;213}214assert(res & 1, "tautology; see loop condition");215assert(res_offset >= l_offset, "just checking");216return MIN2(res_offset, r_offset);217}218}219return r_offset;220}221222inline BitMap::idx_t223BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const {224assert(l_offset <= size(), "BitMap index out of bounds");225assert(r_offset <= size(), "BitMap index out of bounds");226assert(l_offset <= r_offset, "l_offset > r_offset ?");227228if (l_offset == r_offset) {229return l_offset;230}231idx_t index = word_index(l_offset);232idx_t r_index = word_index(r_offset-1) + 1;233idx_t res_offset = l_offset;234235// check bits including and to the _left_ of offset's position236idx_t pos = res_offset & (BitsPerWord - 1);237idx_t res = (map(index) >> pos) | left_n_bits((int)pos);238239if (res != (uintptr_t)AllBits) {240// find the position of the 0-bit241for (; res & 1; res_offset++) {242res = res >> 1;243}244assert(res_offset >= l_offset, "just checking");245return MIN2(res_offset, r_offset);246}247// skip over all word length 1-bit runs248for (index++; index < r_index; index++) {249res = map(index);250if (res != (uintptr_t)AllBits) {251// found a 0, return the offset252for (res_offset = index << LogBitsPerWord; res & 1;253res_offset++) {254res = res >> 1;255}256assert(!(res & 1), "tautology; see loop condition");257assert(res_offset >= l_offset, "just checking");258return MIN2(res_offset, r_offset);259}260}261return r_offset;262}263264inline BitMap::idx_t265BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset,266idx_t r_offset) const267{268verify_range(l_offset, r_offset);269assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned");270271if (l_offset == r_offset) {272return l_offset;273}274idx_t index = word_index(l_offset);275idx_t r_index = word_index(r_offset);276idx_t res_offset = l_offset;277278// check bits including and to the _left_ of offset's position279idx_t res = map(index) >> bit_in_word(res_offset);280if (res != (uintptr_t)NoBits) {281// find the position of the 1-bit282for (; !(res & 1); res_offset++) {283res = res >> 1;284}285assert(res_offset >= l_offset &&286res_offset < r_offset, "just checking");287return res_offset;288}289// skip over all word length 0-bit runs290for (index++; index < r_index; index++) {291res = map(index);292if (res != (uintptr_t)NoBits) {293// found a 1, return the offset294for (res_offset = bit_index(index); !(res & 1); res_offset++) {295res = res >> 1;296}297assert(res & 1, "tautology; see loop condition");298assert(res_offset >= l_offset && res_offset < r_offset, "just checking");299return res_offset;300}301}302return r_offset;303}304305306// Returns a bit mask for a range of bits [beg, end) within a single word. Each307// bit in the mask is 0 if the bit is in the range, 1 if not in the range. The308// returned mask can be used directly to clear the range, or inverted to set the309// range. Note: end must not be 0.310inline BitMap::bm_word_t311BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {312assert(end != 0, "does not work when end == 0");313assert(beg == end || word_index(beg) == word_index(end - 1),314"must be a single-word range");315bm_word_t mask = bit_mask(beg) - 1; // low (right) bits316if (bit_in_word(end) != 0) {317mask |= ~(bit_mask(end) - 1); // high (left) bits318}319return mask;320}321322inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {323assert(beg <= end, "underflow");324memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));325}326327inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {328assert(beg <= end, "underflow");329memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));330}331332inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {333idx_t bit_rounded_up = bit + (BitsPerWord - 1);334// Check for integer arithmetic overflow.335return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();336}337338inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,339idx_t r_offset) const {340return get_next_one_offset_inline(l_offset, r_offset);341}342343inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,344idx_t r_offset) const {345return get_next_zero_offset_inline(l_offset, r_offset);346}347348inline void BitMap2D::clear() {349_map.clear();350}351352#endif // SHARE_VM_UTILITIES_BITMAP_INLINE_HPP353354355