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