1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP 26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP 27 28 #include "memory/allocation.inline.hpp" 29 #include "oops/symbol.hpp" 30 #include "utilities/hashtable.hpp" 31 32 // The symbol table holds all Symbol*s and corresponding interned strings. 33 // Symbol*s and literal strings should be canonicalized. 34 // 35 // The interned strings are created lazily. 36 // 37 // It is implemented as an open hash table with a fixed number of buckets. 38 // 39 // %note: 40 // - symbolTableEntrys are allocated in blocks to reduce the space overhead. 41 42 class BoolObjectClosure; 43 class outputStream; 44 45 // Class to hold a newly created or referenced Symbol* temporarily in scope. 46 // new_symbol() and lookup() will create a Symbol* if not already in the 47 // symbol table and add to the symbol's reference count. 48 // probe() and lookup_only() will increment the refcount if symbol is found. 49 class TempNewSymbol : public StackObj { 50 Symbol* _temp; 51 52 public: 53 TempNewSymbol() : _temp(NULL) {} 54 // Creating or looking up a symbol increments the symbol's reference count 55 TempNewSymbol(Symbol *s) : _temp(s) {} 56 57 // Operator= increments reference count. 58 void operator=(const TempNewSymbol &s) { 59 //clear(); //FIXME 60 _temp = s._temp; 61 if (_temp !=NULL) _temp->increment_refcount(); 62 } 63 64 // Decrement reference counter so it can go away if it's unique 65 void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; } 66 67 ~TempNewSymbol() { clear(); } 68 69 // Operators so they can be used like Symbols 70 Symbol* operator -> () const { return _temp; } 71 bool operator == (Symbol* o) const { return _temp == o; } 72 // Sneaky conversion function 73 operator Symbol*() { return _temp; } 74 }; 75 76 template <class T, class N> class CompactHashtable; 77 78 class SymbolTable : public RehashableHashtable<Symbol*, mtSymbol> { 79 friend class VMStructs; 80 friend class ClassFileParser; 81 82 private: 83 // The symbol table 84 static SymbolTable* _the_table; 85 86 // Set if one bucket is out of balance due to hash algorithm deficiency 87 static bool _needs_rehashing; 88 static bool _lookup_shared_first; 89 90 // For statistics 91 static int _symbols_removed; 92 static int _symbols_counted; 93 94 // shared symbol table. 95 static CompactHashtable<Symbol*, char> _shared_table; 96 97 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F 98 99 // Adding elements 100 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue, 101 bool c_heap, TRAPS); 102 bool basic_add(ClassLoaderData* loader_data, 103 constantPoolHandle cp, int names_count, 104 const char** names, int* lengths, int* cp_indices, 105 unsigned int* hashValues, TRAPS); 106 107 static void new_symbols(ClassLoaderData* loader_data, 108 constantPoolHandle cp, int names_count, 109 const char** name, int* lengths, 110 int* cp_indices, unsigned int* hashValues, 111 TRAPS) { 112 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); 113 } 114 115 static Symbol* lookup_shared(const char* name, int len, unsigned int hash); 116 Symbol* lookup_dynamic(int index, const char* name, int len, unsigned int hash); 117 Symbol* lookup(int index, const char* name, int len, unsigned int hash); 118 119 SymbolTable() 120 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {} 121 122 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 123 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t, 124 number_of_entries) {} 125 126 // Arena for permanent symbols (null class loader) that are never unloaded 127 static Arena* _arena; 128 static Arena* arena() { return _arena; } // called for statistics 129 130 static void initialize_symbols(int arena_alloc_size = 0); 131 132 static volatile int _parallel_claimed_idx; 133 134 // Release any dead symbols 135 static void buckets_unlink(int start_idx, int end_idx, int* processed, int* removed, size_t* memory_total); 136 public: 137 enum { 138 symbol_alloc_batch_size = 8, 139 // Pick initial size based on java -version size measurements 140 symbol_alloc_arena_size = 360*K 141 }; 142 143 // The symbol table 144 static SymbolTable* the_table() { return _the_table; } 145 146 // Size of one bucket in the string table. Used when checking for rollover. 147 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 148 149 static void create_table() { 150 assert(_the_table == NULL, "One symbol table allowed."); 151 _the_table = new SymbolTable(); 152 initialize_symbols(symbol_alloc_arena_size); 153 } 154 155 static unsigned int hash_symbol(const char* s, int len); 156 157 static Symbol* lookup(const char* name, int len, TRAPS); 158 // lookup only, won't add. Also calculate hash. 159 static Symbol* lookup_only(const char* name, int len, unsigned int& hash); 160 // Only copy to C string to be added if lookup failed. 161 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS); 162 163 static void release(Symbol* sym); 164 165 // Look up the address of the literal in the SymbolTable for this Symbol* 166 static Symbol** lookup_symbol_addr(Symbol* sym); 167 168 // jchar (UTF16) version of lookups 169 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS); 170 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash); 171 172 static void add(ClassLoaderData* loader_data, 173 constantPoolHandle cp, int names_count, 174 const char** names, int* lengths, int* cp_indices, 175 unsigned int* hashValues, TRAPS); 176 177 // Release any dead symbols 178 static void unlink() { 179 int processed = 0; 180 int removed = 0; 181 unlink(&processed, &removed); 182 } 183 static void unlink(int* processed, int* removed); 184 // Release any dead symbols, possibly parallel version 185 static void possibly_parallel_unlink(int* processed, int* removed); 186 187 // iterate over symbols 188 static void symbols_do(SymbolClosure *cl); 189 190 // Symbol creation 191 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 192 assert(utf8_buffer != NULL, "just checking"); 193 return lookup(utf8_buffer, length, THREAD); 194 } 195 static Symbol* new_symbol(const char* name, TRAPS) { 196 return new_symbol(name, (int)strlen(name), THREAD); 197 } 198 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 199 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 200 return lookup(sym, begin, end, THREAD); 201 } 202 203 // Create a symbol in the arena for symbols that are not deleted 204 static Symbol* new_permanent_symbol(const char* name, TRAPS); 205 206 // Symbol lookup 207 static Symbol* lookup(int index, const char* name, int len, TRAPS); 208 209 // Needed for preloading classes in signatures when compiling. 210 // Returns the symbol is already present in symbol table, otherwise 211 // NULL. NO ALLOCATION IS GUARANTEED! 212 static Symbol* probe(const char* name, int len) { 213 unsigned int ignore_hash; 214 return lookup_only(name, len, ignore_hash); 215 } 216 static Symbol* probe_unicode(const jchar* name, int len) { 217 unsigned int ignore_hash; 218 return lookup_only_unicode(name, len, ignore_hash); 219 } 220 221 // Histogram 222 static void print_histogram() PRODUCT_RETURN; 223 static void print() PRODUCT_RETURN; 224 225 // Debugging 226 static void verify(); 227 static void dump(outputStream* st, bool verbose=false); 228 static void read(const char* filename, TRAPS); 229 230 // Sharing 231 static bool copy_compact_table(char** top, char* end); 232 static const char* init_shared_table(const char* buffer); 233 234 // Rehash the symbol table if it gets out of balance 235 static void rehash_table(); 236 static bool needs_rehashing() { return _needs_rehashing; } 237 // Parallel chunked scanning 238 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 239 static int parallel_claimed_index() { return _parallel_claimed_idx; } 240 }; 241 242 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP