1 /* 2 * Copyright (c) 2000, 2022, 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.oops; 26 27 import java.io.*; 28 import sun.jvm.hotspot.debugger.*; 29 import sun.jvm.hotspot.runtime.*; 30 import sun.jvm.hotspot.types.*; 31 import sun.jvm.hotspot.utilities.Observable; 32 import sun.jvm.hotspot.utilities.Observer; 33 34 // Oop represents the superclass for all types of 35 // objects in the HotSpot object heap. 36 37 public class Oop { 38 static { 39 VM.registerVMInitializedObserver(new Observer() { 40 public void update(Observable o, Object data) { 41 initialize(VM.getVM().getTypeDataBase()); 42 } 43 }); 44 } 45 46 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { 47 Type type = db.lookupType("oopDesc"); 48 mark = new CIntField(type.getCIntegerField("_mark"), 0); 49 if (VM.getVM().isCompactObjectHeadersEnabled()) { 50 Type markType = db.lookupType("markWord"); 51 headerSize = markType.getSize(); 52 } else { 53 headerSize = type.getSize(); 54 klass = new MetadataField(type.getAddressField("_metadata._klass"), 0); 55 compressedKlass = new NarrowKlassField(type.getAddressField("_metadata._compressed_klass"), 0); 56 } 57 } 58 59 private OopHandle handle; 60 private ObjectHeap heap; 61 62 Oop(OopHandle handle, ObjectHeap heap) { 63 this.handle = handle; 64 this.heap = heap; 65 } 66 67 ObjectHeap getHeap() { return heap; } 68 69 /** Should not be used or needed by most clients outside this 70 package; is needed, however, by {@link 71 sun.jvm.hotspot.utilities.MarkBits}. */ 72 public OopHandle getHandle() { return handle; } 73 74 private static long headerSize; 75 public static long getHeaderSize() { return headerSize; } // Header size in bytes. 76 77 private static CIntField mark; 78 private static MetadataField klass; 79 private static NarrowKlassField compressedKlass; 80 81 // Accessors for declared fields 82 public Mark getMark() { return new Mark(getHandle()); } 83 84 public Klass getKlass() { 85 if (VM.getVM().isCompactObjectHeadersEnabled()) { 86 assert(VM.getVM().isCompressedKlassPointersEnabled()); 87 return getMark().getKlass(); 88 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 89 return (Klass)compressedKlass.getValue(getHandle()); 90 } else { 91 return (Klass)klass.getValue(getHandle()); 92 } 93 } 94 95 public boolean isA(Klass k) { 96 return getKlass().isSubtypeOf(k); 97 } 98 99 // Returns the byte size of this object 100 public long getObjectSize() { 101 Klass k = getKlass(); 102 // All other types should be overriding getObjectSize directly 103 return ((InstanceKlass)k).getObjectSize(this); 104 } 105 106 // Type test operations 107 public boolean isInstance() { return false; } 108 public boolean isInstanceRef() { return false; } 109 public boolean isArray() { return false; } 110 public boolean isObjArray() { return false; } 111 public boolean isTypeArray() { return false; } 112 public boolean isThread() { return false; } 113 114 // Align the object size. 115 public static long alignObjectSize(long size) { 116 return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes()); 117 } 118 119 // All vm's align longs, so pad out certain offsets. 120 public static long alignObjectOffset(long offset) { 121 return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong()); 122 } 123 124 public boolean equals(Object obj) { 125 if (obj instanceof Oop other) { 126 return getHandle().equals(other.getHandle()); 127 } 128 return false; 129 } 130 131 public int hashCode() { return getHandle().hashCode(); } 132 133 /** Identity hash in the target VM */ 134 public long identityHash() { 135 Mark mark = getMark(); 136 if (mark.isUnlocked() && (!mark.hasNoHash())) { 137 return (int) mark.hash(); 138 } else if (mark.isMarked()) { 139 return (int) mark.hash(); 140 } else { 141 return slowIdentityHash(); 142 } 143 } 144 145 public long slowIdentityHash() { 146 return VM.getVM().getObjectSynchronizer().identityHashValueFor(this); 147 } 148 149 public void iterate(OopVisitor visitor, boolean doVMFields) { 150 visitor.setObj(this); 151 visitor.prologue(); 152 iterateFields(visitor, doVMFields); 153 visitor.epilogue(); 154 } 155 156 void iterateFields(OopVisitor visitor, boolean doVMFields) { 157 if (doVMFields) { 158 visitor.doCInt(mark, true); 159 if (!VM.getVM().isCompactObjectHeadersEnabled()) { 160 if (VM.getVM().isCompressedKlassPointersEnabled()) { 161 visitor.doMetadata(compressedKlass, true); 162 } else { 163 visitor.doMetadata(klass, true); 164 } 165 } 166 } 167 } 168 169 public void print() { printOn(System.out); } 170 public void printValue() { printValueOn(System.out); } 171 public void printRaw() { printRawOn(System.out); } 172 173 public static void printOopValueOn(Oop obj, PrintStream tty) { 174 if (obj == null) { 175 tty.print("null"); 176 } else { 177 obj.printValueOn(tty); 178 tty.print(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(obj.getHandle())); 179 } 180 } 181 182 public static void printOopAddressOn(Oop obj, PrintStream tty) { 183 if (obj == null) { 184 tty.print("null"); 185 } else { 186 tty.print(obj.getHandle().toString()); 187 } 188 } 189 190 public void printOn(PrintStream tty) { 191 OopPrinter printer = new OopPrinter(tty); 192 iterate(printer, true); 193 } 194 195 public void printValueOn(PrintStream tty) { 196 try { 197 tty.print("Oop for " + getKlass().getName().asString()); 198 } catch (java.lang.NullPointerException e) { 199 tty.print("Oop"); 200 } 201 } 202 203 public void printRawOn(PrintStream tty) { 204 tty.print("Dumping raw memory for "); 205 printValueOn(tty); 206 tty.println(); 207 long size = getObjectSize() * 4; 208 for (long i = 0; i < size; i += 4) { 209 long memVal = getHandle().getCIntegerAt(i, 4, true); 210 tty.println(Long.toHexString(memVal)); 211 } 212 } 213 214 public boolean verify() { return true;} 215 216 public static Klass getKlassForOopHandle(OopHandle handle) { 217 if (handle == null) { 218 return null; 219 } 220 if (VM.getVM().isCompactObjectHeadersEnabled()) { 221 Mark mark = new Mark(handle); 222 return mark.getKlass(); 223 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 224 return (Klass)Metadata.instantiateWrapperFor(handle.getCompKlassAddressAt(compressedKlass.getOffset())); 225 } else { 226 return (Klass)Metadata.instantiateWrapperFor(handle.getAddressAt(klass.getOffset())); 227 } 228 } 229 };