0N/A/*
3659N/A * Copyright (c) 1997, 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#include "precompiled.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "utilities/bitMap.inline.hpp"
1879N/A#include "utilities/copy.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "os_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "os_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "os_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "os_bsd.inline.hpp"
2796N/A#endif
0N/A
0N/A
342N/ABitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
4573N/A _map(map), _size(size_in_bits), _map_allocator(false)
342N/A{
342N/A assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
0N/A assert(size_in_bits >= 0, "just checking");
0N/A}
0N/A
0N/A
342N/ABitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
4573N/A _map(NULL), _size(0), _map_allocator(false)
342N/A{
342N/A assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
342N/A resize(size_in_bits, in_resource_area);
0N/A}
0N/A
342N/Avoid BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
0N/A assert(size_in_bits >= 0, "just checking");
342N/A idx_t old_size_in_words = size_in_words();
342N/A bm_word_t* old_map = map();
342N/A
0N/A _size = size_in_bits;
342N/A idx_t new_size_in_words = size_in_words();
342N/A if (in_resource_area) {
342N/A _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
342N/A } else {
4573N/A if (old_map != NULL) {
4573N/A _map_allocator.free();
4573N/A }
4573N/A _map = _map_allocator.allocate(new_size_in_words);
342N/A }
342N/A Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
342N/A MIN2(old_size_in_words, new_size_in_words));
0N/A if (new_size_in_words > old_size_in_words) {
0N/A clear_range_of_words(old_size_in_words, size_in_words());
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::set_range_within_word(idx_t beg, idx_t end) {
0N/A // With a valid range (beg <= end), this test ensures that end != 0, as
0N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
0N/A if (beg != end) {
342N/A bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0N/A *word_addr(beg) |= ~mask;
0N/A }
0N/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
0N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
0N/A if (beg != end) {
342N/A bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0N/A *word_addr(beg) &= mask;
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
0N/A assert(value == 0 || value == 1, "0 for clear, 1 for set");
0N/A // With a valid range (beg <= end), this test ensures that end != 0, as
0N/A // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
0N/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);
0N/A intptr_t nw = value ? (w | ~mr) : (w & mr);
0N/A while (true) {
0N/A intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
0N/A if (res == w) break;
0N/A w = *pw;
0N/A nw = value ? (w | ~mr) : (w & mr);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid BitMap::set_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 set_range_within_word(beg, bit_index(beg_full_word));
0N/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}
0N/A
0N/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);
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 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
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 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
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_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}
0N/A
0N/Abool BitMap::contains(const BitMap other) const {
0N/A assert(size() == other.size(), "must have same size");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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++) {
342N/A bm_word_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
0N/Abool BitMap::intersects(const BitMap other) const {
0N/A assert(size() == other.size(), "must have same size");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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;
0N/A}
0N/A
0N/Avoid BitMap::set_union(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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 dest_map[index] = dest_map[index] & other_map[index];
0N/A }
0N/A}
0N/A
0N/A
342N/Avoid BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
342N/A assert(other.size() >= offset, "offset not in range");
342N/A assert(other.size() - offset >= size(), "other not large enough");
342N/A // XXX Ideally, we would remove this restriction.
342N/A guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
342N/A "Only handle aligned cases so far.");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_t* other_map = other.map();
342N/A idx_t offset_word_ind = word_index(offset);
342N/A idx_t size = size_in_words();
342N/A for (idx_t index = 0; index < size; index++) {
342N/A dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
342N/A }
342N/A}
342N/A
0N/Abool BitMap::set_union_with_result(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A bool changed = false;
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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 idx_t temp = map(index) | other_map[index];
0N/A changed = changed || (temp != map(index));
0N/A map()[index] = temp;
0N/A }
0N/A return changed;
0N/A}
0N/A
0N/A
0N/Abool BitMap::set_difference_with_result(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A bool changed = false;
342N/A bm_word_t* dest_map = map();
342N/A bm_word_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size; index++) {
342N/A bm_word_t temp = dest_map[index] & ~(other_map[index]);
0N/A changed = changed || (temp != dest_map[index]);
0N/A dest_map[index] = temp;
0N/A }
0N/A return changed;
0N/A}
0N/A
0N/A
0N/Abool BitMap::set_intersection_with_result(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
0N/A bool changed = false;
342N/A bm_word_t* dest_map = map();
342N/A bm_word_t* other_map = other.map();
0N/A idx_t size = size_in_words();
0N/A for (idx_t index = 0; index < size; index++) {
342N/A bm_word_t orig = dest_map[index];
342N/A bm_word_t temp = orig & other_map[index];
0N/A changed = changed || (temp != orig);
0N/A dest_map[index] = temp;
0N/A }
0N/A return changed;
0N/A}
0N/A
0N/A
0N/Avoid BitMap::set_from(BitMap other) {
0N/A assert(size() == other.size(), "must have same size");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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 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");
342N/A bm_word_t* dest_map = map();
342N/A bm_word_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 {
342N/A bm_word_t* word = map();
0N/A idx_t rest = size();
0N/A for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342N/A if (*word != (bm_word_t) AllBits) return false;
0N/A word++;
0N/A }
342N/A return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
0N/A}
0N/A
0N/A
0N/Abool BitMap::is_empty() const {
342N/A bm_word_t* word = map();
0N/A idx_t rest = size();
0N/A for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342N/A if (*word != (bm_word_t) NoBits) return false;
0N/A word++;
0N/A }
342N/A return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
0N/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
0N/A// currently sampled will not be seen. Note also that the
0N/A// interval [leftOffset, rightOffset) is right open.
342N/Abool 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);
0N/A idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
0N/A for (idx_t index = startIndex, offset = leftOffset;
0N/A offset < rightOffset && index < endIndex;
0N/A offset = (++index) << LogBitsPerWord) {
0N/A idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
342N/A for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
0N/A if (rest & 1) {
342N/A if (!blk->do_bit(offset)) return false;
0N/A // resample at each closure application
0N/A // (see, for instance, CMS bug 4525989)
0N/A rest = map(index) >> (offset & (BitsPerWord -1));
0N/A }
0N/A rest = rest >> 1;
0N/A }
0N/A }
342N/A return true;
342N/A}
342N/A
342N/ABitMap::idx_t* BitMap::_pop_count_table = NULL;
342N/A
342N/Avoid BitMap::init_pop_count_table() {
342N/A if (_pop_count_table == NULL) {
3863N/A BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
342N/A for (uint i = 0; i < 256; i++) {
342N/A table[i] = num_set_bits(i);
342N/A }
342N/A
342N/A intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table,
342N/A (intptr_t*) &_pop_count_table,
342N/A (intptr_t) NULL_WORD);
342N/A if (res != NULL_WORD) {
342N/A guarantee( _pop_count_table == (void*) res, "invariant" );
3863N/A FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
342N/A }
342N/A }
0N/A}
0N/A
342N/ABitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
342N/A idx_t bits = 0;
0N/A
342N/A while (w != 0) {
342N/A while ((w & 1) == 0) {
342N/A w >>= 1;
0N/A }
342N/A bits++;
342N/A w >>= 1;
0N/A }
342N/A return bits;
0N/A}
0N/A
342N/ABitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
342N/A assert(_pop_count_table != NULL, "precondition");
342N/A return _pop_count_table[c];
342N/A}
0N/A
342N/ABitMap::idx_t BitMap::count_one_bits() const {
342N/A init_pop_count_table(); // If necessary.
342N/A idx_t sum = 0;
342N/A typedef unsigned char uchar;
342N/A for (idx_t i = 0; i < size_in_words(); i++) {
342N/A bm_word_t w = map()[i];
342N/A for (size_t j = 0; j < sizeof(bm_word_t); j++) {
342N/A sum += num_set_bits_from_table(uchar(w & 255));
342N/A w >>= 8;
0N/A }
0N/A }
342N/A return sum;
0N/A}
0N/A
342N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid BitMap::print_on(outputStream* st) const {
0N/A tty->print("Bitmap(%d):", size());
0N/A for (idx_t index = 0; index < size(); index++) {
0N/A tty->print("%c", at(index) ? '1' : '0');
0N/A }
0N/A tty->cr();
0N/A}
0N/A
0N/A#endif
0N/A
0N/A
342N/ABitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
0N/A : _bits_per_slot(bits_per_slot)
0N/A , _map(map, size_in_slots * bits_per_slot)
0N/A{
0N/A}
0N/A
0N/A
0N/ABitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
0N/A : _bits_per_slot(bits_per_slot)
0N/A , _map(size_in_slots * bits_per_slot)
0N/A{
0N/A}