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 46 // Class to hold a newly created or referenced Symbol* temporarily in scope. 47 // new_symbol() and lookup_and_add() will create a Symbol* if not already in the 48 // symbol table and add to the symbol's reference count. 49 // lookup_and_ignore_hash() and lookup_and_hash() will increment the refcount if symbol is found. 50 class TempNewSymbol : public StackObj { 51 Symbol* _temp; 52 53 public: 54 TempNewSymbol() : _temp(NULL) {} 55 // Creating or looking up a symbol increments the symbol's reference count 56 TempNewSymbol(Symbol *s) : _temp(s) {} 57 58 // Operator= increments reference count. 59 void operator=(const TempNewSymbol &s) { 60 //clear(); //FIXME 61 _temp = s._temp; 62 if (_temp !=NULL) _temp->increment_refcount(); 63 } 64 65 // Decrement reference counter so it can go away if it's unique 66 void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; } 67 68 ~TempNewSymbol() { clear(); } 69 70 // Operators so they can be used like Symbols 71 Symbol* operator -> () const { return _temp; } 72 bool operator == (Symbol* o) const { return _temp == o; } 73 // Sneaky conversion function 74 operator Symbol*() { return _temp; } 75 }; 76 77 class SymbolTable : public Hashtable<Symbol*, mtSymbol> { 78 friend class VMStructs; 79 friend class ClassFileParser; 80 81 private: 82 // The symbol table 83 static SymbolTable* _the_table; 84 85 // Set if one bucket is out of balance due to hash algorithm deficiency 86 static bool _needs_rehashing; 87 88 // For statistics 89 static int _symbols_removed; 90 static int _symbols_counted; 91 92 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F 93 94 // Adding elements 95 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue, 96 bool c_heap, TRAPS); 97 bool basic_add(ClassLoaderData* loader_data, 98 constantPoolHandle cp, int names_count, 99 const char** names, int* lengths, int* cp_indices, 100 unsigned int* hashValues, TRAPS); 101 102 static void new_symbols(ClassLoaderData* loader_data, 103 constantPoolHandle cp, int names_count, 104 const char** name, int* lengths, 105 int* cp_indices, unsigned int* hashValues, 106 TRAPS) { 107 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD); 108 } 109 110 Symbol* lookup(int index, const char* name, int len, unsigned int hash); 111 112 SymbolTable() 113 : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {} 114 115 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 116 : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t, 117 number_of_entries) {} 118 119 // Arena for permanent symbols (null class loader) that are never unloaded 120 static Arena* _arena; 121 static Arena* arena() { return _arena; } // called for statistics 122 123 static void initialize_symbols(int arena_alloc_size = 0); 124 125 static volatile int _parallel_claimed_idx; 126 127 // Release any dead symbols 128 static void buckets_unlink(int start_idx, int end_idx, int* processed, int* removed, size_t* memory_total); 129 public: 130 enum { 131 symbol_alloc_batch_size = 8, 132 // Pick initial size based on java -version size measurements 133 symbol_alloc_arena_size = 360*K 134 }; 135 136 // The symbol table 137 static SymbolTable* the_table() { return _the_table; } 138 139 // Size of one bucket in the string table. Used when checking for rollover. 140 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 141 142 static void create_table() { 143 assert(_the_table == NULL, "One symbol table allowed."); 144 _the_table = new SymbolTable(); 145 initialize_symbols(symbol_alloc_arena_size); 146 } 147 148 static void create_table(HashtableBucket<mtSymbol>* t, int length, 149 int number_of_entries) { 150 assert(_the_table == NULL, "One symbol table allowed."); 151 152 // If CDS archive used a different symbol table size, use that size instead 153 // which is better than giving an error. 154 SymbolTableSize = length/bucket_size(); 155 156 _the_table = new SymbolTable(t, number_of_entries); 157 // if CDS give symbol table a default arena size since most symbols 158 // are already allocated in the shared misc section. 159 initialize_symbols(); 160 } 161 162 static unsigned int hash_symbol(const char* s, int len); 163 164 // lookup only, won't add. Also calculate hash. 165 static Symbol* lookup_and_hash(const char* name, int len, unsigned int& hash); 166 // lookup and add if lookup failed 167 static Symbol* lookup_and_add(const char* name, int len, TRAPS); 168 // Only copy to C string to be added if lookup failed. 169 static Symbol* lookup_and_add(const Symbol* sym, int begin, int end, TRAPS); 170 171 static void release(Symbol* sym); 172 173 // Look up the address of the literal in the SymbolTable for this Symbol* 174 static Symbol** lookup_symbol_addr(Symbol* sym); 175 176 // jchar (utf16) version of lookups 177 static Symbol* lookup_and_add_unicode(const jchar* name, int len, TRAPS); 178 static Symbol* lookup_and_hash_unicode(const jchar* name, int len, unsigned int& hash); 179 180 static void add(ClassLoaderData* loader_data, 181 constantPoolHandle cp, int names_count, 182 const char** names, int* lengths, int* cp_indices, 183 unsigned int* hashValues, TRAPS); 184 185 // Release any dead symbols 186 static void unlink() { 187 int processed = 0; 188 int removed = 0; 189 unlink(&processed, &removed); 190 } 191 static void unlink(int* processed, int* removed); 192 // Release any dead symbols, possibly parallel version 193 static void possibly_parallel_unlink(int* processed, int* removed); 194 195 // iterate over symbols 196 static void symbols_do(SymbolClosure *cl); 197 198 // Symbol creation 199 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) { 200 assert(utf8_buffer != NULL, "just checking"); 201 return lookup_and_add(utf8_buffer, length, THREAD); 202 } 203 static Symbol* new_symbol(const char* name, TRAPS) { 204 return new_symbol(name, (int)strlen(name), THREAD); 205 } 206 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) { 207 assert(begin <= end && end <= sym->utf8_length(), "just checking"); 208 return lookup_and_add(sym, begin, end, THREAD); 209 } 210 211 // Create a symbol in the arena for symbols that are not deleted 212 static Symbol* new_permanent_symbol(const char* name, TRAPS); 213 214 // Symbol lookup 215 static Symbol* lookup(int index, const char* name, int len, TRAPS); 216 217 // Needed for preloading classes in signatures when compiling. 218 // Returns the symbol is already present in symbol table, otherwise 219 // NULL. NO ALLOCATION IS GUARANTEED! 220 static Symbol* lookup_and_ignore_hash(const char* name, int len) { 221 unsigned int ignore_hash; 222 return lookup_and_hash(name, len, ignore_hash); 223 } 224 static Symbol* lookup_and_ignore_hash_unicode(const jchar* name, int len) { 225 unsigned int ignore_hash; 226 return lookup_and_hash_unicode(name, len, ignore_hash); 227 } 228 229 // Histogram 230 static void print_histogram() PRODUCT_RETURN; 231 static void print() PRODUCT_RETURN; 232 233 // Debugging 234 static void verify(); 235 static void dump(outputStream* st); 236 237 // Sharing 238 static void copy_buckets(char** top, char*end) { 239 the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end); 240 } 241 static void copy_table(char** top, char*end) { 242 the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end); 243 } 244 static void reverse(void* boundary = NULL) { 245 the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary); 246 } 247 248 // Rehash the symbol table if it gets out of balance 249 static void rehash_table(); 250 static bool needs_rehashing() { return _needs_rehashing; } 251 // Parallel chunked scanning 252 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 253 static int parallel_claimed_index() { return _parallel_claimed_idx; } 254 }; 255 256 class StringTable : public Hashtable<oop, mtSymbol> { 257 friend class VMStructs; 258 259 private: 260 // The string table 261 static StringTable* _the_table; 262 263 // Set if one bucket is out of balance due to hash algorithm deficiency 264 static bool _needs_rehashing; 265 266 // Claimed high water mark for parallel chunked scanning 267 static volatile int _parallel_claimed_idx; 268 269 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS); 270 oop basic_add(int index, Handle string_or_null, jchar* name, int len, 271 unsigned int hashValue, TRAPS); 272 273 oop lookup(int index, jchar* chars, int length, unsigned int hashValue); 274 275 // Apply the give oop closure to the entries to the buckets 276 // in the range [start_idx, end_idx). 277 static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx); 278 // Unlink or apply the give oop closure to the entries to the buckets 279 // in the range [start_idx, end_idx). 280 static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed); 281 282 StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize, 283 sizeof (HashtableEntry<oop, mtSymbol>)) {} 284 285 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 286 : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t, 287 number_of_entries) {} 288 public: 289 // The string table 290 static StringTable* the_table() { return _the_table; } 291 292 // Size of one bucket in the string table. Used when checking for rollover. 293 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 294 295 static void create_table() { 296 assert(_the_table == NULL, "One string table allowed."); 297 _the_table = new StringTable(); 298 } 299 300 // GC support 301 // Delete pointers to otherwise-unreachable objects. 302 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) { 303 int processed = 0; 304 int removed = 0; 305 unlink_or_oops_do(cl, f, &processed, &removed); 306 } 307 static void unlink(BoolObjectClosure* cl) { 308 int processed = 0; 309 int removed = 0; 310 unlink_or_oops_do(cl, NULL, &processed, &removed); 311 } 312 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 313 static void unlink(BoolObjectClosure* cl, int* processed, int* removed) { 314 unlink_or_oops_do(cl, NULL, processed, removed); 315 } 316 // Serially invoke "f->do_oop" on the locations of all oops in the table. 317 static void oops_do(OopClosure* f); 318 319 // Possibly parallel versions of the above 320 static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 321 static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) { 322 possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed); 323 } 324 static void possibly_parallel_oops_do(OopClosure* f); 325 326 // Hashing algorithm, used as the hash value used by the 327 // StringTable for bucket selection and comparison (stored in the 328 // HashtableEntry structures). This is used in the String.intern() method. 329 static unsigned int hash_string(const jchar* s, int len); 330 331 // Internal test. 332 static void test_alt_hash() PRODUCT_RETURN; 333 334 // Probing 335 static oop lookup(Symbol* symbol); 336 static oop lookup(jchar* chars, int length); 337 338 // Interning 339 static oop intern(Symbol* symbol, TRAPS); 340 static oop intern(oop string, TRAPS); 341 static oop intern(const char *utf8_string, TRAPS); 342 343 // Debugging 344 static void verify(); 345 static void dump(outputStream* st); 346 347 enum VerifyMesgModes { 348 _verify_quietly = 0, 349 _verify_with_mesgs = 1 350 }; 351 352 enum VerifyRetTypes { 353 _verify_pass = 0, 354 _verify_fail_continue = 1, 355 _verify_fail_done = 2 356 }; 357 358 static VerifyRetTypes compare_entries(int bkt1, int e_cnt1, 359 HashtableEntry<oop, mtSymbol>* e_ptr1, 360 int bkt2, int e_cnt2, 361 HashtableEntry<oop, mtSymbol>* e_ptr2); 362 static VerifyRetTypes verify_entry(int bkt, int e_cnt, 363 HashtableEntry<oop, mtSymbol>* e_ptr, 364 VerifyMesgModes mesg_mode); 365 static int verify_and_compare_entries(); 366 367 // Sharing 368 static void copy_buckets(char** top, char*end) { 369 the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end); 370 } 371 static void copy_table(char** top, char*end) { 372 the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end); 373 } 374 static void reverse() { 375 the_table()->Hashtable<oop, mtSymbol>::reverse(); 376 } 377 378 // Rehash the symbol table if it gets out of balance 379 static void rehash_table(); 380 static bool needs_rehashing() { return _needs_rehashing; } 381 382 // Parallel chunked scanning 383 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 384 static int parallel_claimed_index() { return _parallel_claimed_idx; } 385 }; 386 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP