symbolTable.hpp 14.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
#define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP

#include "memory/allocation.inline.hpp"
29
#include "oops/symbol.hpp"
30 31
#include "utilities/hashtable.hpp"

32 33
// The symbol table holds all Symbol*s and corresponding interned strings.
// Symbol*s and literal strings should be canonicalized.
D
duke 已提交
34 35 36 37 38 39 40 41 42
//
// The interned strings are created lazily.
//
// It is implemented as an open hash table with a fixed number of buckets.
//
// %note:
//  - symbolTableEntrys are allocated in blocks to reduce the space overhead.

class BoolObjectClosure;
43
class outputStream;
D
duke 已提交
44 45


46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Class to hold a newly created or referenced Symbol* temporarily in scope.
// new_symbol() and lookup() will create a Symbol* if not already in the
// symbol table and add to the symbol's reference count.
// probe() and lookup_only() will increment the refcount if symbol is found.
class TempNewSymbol : public StackObj {
  Symbol* _temp;

 public:
  TempNewSymbol() : _temp(NULL) {}
  // Creating or looking up a symbol increments the symbol's reference count
  TempNewSymbol(Symbol *s) : _temp(s) {}

  // Operator= increments reference count.
  void operator=(const TempNewSymbol &s) {
60
    //clear();  //FIXME
61 62 63 64 65
    _temp = s._temp;
    if (_temp !=NULL) _temp->increment_refcount();
  }

  // Decrement reference counter so it can go away if it's unique
66 67 68
  void clear() { if (_temp != NULL)  _temp->decrement_refcount();  _temp = NULL; }

  ~TempNewSymbol() { clear(); }
69 70 71 72 73 74 75 76

  // Operators so they can be used like Symbols
  Symbol* operator -> () const                   { return _temp; }
  bool    operator == (Symbol* o) const          { return _temp == o; }
  // Sneaky conversion function
  operator Symbol*()                             { return _temp; }
};

Z
zgu 已提交
77
class SymbolTable : public Hashtable<Symbol*, mtSymbol> {
D
duke 已提交
78
  friend class VMStructs;
79
  friend class ClassFileParser;
D
duke 已提交
80 81 82 83 84

private:
  // The symbol table
  static SymbolTable* _the_table;

85 86 87
  // Set if one bucket is out of balance due to hash algorithm deficiency
  static bool _needs_rehashing;

88
  // For statistics
89 90
  static int _symbols_removed;
  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

D
duke 已提交
94
  // Adding elements
95 96
  Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
                    bool c_heap, TRAPS);
97 98
  bool basic_add(ClassLoaderData* loader_data,
                 constantPoolHandle cp, int names_count,
D
duke 已提交
99 100 101
                 const char** names, int* lengths, int* cp_indices,
                 unsigned int* hashValues, TRAPS);

102 103
  static void new_symbols(ClassLoaderData* loader_data,
                          constantPoolHandle cp, int names_count,
104 105 106
                          const char** name, int* lengths,
                          int* cp_indices, unsigned int* hashValues,
                          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);
D
duke 已提交
111 112

  SymbolTable()
113
    : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
D
duke 已提交
114

Z
zgu 已提交
115
  SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
116
    : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
D
duke 已提交
117 118
                number_of_entries) {}

119 120 121
  // Arena for permanent symbols (null class loader) that are never unloaded
  static Arena*  _arena;
  static Arena* arena() { return _arena; }  // called for statistics
D
duke 已提交
122

123
  static void initialize_symbols(int arena_alloc_size = 0);
124 125 126 127 128

  static volatile int _parallel_claimed_idx;

  // Release any dead symbols
  static void buckets_unlink(int start_idx, int end_idx, int* processed, int* removed, size_t* memory_total);
D
duke 已提交
129 130
public:
  enum {
131 132 133
    symbol_alloc_batch_size = 8,
    // Pick initial size based on java -version size measurements
    symbol_alloc_arena_size = 360*K
D
duke 已提交
134 135 136 137 138
  };

  // The symbol table
  static SymbolTable* the_table() { return _the_table; }

139 140 141
  // Size of one bucket in the string table.  Used when checking for rollover.
  static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }

D
duke 已提交
142 143 144
  static void create_table() {
    assert(_the_table == NULL, "One symbol table allowed.");
    _the_table = new SymbolTable();
145
    initialize_symbols(symbol_alloc_arena_size);
D
duke 已提交
146 147
  }

Z
zgu 已提交
148
  static void create_table(HashtableBucket<mtSymbol>* t, int length,
D
duke 已提交
149 150
                           int number_of_entries) {
    assert(_the_table == NULL, "One symbol table allowed.");
151 152 153 154 155

    // If CDS archive used a different symbol table size, use that size instead
    // which is better than giving an error.
    SymbolTableSize = length/bucket_size();

D
duke 已提交
156
    _the_table = new SymbolTable(t, number_of_entries);
157 158 159
    // if CDS give symbol table a default arena size since most symbols
    // are already allocated in the shared misc section.
    initialize_symbols();
D
duke 已提交
160 161
  }

162
  static unsigned int hash_symbol(const char* s, int len);
163

164
  static Symbol* lookup(const char* name, int len, TRAPS);
D
duke 已提交
165
  // lookup only, won't add. Also calculate hash.
166
  static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
D
duke 已提交
167
  // Only copy to C string to be added if lookup failed.
168 169 170
  static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);

  static void release(Symbol* sym);
D
duke 已提交
171

172 173 174
  // Look up the address of the literal in the SymbolTable for this Symbol*
  static Symbol** lookup_symbol_addr(Symbol* sym);

175
  // jchar (utf16) version of lookups
176 177
  static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
  static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
178

179 180
  static void add(ClassLoaderData* loader_data,
                  constantPoolHandle cp, int names_count,
D
duke 已提交
181 182 183
                  const char** names, int* lengths, int* cp_indices,
                  unsigned int* hashValues, TRAPS);

184
  // Release any dead symbols
185 186 187 188 189 190 191 192
  static void unlink() {
    int processed = 0;
    int removed = 0;
    unlink(&processed, &removed);
  }
  static void unlink(int* processed, int* removed);
  // Release any dead symbols, possibly parallel version
  static void possibly_parallel_unlink(int* processed, int* removed);
D
duke 已提交
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // iterate over symbols
  static void symbols_do(SymbolClosure *cl);

  // Symbol creation
  static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
    assert(utf8_buffer != NULL, "just checking");
    return lookup(utf8_buffer, length, THREAD);
  }
  static Symbol*       new_symbol(const char* name, TRAPS) {
    return new_symbol(name, (int)strlen(name), THREAD);
  }
  static Symbol*       new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
    assert(begin <= end && end <= sym->utf8_length(), "just checking");
    return lookup(sym, begin, end, THREAD);
D
duke 已提交
208 209
  }

210 211 212
  // Create a symbol in the arena for symbols that are not deleted
  static Symbol* new_permanent_symbol(const char* name, TRAPS);

D
duke 已提交
213
  // Symbol lookup
214
  static Symbol* lookup(int index, const char* name, int len, TRAPS);
D
duke 已提交
215 216 217 218

  // Needed for preloading classes in signatures when compiling.
  // Returns the symbol is already present in symbol table, otherwise
  // NULL.  NO ALLOCATION IS GUARANTEED!
219
  static Symbol* probe(const char* name, int len) {
220 221 222
    unsigned int ignore_hash;
    return lookup_only(name, len, ignore_hash);
  }
223
  static Symbol* probe_unicode(const jchar* name, int len) {
224 225 226
    unsigned int ignore_hash;
    return lookup_only_unicode(name, len, ignore_hash);
  }
D
duke 已提交
227 228 229

  // Histogram
  static void print_histogram()     PRODUCT_RETURN;
230
  static void print()     PRODUCT_RETURN;
D
duke 已提交
231 232 233

  // Debugging
  static void verify();
234
  static void dump(outputStream* st);
D
duke 已提交
235 236 237

  // Sharing
  static void copy_buckets(char** top, char*end) {
Z
zgu 已提交
238
    the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
D
duke 已提交
239 240
  }
  static void copy_table(char** top, char*end) {
Z
zgu 已提交
241
    the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
D
duke 已提交
242 243
  }
  static void reverse(void* boundary = NULL) {
Z
zgu 已提交
244
    the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
D
duke 已提交
245
  }
246 247 248 249

  // Rehash the symbol table if it gets out of balance
  static void rehash_table();
  static bool needs_rehashing()         { return _needs_rehashing; }
250 251 252
  // Parallel chunked scanning
  static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
  static int parallel_claimed_index()        { return _parallel_claimed_idx; }
D
duke 已提交
253 254
};

Z
zgu 已提交
255
class StringTable : public Hashtable<oop, mtSymbol> {
D
duke 已提交
256 257 258 259 260 261
  friend class VMStructs;

private:
  // The string table
  static StringTable* _the_table;

262 263 264
  // Set if one bucket is out of balance due to hash algorithm deficiency
  static bool _needs_rehashing;

265 266 267
  // Claimed high water mark for parallel chunked scanning
  static volatile int _parallel_claimed_idx;

D
duke 已提交
268 269 270 271 272 273
  static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
  oop basic_add(int index, Handle string_or_null, jchar* name, int len,
                unsigned int hashValue, TRAPS);

  oop lookup(int index, jchar* chars, int length, unsigned int hashValue);

274 275
  // Apply the give oop closure to the entries to the buckets
  // in the range [start_idx, end_idx).
276 277 278 279
  static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx);
  // Unlink or apply the give oop closure to the entries to the buckets
  // in the range [start_idx, end_idx).
  static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed);
280

Z
zgu 已提交
281 282
  StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize,
                              sizeof (HashtableEntry<oop, mtSymbol>)) {}
D
duke 已提交
283

Z
zgu 已提交
284 285
  StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
    : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
286
                     number_of_entries) {}
D
duke 已提交
287 288 289 290
public:
  // The string table
  static StringTable* the_table() { return _the_table; }

291 292 293
  // Size of one bucket in the string table.  Used when checking for rollover.
  static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }

D
duke 已提交
294 295 296 297 298 299 300
  static void create_table() {
    assert(_the_table == NULL, "One string table allowed.");
    _the_table = new StringTable();
  }

  // GC support
  //   Delete pointers to otherwise-unreachable objects.
301 302 303 304 305
  static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) {
    int processed = 0;
    int removed = 0;
    unlink_or_oops_do(cl, f, &processed, &removed);
  }
306
  static void unlink(BoolObjectClosure* cl) {
307 308 309 310 311 312 313
    int processed = 0;
    int removed = 0;
    unlink_or_oops_do(cl, NULL, &processed, &removed);
  }
  static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
  static void unlink(BoolObjectClosure* cl, int* processed, int* removed) {
    unlink_or_oops_do(cl, NULL, processed, removed);
314
  }
315
  // Serially invoke "f->do_oop" on the locations of all oops in the table.
316
  static void oops_do(OopClosure* f);
D
duke 已提交
317

318 319 320 321 322
  // Possibly parallel versions of the above
  static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
  static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) {
    possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed);
  }
323 324
  static void possibly_parallel_oops_do(OopClosure* f);

325 326 327
  // Hashing algorithm, used as the hash value used by the
  //     StringTable for bucket selection and comparison (stored in the
  //     HashtableEntry structures).  This is used in the String.intern() method.
328
  static unsigned int hash_string(const jchar* s, int len);
329 330 331 332

  // Internal test.
  static void test_alt_hash() PRODUCT_RETURN;

D
duke 已提交
333
  // Probing
334
  static oop lookup(Symbol* symbol);
335
  static oop lookup(jchar* chars, int length);
D
duke 已提交
336 337

  // Interning
338
  static oop intern(Symbol* symbol, TRAPS);
D
duke 已提交
339 340 341 342 343
  static oop intern(oop string, TRAPS);
  static oop intern(const char *utf8_string, TRAPS);

  // Debugging
  static void verify();
344
  static void dump(outputStream* st);
D
duke 已提交
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  enum VerifyMesgModes {
    _verify_quietly    = 0,
    _verify_with_mesgs = 1
  };

  enum VerifyRetTypes {
    _verify_pass          = 0,
    _verify_fail_continue = 1,
    _verify_fail_done     = 2
  };

  static VerifyRetTypes compare_entries(int bkt1, int e_cnt1,
                                        HashtableEntry<oop, mtSymbol>* e_ptr1,
                                        int bkt2, int e_cnt2,
                                        HashtableEntry<oop, mtSymbol>* e_ptr2);
  static VerifyRetTypes verify_entry(int bkt, int e_cnt,
                                     HashtableEntry<oop, mtSymbol>* e_ptr,
                                     VerifyMesgModes mesg_mode);
  static int verify_and_compare_entries();

D
duke 已提交
366 367
  // Sharing
  static void copy_buckets(char** top, char*end) {
Z
zgu 已提交
368
    the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
D
duke 已提交
369 370
  }
  static void copy_table(char** top, char*end) {
Z
zgu 已提交
371
    the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
D
duke 已提交
372 373
  }
  static void reverse() {
Z
zgu 已提交
374
    the_table()->Hashtable<oop, mtSymbol>::reverse();
D
duke 已提交
375
  }
376

377 378 379
  // Rehash the symbol table if it gets out of balance
  static void rehash_table();
  static bool needs_rehashing() { return _needs_rehashing; }
380 381 382

  // Parallel chunked scanning
  static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
383
  static int parallel_claimed_index() { return _parallel_claimed_idx; }
384
};
385
#endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP