1 /*
   2  * Copyright (c) 2011, 2017, 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.g1;
  26 
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import java.util.Observable;
  30 import java.util.Observer;
  31 import sun.jvm.hotspot.debugger.Address;
  32 import sun.jvm.hotspot.debugger.OopHandle;
  33 import sun.jvm.hotspot.gc.shared.CompactibleSpace;
  34 import sun.jvm.hotspot.memory.MemRegion;
  35 import sun.jvm.hotspot.runtime.VM;
  36 import sun.jvm.hotspot.runtime.VMObjectFactory;
  37 import sun.jvm.hotspot.types.AddressField;
  38 import sun.jvm.hotspot.types.CIntegerField;
  39 import sun.jvm.hotspot.types.Type;
  40 import sun.jvm.hotspot.types.TypeDataBase;
  41 import sun.jvm.hotspot.utilities.Assert;
  42 
  43 // Mirror class for HeapRegion. Currently we don't actually include
  44 // any of its fields but only iterate over it.
  45 
  46 public class HeapRegion extends CompactibleSpace {
  47     // static int GrainBytes;
  48     static private CIntegerField grainBytesField;
  49     static private AddressField topField;
  50     private static long typeFieldOffset;
  51     private static long pointerSize;
  52 
  53     private HeapRegionType type;
  54 
  55     static {
  56         VM.registerVMInitializedObserver(new Observer() {
  57                 public void update(Observable o, Object data) {
  58                     initialize(VM.getVM().getTypeDataBase());
  59                 }
  60             });
  61     }
  62 
  63     static private synchronized void initialize(TypeDataBase db) {
  64         Type t = db.lookupType("HeapRegion");
  65 
  66         grainBytesField = t.getCIntegerField("GrainBytes");
  67         topField = t.getAddressField("_top");
  68         typeFieldOffset = t.getField("_type").getOffset();
  69 
  70         pointerSize = db.lookupType("HeapRegion*").getSize();
  71     }
  72 
  73     static public long grainBytes() {
  74         return grainBytesField.getValue();
  75     }
  76 
  77     public HeapRegion(Address addr) {
  78         super(addr);
  79 
  80         if (Assert.ASSERTS_ENABLED) {
  81             Assert.that(addr instanceof OopHandle, "addr should be OopHandle");
  82         }
  83 
  84         Address typeAddr = addr.addOffsetToAsOopHandle(typeFieldOffset);
  85         type = (HeapRegionType)VMObjectFactory.newObject(
  86                                                 HeapRegionType.class, typeAddr);
  87     }
  88 
  89     public Address top() {
  90         return topField.getValue(addr);
  91     }
  92 
  93     @Override
  94     public List getLiveRegions() {
  95         List res = new ArrayList();
  96         res.add(new MemRegion(bottom(), top()));
  97         return res;
  98     }
  99 
 100     @Override
 101     public long used() {
 102         return top().minus(bottom());
 103     }
 104 
 105     @Override
 106     public long free() {
 107         return end().minus(top());
 108     }
 109 
 110     public boolean isFree() {
 111         return type.isFree();
 112     }
 113 
 114     public boolean isYoung() {
 115         return type.isYoung();
 116     }
 117 
 118     public boolean isHumongous() {
 119         return type.isHumongous();
 120     }
 121 
 122     public boolean isPinned() {
 123         return type.isPinned();
 124     }
 125 
 126     public boolean isOld() {
 127         return type.isOld();
 128     }
 129 
 130     public static long getPointerSize() {
 131         return pointerSize;
 132     }
 133 }