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