bitMap.cpp revision 0
0N/A/*
2362N/A * Copyright 1997-2006 Sun Microsystems, Inc. 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
2362N/A *
2362N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_bitMap.cpp.incl"
0N/A
0N/A
0N/ABitMap::BitMap(idx_t* map, idx_t size_in_bits) {
0N/A assert(size_in_bits >= 0, "just checking");
0N/A _map = map;
0N/A _size = size_in_bits;
0N/A}
0N/A
0N/A
0N/ABitMap::BitMap(idx_t size_in_bits) {
0N/A assert(size_in_bits >= 0, "just checking");
0N/A _size = size_in_bits;
0N/A _map = NEW_RESOURCE_ARRAY(idx_t, size_in_words());
0N/A}
0N/A
0N/A
0N/Avoid BitMap::resize(idx_t size_in_bits) {
0N/A assert(size_in_bits >= 0, "just checking");
0N/A size_t old_size_in_words = size_in_words();
0N/A uintptr_t* old_map = map();
1460N/A _size = size_in_bits;
1460N/A size_t new_size_in_words = size_in_words();
1460N/A _map = NEW_RESOURCE_ARRAY(idx_t, new_size_in_words);
1460N/A Copy::disjoint_words((HeapWord*) old_map, (HeapWord*) _map, MIN2(old_size_in_words, new_size_in_words));
1460N/A if (new_size_in_words > old_size_in_words) {
1460N/A clear_range_of_words(old_size_in_words, size_in_words());
0N/A }
0N/A}
1460N/A
1460N/A// Returns a bit mask for a range of bits [beg, end) within a single word. Each
1460N/A// bit in the mask is 0 if the bit is in the range, 1 if not in the range. The
1460N/A// returned mask can be used directly to clear the range, or inverted to set the
1460N/A// range. Note: end must not be 0.
0N/Ainline BitMap::idx_t
0N/ABitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
0N/A assert(end != 0, "does not work when end == 0");
0N/A assert(beg == end || word_index(beg) == word_index(end - 1),
0N/A "must be a single-word range");
1460N/A idx_t mask = bit_mask(beg) - 1; // low (right) bits
1460N/A if (bit_in_word(end) != 0) {
1460N/A mask |= ~(bit_mask(end) - 1); // high (left) bits
1460N/A }
1460N/A return mask;
1460N/A}
1460N/A
1460N/Avoid BitMap::set_range_within_word(idx_t beg, idx_t end) {
1460N/A // With a valid range (beg <= end), this test ensures that end != 0, as
1460N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
1460N/A if (beg != end) {
0N/A idx_t mask = inverted_bit_mask_for_range(beg, end);
1460N/A *word_addr(beg) |= ~mask;
1460N/A }
1460N/A}
0N/A
0N/Avoid BitMap::clear_range_within_word(idx_t beg, idx_t end) {
0N/A // With a valid range (beg <= end), this test ensures that end != 0, as
1460N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
0N/A if (beg != end) {
1460N/A idx_t mask = inverted_bit_mask_for_range(beg, end);
0N/A *word_addr(beg) &= mask;
1460N/A }
0N/A}
1460N/A
1460N/Avoid BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
1460N/A assert(value == 0 || value == 1, "0 for clear, 1 for set");
1460N/A // With a valid range (beg <= end), this test ensures that end != 0, as
1460N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
1460N/A if (beg != end) {
0N/A intptr_t* pw = (intptr_t*)word_addr(beg);
0N/A intptr_t w = *pw;
0N/A intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
1460N/A intptr_t nw = value ? (w | ~mr) : (w & mr);
1460N/A while (true) {
1460N/A intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
0N/A if (res == w) break;
1460N/A w = *pw;
1460N/A nw = value ? (w | ~mr) : (w & mr);
0N/A }
1460N/A }
0N/A}
0N/A
0N/Ainline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
0N/A memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
0N/A}
2080N/A
0N/Ainline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
0N/A memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
0N/A}
160N/A
160N/Ainline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
1460N/A idx_t bit_rounded_up = bit + (BitsPerWord - 1);
160N/A // Check for integer arithmetic overflow.
160N/A return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
160N/A}
160N/A
160N/Avoid BitMap::set_range(idx_t beg, idx_t end) {
160N/A verify_range(beg, end);
160N/A
160N/A idx_t beg_full_word = word_index_round_up(beg);
160N/A idx_t end_full_word = word_index(end);
160N/A
160N/A if (beg_full_word < end_full_word) {
160N/A // The range includes at least one full word.
160N/A set_range_within_word(beg, bit_index(beg_full_word));
160N/A set_range_of_words(beg_full_word, end_full_word);
0N/A set_range_within_word(bit_index(end_full_word), end);
0N/A } else {
0N/A // The range spans at most 2 partial words.
0N/A idx_t boundary = MIN2(bit_index(beg_full_word), end);
0N/A set_range_within_word(beg, boundary);
0N/A set_range_within_word(boundary, end);
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::clear_range(idx_t beg, idx_t end) {
0N/A verify_range(beg, end);
0N/A
0N/A idx_t beg_full_word = word_index_round_up(beg);
0N/A idx_t end_full_word = word_index(end);
0N/A
0N/A if (beg_full_word < end_full_word) {
0N/A // The range includes at least one full word.
0N/A clear_range_within_word(beg, bit_index(beg_full_word));
0N/A clear_range_of_words(beg_full_word, end_full_word);
0N/A clear_range_within_word(bit_index(end_full_word), end);
0N/A } else {
0N/A // The range spans at most 2 partial words.
0N/A idx_t boundary = MIN2(bit_index(beg_full_word), end);
0N/A clear_range_within_word(beg, boundary);
0N/A clear_range_within_word(boundary, end);
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::set_large_range(idx_t beg, idx_t end) {
0N/A verify_range(beg, end);
0N/A
0N/A idx_t beg_full_word = word_index_round_up(beg);
0N/A idx_t end_full_word = word_index(end);
0N/A
0N/A assert(end_full_word - beg_full_word >= 32,
0N/A "the range must include at least 32 bytes");
0N/A
0N/A // The range includes at least one full word.
0N/A set_range_within_word(beg, bit_index(beg_full_word));
0N/A set_large_range_of_words(beg_full_word, end_full_word);
0N/A set_range_within_word(bit_index(end_full_word), end);
0N/A}
1460N/A
1460N/Avoid BitMap::clear_large_range(idx_t beg, idx_t end) {
0N/A verify_range(beg, end);
0N/A
0N/A idx_t beg_full_word = word_index_round_up(beg);
160N/A idx_t end_full_word = word_index(end);
160N/A
0N/A assert(end_full_word - beg_full_word >= 32,
0N/A "the range must include at least 32 bytes");
1460N/A
0N/A // The range includes at least one full word.
0N/A clear_range_within_word(beg, bit_index(beg_full_word));
0N/A clear_large_range_of_words(beg_full_word, end_full_word);
0N/A clear_range_within_word(bit_index(end_full_word), end);
0N/A}
0N/A
0N/Avoid BitMap::at_put(idx_t offset, bool value) {
0N/A if (value) {
0N/A set_bit(offset);
0N/A } else {
0N/A clear_bit(offset);
0N/A }
0N/A}
0N/A
0N/A// Return true to indicate that this thread changed
0N/A// the bit, false to indicate that someone else did.
0N/A// In either case, the requested bit is in the
0N/A// requested state some time during the period that
0N/A// this thread is executing this call. More importantly,
0N/A// if no other thread is executing an action to
0N/A// change the requested bit to a state other than
0N/A// the one that this thread is trying to set it to,
0N/A// then the the bit is in the expected state
0N/A// at exit from this method. However, rather than
0N/A// make such a strong assertion here, based on
0N/A// assuming such constrained use (which though true
0N/A// today, could change in the future to service some
0N/A// funky parallel algorithm), we encourage callers
0N/A// to do such verification, as and when appropriate.
0N/Abool BitMap::par_at_put(idx_t bit, bool value) {
0N/A return value ? par_set_bit(bit) : par_clear_bit(bit);
0N/A}
0N/A
0N/Avoid BitMap::at_put_grow(idx_t offset, bool value) {
0N/A if (offset >= size()) {
0N/A resize(2 * MAX2(size(), offset));
0N/A }
0N/A at_put(offset, value);
0N/A}
0N/A
0N/Avoid BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
0N/A if (value) {
0N/A set_range(start_offset, end_offset);
0N/A } else {
0N/A clear_range(start_offset, end_offset);
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
0N/A verify_range(beg, end);
0N/A
459N/A idx_t beg_full_word = word_index_round_up(beg);
0N/A idx_t end_full_word = word_index(end);
0N/A
0N/A if (beg_full_word < end_full_word) {
0N/A // The range includes at least one full word.
0N/A par_put_range_within_word(beg, bit_index(beg_full_word), value);
0N/A if (value) {
0N/A set_range_of_words(beg_full_word, end_full_word);
0N/A } else {
0N/A clear_range_of_words(beg_full_word, end_full_word);
0N/A }
0N/A par_put_range_within_word(bit_index(end_full_word), end, value);
0N/A } else {
0N/A // The range spans at most 2 partial words.
0N/A idx_t boundary = MIN2(bit_index(beg_full_word), end);
0N/A par_put_range_within_word(beg, boundary, value);
0N/A par_put_range_within_word(boundary, end, value);
0N/A }
0N/A
0N/A}
0N/A
0N/Avoid BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
0N/A if (value) {
0N/A set_large_range(beg, end);
0N/A } else {
0N/A clear_large_range(beg, end);
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
0N/A verify_range(beg, end);
0N/A
0N/A idx_t beg_full_word = word_index_round_up(beg);
0N/A idx_t end_full_word = word_index(end);
0N/A
0N/A assert(end_full_word - beg_full_word >= 32,
0N/A "the range must include at least 32 bytes");
0N/A
459N/A // The range includes at least one full word.
0N/A par_put_range_within_word(beg, bit_index(beg_full_word), value);
0N/A if (value) {
0N/A set_large_range_of_words(beg_full_word, end_full_word);
0N/A } else {
0N/A clear_large_range_of_words(beg_full_word, end_full_word);
0N/A }
0N/A par_put_range_within_word(bit_index(end_full_word), end, value);
0N/A}
459N/A
0N/Abool BitMap::contains(const BitMap other) const {
0N/A assert(size() == other.size(), "must have same size");
0N/A uintptr_t* dest_map = map();
0N/A uintptr_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size_in_words(); index++) {
0N/A uintptr_t word_union = dest_map[index] | other_map[index];
0N/A // If this has more bits set than dest_map[index], then other is not a
0N/A // subset.
0N/A if (word_union != dest_map[index]) return false;
0N/A }
0N/A return true;
0N/A}
0N/A
2080N/Abool BitMap::intersects(const BitMap other) const {
0N/A assert(size() == other.size(), "must have same size");
0N/A uintptr_t* dest_map = map();
0N/A uintptr_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size_in_words(); index++) {
0N/A if ((dest_map[index] & other_map[index]) != 0) return true;
0N/A }
0N/A // Otherwise, no intersection.
0N/A return false;
459N/A}
0N/A
0N/Avoid BitMap::set_union(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size_in_words(); index++) {
0N/A dest_map[index] = dest_map[index] | other_map[index];
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid BitMap::set_difference(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
160N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size_in_words(); index++) {
0N/A dest_map[index] = dest_map[index] & ~(other_map[index]);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid BitMap::set_intersection(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
160N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size; index++) {
160N/A dest_map[index] = dest_map[index] & other_map[index];
160N/A }
160N/A}
160N/A
160N/A
160N/Abool BitMap::set_union_with_result(BitMap other) {
160N/A assert(size() == other.size(), "must have same size");
160N/A bool changed = false;
160N/A idx_t* dest_map = map();
160N/A idx_t* other_map = other.map();
160N/A idx_t size = size_in_words();
160N/A for (idx_t index = 0; index < size; index++) {
0N/A idx_t temp = map(index) | other_map[index];
0N/A changed = changed || (temp != map(index));
160N/A map()[index] = temp;
160N/A }
0N/A return changed;
0N/A}
160N/A
160N/A
0N/Abool BitMap::set_difference_with_result(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
160N/A bool changed = false;
160N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
0N/A idx_t size = size_in_words();
160N/A for (idx_t index = 0; index < size; index++) {
160N/A idx_t temp = dest_map[index] & ~(other_map[index]);
0N/A changed = changed || (temp != dest_map[index]);
0N/A dest_map[index] = temp;
0N/A }
1460N/A return changed;
1460N/A}
0N/A
0N/A
0N/Abool BitMap::set_intersection_with_result(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
1460N/A bool changed = false;
160N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
160N/A idx_t size = size_in_words();
160N/A for (idx_t index = 0; index < size; index++) {
160N/A idx_t orig = dest_map[index];
160N/A idx_t temp = orig & other_map[index];
160N/A changed = changed || (temp != orig);
160N/A dest_map[index] = temp;
1460N/A }
160N/A return changed;
160N/A}
160N/A
160N/A
0N/Avoid BitMap::set_from(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
160N/A idx_t* dest_map = map();
160N/A idx_t* other_map = other.map();
160N/A idx_t size = size_in_words();
160N/A for (idx_t index = 0; index < size; index++) {
160N/A dest_map[index] = other_map[index];
0N/A }
0N/A}
0N/A
0N/A
0N/Abool BitMap::is_same(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A idx_t* dest_map = map();
0N/A idx_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size; index++) {
0N/A if (dest_map[index] != other_map[index]) return false;
0N/A }
0N/A return true;
0N/A}
0N/A
0N/Abool BitMap::is_full() const {
0N/A uintptr_t* word = map();
0N/A idx_t rest = size();
0N/A for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
0N/A if (*word != (uintptr_t) AllBits) return false;
0N/A word++;
0N/A }
0N/A return rest == 0 || (*word | ~right_n_bits((int)rest)) == (uintptr_t) AllBits;
2080N/A}
0N/A
0N/A
0N/Abool BitMap::is_empty() const {
0N/A uintptr_t* word = map();
0N/A idx_t rest = size();
0N/A for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
0N/A if (*word != (uintptr_t) NoBits) return false;
0N/A word++;
0N/A }
0N/A return rest == 0 || (*word & right_n_bits((int)rest)) == (uintptr_t) NoBits;
2080N/A}
0N/A
0N/Avoid BitMap::clear_large() {
0N/A clear_large_range_of_words(0, size_in_words());
0N/A}
0N/A
0N/A// Note that if the closure itself modifies the bitmap
0N/A// then modifications in and to the left of the _bit_ being
2080N/A// currently sampled will not be seen. Note also that the
2080N/A// interval [leftOffset, rightOffset) is right open.
0N/Avoid BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
0N/A verify_range(leftOffset, rightOffset);
0N/A
0N/A idx_t startIndex = word_index(leftOffset);
2080N/A idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
0N/A for (idx_t index = startIndex, offset = leftOffset;
2080N/A offset < rightOffset && index < endIndex;
0N/A offset = (++index) << LogBitsPerWord) {
0N/A idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
0N/A for (; offset < rightOffset && rest != (uintptr_t)NoBits; offset++) {
0N/A if (rest & 1) {
0N/A blk->do_bit(offset);
2080N/A // resample at each closure application
2080N/A // (see, for instance, CMS bug 4525989)
0N/A rest = map(index) >> (offset & (BitsPerWord -1));
0N/A // XXX debugging: remove
0N/A // The following assertion assumes that closure application
0N/A // doesn't clear bits (may not be true in general, e.g. G1).
0N/A assert(rest & 1,
2080N/A "incorrect shift or closure application can clear bits?");
0N/A }
0N/A rest = rest >> 1;
0N/A }
0N/A }
0N/A}
0N/A
0N/ABitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
0N/A idx_t r_offset) const {
0N/A assert(l_offset <= size(), "BitMap index out of bounds");
2080N/A assert(r_offset <= size(), "BitMap index out of bounds");
2080N/A assert(l_offset <= r_offset, "l_offset > r_offset ?");
0N/A
0N/A if (l_offset == r_offset) {
2080N/A return l_offset;
2080N/A }
2080N/A idx_t index = word_index(l_offset);
2080N/A idx_t r_index = word_index(r_offset-1) + 1;
2080N/A idx_t res_offset = l_offset;
2080N/A
0N/A // check bits including and to the _left_ of offset's position
0N/A idx_t pos = bit_in_word(res_offset);
0N/A idx_t res = map(index) >> pos;
0N/A if (res != (uintptr_t)NoBits) {
0N/A // find the position of the 1-bit
0N/A for (; !(res & 1); res_offset++) {
0N/A res = res >> 1;
160N/A }
160N/A assert(res_offset >= l_offset, "just checking");
0N/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++) {
1460N/A res = map(index);
0N/A if (res != (uintptr_t)NoBits) {
// found a 1, return the offset
for (res_offset = index << LogBitsPerWord; !(res & 1);
res_offset++) {
res = res >> 1;
}
assert(res & 1, "tautology; see loop condition");
assert(res_offset >= l_offset, "just checking");
return MIN2(res_offset, r_offset);
}
}
return r_offset;
}
BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
idx_t r_offset) const {
assert(l_offset <= size(), "BitMap index out of bounds");
assert(r_offset <= size(), "BitMap index out of bounds");
assert(l_offset <= r_offset, "l_offset > r_offset ?");
if (l_offset == r_offset) {
return l_offset;
}
idx_t index = word_index(l_offset);
idx_t r_index = word_index(r_offset-1) + 1;
idx_t res_offset = l_offset;
// check bits including and to the _left_ of offset's position
idx_t pos = res_offset & (BitsPerWord - 1);
idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
if (res != (uintptr_t)AllBits) {
// find the position of the 0-bit
for (; res & 1; res_offset++) {
res = res >> 1;
}
assert(res_offset >= l_offset, "just checking");
return MIN2(res_offset, r_offset);
}
// skip over all word length 1-bit runs
for (index++; index < r_index; index++) {
res = map(index);
if (res != (uintptr_t)AllBits) {
// found a 0, return the offset
for (res_offset = index << LogBitsPerWord; res & 1;
res_offset++) {
res = res >> 1;
}
assert(!(res & 1), "tautology; see loop condition");
assert(res_offset >= l_offset, "just checking");
return MIN2(res_offset, r_offset);
}
}
return r_offset;
}
#ifndef PRODUCT
void BitMap::print_on(outputStream* st) const {
tty->print("Bitmap(%d):", size());
for (idx_t index = 0; index < size(); index++) {
tty->print("%c", at(index) ? '1' : '0');
}
tty->cr();
}
#endif
BitMap2D::BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot)
: _bits_per_slot(bits_per_slot)
, _map(map, size_in_slots * bits_per_slot)
{
}
BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
: _bits_per_slot(bits_per_slot)
, _map(size_in_slots * bits_per_slot)
{
}