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.ShenandoahHeap;
 39 import sun.jvm.hotspot.gc.z.ZCollectedHeap;
 40 import sun.jvm.hotspot.oops.Oop;
 41 import sun.jvm.hotspot.runtime.BasicType;
 42 import sun.jvm.hotspot.runtime.VM;
 43 import sun.jvm.hotspot.runtime.VMObject;
 44 import sun.jvm.hotspot.runtime.VirtualConstructor;
 45 import sun.jvm.hotspot.types.AddressField;
 46 import sun.jvm.hotspot.types.CIntegerField;
 47 import sun.jvm.hotspot.types.Type;
 48 import sun.jvm.hotspot.types.TypeDataBase;
 49 
 50 
 51 public class Universe {
 52   private static AddressField collectedHeapField;
 53   private static VirtualConstructor heapConstructor;
 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   private static boolean typeExists(TypeDataBase db, String type) {
 64       try {
 65           db.lookupType(type);
 66       } catch (RuntimeException e) {
 67           return false;
 68       }
 69       return true;
 70   }
 71 
 72   private static void addHeapTypeIfInDB(TypeDataBase db, Class<? extends VMObject> heapClass) {
 73       String heapName = heapClass.getSimpleName();
 74       if (typeExists(db, heapName)) {
 75           heapConstructor.addMapping(heapName, heapClass);
 76       }
 77   }
 78 
 79   private static synchronized void initialize(TypeDataBase db) {
 80     Type type = db.lookupType("Universe");
 81 
 82     collectedHeapField = type.getAddressField("_collectedHeap");
 83 
 84     heapConstructor = new VirtualConstructor(db);
 85     addHeapTypeIfInDB(db, SerialHeap.class);
 86     addHeapTypeIfInDB(db, ParallelScavengeHeap.class);
 87     addHeapTypeIfInDB(db, G1CollectedHeap.class);
 88     addHeapTypeIfInDB(db, EpsilonHeap.class);
 89     addHeapTypeIfInDB(db, ZCollectedHeap.class);
 90     addHeapTypeIfInDB(db, ShenandoahHeap.class);
 91 
 92     UniverseExt.initialize(heapConstructor);
 93   }
 94 
 95   public Universe() {
 96   }
 97   public CollectedHeap heap() {
 98     return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
 99   }
100 
101 
102   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
103   public boolean isIn(Address p) {
104     return heap().isIn(p);
105   }
106 
107   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
108   public boolean isInReserved(Address p) {
109     return heap().isInReserved(p);
110   }
111 
112   public void print() { printOn(System.out); }
113   public void printOn(PrintStream tty) {
114     heap().printOn(tty);
115   }
116 
117   // Check whether an object field (static/non-static) of the given type must be
118   // aligned 0 mod 8.
119   public static boolean fieldTypeShouldBeAligned(BasicType type) {
120     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
121   }
122 }