1 /*
   2  * Copyright (c) 2003, 2017, 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_UTILITIES_HASHTABLE_HPP
  26 #define SHARE_VM_UTILITIES_HASHTABLE_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "oops/oop.hpp"
  31 #include "oops/symbol.hpp"
  32 #include "runtime/handles.hpp"
  33 
  34 // This is a generic hashtable, designed to be used for the symbol
  35 // and string tables.
  36 //
  37 // It is implemented as an open hash table with a fixed number of buckets.
  38 //
  39 // %note:
  40 //  - TableEntrys are allocated in blocks to reduce the space overhead.
  41 
  42 
  43 
  44 template <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> {
  45   friend class VMStructs;
  46 private:
  47   unsigned int         _hash;           // 32-bit hash for item
  48 
  49   // Link to next element in the linked list for this bucket.  EXCEPT
  50   // bit 0 set indicates that this entry is shared and must not be
  51   // unlinked from the table. Bit 0 is set during the dumping of the
  52   // archive. Since shared entries are immutable, _next fields in the
  53   // shared entries will not change.  New entries will always be
  54   // unshared and since pointers are align, bit 0 will always remain 0
  55   // with no extra effort.
  56   BasicHashtableEntry<F>* _next;
  57 
  58   // Windows IA64 compiler requires subclasses to be able to access these
  59 protected:
  60   // Entry objects should not be created, they should be taken from the
  61   // free list with BasicHashtable.new_entry().
  62   BasicHashtableEntry() { ShouldNotReachHere(); }
  63   // Entry objects should not be destroyed.  They should be placed on
  64   // the free list instead with BasicHashtable.free_entry().
  65   ~BasicHashtableEntry() { ShouldNotReachHere(); }
  66 
  67 public:
  68 
  69   unsigned int hash() const             { return _hash; }
  70   void set_hash(unsigned int hash)      { _hash = hash; }
  71   unsigned int* hash_addr()             { return &_hash; }
  72 
  73   static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) {
  74     return (BasicHashtableEntry*)((intptr_t)p & -2);
  75   }
  76 
  77   BasicHashtableEntry<F>* next() const {
  78     return make_ptr(_next);
  79   }
  80 
  81   void set_next(BasicHashtableEntry<F>* next) {
  82     _next = next;
  83   }
  84 
  85   BasicHashtableEntry<F>** next_addr() {
  86     return &_next;
  87   }
  88 
  89   bool is_shared() const {
  90     return ((intptr_t)_next & 1) != 0;
  91   }
  92 
  93   void set_shared() {
  94     _next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1);
  95   }
  96 };
  97 
  98 
  99 
 100 template <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> {
 101   friend class VMStructs;
 102 private:
 103   T               _literal;          // ref to item in table.
 104 
 105 public:
 106   // Literal
 107   T literal() const                   { return _literal; }
 108   T* literal_addr()                   { return &_literal; }
 109   void set_literal(T s)               { _literal = s; }
 110 
 111   HashtableEntry* next() const {
 112     return (HashtableEntry*)BasicHashtableEntry<F>::next();
 113   }
 114   HashtableEntry** next_addr() {
 115     return (HashtableEntry**)BasicHashtableEntry<F>::next_addr();
 116   }
 117 };
 118 
 119 
 120 
 121 template <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> {
 122   friend class VMStructs;
 123 private:
 124   // Instance variable
 125   BasicHashtableEntry<F>*       _entry;
 126 
 127 public:
 128   // Accessing
 129   void clear()                        { _entry = NULL; }
 130 
 131   // The following methods use order access methods to avoid race
 132   // conditions in multiprocessor systems.
 133   BasicHashtableEntry<F>* get_entry() const;
 134   void set_entry(BasicHashtableEntry<F>* l);
 135 
 136   // The following method is not MT-safe and must be done under lock.
 137   BasicHashtableEntry<F>** entry_addr()  { return &_entry; }
 138 
 139 };
 140 
 141 
 142 template <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> {
 143   friend class VMStructs;
 144 
 145 public:
 146   BasicHashtable(int table_size, int entry_size);
 147   BasicHashtable(int table_size, int entry_size,
 148                  HashtableBucket<F>* buckets, int number_of_entries);
 149 
 150   // Sharing support.
 151   void copy_buckets(char** top, char* end);
 152   void copy_table(char** top, char* end);
 153 
 154   // Bucket handling
 155   int hash_to_index(unsigned int full_hash) const {
 156     int h = full_hash % _table_size;
 157     assert(h >= 0 && h < _table_size, "Illegal hash value");
 158     return h;
 159   }
 160 
 161 private:
 162   // Instance variables
 163   int               _table_size;
 164   HashtableBucket<F>*     _buckets;
 165   BasicHashtableEntry<F>* volatile _free_list;
 166   char*             _first_free_entry;
 167   char*             _end_block;
 168   int               _entry_size;
 169   volatile int      _number_of_entries;
 170 
 171 protected:
 172 
 173 #ifdef ASSERT
 174   void verify_lookup_length(int max_bucket_count, const char *table_name);
 175 #endif
 176 
 177   void initialize(int table_size, int entry_size, int number_of_entries);
 178 
 179   // Accessor
 180   int entry_size() const { return _entry_size; }
 181 
 182   // The following method is MT-safe and may be used with caution.
 183   BasicHashtableEntry<F>* bucket(int i) const;
 184 
 185   // The following method is not MT-safe and must be done under lock.
 186   BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); }
 187 
 188   // Attempt to get an entry from the free list
 189   BasicHashtableEntry<F>* new_entry_free_list();
 190 
 191   // Table entry management
 192   BasicHashtableEntry<F>* new_entry(unsigned int hashValue);
 193 
 194   // Used when moving the entry to another table
 195   // Clean up links, but do not add to free_list
 196   void unlink_entry(BasicHashtableEntry<F>* entry) {
 197     entry->set_next(NULL);
 198     --_number_of_entries;
 199   }
 200 
 201   // Move over freelist and free block for allocation
 202   void copy_freelist(BasicHashtable* src) {
 203     _free_list = src->_free_list;
 204     src->_free_list = NULL;
 205     _first_free_entry = src->_first_free_entry;
 206     src->_first_free_entry = NULL;
 207     _end_block = src->_end_block;
 208     src->_end_block = NULL;
 209   }
 210 
 211   // Free the buckets in this hashtable
 212   void free_buckets();
 213 
 214   // Helper data structure containing context for the bucket entry unlink process,
 215   // storing the unlinked buckets in a linked list.
 216   // Also avoids the need to pass around these four members as parameters everywhere.
 217   struct BucketUnlinkContext {
 218     int _num_processed;
 219     int _num_removed;
 220     // Head and tail pointers for the linked list of removed entries.
 221     BasicHashtableEntry<F>* _removed_head;
 222     BasicHashtableEntry<F>* _removed_tail;
 223 
 224     BucketUnlinkContext() : _num_processed(0), _num_removed(0), _removed_head(NULL), _removed_tail(NULL) {
 225     }
 226 
 227     void free_entry(BasicHashtableEntry<F>* entry);
 228   };
 229   // Add of bucket entries linked together in the given context to the global free list. This method
 230   // is mt-safe wrt. to other calls of this method.
 231   void bulk_free_entries(BucketUnlinkContext* context);
 232 public:
 233   int table_size() { return _table_size; }
 234   void set_entry(int index, BasicHashtableEntry<F>* entry);
 235 
 236   void add_entry(int index, BasicHashtableEntry<F>* entry);
 237 
 238   void free_entry(BasicHashtableEntry<F>* entry);
 239 
 240   int number_of_entries() { return _number_of_entries; }
 241 
 242   template <class T> void verify_table(const char* table_name) PRODUCT_RETURN;
 243 };
 244 
 245 
 246 template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> {
 247   friend class VMStructs;
 248 
 249 public:
 250   Hashtable(int table_size, int entry_size)
 251     : BasicHashtable<F>(table_size, entry_size) { }
 252 
 253   Hashtable(int table_size, int entry_size,
 254                    HashtableBucket<F>* buckets, int number_of_entries)
 255     : BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { }
 256 
 257   // Debugging
 258   void print()               PRODUCT_RETURN;
 259 
 260 protected:
 261 
 262   unsigned int compute_hash(Symbol* name) {
 263     return (unsigned int) name->identity_hash();
 264   }
 265 
 266   int index_for(Symbol* name) {
 267     return this->hash_to_index(compute_hash(name));
 268   }
 269 
 270   // Table entry management
 271   HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj);
 272 
 273   // The following method is MT-safe and may be used with caution.
 274   HashtableEntry<T, F>* bucket(int i) const {
 275     return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i);
 276   }
 277 
 278   // The following method is not MT-safe and must be done under lock.
 279   HashtableEntry<T, F>** bucket_addr(int i) {
 280     return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i);
 281   }
 282 
 283 };
 284 
 285 template <class T, MEMFLAGS F> class RehashableHashtable : public Hashtable<T, F> {
 286  friend class VMStructs;
 287  protected:
 288 
 289   enum {
 290     rehash_count = 100,
 291     rehash_multiple = 60
 292   };
 293 
 294   // Check that the table is unbalanced
 295   bool check_rehash_table(int count);
 296 
 297  public:
 298   RehashableHashtable(int table_size, int entry_size)
 299     : Hashtable<T, F>(table_size, entry_size) { }
 300 
 301   RehashableHashtable(int table_size, int entry_size,
 302                    HashtableBucket<F>* buckets, int number_of_entries)
 303     : Hashtable<T, F>(table_size, entry_size, buckets, number_of_entries) { }
 304 
 305 
 306   // Function to move these elements into the new table.
 307   void move_to(RehashableHashtable<T, F>* new_table);
 308   static bool use_alternate_hashcode()  { return _seed != 0; }
 309   static juint seed()                    { return _seed; }
 310 
 311   static int literal_size(Symbol *symbol);
 312   static int literal_size(oop oop);
 313 
 314   // The following two are currently not used, but are needed anyway because some
 315   // C++ compilers (MacOS and Solaris) force the instantiation of
 316   // Hashtable<ConstantPool*, mtClass>::dump_table() even though we never call this function
 317   // in the VM code.
 318   static int literal_size(ConstantPool *cp) {Unimplemented(); return 0;}
 319   static int literal_size(Klass *k)         {Unimplemented(); return 0;}
 320 
 321   void dump_table(outputStream* st, const char *table_name);
 322 
 323  private:
 324   static juint _seed;
 325 };
 326 
 327 
 328 // Versions of hashtable where two handles are used to compute the index.
 329 
 330 template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> {
 331   friend class VMStructs;
 332 protected:
 333   TwoOopHashtable(int table_size, int entry_size)
 334     : Hashtable<T, F>(table_size, entry_size) {}
 335 
 336   TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t,
 337                   int number_of_entries)
 338     : Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {}
 339 
 340 public:
 341   unsigned int compute_hash(const Symbol* name, const ClassLoaderData* loader_data) const {
 342     unsigned int name_hash = name->identity_hash();
 343     // loader is null with CDS
 344     assert(loader_data != NULL || UseSharedSpaces || DumpSharedSpaces,
 345            "only allowed with shared spaces");
 346     unsigned int loader_hash = loader_data == NULL ? 0 : loader_data->identity_hash();
 347     return name_hash ^ loader_hash;
 348   }
 349 
 350   int index_for(Symbol* name, ClassLoaderData* loader_data) {
 351     return this->hash_to_index(compute_hash(name, loader_data));
 352   }
 353 };
 354 
 355 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP