annotate agent/src/share/classes/sun/jvm/hotspot/oops/Symbol.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.types.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // A Symbol is a canonicalized string.
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // All Symbols reside in global symbolTable.
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 public class Symbol extends Oop {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 static {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 VM.registerVMInitializedObserver(new Observer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 public void update(Observable o, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 initialize(VM.getVM().getTypeDataBase());
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43 });
a61af66fc99e Initial load
duke
parents:
diff changeset
44 }
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 Type type = db.lookupType("symbolOopDesc");
a61af66fc99e Initial load
duke
parents:
diff changeset
48 length = new CIntField(type.getCIntegerField("_length"), 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
49 baseOffset = type.getField("_body").getOffset();
a61af66fc99e Initial load
duke
parents:
diff changeset
50 }
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // Format:
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // [header]
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // [klass ]
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // [length] byte size of uft8 string
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // ..body..
a61af66fc99e Initial load
duke
parents:
diff changeset
57
a61af66fc99e Initial load
duke
parents:
diff changeset
58 Symbol(OopHandle handle, ObjectHeap heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
59 super(handle, heap);
a61af66fc99e Initial load
duke
parents:
diff changeset
60 }
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 public boolean isSymbol() { return true; }
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 private static long baseOffset; // tells where the array part starts
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // Fields
a61af66fc99e Initial load
duke
parents:
diff changeset
67 private static CIntField length;
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // Accessors for declared fields
a61af66fc99e Initial load
duke
parents:
diff changeset
70 public long getLength() { return length.getValue(this); }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 public byte getByteAt(long index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 return getHandle().getJByteAt(baseOffset + index);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75
a61af66fc99e Initial load
duke
parents:
diff changeset
76 public boolean equals(byte[] modUTF8Chars) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 int l = (int) getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
78 if (l != modUTF8Chars.length) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 while (l-- > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
80 if (modUTF8Chars[l] != getByteAt(l)) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 Assert.that(l == -1, "we should be at the beginning");
a61af66fc99e Initial load
duke
parents:
diff changeset
84 }
a61af66fc99e Initial load
duke
parents:
diff changeset
85 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 public byte[] asByteArray() {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 int length = (int) getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
90 byte [] result = new byte [length];
a61af66fc99e Initial load
duke
parents:
diff changeset
91 for (int index = 0; index < length; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 result[index] = getByteAt(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 }
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 public String asString() {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // Decode the byte array and return the string.
a61af66fc99e Initial load
duke
parents:
diff changeset
99 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return readModifiedUTF8(asByteArray());
a61af66fc99e Initial load
duke
parents:
diff changeset
101 } catch(IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 public boolean startsWith(String str) {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 return asString().startsWith(str);
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 public void printValueOn(PrintStream tty) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 tty.print("#" + asString());
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 public long getObjectSize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 return alignObjectSize(baseOffset + getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 void iterateFields(OopVisitor visitor, boolean doVMFields) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 super.iterateFields(visitor, doVMFields);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 if (doVMFields) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 visitor.doCInt(length, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 int length = (int) getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
123 for (int index = 0; index < length; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 visitor.doByte(new ByteField(new IndexableFieldIdentifier(index), baseOffset + index, false), true);
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 /** Note: this comparison is used for vtable sorting only; it
a61af66fc99e Initial load
duke
parents:
diff changeset
130 doesn't matter what order it defines, as long as it is a total,
a61af66fc99e Initial load
duke
parents:
diff changeset
131 time-invariant order Since symbolOops are in permSpace, their
a61af66fc99e Initial load
duke
parents:
diff changeset
132 relative order in memory never changes, so use address
a61af66fc99e Initial load
duke
parents:
diff changeset
133 comparison for speed. */
a61af66fc99e Initial load
duke
parents:
diff changeset
134 public int fastCompare(Symbol other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 return (int) getHandle().minus(other.getHandle());
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 private static String readModifiedUTF8(byte[] buf) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 final int len = buf.length;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 byte[] tmp = new byte[len + 2];
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // write modified UTF-8 length as short in big endian
a61af66fc99e Initial load
duke
parents:
diff changeset
142 tmp[0] = (byte) ((len >>> 8) & 0xFF);
a61af66fc99e Initial load
duke
parents:
diff changeset
143 tmp[1] = (byte) ((len >>> 0) & 0xFF);
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // copy the data
a61af66fc99e Initial load
duke
parents:
diff changeset
145 System.arraycopy(buf, 0, tmp, 2, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(tmp));
a61af66fc99e Initial load
duke
parents:
diff changeset
147 return dis.readUTF();
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }