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: // The symbol table holds all symbolOops and corresponding interned strings. duke@435: // symbolOops 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; duke@435: duke@435: duke@435: class SymbolTable : public Hashtable { duke@435: friend class VMStructs; duke@435: duke@435: private: duke@435: // The symbol table duke@435: static SymbolTable* _the_table; duke@435: duke@435: // Adding elements duke@435: symbolOop basic_add(int index, u1* name, int len, duke@435: unsigned int hashValue, TRAPS); duke@435: bool basic_add(constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS); duke@435: duke@435: // Table size duke@435: enum { duke@435: symbol_table_size = 20011 duke@435: }; duke@435: duke@435: symbolOop lookup(int index, const char* name, int len, unsigned int hash); duke@435: duke@435: SymbolTable() duke@435: : Hashtable(symbol_table_size, sizeof (HashtableEntry)) {} duke@435: duke@435: SymbolTable(HashtableBucket* t, int number_of_entries) duke@435: : Hashtable(symbol_table_size, sizeof (HashtableEntry), t, duke@435: number_of_entries) {} duke@435: duke@435: duke@435: public: duke@435: enum { duke@435: symbol_alloc_batch_size = 8 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(); 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); duke@435: } duke@435: duke@435: static symbolOop lookup(const char* name, int len, TRAPS); duke@435: // lookup only, won't add. Also calculate hash. duke@435: static symbolOop lookup_only(const char* name, int len, unsigned int& hash); duke@435: // Only copy to C string to be added if lookup failed. duke@435: static symbolOop lookup(symbolHandle sym, int begin, int end, TRAPS); duke@435: jrose@1100: // jchar (utf16) version of lookups jrose@1100: static symbolOop lookup_unicode(const jchar* name, int len, TRAPS); jrose@1100: static symbolOop lookup_only_unicode(const jchar* name, int len, unsigned int& hash); jrose@1100: duke@435: static void add(constantPoolHandle cp, int names_count, duke@435: const char** names, int* lengths, int* cp_indices, duke@435: unsigned int* hashValues, TRAPS); duke@435: duke@435: // GC support duke@435: // Delete pointers to otherwise-unreachable objects. duke@435: static void unlink(BoolObjectClosure* cl) { duke@435: the_table()->Hashtable::unlink(cl); duke@435: } duke@435: duke@435: // Invoke "f->do_oop" on the locations of all oops in the table. duke@435: static void oops_do(OopClosure* f) { duke@435: the_table()->Hashtable::oops_do(f); duke@435: } duke@435: duke@435: // Symbol lookup duke@435: static symbolOop 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! jrose@1100: static symbolOop probe(const char* name, int len) { jrose@1100: unsigned int ignore_hash; jrose@1100: return lookup_only(name, len, ignore_hash); jrose@1100: } jrose@1100: static symbolOop 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; duke@435: duke@435: // Debugging duke@435: static void verify(); duke@435: duke@435: // Sharing duke@435: static void copy_buckets(char** top, char*end) { duke@435: the_table()->Hashtable::copy_buckets(top, end); duke@435: } duke@435: static void copy_table(char** top, char*end) { duke@435: the_table()->Hashtable::copy_table(top, end); duke@435: } duke@435: static void reverse(void* boundary = NULL) { duke@435: ((Hashtable*)the_table())->reverse(boundary); duke@435: } duke@435: }; duke@435: duke@435: duke@435: 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: 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: // Table size duke@435: enum { duke@435: string_table_size = 1009 duke@435: }; duke@435: duke@435: oop lookup(int index, jchar* chars, int length, unsigned int hashValue); duke@435: duke@435: StringTable() : Hashtable(string_table_size, sizeof (HashtableEntry)) {} duke@435: duke@435: StringTable(HashtableBucket* t, int number_of_entries) duke@435: : Hashtable(string_table_size, sizeof (HashtableEntry), t, duke@435: number_of_entries) {} duke@435: 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."); duke@435: assert(length == string_table_size * sizeof(HashtableBucket), duke@435: "bad shared string size."); duke@435: _the_table = new StringTable(t, number_of_entries); duke@435: } duke@435: duke@435: duke@435: static int hash_string(jchar* s, int len); duke@435: duke@435: duke@435: // GC support duke@435: // Delete pointers to otherwise-unreachable objects. duke@435: static void unlink(BoolObjectClosure* cl) { duke@435: the_table()->Hashtable::unlink(cl); duke@435: } duke@435: duke@435: // Invoke "f->do_oop" on the locations of all oops in the table. duke@435: static void oops_do(OopClosure* f) { duke@435: the_table()->Hashtable::oops_do(f); duke@435: } duke@435: duke@435: // Probing duke@435: static oop lookup(symbolOop symbol); duke@435: duke@435: // Interning duke@435: static oop intern(symbolOop 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(); duke@435: duke@435: // Sharing duke@435: static void copy_buckets(char** top, char*end) { duke@435: the_table()->Hashtable::copy_buckets(top, end); duke@435: } duke@435: static void copy_table(char** top, char*end) { duke@435: the_table()->Hashtable::copy_table(top, end); duke@435: } duke@435: static void reverse() { duke@435: ((BasicHashtable*)the_table())->reverse(); duke@435: } duke@435: };