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 private static Klass getKlass(Mark mark) { 85 assert(VM.getVM().isCompactObjectHeadersEnabled()); 86 return mark.getKlass(); 87 } 88 89 public Klass getKlass() { 90 if (VM.getVM().isCompactObjectHeadersEnabled()) { 91 assert(VM.getVM().isCompressedKlassPointersEnabled()); 92 return getKlass(getMark()); 93 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 94 return (Klass)compressedKlass.getValue(getHandle()); 95 } else { 96 return (Klass)klass.getValue(getHandle()); 97 } 98 } 99 100 public boolean isA(Klass k) { 101 return getKlass().isSubtypeOf(k); 102 } 103 104 // Returns the byte size of this object 105 public long getObjectSize() { 106 Klass k = getKlass(); 107 // All other types should be overriding getObjectSize directly 108 return ((InstanceKlass)k).getObjectSize(this); 109 } 110 111 // Type test operations 112 public boolean isInstance() { return false; } 113 public boolean isInstanceRef() { return false; } 114 public boolean isArray() { return false; } 115 public boolean isObjArray() { return false; } 116 public boolean isTypeArray() { return false; } 117 public boolean isThread() { return false; } 118 119 // Align the object size. 120 public static long alignObjectSize(long size) { 121 return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes()); 122 } 123 124 // All vm's align longs, so pad out certain offsets. 125 public static long alignObjectOffset(long offset) { 126 return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong()); 127 } 128 129 public boolean equals(Object obj) { 130 if (obj instanceof Oop other) { 131 return getHandle().equals(other.getHandle()); 132 } 133 return false; 134 } 135 136 public int hashCode() { return getHandle().hashCode(); } 137 138 /** Identity hash in the target VM */ 139 public long identityHash() { 140 Mark mark = getMark(); 141 if (mark.isUnlocked() && (!mark.hasNoHash())) { 142 return (int) mark.hash(); 143 } else if (mark.isMarked()) { 144 return (int) mark.hash(); 145 } else { 146 return slowIdentityHash(); 147 } 148 } 149 150 public long slowIdentityHash() { 151 return VM.getVM().getObjectSynchronizer().identityHashValueFor(this); 152 } 153 154 public void iterate(OopVisitor visitor, boolean doVMFields) { 155 visitor.setObj(this); 156 visitor.prologue(); 157 iterateFields(visitor, doVMFields); 158 visitor.epilogue(); 159 } 160 161 void iterateFields(OopVisitor visitor, boolean doVMFields) { 162 if (doVMFields) { 163 visitor.doCInt(mark, true); 164 if (!VM.getVM().isCompactObjectHeadersEnabled()) { 165 if (VM.getVM().isCompressedKlassPointersEnabled()) { 166 visitor.doMetadata(compressedKlass, true); 167 } else { 168 visitor.doMetadata(klass, true); 169 } 170 } 171 } 172 } 173 174 public void print() { printOn(System.out); } 175 public void printValue() { printValueOn(System.out); } 176 public void printRaw() { printRawOn(System.out); } 177 178 public static void printOopValueOn(Oop obj, PrintStream tty) { 179 if (obj == null) { 180 tty.print("null"); 181 } else { 182 obj.printValueOn(tty); 183 tty.print(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(obj.getHandle())); 184 } 185 } 186 187 public static void printOopAddressOn(Oop obj, PrintStream tty) { 188 if (obj == null) { 189 tty.print("null"); 190 } else { 191 tty.print(obj.getHandle().toString()); 192 } 193 } 194 195 public void printOn(PrintStream tty) { 196 OopPrinter printer = new OopPrinter(tty); 197 iterate(printer, true); 198 } 199 200 public void printValueOn(PrintStream tty) { 201 try { 202 tty.print("Oop for " + getKlass().getName().asString()); 203 } catch (java.lang.NullPointerException e) { 204 tty.print("Oop"); 205 } 206 } 207 208 public void printRawOn(PrintStream tty) { 209 tty.print("Dumping raw memory for "); 210 printValueOn(tty); 211 tty.println(); 212 long size = getObjectSize() * 4; 213 for (long i = 0; i < size; i += 4) { 214 long memVal = getHandle().getCIntegerAt(i, 4, true); 215 tty.println(Long.toHexString(memVal)); 216 } 217 } 218 219 public boolean verify() { return true;} 220 221 public static Klass getKlassForOopHandle(OopHandle handle) { 222 if (handle == null) { 223 return null; 224 } 225 if (VM.getVM().isCompactObjectHeadersEnabled()) { 226 Mark mark = new Mark(handle); 227 return getKlass(mark); 228 } else if (VM.getVM().isCompressedKlassPointersEnabled()) { 229 return (Klass)Metadata.instantiateWrapperFor(handle.getCompKlassAddressAt(compressedKlass.getOffset())); 230 } else { 231 return (Klass)Metadata.instantiateWrapperFor(handle.getAddressAt(klass.getOffset())); 232 } 233 } 234 };