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