annotate src/share/vm/classfile/symbolTable.hpp @ 3602:da91efe96a93

6964458: Reimplement class meta-data storage to use native memory Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>
author coleenp
date Sat, 01 Sep 2012 13:25:18 -0400
parents 1d7922586cf6
children e4f764ddb06a
rev   line source
duke@0 1 /*
coleenp@3247 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
stefank@1879 26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
stefank@1879 27
stefank@1879 28 #include "memory/allocation.inline.hpp"
coleenp@2062 29 #include "oops/symbol.hpp"
stefank@1879 30 #include "utilities/hashtable.hpp"
stefank@1879 31
coleenp@2062 32 // The symbol table holds all Symbol*s and corresponding interned strings.
coleenp@2062 33 // Symbol*s and literal strings should be canonicalized.
duke@0 34 //
duke@0 35 // The interned strings are created lazily.
duke@0 36 //
duke@0 37 // It is implemented as an open hash table with a fixed number of buckets.
duke@0 38 //
duke@0 39 // %note:
duke@0 40 // - symbolTableEntrys are allocated in blocks to reduce the space overhead.
duke@0 41
duke@0 42 class BoolObjectClosure;
coleenp@3430 43 class outputStream;
duke@0 44
duke@0 45
coleenp@2062 46 // Class to hold a newly created or referenced Symbol* temporarily in scope.
coleenp@2062 47 // new_symbol() and lookup() will create a Symbol* if not already in the
coleenp@2062 48 // symbol table and add to the symbol's reference count.
coleenp@2062 49 // probe() and lookup_only() will increment the refcount if symbol is found.
coleenp@2062 50 class TempNewSymbol : public StackObj {
coleenp@2062 51 Symbol* _temp;
coleenp@2062 52
coleenp@2062 53 public:
coleenp@2062 54 TempNewSymbol() : _temp(NULL) {}
coleenp@2062 55 // Creating or looking up a symbol increments the symbol's reference count
coleenp@2062 56 TempNewSymbol(Symbol *s) : _temp(s) {}
coleenp@2062 57
coleenp@2062 58 // Operator= increments reference count.
coleenp@2062 59 void operator=(const TempNewSymbol &s) {
twisti@3534 60 //clear(); //FIXME
coleenp@2062 61 _temp = s._temp;
coleenp@2062 62 if (_temp !=NULL) _temp->increment_refcount();
coleenp@2062 63 }
coleenp@2062 64
coleenp@2062 65 // Decrement reference counter so it can go away if it's unique
twisti@3534 66 void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; }
twisti@3534 67
twisti@3534 68 ~TempNewSymbol() { clear(); }
coleenp@2062 69
coleenp@2062 70 // Operators so they can be used like Symbols
coleenp@2062 71 Symbol* operator -> () const { return _temp; }
coleenp@2062 72 bool operator == (Symbol* o) const { return _temp == o; }
coleenp@2062 73 // Sneaky conversion function
coleenp@2062 74 operator Symbol*() { return _temp; }
coleenp@2062 75 };
coleenp@2062 76
zgu@3465 77 class SymbolTable : public Hashtable<Symbol*, mtSymbol> {
duke@0 78 friend class VMStructs;
coleenp@2062 79 friend class ClassFileParser;
duke@0 80
duke@0 81 private:
duke@0 82 // The symbol table
duke@0 83 static SymbolTable* _the_table;
duke@0 84
coleenp@3430 85 // Set if one bucket is out of balance due to hash algorithm deficiency
coleenp@3430 86 static bool _needs_rehashing;
coleenp@3430 87
coleenp@2062 88 // For statistics
coleenp@2062 89 static int symbols_removed;
coleenp@2062 90 static int symbols_counted;
coleenp@2062 91
coleenp@3247 92 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
coleenp@2062 93
duke@0 94 // Adding elements
coleenp@3247 95 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
coleenp@3247 96 bool c_heap, TRAPS);
coleenp@3602 97 bool basic_add(ClassLoaderData* loader_data,
coleenp@3602 98 constantPoolHandle cp, int names_count,
duke@0 99 const char** names, int* lengths, int* cp_indices,
duke@0 100 unsigned int* hashValues, TRAPS);
duke@0 101
coleenp@3602 102 static void new_symbols(ClassLoaderData* loader_data,
coleenp@3602 103 constantPoolHandle cp, int names_count,
coleenp@2062 104 const char** name, int* lengths,
coleenp@2062 105 int* cp_indices, unsigned int* hashValues,
coleenp@2062 106 TRAPS) {
coleenp@3602 107 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
coleenp@2062 108 }
coleenp@2062 109
duke@0 110 // Table size
duke@0 111 enum {
duke@0 112 symbol_table_size = 20011
duke@0 113 };
duke@0 114
coleenp@2062 115 Symbol* lookup(int index, const char* name, int len, unsigned int hash);
duke@0 116
duke@0 117 SymbolTable()
zgu@3465 118 : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
duke@0 119
zgu@3465 120 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
zgu@3465 121 : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
duke@0 122 number_of_entries) {}
duke@0 123
coleenp@3247 124 // Arena for permanent symbols (null class loader) that are never unloaded
coleenp@3247 125 static Arena* _arena;
coleenp@3247 126 static Arena* arena() { return _arena; } // called for statistics
duke@0 127
coleenp@3247 128 static void initialize_symbols(int arena_alloc_size = 0);
duke@0 129 public:
duke@0 130 enum {
coleenp@3247 131 symbol_alloc_batch_size = 8,
coleenp@3247 132 // Pick initial size based on java -version size measurements
coleenp@3247 133 symbol_alloc_arena_size = 360*K
duke@0 134 };
duke@0 135
duke@0 136 // The symbol table
duke@0 137 static SymbolTable* the_table() { return _the_table; }
duke@0 138
duke@0 139 static void create_table() {
duke@0 140 assert(_the_table == NULL, "One symbol table allowed.");
duke@0 141 _the_table = new SymbolTable();
coleenp@3247 142 initialize_symbols(symbol_alloc_arena_size);
duke@0 143 }
duke@0 144
zgu@3465 145 static void create_table(HashtableBucket<mtSymbol>* t, int length,
duke@0 146 int number_of_entries) {
duke@0 147 assert(_the_table == NULL, "One symbol table allowed.");
zgu@3465 148 assert(length == symbol_table_size * sizeof(HashtableBucket<mtSymbol>),
duke@0 149 "bad shared symbol size.");
duke@0 150 _the_table = new SymbolTable(t, number_of_entries);
coleenp@3247 151 // if CDS give symbol table a default arena size since most symbols
coleenp@3247 152 // are already allocated in the shared misc section.
coleenp@3247 153 initialize_symbols();
duke@0 154 }
duke@0 155
coleenp@3440 156 static unsigned int hash_symbol(const char* s, int len);
coleenp@3430 157
coleenp@2062 158 static Symbol* lookup(const char* name, int len, TRAPS);
duke@0 159 // lookup only, won't add. Also calculate hash.
coleenp@2062 160 static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
duke@0 161 // Only copy to C string to be added if lookup failed.
coleenp@2062 162 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
coleenp@2062 163
coleenp@2062 164 static void release(Symbol* sym);
duke@0 165
phh@2992 166 // Look up the address of the literal in the SymbolTable for this Symbol*
phh@2992 167 static Symbol** lookup_symbol_addr(Symbol* sym);
phh@2992 168
jrose@665 169 // jchar (utf16) version of lookups
coleenp@2062 170 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
coleenp@2062 171 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
jrose@665 172
coleenp@3602 173 static void add(ClassLoaderData* loader_data,
coleenp@3602 174 constantPoolHandle cp, int names_count,
duke@0 175 const char** names, int* lengths, int* cp_indices,
duke@0 176 unsigned int* hashValues, TRAPS);
duke@0 177
coleenp@2062 178 // Release any dead symbols
coleenp@2062 179 static void unlink();
coleenp@2062 180
coleenp@2062 181 // iterate over symbols
coleenp@2062 182 static void symbols_do(SymbolClosure *cl);
coleenp@2062 183
coleenp@2062 184 // Symbol creation
coleenp@2062 185 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
coleenp@2062 186 assert(utf8_buffer != NULL, "just checking");
coleenp@2062 187 return lookup(utf8_buffer, length, THREAD);
duke@0 188 }
coleenp@2062 189 static Symbol* new_symbol(const char* name, TRAPS) {
coleenp@2062 190 return new_symbol(name, (int)strlen(name), THREAD);
coleenp@2062 191 }
coleenp@2062 192 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
coleenp@2062 193 assert(begin <= end && end <= sym->utf8_length(), "just checking");
coleenp@2062 194 return lookup(sym, begin, end, THREAD);
duke@0 195 }
duke@0 196
coleenp@3247 197 // Create a symbol in the arena for symbols that are not deleted
coleenp@3247 198 static Symbol* new_permanent_symbol(const char* name, TRAPS);
coleenp@3247 199
duke@0 200 // Symbol lookup
coleenp@2062 201 static Symbol* lookup(int index, const char* name, int len, TRAPS);
duke@0 202
duke@0 203 // Needed for preloading classes in signatures when compiling.
duke@0 204 // Returns the symbol is already present in symbol table, otherwise
duke@0 205 // NULL. NO ALLOCATION IS GUARANTEED!
coleenp@2062 206 static Symbol* probe(const char* name, int len) {
jrose@665 207 unsigned int ignore_hash;
jrose@665 208 return lookup_only(name, len, ignore_hash);
jrose@665 209 }
coleenp@2062 210 static Symbol* probe_unicode(const jchar* name, int len) {
jrose@665 211 unsigned int ignore_hash;
jrose@665 212 return lookup_only_unicode(name, len, ignore_hash);
jrose@665 213 }
duke@0 214
duke@0 215 // Histogram
duke@0 216 static void print_histogram() PRODUCT_RETURN;
coleenp@2062 217 static void print() PRODUCT_RETURN;
duke@0 218
duke@0 219 // Debugging
duke@0 220 static void verify();
coleenp@3430 221 static void dump(outputStream* st);
duke@0 222
duke@0 223 // Sharing
duke@0 224 static void copy_buckets(char** top, char*end) {
zgu@3465 225 the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
duke@0 226 }
duke@0 227 static void copy_table(char** top, char*end) {
zgu@3465 228 the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
duke@0 229 }
duke@0 230 static void reverse(void* boundary = NULL) {
zgu@3465 231 the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
duke@0 232 }
coleenp@3430 233
coleenp@3430 234 // Rehash the symbol table if it gets out of balance
coleenp@3430 235 static void rehash_table();
coleenp@3430 236 static bool needs_rehashing() { return _needs_rehashing; }
duke@0 237 };
duke@0 238
zgu@3465 239 class StringTable : public Hashtable<oop, mtSymbol> {
duke@0 240 friend class VMStructs;
duke@0 241
duke@0 242 private:
duke@0 243 // The string table
duke@0 244 static StringTable* _the_table;
duke@0 245
coleenp@3430 246 // Set if one bucket is out of balance due to hash algorithm deficiency
coleenp@3430 247 static bool _needs_rehashing;
coleenp@3430 248
duke@0 249 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
duke@0 250 oop basic_add(int index, Handle string_or_null, jchar* name, int len,
duke@0 251 unsigned int hashValue, TRAPS);
duke@0 252
duke@0 253 oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
duke@0 254
zgu@3465 255 StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize,
zgu@3465 256 sizeof (HashtableEntry<oop, mtSymbol>)) {}
duke@0 257
zgu@3465 258 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
zgu@3465 259 : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
jcoomes@2225 260 number_of_entries) {}
duke@0 261 public:
duke@0 262 // The string table
duke@0 263 static StringTable* the_table() { return _the_table; }
duke@0 264
duke@0 265 static void create_table() {
duke@0 266 assert(_the_table == NULL, "One string table allowed.");
duke@0 267 _the_table = new StringTable();
duke@0 268 }
duke@0 269
zgu@3465 270 static void create_table(HashtableBucket<mtSymbol>* t, int length,
duke@0 271 int number_of_entries) {
duke@0 272 assert(_the_table == NULL, "One string table allowed.");
zgu@3465 273 assert((size_t)length == StringTableSize * sizeof(HashtableBucket<mtSymbol>),
duke@0 274 "bad shared string size.");
duke@0 275 _the_table = new StringTable(t, number_of_entries);
duke@0 276 }
duke@0 277
duke@0 278 // GC support
duke@0 279 // Delete pointers to otherwise-unreachable objects.
coleenp@2062 280 static void unlink(BoolObjectClosure* cl);
duke@0 281
duke@0 282 // Invoke "f->do_oop" on the locations of all oops in the table.
coleenp@2062 283 static void oops_do(OopClosure* f);
duke@0 284
coleenp@3430 285 // Hashing algorithm, used as the hash value used by the
coleenp@3430 286 // StringTable for bucket selection and comparison (stored in the
coleenp@3430 287 // HashtableEntry structures). This is used in the String.intern() method.
coleenp@3440 288 static unsigned int hash_string(const jchar* s, int len);
coleenp@3430 289
coleenp@3430 290 // Internal test.
coleenp@3430 291 static void test_alt_hash() PRODUCT_RETURN;
coleenp@3430 292
duke@0 293 // Probing
coleenp@2062 294 static oop lookup(Symbol* symbol);
duke@0 295
duke@0 296 // Interning
coleenp@2062 297 static oop intern(Symbol* symbol, TRAPS);
duke@0 298 static oop intern(oop string, TRAPS);
duke@0 299 static oop intern(const char *utf8_string, TRAPS);
duke@0 300
duke@0 301 // Debugging
duke@0 302 static void verify();
coleenp@3430 303 static void dump(outputStream* st);
duke@0 304
duke@0 305 // Sharing
duke@0 306 static void copy_buckets(char** top, char*end) {
zgu@3465 307 the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
duke@0 308 }
duke@0 309 static void copy_table(char** top, char*end) {
zgu@3465 310 the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
duke@0 311 }
duke@0 312 static void reverse() {
zgu@3465 313 the_table()->Hashtable<oop, mtSymbol>::reverse();
duke@0 314 }
coleenp@3430 315
coleenp@3430 316 // Rehash the symbol table if it gets out of balance
coleenp@3430 317 static void rehash_table();
coleenp@3430 318 static bool needs_rehashing() { return _needs_rehashing; }
duke@0 319 };
stefank@1879 320 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP