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