1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.gc.z; 26 27 import java.util.HashMap; 28 29 import sun.jvm.hotspot.runtime.VM; 30 import sun.jvm.hotspot.utilities.BitMap; 31 import sun.jvm.hotspot.utilities.BitMapInterface; 32 33 /** Discontiguous bitmap for ZGC. */ 34 public class ZExternalBitMap implements BitMapInterface { 35 private ZPageTable pageTable; 36 private final long oopSize; 37 38 private HashMap<ZPage, BitMap> pageToBitMap = new HashMap<ZPage, BitMap>(); 39 40 public ZExternalBitMap(ZCollectedHeap collectedHeap) { 41 pageTable = collectedHeap.heap().pageTable(); 42 oopSize = VM.getVM().getOopSize(); 43 } 44 45 private ZPage getPage(long zOffset) { 46 if (zOffset > ZGlobals.ZAddressOffsetMask) { 47 throw new RuntimeException("Not a Z offset: " + zOffset); 48 } 49 50 ZPage page = pageTable.get(ZUtils.longToAddress(zOffset)); 51 if (page == null) { 52 throw new RuntimeException("Address not in pageTable: " + zOffset); 53 } 54 return page; 55 } 56 57 private BitMap getOrAddBitMap(ZPage page) { 58 BitMap bitMap = pageToBitMap.get(page); 59 if (bitMap == null) { 60 long size = page.size(); 61 62 long maxNumObjects = size >>> page.object_alignment_shift(); 63 if (maxNumObjects > Integer.MAX_VALUE) { 64 throw new RuntimeException("int overflow"); 65 } 66 int intMaxNumObjects = (int)maxNumObjects; 67 68 bitMap = new BitMap(intMaxNumObjects); 69 pageToBitMap.put(page, bitMap); 70 } 71 72 return bitMap; 73 } 74 75 private int pageLocalBitMapIndex(ZPage page, long zOffset) { 76 long pageLocalZOffset = zOffset - page.start(); 77 return (int)(pageLocalZOffset >>> page.object_alignment_shift()); 78 } 79 80 private long convertToZOffset(long offset) { 81 long addr = ZGlobals.ZAddressSpaceStart + oopSize * offset; 82 return addr & ZGlobals.ZAddressOffsetMask; 83 } 84 85 @Override 86 public boolean at(long offset) { 87 long zOffset = convertToZOffset(offset); 88 ZPage page = getPage(zOffset); 89 BitMap bitMap = getOrAddBitMap(page); 90 int index = pageLocalBitMapIndex(page, zOffset); 91 92 return bitMap.at(index); 93 } 94 95 @Override 96 public void atPut(long offset, boolean value) { 97 long zOffset = convertToZOffset(offset); 98 ZPage page = getPage(zOffset); 99 BitMap bitMap = getOrAddBitMap(page); 100 int index = pageLocalBitMapIndex(page, zOffset); 101 102 bitMap.atPut(index, value); 103 } 104 105 @Override 106 public void clear() { 107 for (BitMap bitMap : pageToBitMap.values()) { 108 bitMap.clear(); 109 } 110 } 111 }