1 /*
   2  * Copyright (c) 1997, 2010, 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 
  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     _temp = s._temp;
  60     if (_temp !=NULL) _temp->increment_refcount();
  61   }
  62 
  63   // Decrement reference counter so it can go away if it's unique
  64   ~TempNewSymbol() { if (_temp != NULL) _temp->decrement_refcount(); }
  65 
  66   // Operators so they can be used like Symbols
  67   Symbol* operator -> () const                   { return _temp; }
  68   bool    operator == (Symbol* o) const          { return _temp == o; }
  69   // Sneaky conversion function
  70   operator Symbol*()                             { return _temp; }
  71 };
  72 
  73 class SymbolTable : public Hashtable<Symbol*> {
  74   friend class VMStructs;
  75   friend class ClassFileParser;
  76 
  77 private:
  78   // The symbol table
  79   static SymbolTable* _the_table;
  80 
  81   // For statistics
  82   static int symbols_removed;
  83   static int symbols_counted;
  84 
  85   Symbol* allocate_symbol(const u1* name, int len, TRAPS);   // Assumes no characters larger than 0x7F
  86   bool allocate_symbols(int names_count, const u1** names, int* lengths, Symbol** syms, TRAPS);
  87 
  88   // Adding elements
  89   Symbol* basic_add(int index, u1* name, int len,
  90                       unsigned int hashValue, TRAPS);
  91   bool basic_add(constantPoolHandle cp, int names_count,
  92                  const char** names, int* lengths, int* cp_indices,
  93                  unsigned int* hashValues, TRAPS);
  94 
  95   static void new_symbols(constantPoolHandle cp, int names_count,
  96                           const char** name, int* lengths,
  97                           int* cp_indices, unsigned int* hashValues,
  98                           TRAPS) {
  99     add(cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
 100   }
 101 
 102 
 103   // Table size
 104   enum {
 105     symbol_table_size = 20011
 106   };
 107 
 108   Symbol* lookup(int index, const char* name, int len, unsigned int hash);
 109 
 110   SymbolTable()
 111     : Hashtable<Symbol*>(symbol_table_size, sizeof (HashtableEntry<Symbol*>)) {}
 112 
 113   SymbolTable(HashtableBucket* t, int number_of_entries)
 114     : Hashtable<Symbol*>(symbol_table_size, sizeof (HashtableEntry<Symbol*>), t,
 115                 number_of_entries) {}
 116 
 117 
 118 public:
 119   enum {
 120     symbol_alloc_batch_size = 8
 121   };
 122 
 123   // The symbol table
 124   static SymbolTable* the_table() { return _the_table; }
 125 
 126   static void create_table() {
 127     assert(_the_table == NULL, "One symbol table allowed.");
 128     _the_table = new SymbolTable();
 129   }
 130 
 131   static void create_table(HashtableBucket* t, int length,
 132                            int number_of_entries) {
 133     assert(_the_table == NULL, "One symbol table allowed.");
 134     assert(length == symbol_table_size * sizeof(HashtableBucket),
 135            "bad shared symbol size.");
 136     _the_table = new SymbolTable(t, number_of_entries);
 137   }
 138 
 139   static Symbol* lookup(const char* name, int len, TRAPS);
 140   // lookup only, won't add. Also calculate hash.
 141   static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
 142   // Only copy to C string to be added if lookup failed.
 143   static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
 144 
 145   static void release(Symbol* sym);
 146 
 147   // jchar (utf16) version of lookups
 148   static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
 149   static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
 150 
 151   static void add(constantPoolHandle cp, int names_count,
 152                   const char** names, int* lengths, int* cp_indices,
 153                   unsigned int* hashValues, TRAPS);
 154 
 155   // Release any dead symbols
 156   static void unlink();
 157 
 158   // iterate over symbols
 159   static void symbols_do(SymbolClosure *cl);
 160 
 161   // Symbol creation
 162   static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
 163     assert(utf8_buffer != NULL, "just checking");
 164     return lookup(utf8_buffer, length, THREAD);
 165   }
 166   static Symbol*       new_symbol(const char* name, TRAPS) {
 167     return new_symbol(name, (int)strlen(name), THREAD);
 168   }
 169   static Symbol*       new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
 170     assert(begin <= end && end <= sym->utf8_length(), "just checking");
 171     return lookup(sym, begin, end, THREAD);
 172   }
 173 
 174   // Symbol lookup
 175   static Symbol* lookup(int index, const char* name, int len, TRAPS);
 176 
 177   // Needed for preloading classes in signatures when compiling.
 178   // Returns the symbol is already present in symbol table, otherwise
 179   // NULL.  NO ALLOCATION IS GUARANTEED!
 180   static Symbol* probe(const char* name, int len) {
 181     unsigned int ignore_hash;
 182     return lookup_only(name, len, ignore_hash);
 183   }
 184   static Symbol* probe_unicode(const jchar* name, int len) {
 185     unsigned int ignore_hash;
 186     return lookup_only_unicode(name, len, ignore_hash);
 187   }
 188 
 189   // Histogram
 190   static void print_histogram()     PRODUCT_RETURN;
 191   static void print()     PRODUCT_RETURN;
 192 
 193   // Debugging
 194   static void verify();
 195 
 196   // Sharing
 197   static void copy_buckets(char** top, char*end) {
 198     the_table()->Hashtable<Symbol*>::copy_buckets(top, end);
 199   }
 200   static void copy_table(char** top, char*end) {
 201     the_table()->Hashtable<Symbol*>::copy_table(top, end);
 202   }
 203   static void reverse(void* boundary = NULL) {
 204     the_table()->Hashtable<Symbol*>::reverse(boundary);
 205   }
 206 };
 207 
 208 class StringTable : public Hashtable<oop> {
 209   friend class VMStructs;
 210 
 211 private:
 212   // The string table
 213   static StringTable* _the_table;
 214 
 215   static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
 216   oop basic_add(int index, Handle string_or_null, jchar* name, int len,
 217                 unsigned int hashValue, TRAPS);
 218 
 219   oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
 220 
 221   StringTable() : Hashtable<oop>((int)StringTableSize,
 222                                  sizeof (HashtableEntry<oop>)) {}
 223 
 224   StringTable(HashtableBucket* t, int number_of_entries)
 225     : Hashtable<oop>((int)StringTableSize, sizeof (HashtableEntry<oop>), t,
 226                      number_of_entries) {}
 227 
 228 public:
 229   // The string table
 230   static StringTable* the_table() { return _the_table; }
 231 
 232   static void create_table() {
 233     assert(_the_table == NULL, "One string table allowed.");
 234     _the_table = new StringTable();
 235   }
 236 
 237   static void create_table(HashtableBucket* t, int length,
 238                            int number_of_entries) {
 239     assert(_the_table == NULL, "One string table allowed.");
 240     assert((size_t)length == StringTableSize * sizeof(HashtableBucket),
 241            "bad shared string size.");
 242     _the_table = new StringTable(t, number_of_entries);
 243   }
 244 
 245   static int hash_string(jchar* s, int len);
 246 
 247   // GC support
 248   //   Delete pointers to otherwise-unreachable objects.
 249   static void unlink(BoolObjectClosure* cl);
 250 
 251   // Invoke "f->do_oop" on the locations of all oops in the table.
 252   static void oops_do(OopClosure* f);
 253 
 254   // Probing
 255   static oop lookup(Symbol* symbol);
 256 
 257   // Interning
 258   static oop intern(Symbol* symbol, TRAPS);
 259   static oop intern(oop string, TRAPS);
 260   static oop intern(const char *utf8_string, TRAPS);
 261 
 262   // Debugging
 263   static void verify();
 264 
 265   // Sharing
 266   static void copy_buckets(char** top, char*end) {
 267     the_table()->Hashtable<oop>::copy_buckets(top, end);
 268   }
 269   static void copy_table(char** top, char*end) {
 270     the_table()->Hashtable<oop>::copy_table(top, end);
 271   }
 272   static void reverse() {
 273     the_table()->Hashtable<oop>::reverse();
 274   }
 275 };
 276 
 277 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP