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