1 /*
2 * Copyright (c) 2000, 2026, 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 Type markType = db.lookupType("markWord");
51 headerSize = markType.getSize();
52 } else {
53 headerSize = type.getSize();
54 compressedKlass = new NarrowKlassField(type.getAddressField("_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 NarrowKlassField compressedKlass;
78
79 // Accessors for declared fields
80 public Mark getMark() { return new Mark(getHandle()); }
81
82 public Klass getKlass() {
83 if (VM.getVM().isCompactObjectHeadersEnabled()) {
84 return getMark().getKlass();
85 } else {
86 return (Klass)compressedKlass.getValue(getHandle());
87 }
88 }
89
90 public boolean isA(Klass k) {
91 return getKlass().isSubtypeOf(k);
92 }
93
94 // Returns the byte size of this object
95 public long getObjectSize() {
96 Klass k = getKlass();
97 // All other types should be overriding getObjectSize directly
98 return ((InstanceKlass)k).getObjectSize(this);
99 }
100
101 // Type test operations
102 public boolean isInstance() { return false; }
103 public boolean isInstanceRef() { return false; }
104 public boolean isArray() { return false; }
105 public boolean isObjArray() { return false; }
106 public boolean isTypeArray() { return false; }
107 public boolean isFlatArray() { return false; }
108 public boolean isThread() { return false; }
109
110 // Align the object size.
111 public static long alignObjectSize(long size) {
112 return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes());
113 }
114
115 // All vm's align longs, so pad out certain offsets.
116 public static long alignObjectOffset(long offset) {
117 return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong());
118 }
119
120 public boolean equals(Object obj) {
121 if (obj instanceof Oop other) {
122 return getHandle().equals(other.getHandle());
123 }
124 return false;
125 }
126
127 public int hashCode() { return getHandle().hashCode(); }
128
129 /** Identity hash in the target VM */
130 public long identityHash() {
131 Mark mark = getMark();
132 if (mark.isUnlocked() && (!mark.hasNoHash())) {
133 return (int) mark.hash();
134 } else if (mark.isMarked()) {
135 return (int) mark.hash();
136 } else {
137 return slowIdentityHash();
138 }
139 }
140
141 public long slowIdentityHash() {
142 return VM.getVM().getObjectSynchronizer().identityHashValueFor(this);
143 }
144
145 public void iterate(OopVisitor visitor, boolean doVMFields) {
146 visitor.setObj(this);
147 visitor.prologue();
148 iterateFields(visitor, doVMFields);
149 visitor.epilogue();
150 }
151
152 void iterateFields(OopVisitor visitor, boolean doVMFields) {
153 if (doVMFields) {
154 visitor.doCInt(mark, true);
155 if (!VM.getVM().isCompactObjectHeadersEnabled()) {
156 visitor.doMetadata(compressedKlass, 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(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(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 public static Klass getKlassForOopHandle(OopHandle handle) {
209 if (handle == null) {
210 return null;
211 }
212 if (VM.getVM().isCompactObjectHeadersEnabled()) {
213 Mark mark = new Mark(handle);
214 return mark.getKlass();
215 } else {
216 return (Klass)Metadata.instantiateWrapperFor(handle.getCompKlassAddressAt(compressedKlass.getOffset()));
217 }
218 }
219 };