src/share/vm/classfile/symbolTable.cpp

Tue, 22 Mar 2011 13:36:33 -0700

author
jcoomes
date
Tue, 22 Mar 2011 13:36:33 -0700
changeset 2661
b099aaf51bf8
parent 2618
df1347358fe6
child 2700
352622fd140a
permissions
-rw-r--r--

6962931: move interned strings out of the perm gen
Reviewed-by: never, coleenp, ysr, jwilhelm

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 "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
jrose@1100 207 // Suggestion: Push unicode-based lookup all the way into the hashing
jrose@1100 208 // and probing logic, so there is no need for convert_to_utf8 until
coleenp@2497 209 // an actual new Symbol* is created.
coleenp@2497 210 Symbol* SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
jrose@1100 211 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 212 char stack_buf[128];
jrose@1100 213 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 214 char* chars = stack_buf;
jrose@1100 215 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 216 return lookup(chars, utf8_length, THREAD);
jrose@1100 217 } else {
jrose@1100 218 ResourceMark rm(THREAD);
jrose@1100 219 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 220 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 221 return lookup(chars, utf8_length, THREAD);
jrose@1100 222 }
jrose@1100 223 }
jrose@1100 224
coleenp@2497 225 Symbol* SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
jrose@1100 226 unsigned int& hash) {
jrose@1100 227 int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
jrose@1100 228 char stack_buf[128];
jrose@1100 229 if (utf8_length < (int) sizeof(stack_buf)) {
jrose@1100 230 char* chars = stack_buf;
jrose@1100 231 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 232 return lookup_only(chars, utf8_length, hash);
jrose@1100 233 } else {
jrose@1100 234 ResourceMark rm;
jrose@1100 235 char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
jrose@1100 236 UNICODE::convert_to_utf8(name, utf16_length, chars);
jrose@1100 237 return lookup_only(chars, utf8_length, hash);
jrose@1100 238 }
jrose@1100 239 }
jrose@1100 240
duke@435 241 void SymbolTable::add(constantPoolHandle cp, int names_count,
duke@435 242 const char** names, int* lengths, int* cp_indices,
duke@435 243 unsigned int* hashValues, TRAPS) {
duke@435 244 SymbolTable* table = the_table();
duke@435 245 bool added = table->basic_add(cp, names_count, names, lengths,
duke@435 246 cp_indices, hashValues, CHECK);
duke@435 247 if (!added) {
duke@435 248 // do it the hard way
duke@435 249 for (int i=0; i<names_count; i++) {
duke@435 250 int index = table->hash_to_index(hashValues[i]);
coleenp@2497 251 Symbol* sym = table->basic_add(index, (u1*)names[i], lengths[i],
duke@435 252 hashValues[i], CHECK);
duke@435 253 cp->symbol_at_put(cp_indices[i], sym);
duke@435 254 }
duke@435 255 }
duke@435 256 }
duke@435 257
coleenp@2497 258 Symbol* SymbolTable::basic_add(int index, u1 *name, int len,
duke@435 259 unsigned int hashValue, TRAPS) {
duke@435 260 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
duke@435 261 "proposed name of symbol must be stable");
duke@435 262
duke@435 263 // We assume that lookup() has been called already, that it failed,
duke@435 264 // and symbol was not found. We create the symbol here.
coleenp@2497 265 Symbol* sym = allocate_symbol(name, len, CHECK_NULL);
duke@435 266
coleenp@2497 267 // Allocation must be done before grabbing the SymbolTable_lock lock
duke@435 268 MutexLocker ml(SymbolTable_lock, THREAD);
duke@435 269
duke@435 270 assert(sym->equals((char*)name, len), "symbol must be properly initialized");
duke@435 271
duke@435 272 // Since look-up was done lock-free, we need to check if another
duke@435 273 // thread beat us in the race to insert the symbol.
duke@435 274
coleenp@2497 275 Symbol* test = lookup(index, (char*)name, len, hashValue);
duke@435 276 if (test != NULL) {
twisti@1040 277 // A race occurred and another thread introduced the symbol, this one
duke@435 278 // will be dropped and collected.
coleenp@2497 279 delete sym;
coleenp@2497 280 assert(test->refcount() != 0, "lookup should have incremented the count");
duke@435 281 return test;
duke@435 282 }
duke@435 283
coleenp@2497 284 HashtableEntry<Symbol*>* entry = new_entry(hashValue, sym);
coleenp@2497 285 sym->increment_refcount();
duke@435 286 add_entry(index, entry);
coleenp@2497 287 return sym;
duke@435 288 }
duke@435 289
duke@435 290 bool SymbolTable::basic_add(constantPoolHandle cp, int names_count,
duke@435 291 const char** names, int* lengths,
duke@435 292 int* cp_indices, unsigned int* hashValues,
duke@435 293 TRAPS) {
coleenp@2497 294 Symbol* syms[symbol_alloc_batch_size];
coleenp@2497 295 bool allocated = allocate_symbols(names_count, (const u1**)names, lengths,
coleenp@2497 296 syms, CHECK_false);
duke@435 297 if (!allocated) {
duke@435 298 return false;
duke@435 299 }
duke@435 300
duke@435 301 // Allocation must be done before grabbing the SymbolTable_lock lock
duke@435 302 MutexLocker ml(SymbolTable_lock, THREAD);
duke@435 303
coleenp@2497 304 for (int i=0; i<names_count; i++) {
duke@435 305 assert(syms[i]->equals(names[i], lengths[i]), "symbol must be properly initialized");
duke@435 306 // Since look-up was done lock-free, we need to check if another
duke@435 307 // thread beat us in the race to insert the symbol.
duke@435 308 int index = hash_to_index(hashValues[i]);
coleenp@2497 309 Symbol* test = lookup(index, names[i], lengths[i], hashValues[i]);
duke@435 310 if (test != NULL) {
twisti@1040 311 // A race occurred and another thread introduced the symbol, this one
duke@435 312 // will be dropped and collected. Use test instead.
duke@435 313 cp->symbol_at_put(cp_indices[i], test);
coleenp@2497 314 assert(test->refcount() != 0, "lookup should have incremented the count");
coleenp@2497 315 delete syms[i];
duke@435 316 } else {
coleenp@2497 317 Symbol* sym = syms[i];
coleenp@2497 318 HashtableEntry<Symbol*>* entry = new_entry(hashValues[i], sym);
coleenp@2497 319 sym->increment_refcount(); // increment refcount in external hashtable
duke@435 320 add_entry(index, entry);
duke@435 321 cp->symbol_at_put(cp_indices[i], sym);
duke@435 322 }
duke@435 323 }
duke@435 324
duke@435 325 return true;
duke@435 326 }
duke@435 327
duke@435 328
duke@435 329 void SymbolTable::verify() {
duke@435 330 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 331 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 332 for ( ; p != NULL; p = p->next()) {
coleenp@2497 333 Symbol* s = (Symbol*)(p->literal());
duke@435 334 guarantee(s != NULL, "symbol is NULL");
duke@435 335 unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
duke@435 336 guarantee(p->hash() == h, "broken hash in symbol table entry");
duke@435 337 guarantee(the_table()->hash_to_index(h) == i,
duke@435 338 "wrong index in symbol table");
duke@435 339 }
duke@435 340 }
duke@435 341 }
duke@435 342
duke@435 343
duke@435 344 //---------------------------------------------------------------------------
duke@435 345 // Non-product code
duke@435 346
duke@435 347 #ifndef PRODUCT
duke@435 348
duke@435 349 void SymbolTable::print_histogram() {
duke@435 350 MutexLocker ml(SymbolTable_lock);
duke@435 351 const int results_length = 100;
duke@435 352 int results[results_length];
duke@435 353 int i,j;
duke@435 354
duke@435 355 // initialize results to zero
duke@435 356 for (j = 0; j < results_length; j++) {
duke@435 357 results[j] = 0;
duke@435 358 }
duke@435 359
duke@435 360 int total = 0;
duke@435 361 int max_symbols = 0;
duke@435 362 int out_of_range = 0;
coleenp@2497 363 int memory_total = 0;
coleenp@2497 364 int count = 0;
duke@435 365 for (i = 0; i < the_table()->table_size(); i++) {
coleenp@2497 366 HashtableEntry<Symbol*>* p = the_table()->bucket(i);
duke@435 367 for ( ; p != NULL; p = p->next()) {
coleenp@2497 368 memory_total += p->literal()->object_size();
coleenp@2497 369 count++;
coleenp@2497 370 int counter = p->literal()->utf8_length();
duke@435 371 total += counter;
duke@435 372 if (counter < results_length) {
duke@435 373 results[counter]++;
duke@435 374 } else {
duke@435 375 out_of_range++;
duke@435 376 }
duke@435 377 max_symbols = MAX2(max_symbols, counter);
duke@435 378 }
duke@435 379 }
duke@435 380 tty->print_cr("Symbol Table:");
coleenp@2497 381 tty->print_cr("Total number of symbols %5d", count);
coleenp@2497 382 tty->print_cr("Total size in memory %5dK",
coleenp@2497 383 (memory_total*HeapWordSize)/1024);
coleenp@2497 384 tty->print_cr("Total counted %5d", symbols_counted);
coleenp@2497 385 tty->print_cr("Total removed %5d", symbols_removed);
coleenp@2497 386 if (symbols_counted > 0) {
coleenp@2497 387 tty->print_cr("Percent removed %3.2f",
coleenp@2497 388 ((float)symbols_removed/(float)symbols_counted)* 100);
coleenp@2497 389 }
coleenp@2497 390 tty->print_cr("Reference counts %5d", Symbol::_total_count);
coleenp@2497 391 tty->print_cr("Histogram of symbol length:");
duke@435 392 tty->print_cr("%8s %5d", "Total ", total);
duke@435 393 tty->print_cr("%8s %5d", "Maximum", max_symbols);
duke@435 394 tty->print_cr("%8s %3.2f", "Average",
duke@435 395 ((float) total / (float) the_table()->table_size()));
duke@435 396 tty->print_cr("%s", "Histogram:");
duke@435 397 tty->print_cr(" %s %29s", "Length", "Number chains that length");
duke@435 398 for (i = 0; i < results_length; i++) {
duke@435 399 if (results[i] > 0) {
duke@435 400 tty->print_cr("%6d %10d", i, results[i]);
duke@435 401 }
duke@435 402 }
coleenp@2497 403 if (Verbose) {
coleenp@2497 404 int line_length = 70;
coleenp@2497 405 tty->print_cr("%s %30s", " Length", "Number chains that length");
coleenp@2497 406 for (i = 0; i < results_length; i++) {
coleenp@2497 407 if (results[i] > 0) {
coleenp@2497 408 tty->print("%4d", i);
coleenp@2497 409 for (j = 0; (j < results[i]) && (j < line_length); j++) {
coleenp@2497 410 tty->print("%1s", "*");
coleenp@2497 411 }
coleenp@2497 412 if (j == line_length) {
coleenp@2497 413 tty->print("%1s", "+");
coleenp@2497 414 }
coleenp@2497 415 tty->cr();
duke@435 416 }
coleenp@2497 417 }
coleenp@2497 418 }
coleenp@2497 419 tty->print_cr(" %s %d: %d\n", "Number chains longer than",
coleenp@2497 420 results_length, out_of_range);
coleenp@2497 421 }
coleenp@2497 422
coleenp@2497 423 void SymbolTable::print() {
coleenp@2497 424 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 425 HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i);
coleenp@2497 426 HashtableEntry<Symbol*>* entry = the_table()->bucket(i);
coleenp@2497 427 if (entry != NULL) {
coleenp@2497 428 while (entry != NULL) {
coleenp@2497 429 tty->print(PTR_FORMAT " ", entry->literal());
coleenp@2497 430 entry->literal()->print();
coleenp@2497 431 tty->print(" %d", entry->literal()->refcount());
coleenp@2497 432 p = entry->next_addr();
coleenp@2497 433 entry = (HashtableEntry<Symbol*>*)HashtableEntry<Symbol*>::make_ptr(*p);
duke@435 434 }
duke@435 435 tty->cr();
duke@435 436 }
duke@435 437 }
duke@435 438 }
duke@435 439
duke@435 440 #endif // PRODUCT
duke@435 441
duke@435 442 // --------------------------------------------------------------------------
duke@435 443
duke@435 444 #ifdef ASSERT
duke@435 445 class StableMemoryChecker : public StackObj {
duke@435 446 enum { _bufsize = wordSize*4 };
duke@435 447
duke@435 448 address _region;
duke@435 449 jint _size;
duke@435 450 u1 _save_buf[_bufsize];
duke@435 451
duke@435 452 int sample(u1* save_buf) {
duke@435 453 if (_size <= _bufsize) {
duke@435 454 memcpy(save_buf, _region, _size);
duke@435 455 return _size;
duke@435 456 } else {
duke@435 457 // copy head and tail
duke@435 458 memcpy(&save_buf[0], _region, _bufsize/2);
duke@435 459 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
duke@435 460 return (_bufsize/2)*2;
duke@435 461 }
duke@435 462 }
duke@435 463
duke@435 464 public:
duke@435 465 StableMemoryChecker(const void* region, jint size) {
duke@435 466 _region = (address) region;
duke@435 467 _size = size;
duke@435 468 sample(_save_buf);
duke@435 469 }
duke@435 470
duke@435 471 bool verify() {
duke@435 472 u1 check_buf[sizeof(_save_buf)];
duke@435 473 int check_size = sample(check_buf);
duke@435 474 return (0 == memcmp(_save_buf, check_buf, check_size));
duke@435 475 }
duke@435 476
duke@435 477 void set_region(const void* region) { _region = (address) region; }
duke@435 478 };
duke@435 479 #endif
duke@435 480
duke@435 481
duke@435 482 // --------------------------------------------------------------------------
duke@435 483
duke@435 484
duke@435 485 // Compute the hash value for a java.lang.String object which would
duke@435 486 // contain the characters passed in. This hash value is used for at
duke@435 487 // least two purposes.
duke@435 488 //
duke@435 489 // (a) As the hash value used by the StringTable for bucket selection
duke@435 490 // and comparison (stored in the HashtableEntry structures). This
duke@435 491 // is used in the String.intern() method.
duke@435 492 //
duke@435 493 // (b) As the hash value used by the String object itself, in
duke@435 494 // String.hashCode(). This value is normally calculate in Java code
duke@435 495 // in the String.hashCode method(), but is precomputed for String
duke@435 496 // objects in the shared archive file.
duke@435 497 //
duke@435 498 // For this reason, THIS ALGORITHM MUST MATCH String.hashCode().
duke@435 499
duke@435 500 int StringTable::hash_string(jchar* s, int len) {
duke@435 501 unsigned h = 0;
duke@435 502 while (len-- > 0) {
duke@435 503 h = 31*h + (unsigned) *s;
duke@435 504 s++;
duke@435 505 }
duke@435 506 return h;
duke@435 507 }
duke@435 508
duke@435 509
duke@435 510 StringTable* StringTable::_the_table = NULL;
duke@435 511
duke@435 512 oop StringTable::lookup(int index, jchar* name,
duke@435 513 int len, unsigned int hash) {
coleenp@2497 514 for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) {
duke@435 515 if (l->hash() == hash) {
duke@435 516 if (java_lang_String::equals(l->literal(), name, len)) {
duke@435 517 return l->literal();
duke@435 518 }
duke@435 519 }
duke@435 520 }
duke@435 521 return NULL;
duke@435 522 }
duke@435 523
duke@435 524
duke@435 525 oop StringTable::basic_add(int index, Handle string_or_null, jchar* name,
duke@435 526 int len, unsigned int hashValue, TRAPS) {
duke@435 527 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
duke@435 528 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
duke@435 529 "proposed name of symbol must be stable");
duke@435 530
duke@435 531 Handle string;
duke@435 532 // try to reuse the string if possible
jcoomes@2661 533 if (!string_or_null.is_null() && (!JavaObjectsInPerm || string_or_null()->is_perm())) {
duke@435 534 string = string_or_null;
duke@435 535 } else {
duke@435 536 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL);
duke@435 537 }
duke@435 538
duke@435 539 // Allocation must be done before grapping the SymbolTable_lock lock
duke@435 540 MutexLocker ml(StringTable_lock, THREAD);
duke@435 541
duke@435 542 assert(java_lang_String::equals(string(), name, len),
duke@435 543 "string must be properly initialized");
duke@435 544
duke@435 545 // Since look-up was done lock-free, we need to check if another
duke@435 546 // thread beat us in the race to insert the symbol.
duke@435 547
duke@435 548 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
duke@435 549 if (test != NULL) {
duke@435 550 // Entry already added
duke@435 551 return test;
duke@435 552 }
duke@435 553
coleenp@2497 554 HashtableEntry<oop>* entry = new_entry(hashValue, string());
duke@435 555 add_entry(index, entry);
duke@435 556 return string();
duke@435 557 }
duke@435 558
duke@435 559
coleenp@2497 560 oop StringTable::lookup(Symbol* symbol) {
duke@435 561 ResourceMark rm;
duke@435 562 int length;
duke@435 563 jchar* chars = symbol->as_unicode(length);
duke@435 564 unsigned int hashValue = hash_string(chars, length);
duke@435 565 int index = the_table()->hash_to_index(hashValue);
duke@435 566 return the_table()->lookup(index, chars, length, hashValue);
duke@435 567 }
duke@435 568
duke@435 569
duke@435 570 oop StringTable::intern(Handle string_or_null, jchar* name,
duke@435 571 int len, TRAPS) {
duke@435 572 unsigned int hashValue = hash_string(name, len);
duke@435 573 int index = the_table()->hash_to_index(hashValue);
duke@435 574 oop string = the_table()->lookup(index, name, len, hashValue);
duke@435 575
duke@435 576 // Found
duke@435 577 if (string != NULL) return string;
duke@435 578
duke@435 579 // Otherwise, add to symbol to table
duke@435 580 return the_table()->basic_add(index, string_or_null, name, len,
duke@435 581 hashValue, CHECK_NULL);
duke@435 582 }
duke@435 583
coleenp@2497 584 oop StringTable::intern(Symbol* symbol, TRAPS) {
duke@435 585 if (symbol == NULL) return NULL;
duke@435 586 ResourceMark rm(THREAD);
duke@435 587 int length;
duke@435 588 jchar* chars = symbol->as_unicode(length);
duke@435 589 Handle string;
duke@435 590 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 591 return result;
duke@435 592 }
duke@435 593
duke@435 594
duke@435 595 oop StringTable::intern(oop string, TRAPS)
duke@435 596 {
duke@435 597 if (string == NULL) return NULL;
duke@435 598 ResourceMark rm(THREAD);
duke@435 599 int length;
duke@435 600 Handle h_string (THREAD, string);
duke@435 601 jchar* chars = java_lang_String::as_unicode_string(string, length);
duke@435 602 oop result = intern(h_string, chars, length, CHECK_NULL);
duke@435 603 return result;
duke@435 604 }
duke@435 605
duke@435 606
duke@435 607 oop StringTable::intern(const char* utf8_string, TRAPS) {
duke@435 608 if (utf8_string == NULL) return NULL;
duke@435 609 ResourceMark rm(THREAD);
duke@435 610 int length = UTF8::unicode_length(utf8_string);
duke@435 611 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
duke@435 612 UTF8::convert_to_unicode(utf8_string, chars, length);
duke@435 613 Handle string;
duke@435 614 oop result = intern(string, chars, length, CHECK_NULL);
duke@435 615 return result;
duke@435 616 }
duke@435 617
coleenp@2497 618 void StringTable::unlink(BoolObjectClosure* is_alive) {
coleenp@2497 619 // Readers of the table are unlocked, so we should only be removing
coleenp@2497 620 // entries at a safepoint.
coleenp@2497 621 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
coleenp@2497 622 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 623 for (HashtableEntry<oop>** p = the_table()->bucket_addr(i); *p != NULL; ) {
coleenp@2497 624 HashtableEntry<oop>* entry = *p;
coleenp@2497 625 if (entry->is_shared()) {
coleenp@2497 626 break;
coleenp@2497 627 }
coleenp@2497 628 assert(entry->literal() != NULL, "just checking");
coleenp@2497 629 if (is_alive->do_object_b(entry->literal())) {
coleenp@2497 630 p = entry->next_addr();
coleenp@2497 631 } else {
coleenp@2497 632 *p = entry->next();
coleenp@2497 633 the_table()->free_entry(entry);
coleenp@2497 634 }
coleenp@2497 635 }
coleenp@2497 636 }
coleenp@2497 637 }
coleenp@2497 638
coleenp@2497 639 void StringTable::oops_do(OopClosure* f) {
coleenp@2497 640 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 641 HashtableEntry<oop>** p = the_table()->bucket_addr(i);
coleenp@2497 642 HashtableEntry<oop>* entry = the_table()->bucket(i);
coleenp@2497 643 while (entry != NULL) {
coleenp@2497 644 f->do_oop((oop*)entry->literal_addr());
coleenp@2497 645
coleenp@2497 646 // Did the closure remove the literal from the table?
coleenp@2497 647 if (entry->literal() == NULL) {
coleenp@2497 648 assert(!entry->is_shared(), "immutable hashtable entry?");
coleenp@2497 649 *p = entry->next();
coleenp@2497 650 the_table()->free_entry(entry);
coleenp@2497 651 } else {
coleenp@2497 652 p = entry->next_addr();
coleenp@2497 653 }
coleenp@2497 654 entry = (HashtableEntry<oop>*)HashtableEntry<oop>::make_ptr(*p);
coleenp@2497 655 }
coleenp@2497 656 }
coleenp@2497 657 }
coleenp@2497 658
duke@435 659 void StringTable::verify() {
duke@435 660 for (int i = 0; i < the_table()->table_size(); ++i) {
coleenp@2497 661 HashtableEntry<oop>* p = the_table()->bucket(i);
duke@435 662 for ( ; p != NULL; p = p->next()) {
duke@435 663 oop s = p->literal();
duke@435 664 guarantee(s != NULL, "interned string is NULL");
jcoomes@2661 665 guarantee(s->is_perm() || !JavaObjectsInPerm, "interned string not in permspace");
duke@435 666
duke@435 667 int length;
duke@435 668 jchar* chars = java_lang_String::as_unicode_string(s, length);
duke@435 669 unsigned int h = hash_string(chars, length);
duke@435 670 guarantee(p->hash() == h, "broken hash in string table entry");
duke@435 671 guarantee(the_table()->hash_to_index(h) == i,
duke@435 672 "wrong index in string table");
duke@435 673 }
duke@435 674 }
duke@435 675 }

mercurial