1 /*
   2  * Copyright (c) 1997, 2015, 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 // TempNewSymbol acts as a handle class in a handle/body idiom and is
  46 // responsible for proper resource management of the body (which is a Symbol*).
  47 // The body is resource managed by a reference counting scheme.
  48 // TempNewSymbol can therefore be used to properly hold a newly created or referenced
  49 // Symbol* temporarily in scope.
  50 //
  51 // Routines in SymbolTable will initialize the reference count of a Symbol* before
  52 // it becomes "managed" by TempNewSymbol instances. As a handle class, TempNewSymbol
  53 // needs to maintain proper reference counting in context of copy semantics.
  54 //
  55 // In SymbolTable, new_symbol() and lookup() will create a Symbol* if not already in the
  56 // symbol table and add to the symbol's reference count.
  57 // probe() and lookup_only() will increment the refcount if symbol is found.
  58 class TempNewSymbol : public StackObj {
  59   Symbol* _temp;
  60 
  61  public:
  62   TempNewSymbol() : _temp(NULL) {}
  63 
  64   // Conversion from a Symbol* to a TempNewSymbol.
  65   // Does not increment the current reference count.
  66   TempNewSymbol(Symbol *s) : _temp(s) {}
  67 
  68   // Copy constructor increments reference count.
  69   TempNewSymbol(const TempNewSymbol& rhs) {
  70     _temp = rhs._temp;
  71     if (_temp != NULL) {
  72       _temp->increment_refcount();
  73     }
  74   }
  75 
  76   // Assignment operator decrements the reference count for any
  77   // existing resource. It increments the reference count
  78   // for the newly assigned resource.
  79   const TempNewSymbol& operator=(const TempNewSymbol &rhs) {
  80     if (this != &rhs && _temp != rhs._temp) {
  81       clear();
  82       _temp = rhs._temp;
  83       if (_temp != NULL) {
  84         _temp->increment_refcount();
  85       }
  86     }
  87     return *this;
  88   }
  89 
  90   // Decrement reference counter so it can go away if it's unique
  91   void clear() {
  92     if (_temp != NULL) {
  93       _temp->decrement_refcount();
  94     }
  95   }
  96 
  97   ~TempNewSymbol() { clear(); }
  98 
  99   // Symbol* conversion operators
 100   Symbol* operator -> () const                   { return _temp; }
 101   bool    operator == (Symbol* o) const          { return _temp == o; }
 102   operator Symbol*()                             { return _temp; }
 103 };
 104 
 105 template <class T, class N> class CompactHashtable;
 106 
 107 class SymbolTable : public RehashableHashtable<Symbol*, mtSymbol> {
 108   friend class VMStructs;
 109   friend class ClassFileParser;
 110 
 111 private:
 112   // The symbol table
 113   static SymbolTable* _the_table;
 114 
 115   // Set if one bucket is out of balance due to hash algorithm deficiency
 116   static bool _needs_rehashing;
 117   static bool _lookup_shared_first;
 118 
 119   // For statistics
 120   static int _symbols_removed;
 121   static int _symbols_counted;
 122 
 123   // shared symbol table.
 124   static CompactHashtable<Symbol*, char> _shared_table;
 125 
 126   Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
 127 
 128   // Adding elements
 129   Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
 130                     bool c_heap, TRAPS);
 131   bool basic_add(ClassLoaderData* loader_data,
 132                  const constantPoolHandle& cp, int names_count,
 133                  const char** names, int* lengths, int* cp_indices,
 134                  unsigned int* hashValues, TRAPS);
 135 
 136   static void new_symbols(ClassLoaderData* loader_data,
 137                           const constantPoolHandle& cp, int names_count,
 138                           const char** name, int* lengths,
 139                           int* cp_indices, unsigned int* hashValues,
 140                           TRAPS) {
 141     add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
 142   }
 143 
 144   static Symbol* lookup_shared(const char* name, int len, unsigned int hash);
 145   Symbol* lookup_dynamic(int index, const char* name, int len, unsigned int hash);
 146   Symbol* lookup(int index, const char* name, int len, unsigned int hash);
 147 
 148   SymbolTable()
 149     : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
 150 
 151   SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
 152     : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
 153                 number_of_entries) {}
 154 
 155   // Arena for permanent symbols (null class loader) that are never unloaded
 156   static Arena*  _arena;
 157   static Arena* arena() { return _arena; }  // called for statistics
 158 
 159   static void initialize_symbols(int arena_alloc_size = 0);
 160 
 161   static volatile int _parallel_claimed_idx;
 162 
 163   // Release any dead symbols
 164   static void buckets_unlink(int start_idx, int end_idx, int* processed, int* removed);
 165 public:
 166   enum {
 167     symbol_alloc_batch_size = 8,
 168     // Pick initial size based on java -version size measurements
 169     symbol_alloc_arena_size = 360*K
 170   };
 171 
 172   // The symbol table
 173   static SymbolTable* the_table() { return _the_table; }
 174 
 175   // Size of one bucket in the string table.  Used when checking for rollover.
 176   static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
 177 
 178   static void create_table() {
 179     assert(_the_table == NULL, "One symbol table allowed.");
 180     _the_table = new SymbolTable();
 181     initialize_symbols(symbol_alloc_arena_size);
 182   }
 183 
 184   static unsigned int hash_symbol(const char* s, int len);
 185 
 186   static Symbol* lookup(const char* name, int len, TRAPS);
 187   // lookup only, won't add. Also calculate hash.
 188   static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
 189   // Only copy to C string to be added if lookup failed.
 190   static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
 191 
 192   static void release(Symbol* sym);
 193 
 194   // Look up the address of the literal in the SymbolTable for this Symbol*
 195   static Symbol** lookup_symbol_addr(Symbol* sym);
 196 
 197   // jchar (UTF16) version of lookups
 198   static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
 199   static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
 200 
 201   static void add(ClassLoaderData* loader_data,
 202                   const constantPoolHandle& cp, int names_count,
 203                   const char** names, int* lengths, int* cp_indices,
 204                   unsigned int* hashValues, TRAPS);
 205 
 206   // Release any dead symbols
 207   static void unlink() {
 208     int processed = 0;
 209     int removed = 0;
 210     unlink(&processed, &removed);
 211   }
 212   static void unlink(int* processed, int* removed);
 213   // Release any dead symbols, possibly parallel version
 214   static void possibly_parallel_unlink(int* processed, int* removed);
 215 
 216   // iterate over symbols
 217   static void symbols_do(SymbolClosure *cl);
 218 
 219   // Symbol creation
 220   static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
 221     assert(utf8_buffer != NULL, "just checking");
 222     return lookup(utf8_buffer, length, THREAD);
 223   }
 224   static Symbol*       new_symbol(const char* name, TRAPS) {
 225     return new_symbol(name, (int)strlen(name), THREAD);
 226   }
 227   static Symbol*       new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
 228     assert(begin <= end && end <= sym->utf8_length(), "just checking");
 229     return lookup(sym, begin, end, THREAD);
 230   }
 231 
 232   // Create a symbol in the arena for symbols that are not deleted
 233   static Symbol* new_permanent_symbol(const char* name, TRAPS);
 234 
 235   // Symbol lookup
 236   static Symbol* lookup(int index, const char* name, int len, TRAPS);
 237 
 238   // Needed for preloading classes in signatures when compiling.
 239   // Returns the symbol is already present in symbol table, otherwise
 240   // NULL.  NO ALLOCATION IS GUARANTEED!
 241   static Symbol* probe(const char* name, int len) {
 242     unsigned int ignore_hash;
 243     return lookup_only(name, len, ignore_hash);
 244   }
 245   static Symbol* probe_unicode(const jchar* name, int len) {
 246     unsigned int ignore_hash;
 247     return lookup_only_unicode(name, len, ignore_hash);
 248   }
 249 
 250   // Histogram
 251   static void print_histogram()     PRODUCT_RETURN;
 252   static void print()     PRODUCT_RETURN;
 253 
 254   // Debugging
 255   static void verify();
 256   static void dump(outputStream* st, bool verbose=false);
 257   static void read(const char* filename, TRAPS);
 258 
 259   // Sharing
 260   static bool copy_compact_table(char** top, char* end);
 261   static const char* init_shared_table(const char* buffer);
 262 
 263   // Rehash the symbol table if it gets out of balance
 264   static void rehash_table();
 265   static bool needs_rehashing()         { return _needs_rehashing; }
 266   // Parallel chunked scanning
 267   static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
 268   static int parallel_claimed_index()        { return _parallel_claimed_idx; }
 269 };
 270 
 271 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP