Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/BitMap.java
41161 views
/*1* Copyright (c) 2001, 2007, 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*/2324package sun.jvm.hotspot.utilities;2526import sun.jvm.hotspot.debugger.*;2728/** Manages a bitmap of the specified bit size */29public class BitMap {30public BitMap(int sizeInBits) {31this.size = sizeInBits;32int nofWords = sizeInWords();33data = new int[nofWords];34}3536public int size() {37return size;38}3940// Accessors41public boolean at(int offset) {42if (Assert.ASSERTS_ENABLED) {43Assert.that(offset>=0 && offset < size(), "BitMap index out of bounds");44}45return Bits.isSetNthBit(wordFor(offset), offset % bitsPerWord);46}4748public void atPut(int offset, boolean value) {49int index = indexFor(offset);50int pos = offset % bitsPerWord;51if (value) {52data[index] = Bits.setNthBit(data[index], pos);53} else {54data[index] = Bits.clearNthBit(data[index], pos);55}56}5758public void set_size(int value) {59size = value;60}6162public void set_map(Address addr) {63for (int i=0; i<sizeInWords(); i++) {64data[i] = (int) addr.getCIntegerAt(0, bytesPerWord, true);65addr = addr.addOffsetTo(bytesPerWord);66}6768}6970public void clear() {71for (int i = 0; i < sizeInWords(); i++) {72data[i] = Bits.NoBits;73}74}7576public void iterate(BitMapClosure blk) {77for (int index = 0; index < sizeInWords(); index++) {78int rest = data[index];79for (int offset = index * bitsPerWord; rest != Bits.NoBits; offset++) {80if (rest % 2 == 1) {81if (offset < size()) {82blk.doBit(offset);83} else {84return; // Passed end of map85}86}87rest = rest >>> 1;88}89}90}9192/** Sets this bitmap to the logical union of it and the93argument. Both bitmaps must be the same size. Returns true if a94change was caused in this bitmap. */95public boolean setUnion(BitMap other) {96if (Assert.ASSERTS_ENABLED) {97Assert.that(size() == other.size(), "must have same size");98}99boolean changed = false;100for (int index = 0; index < sizeInWords(); index++) {101int temp = data[index] | other.data[index];102changed = changed || (temp != data[index]);103data[index] = temp;104}105return changed;106}107108/** Sets this bitmap to the logical intersection of it and the109argument. Both bitmaps must be the same size. */110public void setIntersection(BitMap other) {111if (Assert.ASSERTS_ENABLED) {112Assert.that(size() == other.size(), "must have same size");113}114for (int index = 0; index < sizeInWords(); index++) {115data[index] = data[index] & (other.data[index]);116}117}118119/** Sets this bitmap to the contents of the argument. Both bitmaps120must be the same size. */121public void setFrom(BitMap other) {122if (Assert.ASSERTS_ENABLED) {123Assert.that(size() == other.size(), "must have same size");124}125for (int index = 0; index < sizeInWords(); index++) {126data[index] = other.data[index];127}128}129130/** Sets this bitmap to the logical difference between it and the131argument; that is, any bits that are set in the argument are132cleared in this bitmap. Both bitmaps must be the same size.133Returns true if a change was caused in this bitmap. */134public boolean setDifference(BitMap other) {135if (Assert.ASSERTS_ENABLED) {136Assert.that(size() == other.size(), "must have same size");137}138boolean changed = false;139for (int index = 0; index < sizeInWords(); index++) {140int temp = data[index] & ~(other.data[index]);141changed = changed || (temp != data[index]);142data[index] = temp;143}144return changed;145}146147/** Both bitmaps must be the same size. */148public boolean isSame(BitMap other) {149if (Assert.ASSERTS_ENABLED) {150Assert.that(size() == other.size(), "must have same size");151}152for (int index = 0; index < sizeInWords(); index++) {153if (data[index] != (other.data[index])) return false;154}155return true;156}157158public int getNextOneOffset(int l_offset, int r_offset) {159if (l_offset == r_offset) {160return l_offset;161}162163int index = indexFor(l_offset);164int r_index = indexFor(r_offset);165int res_offset = l_offset;166167int pos = bitInWord(res_offset);168int res = data[index] >> pos;169170if (res != 0) {171// find the position of the 1-bit172for (; (res & 1) == 0; res_offset++) {173res = res >> 1;174}175return res_offset;176}177// skip over all word length 0-bit runs178for (index++; index < r_index; index++) {179res = data[index];180if (res != 0) {181// found a 1, return the offset182for (res_offset = index * bitsPerWord; (res & 1) == 0; res_offset++) {183res = res >> 1;184}185return res_offset;186}187}188return r_offset;189}190191//----------------------------------------------------------------------192// Internals only below this point193//194private int size; // in bits195private int[] data;196private static final int bitsPerWord = 32;197private static final int bytesPerWord = 4;198199private int sizeInWords() {200return (size() + bitsPerWord - 1) / bitsPerWord;201}202203private int indexFor(int offset) {204return offset / bitsPerWord;205}206207private int wordFor(int offset) {208return data[offset / bitsPerWord];209}210211private int bitInWord(int offset) {212return offset & (bitsPerWord - 1);213}214}215216217