annotate src/share/vm/classfile/symbolTable.cpp @ 665:c89f86385056

6814659: separable cleanups and subroutines for 6655638 Summary: preparatory but separable changes for method handles Reviewed-by: kvn, never
author jrose
date Fri, 20 Mar 2009 23:19:36 -0700
parents 98cb887364d3
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
665
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
0
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 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_symbolTable.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // --------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 SymbolTable* SymbolTable::_the_table = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 // Lookup a symbol in a bucket.
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 symbolOop SymbolTable::lookup(int index, const char* name,
a61af66fc99e Initial load
duke
parents:
diff changeset
35 int len, unsigned int hash) {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 for (HashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 if (e->hash() == hash) {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 symbolOop sym = symbolOop(e->literal());
a61af66fc99e Initial load
duke
parents:
diff changeset
39 if (sym->equals(name, len)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 return sym;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 }
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43 }
a61af66fc99e Initial load
duke
parents:
diff changeset
44 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // We take care not to be blocking while holding the
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // SymbolTable_lock. Otherwise, the system might deadlock, since the
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // symboltable is used during compilation (VM_thread) The lock free
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // synchronization is simplified by the fact that we do not delete
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // entries in the symbol table during normal execution (only during
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // safepoints).
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 symbolOop SymbolTable::lookup(const char* name, int len, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 unsigned int hashValue = hash_symbol(name, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
57 int index = the_table()->hash_to_index(hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 symbolOop s = the_table()->lookup(index, name, len, hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // Found
a61af66fc99e Initial load
duke
parents:
diff changeset
62 if (s != NULL) return s;
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // Otherwise, add to symbol to table
a61af66fc99e Initial load
duke
parents:
diff changeset
65 return the_table()->basic_add(index, (u1*)name, len, hashValue, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 symbolOop SymbolTable::lookup(symbolHandle sym, int begin, int end, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 char* buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 int index, len;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 unsigned int hashValue;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 char* name;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 debug_only(No_Safepoint_Verifier nsv;)
a61af66fc99e Initial load
duke
parents:
diff changeset
75
a61af66fc99e Initial load
duke
parents:
diff changeset
76 name = (char*)sym->base() + begin;
a61af66fc99e Initial load
duke
parents:
diff changeset
77 len = end - begin;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 hashValue = hash_symbol(name, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
79 index = the_table()->hash_to_index(hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 symbolOop s = the_table()->lookup(index, name, len, hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // Found
a61af66fc99e Initial load
duke
parents:
diff changeset
83 if (s != NULL) return s;
a61af66fc99e Initial load
duke
parents:
diff changeset
84 }
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86 // Otherwise, add to symbol to table. Copy to a C string first.
a61af66fc99e Initial load
duke
parents:
diff changeset
87 char stack_buf[128];
a61af66fc99e Initial load
duke
parents:
diff changeset
88 ResourceMark rm(THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
89 if (len <= 128) {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 buffer = stack_buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 for (int i=0; i<len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 buffer[i] = name[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // Make sure there is no safepoint in the code above since name can't move.
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // We can't include the code in No_Safepoint_Verifier because of the
a61af66fc99e Initial load
duke
parents:
diff changeset
99 // ResourceMark.
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 symbolOop SymbolTable::lookup_only(const char* name, int len,
a61af66fc99e Initial load
duke
parents:
diff changeset
105 unsigned int& hash) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 hash = hash_symbol(name, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 int index = the_table()->hash_to_index(hash);
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 return the_table()->lookup(index, name, len, hash);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111
665
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
112 // Suggestion: Push unicode-based lookup all the way into the hashing
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
113 // and probing logic, so there is no need for convert_to_utf8 until
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
114 // an actual new symbolOop is created.
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
115 symbolOop SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
116 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
117 char stack_buf[128];
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
118 if (utf8_length < (int) sizeof(stack_buf)) {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
119 char* chars = stack_buf;
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
120 UNICODE::convert_to_utf8(name, utf16_length, chars);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
121 return lookup(chars, utf8_length, THREAD);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
122 } else {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
123 ResourceMark rm(THREAD);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
124 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
125 UNICODE::convert_to_utf8(name, utf16_length, chars);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
126 return lookup(chars, utf8_length, THREAD);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
127 }
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
128 }
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
129
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
130 symbolOop SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
131 unsigned int& hash) {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
132 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
133 char stack_buf[128];
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
134 if (utf8_length < (int) sizeof(stack_buf)) {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
135 char* chars = stack_buf;
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
136 UNICODE::convert_to_utf8(name, utf16_length, chars);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
137 return lookup_only(chars, utf8_length, hash);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
138 } else {
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
139 ResourceMark rm;
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
140 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
141 UNICODE::convert_to_utf8(name, utf16_length, chars);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
142 return lookup_only(chars, utf8_length, hash);
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
143 }
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
144 }
c89f86385056 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 605
diff changeset
145
0
a61af66fc99e Initial load
duke
parents:
diff changeset
146 void SymbolTable::add(constantPoolHandle cp, int names_count,
a61af66fc99e Initial load
duke
parents:
diff changeset
147 const char** names, int* lengths, int* cp_indices,
a61af66fc99e Initial load
duke
parents:
diff changeset
148 unsigned int* hashValues, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 SymbolTable* table = the_table();
a61af66fc99e Initial load
duke
parents:
diff changeset
150 bool added = table->basic_add(cp, names_count, names, lengths,
a61af66fc99e Initial load
duke
parents:
diff changeset
151 cp_indices, hashValues, CHECK);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 if (!added) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // do it the hard way
a61af66fc99e Initial load
duke
parents:
diff changeset
154 for (int i=0; i<names_count; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 int index = table->hash_to_index(hashValues[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
156 symbolOop sym = table->basic_add(index, (u1*)names[i], lengths[i],
a61af66fc99e Initial load
duke
parents:
diff changeset
157 hashValues[i], CHECK);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 cp->symbol_at_put(cp_indices[i], sym);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 symbolOop SymbolTable::basic_add(int index, u1 *name, int len,
a61af66fc99e Initial load
duke
parents:
diff changeset
164 unsigned int hashValue, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
a61af66fc99e Initial load
duke
parents:
diff changeset
166 "proposed name of symbol must be stable");
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // We assume that lookup() has been called already, that it failed,
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // and symbol was not found. We create the symbol here.
a61af66fc99e Initial load
duke
parents:
diff changeset
170 symbolKlass* sk = (symbolKlass*) Universe::symbolKlassObj()->klass_part();
a61af66fc99e Initial load
duke
parents:
diff changeset
171 symbolOop s_oop = sk->allocate_symbol(name, len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
172 symbolHandle sym (THREAD, s_oop);
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 // Allocation must be done before grapping the SymbolTable_lock lock
a61af66fc99e Initial load
duke
parents:
diff changeset
175 MutexLocker ml(SymbolTable_lock, THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 assert(sym->equals((char*)name, len), "symbol must be properly initialized");
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 // Since look-up was done lock-free, we need to check if another
a61af66fc99e Initial load
duke
parents:
diff changeset
180 // thread beat us in the race to insert the symbol.
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 symbolOop test = lookup(index, (char*)name, len, hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
183 if (test != NULL) {
605
98cb887364d3 6810672: Comment typos
twisti
parents: 0
diff changeset
184 // A race occurred and another thread introduced the symbol, this one
0
a61af66fc99e Initial load
duke
parents:
diff changeset
185 // will be dropped and collected.
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return test;
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 HashtableEntry* entry = new_entry(hashValue, sym());
a61af66fc99e Initial load
duke
parents:
diff changeset
190 add_entry(index, entry);
a61af66fc99e Initial load
duke
parents:
diff changeset
191 return sym();
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 bool SymbolTable::basic_add(constantPoolHandle cp, int names_count,
a61af66fc99e Initial load
duke
parents:
diff changeset
195 const char** names, int* lengths,
a61af66fc99e Initial load
duke
parents:
diff changeset
196 int* cp_indices, unsigned int* hashValues,
a61af66fc99e Initial load
duke
parents:
diff changeset
197 TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
198 symbolKlass* sk = (symbolKlass*) Universe::symbolKlassObj()->klass_part();
a61af66fc99e Initial load
duke
parents:
diff changeset
199 symbolOop sym_oops[symbol_alloc_batch_size];
a61af66fc99e Initial load
duke
parents:
diff changeset
200 bool allocated = sk->allocate_symbols(names_count, names, lengths,
a61af66fc99e Initial load
duke
parents:
diff changeset
201 sym_oops, CHECK_false);
a61af66fc99e Initial load
duke
parents:
diff changeset
202 if (!allocated) {
a61af66fc99e Initial load
duke
parents:
diff changeset
203 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
204 }
a61af66fc99e Initial load
duke
parents:
diff changeset
205 symbolHandle syms[symbol_alloc_batch_size];
a61af66fc99e Initial load
duke
parents:
diff changeset
206 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 for (i=0; i<names_count; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 syms[i] = symbolHandle(THREAD, sym_oops[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // Allocation must be done before grabbing the SymbolTable_lock lock
a61af66fc99e Initial load
duke
parents:
diff changeset
212 MutexLocker ml(SymbolTable_lock, THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
213
a61af66fc99e Initial load
duke
parents:
diff changeset
214 for (i=0; i<names_count; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
215 assert(syms[i]->equals(names[i], lengths[i]), "symbol must be properly initialized");
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // Since look-up was done lock-free, we need to check if another
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // thread beat us in the race to insert the symbol.
a61af66fc99e Initial load
duke
parents:
diff changeset
218 int index = hash_to_index(hashValues[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 symbolOop test = lookup(index, names[i], lengths[i], hashValues[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
220 if (test != NULL) {
605
98cb887364d3 6810672: Comment typos
twisti
parents: 0
diff changeset
221 // A race occurred and another thread introduced the symbol, this one
0
a61af66fc99e Initial load
duke
parents:
diff changeset
222 // will be dropped and collected. Use test instead.
a61af66fc99e Initial load
duke
parents:
diff changeset
223 cp->symbol_at_put(cp_indices[i], test);
a61af66fc99e Initial load
duke
parents:
diff changeset
224 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 symbolOop sym = syms[i]();
a61af66fc99e Initial load
duke
parents:
diff changeset
226 HashtableEntry* entry = new_entry(hashValues[i], sym);
a61af66fc99e Initial load
duke
parents:
diff changeset
227 add_entry(index, entry);
a61af66fc99e Initial load
duke
parents:
diff changeset
228 cp->symbol_at_put(cp_indices[i], sym);
a61af66fc99e Initial load
duke
parents:
diff changeset
229 }
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
233 }
a61af66fc99e Initial load
duke
parents:
diff changeset
234
a61af66fc99e Initial load
duke
parents:
diff changeset
235
a61af66fc99e Initial load
duke
parents:
diff changeset
236 void SymbolTable::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
237 for (int i = 0; i < the_table()->table_size(); ++i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
238 HashtableEntry* p = the_table()->bucket(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
239 for ( ; p != NULL; p = p->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 symbolOop s = symbolOop(p->literal());
a61af66fc99e Initial load
duke
parents:
diff changeset
241 guarantee(s != NULL, "symbol is NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
242 s->verify();
a61af66fc99e Initial load
duke
parents:
diff changeset
243 guarantee(s->is_perm(), "symbol not in permspace");
a61af66fc99e Initial load
duke
parents:
diff changeset
244 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
a61af66fc99e Initial load
duke
parents:
diff changeset
245 guarantee(p->hash() == h, "broken hash in symbol table entry");
a61af66fc99e Initial load
duke
parents:
diff changeset
246 guarantee(the_table()->hash_to_index(h) == i,
a61af66fc99e Initial load
duke
parents:
diff changeset
247 "wrong index in symbol table");
a61af66fc99e Initial load
duke
parents:
diff changeset
248 }
a61af66fc99e Initial load
duke
parents:
diff changeset
249 }
a61af66fc99e Initial load
duke
parents:
diff changeset
250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 //---------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
254 // Non-product code
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 void SymbolTable::print_histogram() {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 MutexLocker ml(SymbolTable_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
260 const int results_length = 100;
a61af66fc99e Initial load
duke
parents:
diff changeset
261 int results[results_length];
a61af66fc99e Initial load
duke
parents:
diff changeset
262 int i,j;
a61af66fc99e Initial load
duke
parents:
diff changeset
263
a61af66fc99e Initial load
duke
parents:
diff changeset
264 // initialize results to zero
a61af66fc99e Initial load
duke
parents:
diff changeset
265 for (j = 0; j < results_length; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 results[j] = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268
a61af66fc99e Initial load
duke
parents:
diff changeset
269 int total = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
270 int max_symbols = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
271 int out_of_range = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
272 for (i = 0; i < the_table()->table_size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 HashtableEntry* p = the_table()->bucket(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
274 for ( ; p != NULL; p = p->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 int counter = symbolOop(p->literal())->utf8_length();
a61af66fc99e Initial load
duke
parents:
diff changeset
276 total += counter;
a61af66fc99e Initial load
duke
parents:
diff changeset
277 if (counter < results_length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 results[counter]++;
a61af66fc99e Initial load
duke
parents:
diff changeset
279 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
280 out_of_range++;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 }
a61af66fc99e Initial load
duke
parents:
diff changeset
282 max_symbols = MAX2(max_symbols, counter);
a61af66fc99e Initial load
duke
parents:
diff changeset
283 }
a61af66fc99e Initial load
duke
parents:
diff changeset
284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
285 tty->print_cr("Symbol Table:");
a61af66fc99e Initial load
duke
parents:
diff changeset
286 tty->print_cr("%8s %5d", "Total ", total);
a61af66fc99e Initial load
duke
parents:
diff changeset
287 tty->print_cr("%8s %5d", "Maximum", max_symbols);
a61af66fc99e Initial load
duke
parents:
diff changeset
288 tty->print_cr("%8s %3.2f", "Average",
a61af66fc99e Initial load
duke
parents:
diff changeset
289 ((float) total / (float) the_table()->table_size()));
a61af66fc99e Initial load
duke
parents:
diff changeset
290 tty->print_cr("%s", "Histogram:");
a61af66fc99e Initial load
duke
parents:
diff changeset
291 tty->print_cr(" %s %29s", "Length", "Number chains that length");
a61af66fc99e Initial load
duke
parents:
diff changeset
292 for (i = 0; i < results_length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if (results[i] > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 tty->print_cr("%6d %10d", i, results[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296 }
a61af66fc99e Initial load
duke
parents:
diff changeset
297 int line_length = 70;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 tty->print_cr("%s %30s", " Length", "Number chains that length");
a61af66fc99e Initial load
duke
parents:
diff changeset
299 for (i = 0; i < results_length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
300 if (results[i] > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
301 tty->print("%4d", i);
a61af66fc99e Initial load
duke
parents:
diff changeset
302 for (j = 0; (j < results[i]) && (j < line_length); j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 tty->print("%1s", "*");
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if (j == line_length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 tty->print("%1s", "+");
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311 tty->print_cr(" %s %d: %d\n", "Number chains longer than",
a61af66fc99e Initial load
duke
parents:
diff changeset
312 results_length, out_of_range);
a61af66fc99e Initial load
duke
parents:
diff changeset
313 }
a61af66fc99e Initial load
duke
parents:
diff changeset
314
a61af66fc99e Initial load
duke
parents:
diff changeset
315 #endif // PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
316
a61af66fc99e Initial load
duke
parents:
diff changeset
317 // --------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
318
a61af66fc99e Initial load
duke
parents:
diff changeset
319 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
320 class StableMemoryChecker : public StackObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 enum { _bufsize = wordSize*4 };
a61af66fc99e Initial load
duke
parents:
diff changeset
322
a61af66fc99e Initial load
duke
parents:
diff changeset
323 address _region;
a61af66fc99e Initial load
duke
parents:
diff changeset
324 jint _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 u1 _save_buf[_bufsize];
a61af66fc99e Initial load
duke
parents:
diff changeset
326
a61af66fc99e Initial load
duke
parents:
diff changeset
327 int sample(u1* save_buf) {
a61af66fc99e Initial load
duke
parents:
diff changeset
328 if (_size <= _bufsize) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 memcpy(save_buf, _region, _size);
a61af66fc99e Initial load
duke
parents:
diff changeset
330 return _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 // copy head and tail
a61af66fc99e Initial load
duke
parents:
diff changeset
333 memcpy(&save_buf[0], _region, _bufsize/2);
a61af66fc99e Initial load
duke
parents:
diff changeset
334 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
a61af66fc99e Initial load
duke
parents:
diff changeset
335 return (_bufsize/2)*2;
a61af66fc99e Initial load
duke
parents:
diff changeset
336 }
a61af66fc99e Initial load
duke
parents:
diff changeset
337 }
a61af66fc99e Initial load
duke
parents:
diff changeset
338
a61af66fc99e Initial load
duke
parents:
diff changeset
339 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
340 StableMemoryChecker(const void* region, jint size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 _region = (address) region;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 _size = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 sample(_save_buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 }
a61af66fc99e Initial load
duke
parents:
diff changeset
345
a61af66fc99e Initial load
duke
parents:
diff changeset
346 bool verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
347 u1 check_buf[sizeof(_save_buf)];
a61af66fc99e Initial load
duke
parents:
diff changeset
348 int check_size = sample(check_buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
349 return (0 == memcmp(_save_buf, check_buf, check_size));
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 void set_region(const void* region) { _region = (address) region; }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 };
a61af66fc99e Initial load
duke
parents:
diff changeset
354 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356
a61af66fc99e Initial load
duke
parents:
diff changeset
357 // --------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
358
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // Compute the hash value for a java.lang.String object which would
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // contain the characters passed in. This hash value is used for at
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // least two purposes.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 //
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // (a) As the hash value used by the StringTable for bucket selection
a61af66fc99e Initial load
duke
parents:
diff changeset
365 // and comparison (stored in the HashtableEntry structures). This
a61af66fc99e Initial load
duke
parents:
diff changeset
366 // is used in the String.intern() method.
a61af66fc99e Initial load
duke
parents:
diff changeset
367 //
a61af66fc99e Initial load
duke
parents:
diff changeset
368 // (b) As the hash value used by the String object itself, in
a61af66fc99e Initial load
duke
parents:
diff changeset
369 // String.hashCode(). This value is normally calculate in Java code
a61af66fc99e Initial load
duke
parents:
diff changeset
370 // in the String.hashCode method(), but is precomputed for String
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // objects in the shared archive file.
a61af66fc99e Initial load
duke
parents:
diff changeset
372 //
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // For this reason, THIS ALGORITHM MUST MATCH String.hashCode().
a61af66fc99e Initial load
duke
parents:
diff changeset
374
a61af66fc99e Initial load
duke
parents:
diff changeset
375 int StringTable::hash_string(jchar* s, int len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 unsigned h = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 while (len-- > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
378 h = 31*h + (unsigned) *s;
a61af66fc99e Initial load
duke
parents:
diff changeset
379 s++;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 }
a61af66fc99e Initial load
duke
parents:
diff changeset
381 return h;
a61af66fc99e Initial load
duke
parents:
diff changeset
382 }
a61af66fc99e Initial load
duke
parents:
diff changeset
383
a61af66fc99e Initial load
duke
parents:
diff changeset
384
a61af66fc99e Initial load
duke
parents:
diff changeset
385 StringTable* StringTable::_the_table = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387 oop StringTable::lookup(int index, jchar* name,
a61af66fc99e Initial load
duke
parents:
diff changeset
388 int len, unsigned int hash) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 for (HashtableEntry* l = bucket(index); l != NULL; l = l->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 if (l->hash() == hash) {
a61af66fc99e Initial load
duke
parents:
diff changeset
391 if (java_lang_String::equals(l->literal(), name, len)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
392 return l->literal();
a61af66fc99e Initial load
duke
parents:
diff changeset
393 }
a61af66fc99e Initial load
duke
parents:
diff changeset
394 }
a61af66fc99e Initial load
duke
parents:
diff changeset
395 }
a61af66fc99e Initial load
duke
parents:
diff changeset
396 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
397 }
a61af66fc99e Initial load
duke
parents:
diff changeset
398
a61af66fc99e Initial load
duke
parents:
diff changeset
399
a61af66fc99e Initial load
duke
parents:
diff changeset
400 oop StringTable::basic_add(int index, Handle string_or_null, jchar* name,
a61af66fc99e Initial load
duke
parents:
diff changeset
401 int len, unsigned int hashValue, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
402 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
a61af66fc99e Initial load
duke
parents:
diff changeset
403 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
a61af66fc99e Initial load
duke
parents:
diff changeset
404 "proposed name of symbol must be stable");
a61af66fc99e Initial load
duke
parents:
diff changeset
405
a61af66fc99e Initial load
duke
parents:
diff changeset
406 Handle string;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 // try to reuse the string if possible
a61af66fc99e Initial load
duke
parents:
diff changeset
408 if (!string_or_null.is_null() && string_or_null()->is_perm()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
409 string = string_or_null;
a61af66fc99e Initial load
duke
parents:
diff changeset
410 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
411 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
412 }
a61af66fc99e Initial load
duke
parents:
diff changeset
413
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // Allocation must be done before grapping the SymbolTable_lock lock
a61af66fc99e Initial load
duke
parents:
diff changeset
415 MutexLocker ml(StringTable_lock, THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
416
a61af66fc99e Initial load
duke
parents:
diff changeset
417 assert(java_lang_String::equals(string(), name, len),
a61af66fc99e Initial load
duke
parents:
diff changeset
418 "string must be properly initialized");
a61af66fc99e Initial load
duke
parents:
diff changeset
419
a61af66fc99e Initial load
duke
parents:
diff changeset
420 // Since look-up was done lock-free, we need to check if another
a61af66fc99e Initial load
duke
parents:
diff changeset
421 // thread beat us in the race to insert the symbol.
a61af66fc99e Initial load
duke
parents:
diff changeset
422
a61af66fc99e Initial load
duke
parents:
diff changeset
423 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
a61af66fc99e Initial load
duke
parents:
diff changeset
424 if (test != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 // Entry already added
a61af66fc99e Initial load
duke
parents:
diff changeset
426 return test;
a61af66fc99e Initial load
duke
parents:
diff changeset
427 }
a61af66fc99e Initial load
duke
parents:
diff changeset
428
a61af66fc99e Initial load
duke
parents:
diff changeset
429 HashtableEntry* entry = new_entry(hashValue, string());
a61af66fc99e Initial load
duke
parents:
diff changeset
430 add_entry(index, entry);
a61af66fc99e Initial load
duke
parents:
diff changeset
431 return string();
a61af66fc99e Initial load
duke
parents:
diff changeset
432 }
a61af66fc99e Initial load
duke
parents:
diff changeset
433
a61af66fc99e Initial load
duke
parents:
diff changeset
434
a61af66fc99e Initial load
duke
parents:
diff changeset
435 oop StringTable::lookup(symbolOop symbol) {
a61af66fc99e Initial load
duke
parents:
diff changeset
436 ResourceMark rm;
a61af66fc99e Initial load
duke
parents:
diff changeset
437 int length;
a61af66fc99e Initial load
duke
parents:
diff changeset
438 jchar* chars = symbol->as_unicode(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
439 unsigned int hashValue = hash_string(chars, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
440 int index = the_table()->hash_to_index(hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
441 return the_table()->lookup(index, chars, length, hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
442 }
a61af66fc99e Initial load
duke
parents:
diff changeset
443
a61af66fc99e Initial load
duke
parents:
diff changeset
444
a61af66fc99e Initial load
duke
parents:
diff changeset
445 oop StringTable::intern(Handle string_or_null, jchar* name,
a61af66fc99e Initial load
duke
parents:
diff changeset
446 int len, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 unsigned int hashValue = hash_string(name, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
448 int index = the_table()->hash_to_index(hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
449 oop string = the_table()->lookup(index, name, len, hashValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
450
a61af66fc99e Initial load
duke
parents:
diff changeset
451 // Found
a61af66fc99e Initial load
duke
parents:
diff changeset
452 if (string != NULL) return string;
a61af66fc99e Initial load
duke
parents:
diff changeset
453
a61af66fc99e Initial load
duke
parents:
diff changeset
454 // Otherwise, add to symbol to table
a61af66fc99e Initial load
duke
parents:
diff changeset
455 return the_table()->basic_add(index, string_or_null, name, len,
a61af66fc99e Initial load
duke
parents:
diff changeset
456 hashValue, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
457 }
a61af66fc99e Initial load
duke
parents:
diff changeset
458
a61af66fc99e Initial load
duke
parents:
diff changeset
459 oop StringTable::intern(symbolOop symbol, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
460 if (symbol == NULL) return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
461 ResourceMark rm(THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
462 int length;
a61af66fc99e Initial load
duke
parents:
diff changeset
463 jchar* chars = symbol->as_unicode(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
464 Handle string;
a61af66fc99e Initial load
duke
parents:
diff changeset
465 oop result = intern(string, chars, length, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
466 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
467 }
a61af66fc99e Initial load
duke
parents:
diff changeset
468
a61af66fc99e Initial load
duke
parents:
diff changeset
469
a61af66fc99e Initial load
duke
parents:
diff changeset
470 oop StringTable::intern(oop string, TRAPS)
a61af66fc99e Initial load
duke
parents:
diff changeset
471 {
a61af66fc99e Initial load
duke
parents:
diff changeset
472 if (string == NULL) return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
473 ResourceMark rm(THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
474 int length;
a61af66fc99e Initial load
duke
parents:
diff changeset
475 Handle h_string (THREAD, string);
a61af66fc99e Initial load
duke
parents:
diff changeset
476 jchar* chars = java_lang_String::as_unicode_string(string, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
477 oop result = intern(h_string, chars, length, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
478 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
479 }
a61af66fc99e Initial load
duke
parents:
diff changeset
480
a61af66fc99e Initial load
duke
parents:
diff changeset
481
a61af66fc99e Initial load
duke
parents:
diff changeset
482 oop StringTable::intern(const char* utf8_string, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
483 if (utf8_string == NULL) return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
484 ResourceMark rm(THREAD);
a61af66fc99e Initial load
duke
parents:
diff changeset
485 int length = UTF8::unicode_length(utf8_string);
a61af66fc99e Initial load
duke
parents:
diff changeset
486 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
487 UTF8::convert_to_unicode(utf8_string, chars, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
488 Handle string;
a61af66fc99e Initial load
duke
parents:
diff changeset
489 oop result = intern(string, chars, length, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
490 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 }
a61af66fc99e Initial load
duke
parents:
diff changeset
492
a61af66fc99e Initial load
duke
parents:
diff changeset
493 void StringTable::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
494 for (int i = 0; i < the_table()->table_size(); ++i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
495 HashtableEntry* p = the_table()->bucket(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
496 for ( ; p != NULL; p = p->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
497 oop s = p->literal();
a61af66fc99e Initial load
duke
parents:
diff changeset
498 guarantee(s != NULL, "interned string is NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
499 guarantee(s->is_perm(), "interned string not in permspace");
a61af66fc99e Initial load
duke
parents:
diff changeset
500
a61af66fc99e Initial load
duke
parents:
diff changeset
501 int length;
a61af66fc99e Initial load
duke
parents:
diff changeset
502 jchar* chars = java_lang_String::as_unicode_string(s, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
503 unsigned int h = hash_string(chars, length);
a61af66fc99e Initial load
duke
parents:
diff changeset
504 guarantee(p->hash() == h, "broken hash in string table entry");
a61af66fc99e Initial load
duke
parents:
diff changeset
505 guarantee(the_table()->hash_to_index(h) == i,
a61af66fc99e Initial load
duke
parents:
diff changeset
506 "wrong index in string table");
a61af66fc99e Initial load
duke
parents:
diff changeset
507 }
a61af66fc99e Initial load
duke
parents:
diff changeset
508 }
a61af66fc99e Initial load
duke
parents:
diff changeset
509 }