-
Notifications
You must be signed in to change notification settings - Fork 17
/
Oop.java
225 lines (192 loc) · 6.62 KB
/
Oop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package sun.jvm.hotspot.oops;
import java.io.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.types.*;
import sun.jvm.hotspot.utilities.Observable;
import sun.jvm.hotspot.utilities.Observer;
// Oop represents the superclass for all types of
// objects in the HotSpot object heap.
public class Oop {
static {
VM.registerVMInitializedObserver(new Observer() {
public void update(Observable o, Object data) {
initialize(VM.getVM().getTypeDataBase());
}
});
}
private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
Type type = db.lookupType("oopDesc");
mark = new CIntField(type.getCIntegerField("_mark"), 0);
if (!VM.getVM().isLP64()) {
klass = new MetadataField(type.getAddressField("_klass"), 0);
}
headerSize = type.getSize();
}
private OopHandle handle;
private ObjectHeap heap;
Oop(OopHandle handle, ObjectHeap heap) {
this.handle = handle;
this.heap = heap;
}
ObjectHeap getHeap() { return heap; }
/** Should not be used or needed by most clients outside this
package; is needed, however, by {@link
sun.jvm.hotspot.utilities.MarkBits}. */
public OopHandle getHandle() { return handle; }
private static long headerSize;
public static long getHeaderSize() { return headerSize; } // Header size in bytes.
private static CIntField mark;
private static MetadataField klass;
private static NarrowKlassField compressedKlass;
// Accessors for declared fields
public Mark getMark() { return new Mark(getHandle()); }
private static Klass getKlass(Mark mark) {
if (mark.hasMonitor()) {
ObjectMonitor mon = mark.monitor();
mark = mon.header();
}
return mark.getKlass();
}
public Klass getKlass() {
if (VM.getVM().isLP64()) {
assert(VM.getVM().isCompressedKlassPointersEnabled());
return getKlass(getMark());
} else {
return (Klass)klass.getValue(getHandle());
}
}
public boolean isA(Klass k) {
return getKlass().isSubtypeOf(k);
}
// Returns the byte size of this object
public long getObjectSize() {
Klass k = getKlass();
// All other types should be overriding getObjectSize directly
return ((InstanceKlass)k).getObjectSize(this);
}
// Type test operations
public boolean isInstance() { return false; }
public boolean isInstanceRef() { return false; }
public boolean isArray() { return false; }
public boolean isObjArray() { return false; }
public boolean isTypeArray() { return false; }
public boolean isThread() { return false; }
// Align the object size.
public static long alignObjectSize(long size) {
return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes());
}
// All vm's align longs, so pad out certain offsets.
public static long alignObjectOffset(long offset) {
return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong());
}
public boolean equals(Object obj) {
if (obj instanceof Oop other) {
return getHandle().equals(other.getHandle());
}
return false;
}
public int hashCode() { return getHandle().hashCode(); }
/** Identity hash in the target VM */
public long identityHash() {
Mark mark = getMark();
if (mark.isUnlocked() && (!mark.hasNoHash())) {
return (int) mark.hash();
} else if (mark.isMarked()) {
return (int) mark.hash();
} else {
return slowIdentityHash();
}
}
public long slowIdentityHash() {
return VM.getVM().getObjectSynchronizer().identityHashValueFor(this);
}
public void iterate(OopVisitor visitor, boolean doVMFields) {
visitor.setObj(this);
visitor.prologue();
iterateFields(visitor, doVMFields);
visitor.epilogue();
}
void iterateFields(OopVisitor visitor, boolean doVMFields) {
if (doVMFields) {
visitor.doCInt(mark, true);
if (!VM.getVM().isLP64()) {
visitor.doMetadata(klass, true);
}
}
}
public void print() { printOn(System.out); }
public void printValue() { printValueOn(System.out); }
public void printRaw() { printRawOn(System.out); }
public static void printOopValueOn(Oop obj, PrintStream tty) {
if (obj == null) {
tty.print("null");
} else {
obj.printValueOn(tty);
tty.print(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(obj.getHandle()));
}
}
public static void printOopAddressOn(Oop obj, PrintStream tty) {
if (obj == null) {
tty.print("null");
} else {
tty.print(obj.getHandle().toString());
}
}
public void printOn(PrintStream tty) {
OopPrinter printer = new OopPrinter(tty);
iterate(printer, true);
}
public void printValueOn(PrintStream tty) {
try {
tty.print("Oop for " + getKlass().getName().asString());
} catch (java.lang.NullPointerException e) {
tty.print("Oop");
}
}
public void printRawOn(PrintStream tty) {
tty.print("Dumping raw memory for ");
printValueOn(tty);
tty.println();
long size = getObjectSize() * 4;
for (long i = 0; i < size; i += 4) {
long memVal = getHandle().getCIntegerAt(i, 4, true);
tty.println(Long.toHexString(memVal));
}
}
public boolean verify() { return true;}
public static Klass getKlassForOopHandle(OopHandle handle) {
if (handle == null) {
return null;
}
if (VM.getVM().isLP64()) {
Mark mark = new Mark(handle);
return getKlass(mark);
} else {
return (Klass)Metadata.instantiateWrapperFor(handle.getAddressAt(klass.getOffset()));
}
}
};