1 /*
  2  * Copyright (c) 2000, 2020, 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.memory;
 26 
 27 import java.io.PrintStream;
 28 import sun.jvm.hotspot.utilities.Observable;
 29 import sun.jvm.hotspot.utilities.Observer;
 30 
 31 import sun.jvm.hotspot.debugger.Address;
 32 import sun.jvm.hotspot.debugger.OopHandle;
 33 import sun.jvm.hotspot.gc.epsilon.EpsilonHeap;
 34 import sun.jvm.hotspot.gc.g1.G1CollectedHeap;
 35 import sun.jvm.hotspot.gc.parallel.ParallelScavengeHeap;
 36 import sun.jvm.hotspot.gc.serial.SerialHeap;
 37 import sun.jvm.hotspot.gc.shared.CollectedHeap;
 38 import sun.jvm.hotspot.gc.shenandoah.ShenandoahGenerationalHeap;
 39 import sun.jvm.hotspot.gc.shenandoah.ShenandoahHeap;
 40 import sun.jvm.hotspot.gc.x.XCollectedHeap;
 41 import sun.jvm.hotspot.gc.z.ZCollectedHeap;
 42 import sun.jvm.hotspot.oops.Oop;
 43 import sun.jvm.hotspot.runtime.BasicType;
 44 import sun.jvm.hotspot.runtime.VM;
 45 import sun.jvm.hotspot.runtime.VMObject;
 46 import sun.jvm.hotspot.runtime.VirtualConstructor;
 47 import sun.jvm.hotspot.types.AddressField;
 48 import sun.jvm.hotspot.types.CIntegerField;
 49 import sun.jvm.hotspot.types.Type;
 50 import sun.jvm.hotspot.types.TypeDataBase;
 51 
 52 
 53 public class Universe {
 54   private static AddressField collectedHeapField;
 55   private static VirtualConstructor heapConstructor;
 56 
 57   static {
 58     VM.registerVMInitializedObserver(new Observer() {
 59         public void update(Observable o, Object data) {
 60           initialize(VM.getVM().getTypeDataBase());
 61         }
 62       });
 63   }
 64 
 65   private static boolean typeExists(TypeDataBase db, String type) {
 66       try {
 67           db.lookupType(type);
 68       } catch (RuntimeException e) {
 69           return false;
 70       }
 71       return true;
 72   }
 73 
 74   private static void addHeapTypeIfInDB(TypeDataBase db, Class<? extends VMObject> heapClass) {
 75       String heapName = heapClass.getSimpleName();
 76       if (typeExists(db, heapName)) {
 77           heapConstructor.addMapping(heapName, heapClass);
 78       }
 79   }
 80 
 81   private static synchronized void initialize(TypeDataBase db) {
 82     Type type = db.lookupType("Universe");
 83 
 84     collectedHeapField = type.getAddressField("_collectedHeap");
 85 
 86     heapConstructor = new VirtualConstructor(db);
 87     addHeapTypeIfInDB(db, SerialHeap.class);
 88     addHeapTypeIfInDB(db, ParallelScavengeHeap.class);
 89     addHeapTypeIfInDB(db, G1CollectedHeap.class);
 90     addHeapTypeIfInDB(db, EpsilonHeap.class);
 91     addHeapTypeIfInDB(db, XCollectedHeap.class);
 92     addHeapTypeIfInDB(db, ZCollectedHeap.class);
 93     addHeapTypeIfInDB(db, ShenandoahHeap.class);
 94     addHeapTypeIfInDB(db, ShenandoahGenerationalHeap.class);
 95 
 96     UniverseExt.initialize(heapConstructor);
 97   }
 98 
 99   public Universe() {
100   }
101   public CollectedHeap heap() {
102     return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
103   }
104 
105 
106   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
107   public boolean isIn(Address p) {
108     return heap().isIn(p);
109   }
110 
111   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
112   public boolean isInReserved(Address p) {
113     return heap().isInReserved(p);
114   }
115 
116   public void print() { printOn(System.out); }
117   public void printOn(PrintStream tty) {
118     heap().printOn(tty);
119   }
120 
121   // Check whether an element of a typeArrayOop with the given type must be
122   // aligned 0 mod 8.  The typeArrayOop itself must be aligned at least this
123   // strongly.
124   public static boolean elementTypeShouldBeAligned(BasicType type) {
125     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
126   }
127 
128   // Check whether an object field (static/non-static) of the given type must be
129   // aligned 0 mod 8.
130   public static boolean fieldTypeShouldBeAligned(BasicType type) {
131     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
132   }
133 }