0N/A/*
4592N/A * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
1879N/A#define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
1879N/A
1879N/A#include "memory/allocation.inline.hpp"
2062N/A#include "oops/symbol.hpp"
1879N/A#include "utilities/hashtable.hpp"
1879N/A
2062N/A// The symbol table holds all Symbol*s and corresponding interned strings.
2062N/A// Symbol*s and literal strings should be canonicalized.
0N/A//
0N/A// The interned strings are created lazily.
0N/A//
0N/A// It is implemented as an open hash table with a fixed number of buckets.
0N/A//
0N/A// %note:
0N/A// - symbolTableEntrys are allocated in blocks to reduce the space overhead.
0N/A
0N/Aclass BoolObjectClosure;
3829N/Aclass outputStream;
0N/A
0N/A
2062N/A// Class to hold a newly created or referenced Symbol* temporarily in scope.
2062N/A// new_symbol() and lookup() will create a Symbol* if not already in the
2062N/A// symbol table and add to the symbol's reference count.
2062N/A// probe() and lookup_only() will increment the refcount if symbol is found.
2062N/Aclass TempNewSymbol : public StackObj {
2062N/A Symbol* _temp;
2062N/A
2062N/A public:
2062N/A TempNewSymbol() : _temp(NULL) {}
2062N/A // Creating or looking up a symbol increments the symbol's reference count
2062N/A TempNewSymbol(Symbol *s) : _temp(s) {}
2062N/A
2062N/A // Operator= increments reference count.
2062N/A void operator=(const TempNewSymbol &s) {
3932N/A //clear(); //FIXME
2062N/A _temp = s._temp;
2062N/A if (_temp !=NULL) _temp->increment_refcount();
2062N/A }
2062N/A
2062N/A // Decrement reference counter so it can go away if it's unique
3932N/A void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; }
3932N/A
3932N/A ~TempNewSymbol() { clear(); }
2062N/A
2062N/A // Operators so they can be used like Symbols
2062N/A Symbol* operator -> () const { return _temp; }
2062N/A bool operator == (Symbol* o) const { return _temp == o; }
2062N/A // Sneaky conversion function
2062N/A operator Symbol*() { return _temp; }
2062N/A};
2062N/A
3863N/Aclass SymbolTable : public Hashtable<Symbol*, mtSymbol> {
0N/A friend class VMStructs;
2062N/A friend class ClassFileParser;
0N/A
0N/Aprivate:
0N/A // The symbol table
0N/A static SymbolTable* _the_table;
0N/A
3829N/A // Set if one bucket is out of balance due to hash algorithm deficiency
3829N/A static bool _needs_rehashing;
3829N/A
2062N/A // For statistics
2062N/A static int symbols_removed;
2062N/A static int symbols_counted;
2062N/A
3650N/A Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
2062N/A
0N/A // Adding elements
3650N/A Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
3650N/A bool c_heap, TRAPS);
3650N/A
3650N/A bool basic_add(Handle class_loader, constantPoolHandle cp, int names_count,
0N/A const char** names, int* lengths, int* cp_indices,
0N/A unsigned int* hashValues, TRAPS);
0N/A
3650N/A static void new_symbols(Handle class_loader, constantPoolHandle cp,
3650N/A int names_count,
2062N/A const char** name, int* lengths,
2062N/A int* cp_indices, unsigned int* hashValues,
2062N/A TRAPS) {
3650N/A add(class_loader, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
2062N/A }
2062N/A
0N/A // Table size
0N/A enum {
0N/A symbol_table_size = 20011
0N/A };
0N/A
2062N/A Symbol* lookup(int index, const char* name, int len, unsigned int hash);
0N/A
0N/A SymbolTable()
3863N/A : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
0N/A
3863N/A SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
3863N/A : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
0N/A number_of_entries) {}
0N/A
3650N/A // Arena for permanent symbols (null class loader) that are never unloaded
3650N/A static Arena* _arena;
3650N/A static Arena* arena() { return _arena; } // called for statistics
0N/A
3650N/A static void initialize_symbols(int arena_alloc_size = 0);
0N/Apublic:
0N/A enum {
3650N/A symbol_alloc_batch_size = 8,
3650N/A // Pick initial size based on java -version size measurements
3650N/A symbol_alloc_arena_size = 360*K
0N/A };
0N/A
0N/A // The symbol table
0N/A static SymbolTable* the_table() { return _the_table; }
0N/A
0N/A static void create_table() {
0N/A assert(_the_table == NULL, "One symbol table allowed.");
0N/A _the_table = new SymbolTable();
3650N/A initialize_symbols(symbol_alloc_arena_size);
0N/A }
0N/A
3863N/A static void create_table(HashtableBucket<mtSymbol>* t, int length,
0N/A int number_of_entries) {
0N/A assert(_the_table == NULL, "One symbol table allowed.");
3863N/A assert(length == symbol_table_size * sizeof(HashtableBucket<mtSymbol>),
0N/A "bad shared symbol size.");
0N/A _the_table = new SymbolTable(t, number_of_entries);
3650N/A // if CDS give symbol table a default arena size since most symbols
3650N/A // are already allocated in the shared misc section.
3650N/A initialize_symbols();
0N/A }
0N/A
3838N/A static unsigned int hash_symbol(const char* s, int len);
3829N/A
2062N/A static Symbol* lookup(const char* name, int len, TRAPS);
0N/A // lookup only, won't add. Also calculate hash.
2062N/A static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
0N/A // Only copy to C string to be added if lookup failed.
2062N/A static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
2062N/A
2062N/A static void release(Symbol* sym);
0N/A
3081N/A // Look up the address of the literal in the SymbolTable for this Symbol*
3081N/A static Symbol** lookup_symbol_addr(Symbol* sym);
3081N/A
665N/A // jchar (utf16) version of lookups
2062N/A static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
2062N/A static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
665N/A
3650N/A static void add(Handle class_loader, constantPoolHandle cp, int names_count,
0N/A const char** names, int* lengths, int* cp_indices,
0N/A unsigned int* hashValues, TRAPS);
0N/A
2062N/A // Release any dead symbols
2062N/A static void unlink();
2062N/A
2062N/A // iterate over symbols
2062N/A static void symbols_do(SymbolClosure *cl);
2062N/A
2062N/A // Symbol creation
2062N/A static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
2062N/A assert(utf8_buffer != NULL, "just checking");
2062N/A return lookup(utf8_buffer, length, THREAD);
0N/A }
2062N/A static Symbol* new_symbol(const char* name, TRAPS) {
2062N/A return new_symbol(name, (int)strlen(name), THREAD);
2062N/A }
2062N/A static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
2062N/A assert(begin <= end && end <= sym->utf8_length(), "just checking");
2062N/A return lookup(sym, begin, end, THREAD);
0N/A }
0N/A
3650N/A // Create a symbol in the arena for symbols that are not deleted
3650N/A static Symbol* new_permanent_symbol(const char* name, TRAPS);
3650N/A
0N/A // Symbol lookup
2062N/A static Symbol* lookup(int index, const char* name, int len, TRAPS);
0N/A
0N/A // Needed for preloading classes in signatures when compiling.
0N/A // Returns the symbol is already present in symbol table, otherwise
0N/A // NULL. NO ALLOCATION IS GUARANTEED!
2062N/A static Symbol* probe(const char* name, int len) {
665N/A unsigned int ignore_hash;
665N/A return lookup_only(name, len, ignore_hash);
665N/A }
2062N/A static Symbol* probe_unicode(const jchar* name, int len) {
665N/A unsigned int ignore_hash;
665N/A return lookup_only_unicode(name, len, ignore_hash);
665N/A }
0N/A
0N/A // Histogram
0N/A static void print_histogram() PRODUCT_RETURN;
2062N/A static void print() PRODUCT_RETURN;
0N/A
0N/A // Debugging
0N/A static void verify();
3829N/A static void dump(outputStream* st);
0N/A
0N/A // Sharing
0N/A static void copy_buckets(char** top, char*end) {
3863N/A the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
0N/A }
0N/A static void copy_table(char** top, char*end) {
3863N/A the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
0N/A }
0N/A static void reverse(void* boundary = NULL) {
3863N/A the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
0N/A }
3829N/A
3829N/A // Rehash the symbol table if it gets out of balance
3829N/A static void rehash_table();
3829N/A static bool needs_rehashing() { return _needs_rehashing; }
0N/A};
0N/A
3863N/Aclass StringTable : public Hashtable<oop, mtSymbol> {
0N/A friend class VMStructs;
0N/A
0N/Aprivate:
0N/A // The string table
0N/A static StringTable* _the_table;
0N/A
3829N/A // Set if one bucket is out of balance due to hash algorithm deficiency
3829N/A static bool _needs_rehashing;
3829N/A
4592N/A // Claimed high water mark for parallel chunked scanning
4592N/A static volatile int _parallel_claimed_idx;
4592N/A
0N/A static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
0N/A oop basic_add(int index, Handle string_or_null, jchar* name, int len,
0N/A unsigned int hashValue, TRAPS);
0N/A
0N/A oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
0N/A
4592N/A // Apply the give oop closure to the entries to the buckets
4592N/A // in the range [start_idx, end_idx).
4592N/A static void buckets_do(OopClosure* f, int start_idx, int end_idx);
4592N/A
3863N/A StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize,
3863N/A sizeof (HashtableEntry<oop, mtSymbol>)) {}
0N/A
3863N/A StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
3863N/A : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
2225N/A number_of_entries) {}
0N/Apublic:
0N/A // The string table
0N/A static StringTable* the_table() { return _the_table; }
0N/A
0N/A static void create_table() {
0N/A assert(_the_table == NULL, "One string table allowed.");
0N/A _the_table = new StringTable();
0N/A }
0N/A
3863N/A static void create_table(HashtableBucket<mtSymbol>* t, int length,
0N/A int number_of_entries) {
0N/A assert(_the_table == NULL, "One string table allowed.");
3863N/A assert((size_t)length == StringTableSize * sizeof(HashtableBucket<mtSymbol>),
0N/A "bad shared string size.");
0N/A _the_table = new StringTable(t, number_of_entries);
0N/A }
0N/A
0N/A // GC support
0N/A // Delete pointers to otherwise-unreachable objects.
2062N/A static void unlink(BoolObjectClosure* cl);
0N/A
4592N/A // Serially invoke "f->do_oop" on the locations of all oops in the table.
2062N/A static void oops_do(OopClosure* f);
0N/A
4592N/A // Possibly parallel version of the above
4592N/A static void possibly_parallel_oops_do(OopClosure* f);
4592N/A
3829N/A // Hashing algorithm, used as the hash value used by the
3829N/A // StringTable for bucket selection and comparison (stored in the
3829N/A // HashtableEntry structures). This is used in the String.intern() method.
3838N/A static unsigned int hash_string(const jchar* s, int len);
3829N/A
3829N/A // Internal test.
3829N/A static void test_alt_hash() PRODUCT_RETURN;
3829N/A
0N/A // Probing
2062N/A static oop lookup(Symbol* symbol);
0N/A
0N/A // Interning
2062N/A static oop intern(Symbol* symbol, TRAPS);
0N/A static oop intern(oop string, TRAPS);
0N/A static oop intern(const char *utf8_string, TRAPS);
0N/A
0N/A // Debugging
0N/A static void verify();
3829N/A static void dump(outputStream* st);
0N/A
0N/A // Sharing
0N/A static void copy_buckets(char** top, char*end) {
3863N/A the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
0N/A }
0N/A static void copy_table(char** top, char*end) {
3863N/A the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
0N/A }
0N/A static void reverse() {
3863N/A the_table()->Hashtable<oop, mtSymbol>::reverse();
0N/A }
3829N/A
3829N/A // Rehash the symbol table if it gets out of balance
3829N/A static void rehash_table();
3829N/A static bool needs_rehashing() { return _needs_rehashing; }
4592N/A
4592N/A // Parallel chunked scanning
4592N/A static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
0N/A};
1879N/A#endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP