duke@435: /* jrose@1100: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_symbolTable.cpp.incl" duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: duke@435: SymbolTable* SymbolTable::_the_table = NULL; duke@435: duke@435: // Lookup a symbol in a bucket. duke@435: duke@435: symbolOop SymbolTable::lookup(int index, const char* name, duke@435: int len, unsigned int hash) { duke@435: for (HashtableEntry* e = bucket(index); e != NULL; e = e->next()) { duke@435: if (e->hash() == hash) { duke@435: symbolOop sym = symbolOop(e->literal()); duke@435: if (sym->equals(name, len)) { duke@435: return sym; duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: // We take care not to be blocking while holding the duke@435: // SymbolTable_lock. Otherwise, the system might deadlock, since the duke@435: // symboltable is used during compilation (VM_thread) The lock free duke@435: // synchronization is simplified by the fact that we do not delete duke@435: // entries in the symbol table during normal execution (only during duke@435: // safepoints). duke@435: duke@435: symbolOop SymbolTable::lookup(const char* name, int len, TRAPS) { duke@435: unsigned int hashValue = hash_symbol(name, len); duke@435: int index = the_table()->hash_to_index(hashValue); duke@435: duke@435: symbolOop s = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found duke@435: if (s != NULL) return s; duke@435: duke@435: // Otherwise, add to symbol to table duke@435: return the_table()->basic_add(index, (u1*)name, len, hashValue, CHECK_NULL); duke@435: } duke@435: duke@435: symbolOop SymbolTable::lookup(symbolHandle sym, int begin, int end, TRAPS) { duke@435: char* buffer; duke@435: int index, len; duke@435: unsigned int hashValue; duke@435: char* name; duke@435: { duke@435: debug_only(No_Safepoint_Verifier nsv;) duke@435: duke@435: name = (char*)sym->base() + begin; duke@435: len = end - begin; duke@435: hashValue = hash_symbol(name, len); duke@435: index = the_table()->hash_to_index(hashValue); duke@435: symbolOop s = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found duke@435: if (s != NULL) return s; duke@435: } duke@435: duke@435: // Otherwise, add to symbol to table. Copy to a C string first. duke@435: char stack_buf[128]; duke@435: ResourceMark rm(THREAD); duke@435: if (len <= 128) { duke@435: buffer = stack_buf; duke@435: } else { duke@435: buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); duke@435: } duke@435: for (int i=0; ibasic_add(index, (u1*)buffer, len, hashValue, CHECK_NULL); duke@435: } duke@435: duke@435: symbolOop SymbolTable::lookup_only(const char* name, int len, duke@435: unsigned int& hash) { duke@435: hash = hash_symbol(name, len); duke@435: int index = the_table()->hash_to_index(hash); duke@435: duke@435: return the_table()->lookup(index, name, len, hash); duke@435: } duke@435: jrose@1100: // Suggestion: Push unicode-based lookup all the way into the hashing jrose@1100: // and probing logic, so there is no need for convert_to_utf8 until jrose@1100: // an actual new symbolOop is created. jrose@1100: symbolOop SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) { jrose@1100: int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); jrose@1100: char stack_buf[128]; jrose@1100: if (utf8_length < (int) sizeof(stack_buf)) { jrose@1100: char* chars = stack_buf; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup(chars, utf8_length, THREAD); jrose@1100: } else { jrose@1100: ResourceMark rm(THREAD); jrose@1100: char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup(chars, utf8_length, THREAD); jrose@1100: } jrose@1100: } jrose@1100: jrose@1100: symbolOop SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length, jrose@1100: unsigned int& hash) { jrose@1100: int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length); jrose@1100: char stack_buf[128]; jrose@1100: if (utf8_length < (int) sizeof(stack_buf)) { jrose@1100: char* chars = stack_buf; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup_only(chars, utf8_length, hash); jrose@1100: } else { jrose@1100: ResourceMark rm; jrose@1100: char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);; jrose@1100: UNICODE::convert_to_utf8(name, utf16_length, chars); jrose@1100: return lookup_only(chars, utf8_length, hash); jrose@1100: } jrose@1100: } jrose@1100: duke@435: void SymbolTable::add(constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS) { duke@435: SymbolTable* table = the_table(); duke@435: bool added = table->basic_add(cp, names_count, names, lengths, duke@435: cp_indices, hashValues, CHECK); duke@435: if (!added) { duke@435: // do it the hard way duke@435: for (int i=0; ihash_to_index(hashValues[i]); duke@435: symbolOop sym = table->basic_add(index, (u1*)names[i], lengths[i], duke@435: hashValues[i], CHECK); duke@435: cp->symbol_at_put(cp_indices[i], sym); duke@435: } duke@435: } duke@435: } duke@435: duke@435: symbolOop SymbolTable::basic_add(int index, u1 *name, int len, duke@435: unsigned int hashValue, TRAPS) { duke@435: assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), duke@435: "proposed name of symbol must be stable"); duke@435: duke@435: // We assume that lookup() has been called already, that it failed, duke@435: // and symbol was not found. We create the symbol here. duke@435: symbolKlass* sk = (symbolKlass*) Universe::symbolKlassObj()->klass_part(); duke@435: symbolOop s_oop = sk->allocate_symbol(name, len, CHECK_NULL); duke@435: symbolHandle sym (THREAD, s_oop); duke@435: duke@435: // Allocation must be done before grapping the SymbolTable_lock lock duke@435: MutexLocker ml(SymbolTable_lock, THREAD); duke@435: duke@435: assert(sym->equals((char*)name, len), "symbol must be properly initialized"); duke@435: duke@435: // Since look-up was done lock-free, we need to check if another duke@435: // thread beat us in the race to insert the symbol. duke@435: duke@435: symbolOop test = lookup(index, (char*)name, len, hashValue); duke@435: if (test != NULL) { twisti@1040: // A race occurred and another thread introduced the symbol, this one duke@435: // will be dropped and collected. duke@435: return test; duke@435: } duke@435: duke@435: HashtableEntry* entry = new_entry(hashValue, sym()); duke@435: add_entry(index, entry); duke@435: return sym(); duke@435: } duke@435: duke@435: bool SymbolTable::basic_add(constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, duke@435: int* cp_indices, unsigned int* hashValues, duke@435: TRAPS) { duke@435: symbolKlass* sk = (symbolKlass*) Universe::symbolKlassObj()->klass_part(); duke@435: symbolOop sym_oops[symbol_alloc_batch_size]; duke@435: bool allocated = sk->allocate_symbols(names_count, names, lengths, duke@435: sym_oops, CHECK_false); duke@435: if (!allocated) { duke@435: return false; duke@435: } duke@435: symbolHandle syms[symbol_alloc_batch_size]; duke@435: int i; duke@435: for (i=0; iequals(names[i], lengths[i]), "symbol must be properly initialized"); duke@435: // Since look-up was done lock-free, we need to check if another duke@435: // thread beat us in the race to insert the symbol. duke@435: int index = hash_to_index(hashValues[i]); duke@435: symbolOop test = lookup(index, names[i], lengths[i], hashValues[i]); duke@435: if (test != NULL) { twisti@1040: // A race occurred and another thread introduced the symbol, this one duke@435: // will be dropped and collected. Use test instead. duke@435: cp->symbol_at_put(cp_indices[i], test); duke@435: } else { duke@435: symbolOop sym = syms[i](); duke@435: HashtableEntry* entry = new_entry(hashValues[i], sym); duke@435: add_entry(index, entry); duke@435: cp->symbol_at_put(cp_indices[i], sym); duke@435: } duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: duke@435: void SymbolTable::verify() { duke@435: for (int i = 0; i < the_table()->table_size(); ++i) { duke@435: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { duke@435: symbolOop s = symbolOop(p->literal()); duke@435: guarantee(s != NULL, "symbol is NULL"); duke@435: s->verify(); duke@435: guarantee(s->is_perm(), "symbol not in permspace"); duke@435: unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length()); duke@435: guarantee(p->hash() == h, "broken hash in symbol table entry"); duke@435: guarantee(the_table()->hash_to_index(h) == i, duke@435: "wrong index in symbol table"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: //--------------------------------------------------------------------------- duke@435: // Non-product code duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void SymbolTable::print_histogram() { duke@435: MutexLocker ml(SymbolTable_lock); duke@435: const int results_length = 100; duke@435: int results[results_length]; duke@435: int i,j; duke@435: duke@435: // initialize results to zero duke@435: for (j = 0; j < results_length; j++) { duke@435: results[j] = 0; duke@435: } duke@435: duke@435: int total = 0; duke@435: int max_symbols = 0; duke@435: int out_of_range = 0; duke@435: for (i = 0; i < the_table()->table_size(); i++) { duke@435: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { duke@435: int counter = symbolOop(p->literal())->utf8_length(); duke@435: total += counter; duke@435: if (counter < results_length) { duke@435: results[counter]++; duke@435: } else { duke@435: out_of_range++; duke@435: } duke@435: max_symbols = MAX2(max_symbols, counter); duke@435: } duke@435: } duke@435: tty->print_cr("Symbol Table:"); duke@435: tty->print_cr("%8s %5d", "Total ", total); duke@435: tty->print_cr("%8s %5d", "Maximum", max_symbols); duke@435: tty->print_cr("%8s %3.2f", "Average", duke@435: ((float) total / (float) the_table()->table_size())); duke@435: tty->print_cr("%s", "Histogram:"); duke@435: tty->print_cr(" %s %29s", "Length", "Number chains that length"); duke@435: for (i = 0; i < results_length; i++) { duke@435: if (results[i] > 0) { duke@435: tty->print_cr("%6d %10d", i, results[i]); duke@435: } duke@435: } duke@435: int line_length = 70; duke@435: tty->print_cr("%s %30s", " Length", "Number chains that length"); duke@435: for (i = 0; i < results_length; i++) { duke@435: if (results[i] > 0) { duke@435: tty->print("%4d", i); duke@435: for (j = 0; (j < results[i]) && (j < line_length); j++) { duke@435: tty->print("%1s", "*"); duke@435: } duke@435: if (j == line_length) { duke@435: tty->print("%1s", "+"); duke@435: } duke@435: tty->cr(); duke@435: } duke@435: } duke@435: tty->print_cr(" %s %d: %d\n", "Number chains longer than", duke@435: results_length, out_of_range); duke@435: } duke@435: duke@435: #endif // PRODUCT duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: duke@435: #ifdef ASSERT duke@435: class StableMemoryChecker : public StackObj { duke@435: enum { _bufsize = wordSize*4 }; duke@435: duke@435: address _region; duke@435: jint _size; duke@435: u1 _save_buf[_bufsize]; duke@435: duke@435: int sample(u1* save_buf) { duke@435: if (_size <= _bufsize) { duke@435: memcpy(save_buf, _region, _size); duke@435: return _size; duke@435: } else { duke@435: // copy head and tail duke@435: memcpy(&save_buf[0], _region, _bufsize/2); duke@435: memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); duke@435: return (_bufsize/2)*2; duke@435: } duke@435: } duke@435: duke@435: public: duke@435: StableMemoryChecker(const void* region, jint size) { duke@435: _region = (address) region; duke@435: _size = size; duke@435: sample(_save_buf); duke@435: } duke@435: duke@435: bool verify() { duke@435: u1 check_buf[sizeof(_save_buf)]; duke@435: int check_size = sample(check_buf); duke@435: return (0 == memcmp(_save_buf, check_buf, check_size)); duke@435: } duke@435: duke@435: void set_region(const void* region) { _region = (address) region; } duke@435: }; duke@435: #endif duke@435: duke@435: duke@435: // -------------------------------------------------------------------------- duke@435: duke@435: duke@435: // Compute the hash value for a java.lang.String object which would duke@435: // contain the characters passed in. This hash value is used for at duke@435: // least two purposes. duke@435: // duke@435: // (a) As the hash value used by the StringTable for bucket selection duke@435: // and comparison (stored in the HashtableEntry structures). This duke@435: // is used in the String.intern() method. duke@435: // duke@435: // (b) As the hash value used by the String object itself, in duke@435: // String.hashCode(). This value is normally calculate in Java code duke@435: // in the String.hashCode method(), but is precomputed for String duke@435: // objects in the shared archive file. duke@435: // duke@435: // For this reason, THIS ALGORITHM MUST MATCH String.hashCode(). duke@435: duke@435: int StringTable::hash_string(jchar* s, int len) { duke@435: unsigned h = 0; duke@435: while (len-- > 0) { duke@435: h = 31*h + (unsigned) *s; duke@435: s++; duke@435: } duke@435: return h; duke@435: } duke@435: duke@435: duke@435: StringTable* StringTable::_the_table = NULL; duke@435: duke@435: oop StringTable::lookup(int index, jchar* name, duke@435: int len, unsigned int hash) { duke@435: for (HashtableEntry* l = bucket(index); l != NULL; l = l->next()) { duke@435: if (l->hash() == hash) { duke@435: if (java_lang_String::equals(l->literal(), name, len)) { duke@435: return l->literal(); duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: oop StringTable::basic_add(int index, Handle string_or_null, jchar* name, duke@435: int len, unsigned int hashValue, TRAPS) { duke@435: debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); duke@435: assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), duke@435: "proposed name of symbol must be stable"); duke@435: duke@435: Handle string; duke@435: // try to reuse the string if possible duke@435: if (!string_or_null.is_null() && string_or_null()->is_perm()) { duke@435: string = string_or_null; duke@435: } else { duke@435: string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL); duke@435: } duke@435: duke@435: // Allocation must be done before grapping the SymbolTable_lock lock duke@435: MutexLocker ml(StringTable_lock, THREAD); duke@435: duke@435: assert(java_lang_String::equals(string(), name, len), duke@435: "string must be properly initialized"); duke@435: duke@435: // Since look-up was done lock-free, we need to check if another duke@435: // thread beat us in the race to insert the symbol. duke@435: duke@435: oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) duke@435: if (test != NULL) { duke@435: // Entry already added duke@435: return test; duke@435: } duke@435: duke@435: HashtableEntry* entry = new_entry(hashValue, string()); duke@435: add_entry(index, entry); duke@435: return string(); duke@435: } duke@435: duke@435: duke@435: oop StringTable::lookup(symbolOop symbol) { duke@435: ResourceMark rm; duke@435: int length; duke@435: jchar* chars = symbol->as_unicode(length); duke@435: unsigned int hashValue = hash_string(chars, length); duke@435: int index = the_table()->hash_to_index(hashValue); duke@435: return the_table()->lookup(index, chars, length, hashValue); duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(Handle string_or_null, jchar* name, duke@435: int len, TRAPS) { duke@435: unsigned int hashValue = hash_string(name, len); duke@435: int index = the_table()->hash_to_index(hashValue); duke@435: oop string = the_table()->lookup(index, name, len, hashValue); duke@435: duke@435: // Found duke@435: if (string != NULL) return string; duke@435: duke@435: // Otherwise, add to symbol to table duke@435: return the_table()->basic_add(index, string_or_null, name, len, duke@435: hashValue, CHECK_NULL); duke@435: } duke@435: duke@435: oop StringTable::intern(symbolOop symbol, TRAPS) { duke@435: if (symbol == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length; duke@435: jchar* chars = symbol->as_unicode(length); duke@435: Handle string; duke@435: oop result = intern(string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(oop string, TRAPS) duke@435: { duke@435: if (string == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length; duke@435: Handle h_string (THREAD, string); duke@435: jchar* chars = java_lang_String::as_unicode_string(string, length); duke@435: oop result = intern(h_string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: oop StringTable::intern(const char* utf8_string, TRAPS) { duke@435: if (utf8_string == NULL) return NULL; duke@435: ResourceMark rm(THREAD); duke@435: int length = UTF8::unicode_length(utf8_string); duke@435: jchar* chars = NEW_RESOURCE_ARRAY(jchar, length); duke@435: UTF8::convert_to_unicode(utf8_string, chars, length); duke@435: Handle string; duke@435: oop result = intern(string, chars, length, CHECK_NULL); duke@435: return result; duke@435: } duke@435: duke@435: void StringTable::verify() { duke@435: for (int i = 0; i < the_table()->table_size(); ++i) { duke@435: HashtableEntry* p = the_table()->bucket(i); duke@435: for ( ; p != NULL; p = p->next()) { duke@435: oop s = p->literal(); duke@435: guarantee(s != NULL, "interned string is NULL"); duke@435: guarantee(s->is_perm(), "interned string not in permspace"); duke@435: duke@435: int length; duke@435: jchar* chars = java_lang_String::as_unicode_string(s, length); duke@435: unsigned int h = hash_string(chars, length); duke@435: guarantee(p->hash() == h, "broken hash in string table entry"); duke@435: guarantee(the_table()->hash_to_index(h) == i, duke@435: "wrong index in string table"); duke@435: } duke@435: } duke@435: }