src/share/vm/classfile/symbolTable.cpp

Thu, 27 Jan 2011 16:11:27 -0800

author
coleenp
date
Thu, 27 Jan 2011 16:11:27 -0800
changeset 2497
3582bf76420e
parent 2314
f95d63e2154a
child 2618
df1347358fe6
permissions
-rw-r--r--

6990754: Use native memory and reference counting to implement SymbolTable
Summary: move symbols from permgen into C heap and reference count them
Reviewed-by: never, acorn, jmasa, stefank

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

mercurial