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 class SymbolTable : public Hashtable<Symbol*, mtSymbol> {
  77   friend class VMStructs;
  78   friend class ClassFileParser;
  79 
  80 private:
  81   // The symbol table
  82   static SymbolTable* _the_table;
  83 
  84   // Set if one bucket is out of balance due to hash algorithm deficiency
  85   static bool _needs_rehashing;
  86 
  87   // For statistics
  88   static int _symbols_removed;
  89   static int _symbols_counted;
  90 
  91   Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
  92 
  93   // Adding elements
  94   Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
  95                     bool c_heap, TRAPS);
  96   bool basic_add(ClassLoaderData* loader_data,
  97                  constantPoolHandle cp, int names_count,
  98                  const char** names, int* lengths, int* cp_indices,
  99                  unsigned int* hashValues, TRAPS);
 100 
 101   static void new_symbols(ClassLoaderData* loader_data,
 102                           constantPoolHandle cp, int names_count,
 103                           const char** name, int* lengths,
 104                           int* cp_indices, unsigned int* hashValues,
 105                           TRAPS) {
 106     add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
 107   }
 108 
 109   Symbol* lookup(int index, const char* name, int len, unsigned int hash);
 110 
 111   SymbolTable()
 112     : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
 113 
 114   SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
 115     : Hashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
 116                 number_of_entries) {}
 117 
 118   // Arena for permanent symbols (null class loader) that are never unloaded
 119   static Arena*  _arena;
 120   static Arena* arena() { return _arena; }  // called for statistics
 121 
 122   static void initialize_symbols(int arena_alloc_size = 0);
 123 
 124   static volatile int _parallel_claimed_idx;
 125 
 126   // Release any dead symbols
 127   static void buckets_unlink(int start_idx, int end_idx, int* processed, int* removed, size_t* memory_total);
 128 public:
 129   enum {
 130     symbol_alloc_batch_size = 8,
 131     // Pick initial size based on java -version size measurements
 132     symbol_alloc_arena_size = 360*K
 133   };
 134 
 135   // The symbol table
 136   static SymbolTable* the_table() { return _the_table; }
 137 
 138   // Size of one bucket in the string table.  Used when checking for rollover.
 139   static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
 140 
 141   static void create_table() {
 142     assert(_the_table == NULL, "One symbol table allowed.");
 143     _the_table = new SymbolTable();
 144     initialize_symbols(symbol_alloc_arena_size);
 145   }
 146 
 147   static void create_table(HashtableBucket<mtSymbol>* t, int length,
 148                            int number_of_entries) {
 149     assert(_the_table == NULL, "One symbol table allowed.");
 150 
 151     // If CDS archive used a different symbol table size, use that size instead
 152     // which is better than giving an error.
 153     SymbolTableSize = length/bucket_size();
 154 
 155     _the_table = new SymbolTable(t, number_of_entries);
 156     // if CDS give symbol table a default arena size since most symbols
 157     // are already allocated in the shared misc section.
 158     initialize_symbols();
 159   }
 160 
 161   static unsigned int hash_symbol(const char* s, int len);
 162 
 163   static Symbol* lookup(const char* name, int len, TRAPS);
 164   // lookup only, won't add. Also calculate hash.
 165   static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
 166   // Only copy to C string to be added if lookup failed.
 167   static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
 168 
 169   static void release(Symbol* sym);
 170 
 171   // Look up the address of the literal in the SymbolTable for this Symbol*
 172   static Symbol** lookup_symbol_addr(Symbol* sym);
 173 
 174   // jchar (utf16) version of lookups
 175   static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
 176   static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
 177 
 178   static void add(ClassLoaderData* loader_data,
 179                   constantPoolHandle cp, int names_count,
 180                   const char** names, int* lengths, int* cp_indices,
 181                   unsigned int* hashValues, TRAPS);
 182 
 183   // Release any dead symbols
 184   static void unlink() {
 185     int processed = 0;
 186     int removed = 0;
 187     unlink(&processed, &removed);
 188   }
 189   static void unlink(int* processed, int* removed);
 190   // Release any dead symbols, possibly parallel version
 191   static void possibly_parallel_unlink(int* processed, int* removed);
 192 
 193   // iterate over symbols
 194   static void symbols_do(SymbolClosure *cl);
 195 
 196   // Symbol creation
 197   static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
 198     assert(utf8_buffer != NULL, "just checking");
 199     return lookup(utf8_buffer, length, THREAD);
 200   }
 201   static Symbol*       new_symbol(const char* name, TRAPS) {
 202     return new_symbol(name, (int)strlen(name), THREAD);
 203   }
 204   static Symbol*       new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
 205     assert(begin <= end && end <= sym->utf8_length(), "just checking");
 206     return lookup(sym, begin, end, THREAD);
 207   }
 208 
 209   // Create a symbol in the arena for symbols that are not deleted
 210   static Symbol* new_permanent_symbol(const char* name, TRAPS);
 211 
 212   // Symbol lookup
 213   static Symbol* lookup(int index, const char* name, int len, TRAPS);
 214 
 215   // Needed for preloading classes in signatures when compiling.
 216   // Returns the symbol is already present in symbol table, otherwise
 217   // NULL.  NO ALLOCATION IS GUARANTEED!
 218   static Symbol* probe(const char* name, int len) {
 219     unsigned int ignore_hash;
 220     return lookup_only(name, len, ignore_hash);
 221   }
 222   static Symbol* probe_unicode(const jchar* name, int len) {
 223     unsigned int ignore_hash;
 224     return lookup_only_unicode(name, len, ignore_hash);
 225   }
 226 
 227   // Histogram
 228   static void print_histogram()     PRODUCT_RETURN;
 229   static void print()     PRODUCT_RETURN;
 230 
 231   // Debugging
 232   static void verify();
 233   static void dump(outputStream* st);
 234 
 235   // Sharing
 236   static void copy_buckets(char** top, char*end) {
 237     the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
 238   }
 239   static void copy_table(char** top, char*end) {
 240     the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
 241   }
 242   static void reverse(void* boundary = NULL) {
 243     the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
 244   }
 245 
 246   // Rehash the symbol table if it gets out of balance
 247   static void rehash_table();
 248   static bool needs_rehashing()         { return _needs_rehashing; }
 249   // Parallel chunked scanning
 250   static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
 251   static int parallel_claimed_index()        { return _parallel_claimed_idx; }
 252 };
 253 
 254 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP