src/share/vm/classfile/symbolTable.cpp

Wed, 11 Jan 2012 17:34:02 -0500

author
phh
date
Wed, 11 Jan 2012 17:34:02 -0500
changeset 3427
94ec88ca68e2
parent 2700
352622fd140a
child 3682
fc9d8850ab8b
permissions
-rw-r--r--

7115199: Add event tracing hooks and Java Flight Recorder infrastructure
Summary: Added a nop tracing infrastructure, JFR makefile changes and other infrastructure used only by JFR.
Reviewed-by: acorn, sspitsyn
Contributed-by: markus.gronlund@oracle.com

duke@435 1 /*
never@2700 2 * Copyright (c) 1997, 2011, 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 "runtime/mutexLocker.hpp"
stefank@2314 35 #include "utilities/hashtable.inline.hpp"
duke@435 36
duke@435 37 // --------------------------------------------------------------------------
duke@435 38
duke@435 39 SymbolTable* SymbolTable::_the_table = NULL;
duke@435 40
coleenp@2497 41 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, TRAPS) {
coleenp@2497 42 // Don't allow symbols to be created which cannot fit in a Symbol*.
coleenp@2497 43 if (len > Symbol::max_length()) {
coleenp@2497 44 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
coleenp@2497 45 "name is too long to represent");
coleenp@2497 46 }
coleenp@2497 47 Symbol* sym = new (len) Symbol(name, len);
coleenp@2497 48 assert(sym != NULL, "new should call vm_exit_out_of_memory if C_HEAP is exhausted");
coleenp@2497 49 return sym;
coleenp@2497 50 }
coleenp@2497 51
coleenp@2497 52 bool SymbolTable::allocate_symbols(int names_count, const u1** names,
coleenp@2497 53 int* lengths, Symbol** syms, TRAPS) {
coleenp@2497 54 for (int i = 0; i< names_count; i++) {
coleenp@2497 55 if (lengths[i] > Symbol::max_length()) {
coleenp@2497 56 THROW_MSG_0(vmSymbols::java_lang_InternalError(),
coleenp@2497 57 "name is too long to represent");
coleenp@2497 58 }
coleenp@2497 59 }
coleenp@2497 60
coleenp@2497 61 for (int i = 0; i< names_count; i++) {
coleenp@2497 62 int len = lengths[i];
coleenp@2497 63 syms[i] = new (len) Symbol(names[i], len);
coleenp@2497 64 assert(syms[i] != NULL, "new should call vm_exit_out_of_memory if "
coleenp@2497 65 "C_HEAP is exhausted");
coleenp@2497 66 }
coleenp@2497 67 return true;
coleenp@2497 68 }
coleenp@2497 69
coleenp@2497 70 // Call function for all symbols in the symbol table.
coleenp@2497 71 void SymbolTable::symbols_do(SymbolClosure *cl) {
coleenp@2497 72 const int n = the_table()->table_size();
coleenp@2497 73 for (int i = 0; i < n; i++) {
coleenp@2497 74 for (HashtableEntry<Symbol*>* p = the_table()->bucket(i);
coleenp@2497 75 p != NULL;
coleenp@2497 76 p = p->next()) {
coleenp@2497 77 cl->do_symbol(p->literal_addr());
coleenp@2497 78 }
coleenp@2497 79 }
coleenp@2497 80 }
coleenp@2497 81
coleenp@2497 82 int SymbolTable::symbols_removed = 0;
coleenp@2497 83 int SymbolTable::symbols_counted = 0;
coleenp@2497 84
coleenp@2497 85 // Remove unreferenced symbols from the symbol table
coleenp@2497 86 // This is done late during GC. This doesn't use the hash table unlink because
coleenp@2497 87 // it assumes that the literals are oops.
coleenp@2497 88 void SymbolTable::unlink() {
coleenp@2497 89 int removed = 0;
coleenp@2497 90 int total = 0;
coleenp@2618 91 size_t memory_total = 0;
coleenp@2497 92 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 93 for (HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i); *p != NULL; ) {
coleenp@2497 94 HashtableEntry<Symbol*>* entry = *p;
coleenp@2497 95 if (entry->is_shared()) {
coleenp@2497 96 break;
coleenp@2497 97 }
coleenp@2497 98 Symbol* s = entry->literal();
coleenp@2497 99 memory_total += s->object_size();
coleenp@2497 100 total++;
coleenp@2497 101 assert(s != NULL, "just checking");
coleenp@2497 102 // If reference count is zero, remove.
coleenp@2497 103 if (s->refcount() == 0) {
coleenp@2497 104 delete s;
coleenp@2497 105 removed++;
coleenp@2497 106 *p = entry->next();
coleenp@2497 107 the_table()->free_entry(entry);
coleenp@2497 108 } else {
coleenp@2497 109 p = entry->next_addr();
coleenp@2497 110 }
coleenp@2497 111 }
coleenp@2497 112 }
coleenp@2497 113 symbols_removed += removed;
coleenp@2497 114 symbols_counted += total;
coleenp@2618 115 // Exclude printing for normal PrintGCDetails because people parse
coleenp@2618 116 // this output.
coleenp@2618 117 if (PrintGCDetails && Verbose && WizardMode) {
coleenp@2618 118 gclog_or_tty->print(" [Symbols=%d size=" SIZE_FORMAT "K] ", total,
coleenp@2497 119 (memory_total*HeapWordSize)/1024);
coleenp@2497 120 }
coleenp@2497 121 }
coleenp@2497 122
coleenp@2497 123
duke@435 124 // Lookup a symbol in a bucket.
duke@435 125
coleenp@2497 126 Symbol* SymbolTable::lookup(int index, const char* name,
duke@435 127 int len, unsigned int hash) {
coleenp@2497 128 for (HashtableEntry<Symbol*>* e = bucket(index); e != NULL; e = e->next()) {
duke@435 129 if (e->hash() == hash) {
coleenp@2497 130 Symbol* sym = e->literal();
duke@435 131 if (sym->equals(name, len)) {
coleenp@2497 132 // something is referencing this symbol now.
coleenp@2497 133 sym->increment_refcount();
duke@435 134 return sym;
duke@435 135 }
duke@435 136 }
duke@435 137 }
duke@435 138 return NULL;
duke@435 139 }
duke@435 140
duke@435 141
duke@435 142 // We take care not to be blocking while holding the
duke@435 143 // SymbolTable_lock. Otherwise, the system might deadlock, since the
duke@435 144 // symboltable is used during compilation (VM_thread) The lock free
duke@435 145 // synchronization is simplified by the fact that we do not delete
duke@435 146 // entries in the symbol table during normal execution (only during
duke@435 147 // safepoints).
duke@435 148
coleenp@2497 149 Symbol* SymbolTable::lookup(const char* name, int len, TRAPS) {
duke@435 150 unsigned int hashValue = hash_symbol(name, len);
duke@435 151 int index = the_table()->hash_to_index(hashValue);
duke@435 152
coleenp@2497 153 Symbol* s = the_table()->lookup(index, name, len, hashValue);
duke@435 154
duke@435 155 // Found
duke@435 156 if (s != NULL) return s;
duke@435 157
duke@435 158 // Otherwise, add to symbol to table
duke@435 159 return the_table()->basic_add(index, (u1*)name, len, hashValue, CHECK_NULL);
duke@435 160 }
duke@435 161
coleenp@2497 162 Symbol* SymbolTable::lookup(const Symbol* sym, int begin, int end, TRAPS) {
duke@435 163 char* buffer;
duke@435 164 int index, len;
duke@435 165 unsigned int hashValue;
duke@435 166 char* name;
duke@435 167 {
duke@435 168 debug_only(No_Safepoint_Verifier nsv;)
duke@435 169
duke@435 170 name = (char*)sym->base() + begin;
duke@435 171 len = end - begin;
duke@435 172 hashValue = hash_symbol(name, len);
duke@435 173 index = the_table()->hash_to_index(hashValue);
coleenp@2497 174 Symbol* s = the_table()->lookup(index, name, len, hashValue);
duke@435 175
duke@435 176 // Found
duke@435 177 if (s != NULL) return s;
duke@435 178 }
duke@435 179
duke@435 180 // Otherwise, add to symbol to table. Copy to a C string first.
duke@435 181 char stack_buf[128];
duke@435 182 ResourceMark rm(THREAD);
duke@435 183 if (len <= 128) {
duke@435 184 buffer = stack_buf;
duke@435 185 } else {
duke@435 186 buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
duke@435 187 }
duke@435 188 for (int i=0; i<len; i++) {
duke@435 189 buffer[i] = name[i];
duke@435 190 }
duke@435 191 // Make sure there is no safepoint in the code above since name can't move.
duke@435 192 // We can't include the code in No_Safepoint_Verifier because of the
duke@435 193 // ResourceMark.
duke@435 194
duke@435 195 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, CHECK_NULL);
duke@435 196 }
duke@435 197
coleenp@2497 198 Symbol* SymbolTable::lookup_only(const char* name, int len,
duke@435 199 unsigned int& hash) {
duke@435 200 hash = hash_symbol(name, len);
duke@435 201 int index = the_table()->hash_to_index(hash);
duke@435 202
coleenp@2497 203 Symbol* s = the_table()->lookup(index, name, len, hash);
coleenp@2497 204 return s;
duke@435 205 }
duke@435 206
phh@3427 207 // Look up the address of the literal in the SymbolTable for this Symbol*
phh@3427 208 // Do not create any new symbols
phh@3427 209 // Do not increment the reference count to keep this alive
phh@3427 210 Symbol** SymbolTable::lookup_symbol_addr(Symbol* sym){
phh@3427 211 unsigned int hash = hash_symbol((char*)sym->bytes(), sym->utf8_length());
phh@3427 212 int index = the_table()->hash_to_index(hash);
phh@3427 213
phh@3427 214 for (HashtableEntry<Symbol*>* e = the_table()->bucket(index); e != NULL; e = e->next()) {
phh@3427 215 if (e->hash() == hash) {
phh@3427 216 Symbol* literal_sym = e->literal();
phh@3427 217 if (sym == literal_sym) {
phh@3427 218 return e->literal_addr();
phh@3427 219 }
phh@3427 220 }
phh@3427 221 }
phh@3427 222 return NULL;
phh@3427 223 }
phh@3427 224
jrose@1100 225 // Suggestion: Push unicode-based lookup all the way into the hashing
jrose@1100 226 // and probing logic, so there is no need for convert_to_utf8 until
coleenp@2497 227 // an actual new Symbol* is created.
coleenp@2497 228 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
jrose@1100 229 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 230 char stack_buf[128];
jrose@1100 231 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 232 char* chars = stack_buf;
jrose@1100 233 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 234 return lookup(chars, utf8_length, THREAD);
jrose@1100 235 } else {
jrose@1100 236 ResourceMark rm(THREAD);
jrose@1100 237 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 238 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 239 return lookup(chars, utf8_length, THREAD);
jrose@1100 240 }
jrose@1100 241 }
jrose@1100 242
coleenp@2497 243 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
jrose@1100 244 unsigned int& hash) {
jrose@1100 245 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 246 char stack_buf[128];
jrose@1100 247 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 248 char* chars = stack_buf;
jrose@1100 249 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 250 return lookup_only(chars, utf8_length, hash);
jrose@1100 251 } else {
jrose@1100 252 ResourceMark rm;
jrose@1100 253 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 254 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 255 return lookup_only(chars, utf8_length, hash);
jrose@1100 256 }
jrose@1100 257 }
jrose@1100 258
duke@435 259 void SymbolTable::add(constantPoolHandle cp, int names_count,
duke@435 260 const char** names, int* lengths, int* cp_indices,
duke@435 261 unsigned int* hashValues, TRAPS) {
duke@435 262 SymbolTable* table = the_table();
duke@435 263 bool added = table->basic_add(cp, names_count, names, lengths,
duke@435 264 cp_indices, hashValues, CHECK);
duke@435 265 if (!added) {
duke@435 266 // do it the hard way
duke@435 267 for (int i=0; i<names_count; i++) {
duke@435 268 int index = table->hash_to_index(hashValues[i]);
coleenp@2497 269 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i],
duke@435 270 hashValues[i], CHECK);
duke@435 271 cp->symbol_at_put(cp_indices[i], sym);
duke@435 272 }
duke@435 273 }
duke@435 274 }
duke@435 275
coleenp@2497 276 Symbol* SymbolTable::basic_add(int index, u1 *name, int len,
duke@435 277 unsigned int hashValue, TRAPS) {
duke@435 278 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
duke@435 279 "proposed name of symbol must be stable");
duke@435 280
duke@435 281 // We assume that lookup() has been called already, that it failed,
duke@435 282 // and symbol was not found. We create the symbol here.
coleenp@2497 283 Symbol* sym = allocate_symbol(name, len, CHECK_NULL);
duke@435 284
coleenp@2497 285 // Allocation must be done before grabbing the SymbolTable_lock lock
duke@435 286 MutexLocker ml(SymbolTable_lock, THREAD);
duke@435 287
duke@435 288 assert(sym->equals((char*)name, len), "symbol must be properly initialized");
duke@435 289
duke@435 290 // Since look-up was done lock-free, we need to check if another
duke@435 291 // thread beat us in the race to insert the symbol.
duke@435 292
coleenp@2497 293 Symbol* test = lookup(index, (char*)name, len, hashValue);
duke@435 294 if (test != NULL) {
twisti@1040 295 // A race occurred and another thread introduced the symbol, this one
duke@435 296 // will be dropped and collected.
coleenp@2497 297 delete sym;
coleenp@2497 298 assert(test->refcount() != 0, "lookup should have incremented the count");
duke@435 299 return test;
duke@435 300 }
duke@435 301
coleenp@2497 302 HashtableEntry<Symbol*>* entry = new_entry(hashValue, sym);
coleenp@2497 303 sym->increment_refcount();
duke@435 304 add_entry(index, entry);
coleenp@2497 305 return sym;
duke@435 306 }
duke@435 307
duke@435 308 bool SymbolTable::basic_add(constantPoolHandle cp, int names_count,
duke@435 309 const char** names, int* lengths,
duke@435 310 int* cp_indices, unsigned int* hashValues,
duke@435 311 TRAPS) {
coleenp@2497 312 Symbol* syms[symbol_alloc_batch_size];
coleenp@2497 313 bool allocated = allocate_symbols(names_count, (const u1**)names, lengths,
coleenp@2497 314 syms, CHECK_false);
duke@435 315 if (!allocated) {
duke@435 316 return false;
duke@435 317 }
duke@435 318
duke@435 319 // Allocation must be done before grabbing the SymbolTable_lock lock
duke@435 320 MutexLocker ml(SymbolTable_lock, THREAD);
duke@435 321
coleenp@2497 322 for (int i=0; i<names_count; i++) {
duke@435 323 assert(syms[i]->equals(names[i], lengths[i]), "symbol must be properly initialized");
duke@435 324 // Since look-up was done lock-free, we need to check if another
duke@435 325 // thread beat us in the race to insert the symbol.
duke@435 326 int index = hash_to_index(hashValues[i]);
coleenp@2497 327 Symbol* test = lookup(index, names[i], lengths[i], hashValues[i]);
duke@435 328 if (test != NULL) {
twisti@1040 329 // A race occurred and another thread introduced the symbol, this one
duke@435 330 // will be dropped and collected. Use test instead.
duke@435 331 cp->symbol_at_put(cp_indices[i], test);
coleenp@2497 332 assert(test->refcount() != 0, "lookup should have incremented the count");
coleenp@2497 333 delete syms[i];
duke@435 334 } else {
coleenp@2497 335 Symbol* sym = syms[i];
coleenp@2497 336 HashtableEntry<Symbol*>* entry = new_entry(hashValues[i], sym);
coleenp@2497 337 sym->increment_refcount(); // increment refcount in external hashtable
duke@435 338 add_entry(index, entry);
duke@435 339 cp->symbol_at_put(cp_indices[i], sym);
duke@435 340 }
duke@435 341 }
duke@435 342
duke@435 343 return true;
duke@435 344 }
duke@435 345
duke@435 346
duke@435 347 void SymbolTable::verify() {
duke@435 348 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 349 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 350 for ( ; p != NULL; p = p->next()) {
coleenp@2497 351 Symbol* s = (Symbol*)(p->literal());
duke@435 352 guarantee(s != NULL, "symbol is NULL");
duke@435 353 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
duke@435 354 guarantee(p->hash() == h, "broken hash in symbol table entry");
duke@435 355 guarantee(the_table()->hash_to_index(h) == i,
duke@435 356 "wrong index in symbol table");
duke@435 357 }
duke@435 358 }
duke@435 359 }
duke@435 360
duke@435 361
duke@435 362 //---------------------------------------------------------------------------
duke@435 363 // Non-product code
duke@435 364
duke@435 365 #ifndef PRODUCT
duke@435 366
duke@435 367 void SymbolTable::print_histogram() {
duke@435 368 MutexLocker ml(SymbolTable_lock);
duke@435 369 const int results_length = 100;
duke@435 370 int results[results_length];
duke@435 371 int i,j;
duke@435 372
duke@435 373 // initialize results to zero
duke@435 374 for (j = 0; j < results_length; j++) {
duke@435 375 results[j] = 0;
duke@435 376 }
duke@435 377
duke@435 378 int total = 0;
duke@435 379 int max_symbols = 0;
duke@435 380 int out_of_range = 0;
coleenp@2497 381 int memory_total = 0;
coleenp@2497 382 int count = 0;
duke@435 383 for (i = 0; i < the_table()->table_size(); i++) {
coleenp@2497 384 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 385 for ( ; p != NULL; p = p->next()) {
coleenp@2497 386 memory_total += p->literal()->object_size();
coleenp@2497 387 count++;
coleenp@2497 388 int counter = p->literal()->utf8_length();
duke@435 389 total += counter;
duke@435 390 if (counter < results_length) {
duke@435 391 results[counter]++;
duke@435 392 } else {
duke@435 393 out_of_range++;
duke@435 394 }
duke@435 395 max_symbols = MAX2(max_symbols, counter);
duke@435 396 }
duke@435 397 }
duke@435 398 tty->print_cr("Symbol Table:");
coleenp@2497 399 tty->print_cr("Total number of symbols %5d", count);
coleenp@2497 400 tty->print_cr("Total size in memory %5dK",
coleenp@2497 401 (memory_total*HeapWordSize)/1024);
coleenp@2497 402 tty->print_cr("Total counted %5d", symbols_counted);
coleenp@2497 403 tty->print_cr("Total removed %5d", symbols_removed);
coleenp@2497 404 if (symbols_counted > 0) {
coleenp@2497 405 tty->print_cr("Percent removed %3.2f",
coleenp@2497 406 ((float)symbols_removed/(float)symbols_counted)* 100);
coleenp@2497 407 }
coleenp@2497 408 tty->print_cr("Reference counts %5d", Symbol::_total_count);
coleenp@2497 409 tty->print_cr("Histogram of symbol length:");
duke@435 410 tty->print_cr("%8s %5d", "Total ", total);
duke@435 411 tty->print_cr("%8s %5d", "Maximum", max_symbols);
duke@435 412 tty->print_cr("%8s %3.2f", "Average",
duke@435 413 ((float) total / (float) the_table()->table_size()));
duke@435 414 tty->print_cr("%s", "Histogram:");
duke@435 415 tty->print_cr(" %s %29s", "Length", "Number chains that length");
duke@435 416 for (i = 0; i < results_length; i++) {
duke@435 417 if (results[i] > 0) {
duke@435 418 tty->print_cr("%6d %10d", i, results[i]);
duke@435 419 }
duke@435 420 }
coleenp@2497 421 if (Verbose) {
coleenp@2497 422 int line_length = 70;
coleenp@2497 423 tty->print_cr("%s %30s", " Length", "Number chains that length");
coleenp@2497 424 for (i = 0; i < results_length; i++) {
coleenp@2497 425 if (results[i] > 0) {
coleenp@2497 426 tty->print("%4d", i);
coleenp@2497 427 for (j = 0; (j < results[i]) && (j < line_length); j++) {
coleenp@2497 428 tty->print("%1s", "*");
coleenp@2497 429 }
coleenp@2497 430 if (j == line_length) {
coleenp@2497 431 tty->print("%1s", "+");
coleenp@2497 432 }
coleenp@2497 433 tty->cr();
duke@435 434 }
coleenp@2497 435 }
coleenp@2497 436 }
coleenp@2497 437 tty->print_cr(" %s %d: %d\n", "Number chains longer than",
coleenp@2497 438 results_length, out_of_range);
coleenp@2497 439 }
coleenp@2497 440
coleenp@2497 441 void SymbolTable::print() {
coleenp@2497 442 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 443 HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i);
coleenp@2497 444 HashtableEntry<Symbol*>* entry = the_table()->bucket(i);
coleenp@2497 445 if (entry != NULL) {
coleenp@2497 446 while (entry != NULL) {
coleenp@2497 447 tty->print(PTR_FORMAT " ", entry->literal());
coleenp@2497 448 entry->literal()->print();
coleenp@2497 449 tty->print(" %d", entry->literal()->refcount());
coleenp@2497 450 p = entry->next_addr();
coleenp@2497 451 entry = (HashtableEntry<Symbol*>*)HashtableEntry<Symbol*>::make_ptr(*p);
duke@435 452 }
duke@435 453 tty->cr();
duke@435 454 }
duke@435 455 }
duke@435 456 }
duke@435 457
duke@435 458 #endif // PRODUCT
duke@435 459
duke@435 460 // --------------------------------------------------------------------------
duke@435 461
duke@435 462 #ifdef ASSERT
duke@435 463 class StableMemoryChecker : public StackObj {
duke@435 464 enum { _bufsize = wordSize*4 };
duke@435 465
duke@435 466 address _region;
duke@435 467 jint _size;
duke@435 468 u1 _save_buf[_bufsize];
duke@435 469
duke@435 470 int sample(u1* save_buf) {
duke@435 471 if (_size <= _bufsize) {
duke@435 472 memcpy(save_buf, _region, _size);
duke@435 473 return _size;
duke@435 474 } else {
duke@435 475 // copy head and tail
duke@435 476 memcpy(&save_buf[0], _region, _bufsize/2);
duke@435 477 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
duke@435 478 return (_bufsize/2)*2;
duke@435 479 }
duke@435 480 }
duke@435 481
duke@435 482 public:
duke@435 483 StableMemoryChecker(const void* region, jint size) {
duke@435 484 _region = (address) region;
duke@435 485 _size = size;
duke@435 486 sample(_save_buf);
duke@435 487 }
duke@435 488
duke@435 489 bool verify() {
duke@435 490 u1 check_buf[sizeof(_save_buf)];
duke@435 491 int check_size = sample(check_buf);
duke@435 492 return (0 == memcmp(_save_buf, check_buf, check_size));
duke@435 493 }
duke@435 494
duke@435 495 void set_region(const void* region) { _region = (address) region; }
duke@435 496 };
duke@435 497 #endif
duke@435 498
duke@435 499
duke@435 500 // --------------------------------------------------------------------------
duke@435 501 StringTable* StringTable::_the_table = NULL;
duke@435 502
duke@435 503 oop StringTable::lookup(int index, jchar* name,
duke@435 504 int len, unsigned int hash) {
coleenp@2497 505 for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) {
duke@435 506 if (l->hash() == hash) {
duke@435 507 if (java_lang_String::equals(l->literal(), name, len)) {
duke@435 508 return l->literal();
duke@435 509 }
duke@435 510 }
duke@435 511 }
duke@435 512 return NULL;
duke@435 513 }
duke@435 514
duke@435 515
duke@435 516 oop StringTable::basic_add(int index, Handle string_or_null, jchar* name,
duke@435 517 int len, unsigned int hashValue, TRAPS) {
duke@435 518 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
duke@435 519 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
duke@435 520 "proposed name of symbol must be stable");
duke@435 521
duke@435 522 Handle string;
duke@435 523 // try to reuse the string if possible
jcoomes@2661 524 if (!string_or_null.is_null() && (!JavaObjectsInPerm || string_or_null()->is_perm())) {
duke@435 525 string = string_or_null;
duke@435 526 } else {
duke@435 527 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL);
duke@435 528 }
duke@435 529
duke@435 530 // Allocation must be done before grapping the SymbolTable_lock lock
duke@435 531 MutexLocker ml(StringTable_lock, THREAD);
duke@435 532
duke@435 533 assert(java_lang_String::equals(string(), name, len),
duke@435 534 "string must be properly initialized");
duke@435 535
duke@435 536 // Since look-up was done lock-free, we need to check if another
duke@435 537 // thread beat us in the race to insert the symbol.
duke@435 538
duke@435 539 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
duke@435 540 if (test != NULL) {
duke@435 541 // Entry already added
duke@435 542 return test;
duke@435 543 }
duke@435 544
coleenp@2497 545 HashtableEntry<oop>* entry = new_entry(hashValue, string());
duke@435 546 add_entry(index, entry);
duke@435 547 return string();
duke@435 548 }
duke@435 549
duke@435 550
coleenp@2497 551 oop StringTable::lookup(Symbol* symbol) {
duke@435 552 ResourceMark rm;
duke@435 553 int length;
duke@435 554 jchar* chars = symbol->as_unicode(length);
never@2700 555 unsigned int hashValue = java_lang_String::hash_string(chars, length);
duke@435 556 int index = the_table()->hash_to_index(hashValue);
duke@435 557 return the_table()->lookup(index, chars, length, hashValue);
duke@435 558 }
duke@435 559
duke@435 560
duke@435 561 oop StringTable::intern(Handle string_or_null, jchar* name,
duke@435 562 int len, TRAPS) {
never@2700 563 unsigned int hashValue = java_lang_String::hash_string(name, len);
duke@435 564 int index = the_table()->hash_to_index(hashValue);
duke@435 565 oop string = the_table()->lookup(index, name, len, hashValue);
duke@435 566
duke@435 567 // Found
duke@435 568 if (string != NULL) return string;
duke@435 569
duke@435 570 // Otherwise, add to symbol to table
duke@435 571 return the_table()->basic_add(index, string_or_null, name, len,
duke@435 572 hashValue, CHECK_NULL);
duke@435 573 }
duke@435 574
coleenp@2497 575 oop StringTable::intern(Symbol* symbol, TRAPS) {
duke@435 576 if (symbol == NULL) return NULL;
duke@435 577 ResourceMark rm(THREAD);
duke@435 578 int length;
duke@435 579 jchar* chars = symbol->as_unicode(length);
duke@435 580 Handle string;
duke@435 581 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 582 return result;
duke@435 583 }
duke@435 584
duke@435 585
duke@435 586 oop StringTable::intern(oop string, TRAPS)
duke@435 587 {
duke@435 588 if (string == NULL) return NULL;
duke@435 589 ResourceMark rm(THREAD);
duke@435 590 int length;
duke@435 591 Handle h_string (THREAD, string);
duke@435 592 jchar* chars = java_lang_String::as_unicode_string(string, length);
duke@435 593 oop result = intern(h_string, chars, length, CHECK_NULL);
duke@435 594 return result;
duke@435 595 }
duke@435 596
duke@435 597
duke@435 598 oop StringTable::intern(const char* utf8_string, TRAPS) {
duke@435 599 if (utf8_string == NULL) return NULL;
duke@435 600 ResourceMark rm(THREAD);
duke@435 601 int length = UTF8::unicode_length(utf8_string);
duke@435 602 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
duke@435 603 UTF8::convert_to_unicode(utf8_string, chars, length);
duke@435 604 Handle string;
duke@435 605 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 606 return result;
duke@435 607 }
duke@435 608
coleenp@2497 609 void StringTable::unlink(BoolObjectClosure* is_alive) {
coleenp@2497 610 // Readers of the table are unlocked, so we should only be removing
coleenp@2497 611 // entries at a safepoint.
coleenp@2497 612 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
coleenp@2497 613 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 614 for (HashtableEntry<oop>** p = the_table()->bucket_addr(i); *p != NULL; ) {
coleenp@2497 615 HashtableEntry<oop>* entry = *p;
coleenp@2497 616 if (entry->is_shared()) {
coleenp@2497 617 break;
coleenp@2497 618 }
coleenp@2497 619 assert(entry->literal() != NULL, "just checking");
coleenp@2497 620 if (is_alive->do_object_b(entry->literal())) {
coleenp@2497 621 p = entry->next_addr();
coleenp@2497 622 } else {
coleenp@2497 623 *p = entry->next();
coleenp@2497 624 the_table()->free_entry(entry);
coleenp@2497 625 }
coleenp@2497 626 }
coleenp@2497 627 }
coleenp@2497 628 }
coleenp@2497 629
coleenp@2497 630 void StringTable::oops_do(OopClosure* f) {
coleenp@2497 631 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 632 HashtableEntry<oop>** p = the_table()->bucket_addr(i);
coleenp@2497 633 HashtableEntry<oop>* entry = the_table()->bucket(i);
coleenp@2497 634 while (entry != NULL) {
coleenp@2497 635 f->do_oop((oop*)entry->literal_addr());
coleenp@2497 636
coleenp@2497 637 // Did the closure remove the literal from the table?
coleenp@2497 638 if (entry->literal() == NULL) {
coleenp@2497 639 assert(!entry->is_shared(), "immutable hashtable entry?");
coleenp@2497 640 *p = entry->next();
coleenp@2497 641 the_table()->free_entry(entry);
coleenp@2497 642 } else {
coleenp@2497 643 p = entry->next_addr();
coleenp@2497 644 }
coleenp@2497 645 entry = (HashtableEntry<oop>*)HashtableEntry<oop>::make_ptr(*p);
coleenp@2497 646 }
coleenp@2497 647 }
coleenp@2497 648 }
coleenp@2497 649
duke@435 650 void StringTable::verify() {
duke@435 651 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 652 HashtableEntry<oop>* p = the_table()->bucket(i);
duke@435 653 for ( ; p != NULL; p = p->next()) {
duke@435 654 oop s = p->literal();
duke@435 655 guarantee(s != NULL, "interned string is NULL");
jcoomes@2661 656 guarantee(s->is_perm() || !JavaObjectsInPerm, "interned string not in permspace");
never@2700 657 unsigned int h = java_lang_String::hash_string(s);
duke@435 658 guarantee(p->hash() == h, "broken hash in string table entry");
duke@435 659 guarantee(the_table()->hash_to_index(h) == i,
duke@435 660 "wrong index in string table");
duke@435 661 }
duke@435 662 }
duke@435 663 }

mercurial