duke@435: /* coleenp@3682: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP stefank@2314: #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP stefank@2314: stefank@2314: #include "memory/allocation.inline.hpp" coleenp@2497: #include "oops/symbol.hpp" stefank@2314: #include "utilities/hashtable.hpp" stefank@2314: coleenp@2497: // The symbol table holds all Symbol*s and corresponding interned strings. coleenp@2497: // Symbol*s and literal strings should be canonicalized. duke@435: // duke@435: // The interned strings are created lazily. duke@435: // duke@435: // It is implemented as an open hash table with a fixed number of buckets. duke@435: // duke@435: // %note: duke@435: // - symbolTableEntrys are allocated in blocks to reduce the space overhead. duke@435: duke@435: class BoolObjectClosure; coleenp@3865: class outputStream; duke@435: duke@435: coleenp@2497: // Class to hold a newly created or referenced Symbol* temporarily in scope. coleenp@2497: // new_symbol() and lookup() will create a Symbol* if not already in the coleenp@2497: // symbol table and add to the symbol's reference count. coleenp@2497: // probe() and lookup_only() will increment the refcount if symbol is found. coleenp@2497: class TempNewSymbol : public StackObj { coleenp@2497: Symbol* _temp; coleenp@2497: coleenp@2497: public: coleenp@2497: TempNewSymbol() : _temp(NULL) {} coleenp@2497: // Creating or looking up a symbol increments the symbol's reference count coleenp@2497: TempNewSymbol(Symbol *s) : _temp(s) {} coleenp@2497: coleenp@2497: // Operator= increments reference count. coleenp@2497: void operator=(const TempNewSymbol &s) { coleenp@2497: _temp = s._temp; coleenp@2497: if (_temp !=NULL) _temp->increment_refcount(); coleenp@2497: } coleenp@2497: coleenp@2497: // Decrement reference counter so it can go away if it's unique coleenp@2497: ~TempNewSymbol() { if (_temp != NULL) _temp->decrement_refcount(); } coleenp@2497: coleenp@2497: // Operators so they can be used like Symbols coleenp@2497: Symbol* operator -> () const { return _temp; } coleenp@2497: bool operator == (Symbol* o) const { return _temp == o; } coleenp@2497: // Sneaky conversion function coleenp@2497: operator Symbol*() { return _temp; } coleenp@2497: }; coleenp@2497: coleenp@2497: class SymbolTable : public Hashtable { duke@435: friend class VMStructs; coleenp@2497: friend class ClassFileParser; duke@435: duke@435: private: duke@435: // The symbol table duke@435: static SymbolTable* _the_table; duke@435: coleenp@3865: // Set if one bucket is out of balance due to hash algorithm deficiency coleenp@3865: static bool _needs_rehashing; coleenp@3865: static jint _seed; coleenp@3865: coleenp@2497: // For statistics coleenp@2497: static int symbols_removed; coleenp@2497: static int symbols_counted; coleenp@2497: coleenp@3682: Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F coleenp@2497: duke@435: // Adding elements coleenp@3682: Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue, coleenp@3682: bool c_heap, TRAPS); coleenp@3682: coleenp@3682: bool basic_add(Handle class_loader, constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS); duke@435: coleenp@3682: static void new_symbols(Handle class_loader, constantPoolHandle cp, coleenp@3682: int names_count, coleenp@2497: const char** name, int* lengths, coleenp@2497: int* cp_indices, unsigned int* hashValues, coleenp@2497: TRAPS) { coleenp@3682: add(class_loader, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); coleenp@2497: } coleenp@2497: duke@435: // Table size duke@435: enum { duke@435: symbol_table_size = 20011 duke@435: }; duke@435: coleenp@2497: Symbol* lookup(int index, const char* name, int len, unsigned int hash); duke@435: duke@435: SymbolTable() coleenp@2497: : Hashtable(symbol_table_size, sizeof (HashtableEntry)) {} duke@435: duke@435: SymbolTable(HashtableBucket* t, int number_of_entries) coleenp@2497: : Hashtable(symbol_table_size, sizeof (HashtableEntry), t, duke@435: number_of_entries) {} duke@435: coleenp@3682: // Arena for permanent symbols (null class loader) that are never unloaded coleenp@3682: static Arena* _arena; coleenp@3682: static Arena* arena() { return _arena; } // called for statistics duke@435: coleenp@3682: static void initialize_symbols(int arena_alloc_size = 0); coleenp@3865: coleenp@3865: static bool use_alternate_hashcode() { return _seed != 0; } coleenp@3865: static jint seed() { return _seed; } coleenp@3865: coleenp@3865: unsigned int new_hash(Symbol* sym); duke@435: public: duke@435: enum { coleenp@3682: symbol_alloc_batch_size = 8, coleenp@3682: // Pick initial size based on java -version size measurements coleenp@3682: symbol_alloc_arena_size = 360*K duke@435: }; duke@435: duke@435: // The symbol table duke@435: static SymbolTable* the_table() { return _the_table; } duke@435: duke@435: static void create_table() { duke@435: assert(_the_table == NULL, "One symbol table allowed."); duke@435: _the_table = new SymbolTable(); coleenp@3682: initialize_symbols(symbol_alloc_arena_size); duke@435: } duke@435: duke@435: static void create_table(HashtableBucket* t, int length, duke@435: int number_of_entries) { duke@435: assert(_the_table == NULL, "One symbol table allowed."); duke@435: assert(length == symbol_table_size * sizeof(HashtableBucket), duke@435: "bad shared symbol size."); duke@435: _the_table = new SymbolTable(t, number_of_entries); coleenp@3682: // if CDS give symbol table a default arena size since most symbols coleenp@3682: // are already allocated in the shared misc section. coleenp@3682: initialize_symbols(); duke@435: } duke@435: coleenp@3875: static unsigned int hash_symbol(const char* s, int len); coleenp@3865: coleenp@2497: static Symbol* lookup(const char* name, int len, TRAPS); duke@435: // lookup only, won't add. Also calculate hash. coleenp@2497: static Symbol* lookup_only(const char* name, int len, unsigned int& hash); duke@435: // Only copy to C string to be added if lookup failed. coleenp@2497: static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); coleenp@2497: coleenp@2497: static void release(Symbol* sym); duke@435: phh@3427: // Look up the address of the literal in the SymbolTable for this Symbol* phh@3427: static Symbol** lookup_symbol_addr(Symbol* sym); phh@3427: jrose@1100: // jchar (utf16) version of lookups coleenp@2497: static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); coleenp@2497: static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); jrose@1100: coleenp@3682: static void add(Handle class_loader, constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS); duke@435: coleenp@2497: // Release any dead symbols coleenp@2497: static void unlink(); coleenp@2497: coleenp@2497: // iterate over symbols coleenp@2497: static void symbols_do(SymbolClosure *cl); coleenp@2497: coleenp@2497: // Symbol creation coleenp@2497: static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { coleenp@2497: assert(utf8_buffer != NULL, "just checking"); coleenp@2497: return lookup(utf8_buffer, length, THREAD); duke@435: } coleenp@2497: static Symbol* new_symbol(const char* name, TRAPS) { coleenp@2497: return new_symbol(name, (int)strlen(name), THREAD); coleenp@2497: } coleenp@2497: static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { coleenp@2497: assert(begin <= end && end <= sym->utf8_length(), "just checking"); coleenp@2497: return lookup(sym, begin, end, THREAD); duke@435: } duke@435: coleenp@3682: // Create a symbol in the arena for symbols that are not deleted coleenp@3682: static Symbol* new_permanent_symbol(const char* name, TRAPS); coleenp@3682: duke@435: // Symbol lookup coleenp@2497: static Symbol* lookup(int index, const char* name, int len, TRAPS); duke@435: duke@435: // Needed for preloading classes in signatures when compiling. duke@435: // Returns the symbol is already present in symbol table, otherwise duke@435: // NULL. NO ALLOCATION IS GUARANTEED! coleenp@2497: static Symbol* probe(const char* name, int len) { jrose@1100: unsigned int ignore_hash; jrose@1100: return lookup_only(name, len, ignore_hash); jrose@1100: } coleenp@2497: static Symbol* probe_unicode(const jchar* name, int len) { jrose@1100: unsigned int ignore_hash; jrose@1100: return lookup_only_unicode(name, len, ignore_hash); jrose@1100: } duke@435: duke@435: // Histogram duke@435: static void print_histogram() PRODUCT_RETURN; coleenp@2497: static void print() PRODUCT_RETURN; duke@435: duke@435: // Debugging duke@435: static void verify(); coleenp@3865: static void dump(outputStream* st); duke@435: duke@435: // Sharing duke@435: static void copy_buckets(char** top, char*end) { coleenp@2497: the_table()->Hashtable::copy_buckets(top, end); duke@435: } duke@435: static void copy_table(char** top, char*end) { coleenp@2497: the_table()->Hashtable::copy_table(top, end); duke@435: } duke@435: static void reverse(void* boundary = NULL) { coleenp@2497: the_table()->Hashtable::reverse(boundary); duke@435: } coleenp@3865: coleenp@3865: // Rehash the symbol table if it gets out of balance coleenp@3865: static void rehash_table(); coleenp@3865: static bool needs_rehashing() { return _needs_rehashing; } duke@435: }; duke@435: coleenp@3865: coleenp@2497: class StringTable : public Hashtable { duke@435: friend class VMStructs; duke@435: duke@435: private: duke@435: // The string table duke@435: static StringTable* _the_table; duke@435: coleenp@3865: // Set if one bucket is out of balance due to hash algorithm deficiency coleenp@3865: static bool _needs_rehashing; coleenp@3865: static jint _seed; coleenp@3865: duke@435: static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS); duke@435: oop basic_add(int index, Handle string_or_null, jchar* name, int len, duke@435: unsigned int hashValue, TRAPS); duke@435: duke@435: oop lookup(int index, jchar* chars, int length, unsigned int hashValue); duke@435: jcoomes@2660: StringTable() : Hashtable((int)StringTableSize, jcoomes@2660: sizeof (HashtableEntry)) {} duke@435: duke@435: StringTable(HashtableBucket* t, int number_of_entries) jcoomes@2660: : Hashtable((int)StringTableSize, sizeof (HashtableEntry), t, jcoomes@2660: number_of_entries) {} duke@435: coleenp@3865: static bool use_alternate_hashcode() { return _seed != 0; } coleenp@3865: static jint seed() { return _seed; } coleenp@3865: coleenp@3865: unsigned int new_hash(oop s); duke@435: public: duke@435: // The string table duke@435: static StringTable* the_table() { return _the_table; } duke@435: duke@435: static void create_table() { duke@435: assert(_the_table == NULL, "One string table allowed."); duke@435: _the_table = new StringTable(); duke@435: } duke@435: duke@435: static void create_table(HashtableBucket* t, int length, duke@435: int number_of_entries) { duke@435: assert(_the_table == NULL, "One string table allowed."); jcoomes@2660: assert((size_t)length == StringTableSize * sizeof(HashtableBucket), duke@435: "bad shared string size."); duke@435: _the_table = new StringTable(t, number_of_entries); duke@435: } duke@435: duke@435: // GC support duke@435: // Delete pointers to otherwise-unreachable objects. coleenp@2497: static void unlink(BoolObjectClosure* cl); duke@435: duke@435: // Invoke "f->do_oop" on the locations of all oops in the table. coleenp@2497: static void oops_do(OopClosure* f); duke@435: coleenp@3865: // Hashing algorithm, used as the hash value used by the coleenp@3865: // StringTable for bucket selection and comparison (stored in the coleenp@3865: // HashtableEntry structures). This is used in the String.intern() method. coleenp@3875: static unsigned int hash_string(const jchar* s, int len); coleenp@3865: coleenp@3865: // Internal test. coleenp@3865: static void test_alt_hash() PRODUCT_RETURN; coleenp@3865: duke@435: // Probing coleenp@2497: static oop lookup(Symbol* symbol); duke@435: duke@435: // Interning coleenp@2497: static oop intern(Symbol* symbol, TRAPS); duke@435: static oop intern(oop string, TRAPS); duke@435: static oop intern(const char *utf8_string, TRAPS); duke@435: duke@435: // Debugging duke@435: static void verify(); coleenp@3865: static void dump(outputStream* st); duke@435: duke@435: // Sharing duke@435: static void copy_buckets(char** top, char*end) { coleenp@2497: the_table()->Hashtable::copy_buckets(top, end); duke@435: } duke@435: static void copy_table(char** top, char*end) { coleenp@2497: the_table()->Hashtable::copy_table(top, end); duke@435: } duke@435: static void reverse() { coleenp@2497: the_table()->Hashtable::reverse(); duke@435: } coleenp@3865: coleenp@3865: // Rehash the symbol table if it gets out of balance coleenp@3865: static void rehash_table(); coleenp@3865: static bool needs_rehashing() { return _needs_rehashing; } duke@435: }; stefank@2314: #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP