Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/BitMapSegmented.java
41161 views
/*1* Copyright (c) 2019, 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;2526/** A BitMap implementing the BitMapInterface. */27public class BitMapSegmented implements BitMapInterface {28private static final int SegmentSizeBits = 30;29private static final int SegmentSize = 1 << (SegmentSizeBits - 1);3031public BitMapSegmented(long sizeInBits) {32this.size = sizeInBits;3334if (sizeInBits == 0) {35segmentBitMaps = new BitMap[0];36return;37}3839int lastSegmentSize = (int)(sizeInBits % SegmentSize);4041int segments = segmentIndex(sizeInBits - 1) + 1;42int completeSegments = segments - ((lastSegmentSize != 0) ? 1 : 0);4344segmentBitMaps = new BitMap[segments];4546for (int i = 0; i < completeSegments; i++) {47segmentBitMaps[i] = new BitMap(SegmentSize);48}4950if (lastSegmentSize != 0) {51segmentBitMaps[completeSegments] = new BitMap(lastSegmentSize);52}53}5455public long size() {56return size;57}5859// Accessors60public boolean at(long offset) {61assert offset < size;6263int segmentIndex = segmentIndex(offset);64int segmentOffset = segmentOffset(offset);65return segmentBitMaps[segmentIndex].at(segmentOffset);66}6768public void atPut(long offset, boolean value) {69assert offset < size;7071int segmentIndex = segmentIndex(offset);72int segmentOffset = segmentOffset(offset);73segmentBitMaps[segmentIndex].atPut(segmentOffset, value);74}7576public void clear() {77for (BitMap map : segmentBitMaps) {78map.clear();79}80}8182//----------------------------------------------------------------------83// Internals only below this point84//85private final long size; // in bits86private final BitMap[] segmentBitMaps;8788private int segmentIndex(long offset) {89long longIndex = offset / SegmentSize;9091assert longIndex < Integer.MAX_VALUE;92return (int)longIndex;93}9495private int segmentOffset(long offset) {96return (int)(offset % SegmentSize);97}98}99100101