src/share/vm/classfile/symbolTable.hpp

Wed, 18 Sep 2013 07:02:10 -0700

author
dcubed
date
Wed, 18 Sep 2013 07:02:10 -0700
changeset 5743
63147986a428
parent 5277
01522ca68fc7
child 5850
c90e76575b03
permissions
-rw-r--r--

8019835: Strings interned in different threads equal but does not ==
Summary: Add -XX:+VerifyStringTableAtExit option and code to verify StringTable invariants.
Reviewed-by: rdurbin, sspitsyn, coleenp

     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  */
    25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
    26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
    28 #include "memory/allocation.inline.hpp"
    29 #include "oops/symbol.hpp"
    30 #include "utilities/hashtable.hpp"
    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.
    42 class BoolObjectClosure;
    43 class outputStream;
    46 // Class to hold a newly created or referenced Symbol* temporarily in scope.
    47 // new_symbol() and lookup() will create a Symbol* if not already in the
    48 // symbol table and add to the symbol's reference count.
    49 // probe() and lookup_only() will increment the refcount if symbol is found.
    50 class TempNewSymbol : public StackObj {
    51   Symbol* _temp;
    53  public:
    54   TempNewSymbol() : _temp(NULL) {}
    55   // Creating or looking up a symbol increments the symbol's reference count
    56   TempNewSymbol(Symbol *s) : _temp(s) {}
    58   // Operator= increments reference count.
    59   void operator=(const TempNewSymbol &s) {
    60     //clear();  //FIXME
    61     _temp = s._temp;
    62     if (_temp !=NULL) _temp->increment_refcount();
    63   }
    65   // Decrement reference counter so it can go away if it's unique
    66   void clear() { if (_temp != NULL)  _temp->decrement_refcount();  _temp = NULL; }
    68   ~TempNewSymbol() { clear(); }
    70   // Operators so they can be used like Symbols
    71   Symbol* operator -> () const                   { return _temp; }
    72   bool    operator == (Symbol* o) const          { return _temp == o; }
    73   // Sneaky conversion function
    74   operator Symbol*()                             { return _temp; }
    75 };
    77 class SymbolTable : public Hashtable<Symbol*, mtSymbol> {
    78   friend class VMStructs;
    79   friend class ClassFileParser;
    81 private:
    82   // The symbol table
    83   static SymbolTable* _the_table;
    85   // Set if one bucket is out of balance due to hash algorithm deficiency
    86   static bool _needs_rehashing;
    88   // For statistics
    89   static int symbols_removed;
    90   static int symbols_counted;
    92   Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
    94   // Adding elements
    95   Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
    96                     bool c_heap, TRAPS);
    97   bool basic_add(ClassLoaderData* loader_data,
    98                  constantPoolHandle cp, int names_count,
    99                  const char** names, int* lengths, int* cp_indices,
   100                  unsigned int* hashValues, TRAPS);
   102   static void new_symbols(ClassLoaderData* loader_data,
   103                           constantPoolHandle cp, int names_count,
   104                           const char** name, int* lengths,
   105                           int* cp_indices, unsigned int* hashValues,
   106                           TRAPS) {
   107     add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
   108   }
   110   // Table size
   111   enum {
   112     symbol_table_size = 20011
   113   };
   115   Symbol* lookup(int index, const char* name, int len, unsigned int hash);
   117   SymbolTable()
   118     : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
   120   SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
   121     : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
   122                 number_of_entries) {}
   124   // Arena for permanent symbols (null class loader) that are never unloaded
   125   static Arena*  _arena;
   126   static Arena* arena() { return _arena; }  // called for statistics
   128   static void initialize_symbols(int arena_alloc_size = 0);
   129 public:
   130   enum {
   131     symbol_alloc_batch_size = 8,
   132     // Pick initial size based on java -version size measurements
   133     symbol_alloc_arena_size = 360*K
   134   };
   136   // The symbol table
   137   static SymbolTable* the_table() { return _the_table; }
   139   static void create_table() {
   140     assert(_the_table == NULL, "One symbol table allowed.");
   141     _the_table = new SymbolTable();
   142     initialize_symbols(symbol_alloc_arena_size);
   143   }
   145   static void create_table(HashtableBucket<mtSymbol>* t, int length,
   146                            int number_of_entries) {
   147     assert(_the_table == NULL, "One symbol table allowed.");
   148     assert(length == symbol_table_size * sizeof(HashtableBucket<mtSymbol>),
   149            "bad shared symbol size.");
   150     _the_table = new SymbolTable(t, number_of_entries);
   151     // if CDS give symbol table a default arena size since most symbols
   152     // are already allocated in the shared misc section.
   153     initialize_symbols();
   154   }
   156   static unsigned int hash_symbol(const char* s, int len);
   158   static Symbol* lookup(const char* name, int len, TRAPS);
   159   // lookup only, won't add. Also calculate hash.
   160   static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
   161   // Only copy to C string to be added if lookup failed.
   162   static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
   164   static void release(Symbol* sym);
   166   // Look up the address of the literal in the SymbolTable for this Symbol*
   167   static Symbol** lookup_symbol_addr(Symbol* sym);
   169   // jchar (utf16) version of lookups
   170   static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
   171   static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
   173   static void add(ClassLoaderData* loader_data,
   174                   constantPoolHandle cp, int names_count,
   175                   const char** names, int* lengths, int* cp_indices,
   176                   unsigned int* hashValues, TRAPS);
   178   // Release any dead symbols
   179   static void unlink();
   181   // iterate over symbols
   182   static void symbols_do(SymbolClosure *cl);
   184   // Symbol creation
   185   static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
   186     assert(utf8_buffer != NULL, "just checking");
   187     return lookup(utf8_buffer, length, THREAD);
   188   }
   189   static Symbol*       new_symbol(const char* name, TRAPS) {
   190     return new_symbol(name, (int)strlen(name), THREAD);
   191   }
   192   static Symbol*       new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
   193     assert(begin <= end && end <= sym->utf8_length(), "just checking");
   194     return lookup(sym, begin, end, THREAD);
   195   }
   197   // Create a symbol in the arena for symbols that are not deleted
   198   static Symbol* new_permanent_symbol(const char* name, TRAPS);
   200   // Symbol lookup
   201   static Symbol* lookup(int index, const char* name, int len, TRAPS);
   203   // Needed for preloading classes in signatures when compiling.
   204   // Returns the symbol is already present in symbol table, otherwise
   205   // NULL.  NO ALLOCATION IS GUARANTEED!
   206   static Symbol* probe(const char* name, int len) {
   207     unsigned int ignore_hash;
   208     return lookup_only(name, len, ignore_hash);
   209   }
   210   static Symbol* probe_unicode(const jchar* name, int len) {
   211     unsigned int ignore_hash;
   212     return lookup_only_unicode(name, len, ignore_hash);
   213   }
   215   // Histogram
   216   static void print_histogram()     PRODUCT_RETURN;
   217   static void print()     PRODUCT_RETURN;
   219   // Debugging
   220   static void verify();
   221   static void dump(outputStream* st);
   223   // Sharing
   224   static void copy_buckets(char** top, char*end) {
   225     the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
   226   }
   227   static void copy_table(char** top, char*end) {
   228     the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
   229   }
   230   static void reverse(void* boundary = NULL) {
   231     the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
   232   }
   234   // Rehash the symbol table if it gets out of balance
   235   static void rehash_table();
   236   static bool needs_rehashing()         { return _needs_rehashing; }
   237 };
   239 class StringTable : public Hashtable<oop, mtSymbol> {
   240   friend class VMStructs;
   242 private:
   243   // The string table
   244   static StringTable* _the_table;
   246   // Set if one bucket is out of balance due to hash algorithm deficiency
   247   static bool _needs_rehashing;
   249   // Claimed high water mark for parallel chunked scanning
   250   static volatile int _parallel_claimed_idx;
   252   static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
   253   oop basic_add(int index, Handle string_or_null, jchar* name, int len,
   254                 unsigned int hashValue, TRAPS);
   256   oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
   258   // Apply the give oop closure to the entries to the buckets
   259   // in the range [start_idx, end_idx).
   260   static void buckets_do(OopClosure* f, int start_idx, int end_idx);
   262   StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize,
   263                               sizeof (HashtableEntry<oop, mtSymbol>)) {}
   265   StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
   266     : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
   267                      number_of_entries) {}
   268 public:
   269   // The string table
   270   static StringTable* the_table() { return _the_table; }
   272   // Size of one bucket in the string table.  Used when checking for rollover.
   273   static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
   275   static void create_table() {
   276     assert(_the_table == NULL, "One string table allowed.");
   277     _the_table = new StringTable();
   278   }
   280   // GC support
   281   //   Delete pointers to otherwise-unreachable objects.
   282   static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f);
   283   static void unlink(BoolObjectClosure* cl) {
   284     unlink_or_oops_do(cl, NULL);
   285   }
   287   // Serially invoke "f->do_oop" on the locations of all oops in the table.
   288   static void oops_do(OopClosure* f);
   290   // Possibly parallel version of the above
   291   static void possibly_parallel_oops_do(OopClosure* f);
   293   // Hashing algorithm, used as the hash value used by the
   294   //     StringTable for bucket selection and comparison (stored in the
   295   //     HashtableEntry structures).  This is used in the String.intern() method.
   296   static unsigned int hash_string(const jchar* s, int len);
   298   // Internal test.
   299   static void test_alt_hash() PRODUCT_RETURN;
   301   // Probing
   302   static oop lookup(Symbol* symbol);
   303   static oop lookup(jchar* chars, int length);
   305   // Interning
   306   static oop intern(Symbol* symbol, TRAPS);
   307   static oop intern(oop string, TRAPS);
   308   static oop intern(const char *utf8_string, TRAPS);
   310   // Debugging
   311   static void verify();
   312   static void dump(outputStream* st);
   314   enum VerifyMesgModes {
   315     _verify_quietly    = 0,
   316     _verify_with_mesgs = 1
   317   };
   319   enum VerifyRetTypes {
   320     _verify_pass          = 0,
   321     _verify_fail_continue = 1,
   322     _verify_fail_done     = 2
   323   };
   325   static VerifyRetTypes compare_entries(int bkt1, int e_cnt1,
   326                                         HashtableEntry<oop, mtSymbol>* e_ptr1,
   327                                         int bkt2, int e_cnt2,
   328                                         HashtableEntry<oop, mtSymbol>* e_ptr2);
   329   static VerifyRetTypes verify_entry(int bkt, int e_cnt,
   330                                      HashtableEntry<oop, mtSymbol>* e_ptr,
   331                                      VerifyMesgModes mesg_mode);
   332   static int verify_and_compare_entries();
   334   // Sharing
   335   static void copy_buckets(char** top, char*end) {
   336     the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
   337   }
   338   static void copy_table(char** top, char*end) {
   339     the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
   340   }
   341   static void reverse() {
   342     the_table()->Hashtable<oop, mtSymbol>::reverse();
   343   }
   345   // Rehash the symbol table if it gets out of balance
   346   static void rehash_table();
   347   static bool needs_rehashing() { return _needs_rehashing; }
   349   // Parallel chunked scanning
   350   static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
   351 };
   352 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP

mercurial