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