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