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