1 /* 2 * Copyright (c) 2003, 2018, 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 "memory/allocation.hpp" 29 #include "oops/oop.hpp" 30 #include "oops/symbol.hpp" 31 #include "runtime/handles.hpp" 32 33 // This is a generic hashtable, designed to be used for the symbol 34 // and string tables. 35 // 36 // It is implemented as an open hash table with a fixed number of buckets. 37 // 38 // %note: 39 // - TableEntrys are allocated in blocks to reduce the space overhead. 40 41 42 43 template <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> { 44 friend class VMStructs; 45 private: 46 unsigned int _hash; // 32-bit hash for item 47 48 // Link to next element in the linked list for this bucket. EXCEPT 49 // bit 0 set indicates that this entry is shared and must not be 50 // unlinked from the table. Bit 0 is set during the dumping of the 51 // archive. Since shared entries are immutable, _next fields in the 52 // shared entries will not change. New entries will always be 53 // unshared and since pointers are align, bit 0 will always remain 0 54 // with no extra effort. 55 BasicHashtableEntry<F>* _next; 56 57 // Windows IA64 compiler requires subclasses to be able to access these 58 protected: 59 // Entry objects should not be created, they should be taken from the 60 // free list with BasicHashtable.new_entry(). 61 BasicHashtableEntry() { ShouldNotReachHere(); } 62 // Entry objects should not be destroyed. They should be placed on 63 // the free list instead with BasicHashtable.free_entry(). 64 ~BasicHashtableEntry() { ShouldNotReachHere(); } 65 66 public: 67 68 unsigned int hash() const { return _hash; } 69 void set_hash(unsigned int hash) { _hash = hash; } 70 unsigned int* hash_addr() { return &_hash; } 71 72 static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) { 73 return (BasicHashtableEntry*)((intptr_t)p & -2); 74 } 75 76 BasicHashtableEntry<F>* next() const { 77 return make_ptr(_next); 78 } 79 80 void set_next(BasicHashtableEntry<F>* next) { 81 _next = next; 82 } 83 84 BasicHashtableEntry<F>** next_addr() { 85 return &_next; 86 } 87 88 bool is_shared() const { 89 return ((intptr_t)_next & 1) != 0; 90 } 91 92 void set_shared() { 93 _next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1); 94 } 95 }; 96 97 98 99 template <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> { 100 friend class VMStructs; 101 private: 102 T _literal; // ref to item in table. 103 104 public: 105 // Literal 106 T literal() const { return _literal; } 107 T* literal_addr() { return &_literal; } 108 void set_literal(T s) { _literal = s; } 109 110 HashtableEntry* next() const { 111 return (HashtableEntry*)BasicHashtableEntry<F>::next(); 112 } 113 HashtableEntry** next_addr() { 114 return (HashtableEntry**)BasicHashtableEntry<F>::next_addr(); 115 } 116 }; 117 118 119 120 template <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> { 121 friend class VMStructs; 122 private: 123 // Instance variable 124 BasicHashtableEntry<F>* _entry; 125 126 public: 127 // Accessing 128 void clear() { _entry = NULL; } 129 130 // The following methods use order access methods to avoid race 131 // conditions in multiprocessor systems. 132 BasicHashtableEntry<F>* get_entry() const; 133 void set_entry(BasicHashtableEntry<F>* l); 134 135 // The following method is not MT-safe and must be done under lock. 136 BasicHashtableEntry<F>** entry_addr() { return &_entry; } 137 138 }; 139 140 141 template <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> { 142 friend class VMStructs; 143 144 public: 145 BasicHashtable(int table_size, int entry_size); 146 BasicHashtable(int table_size, int entry_size, 147 HashtableBucket<F>* buckets, int number_of_entries); 148 149 // Bucket handling 150 int hash_to_index(unsigned int full_hash) const { 151 int h = full_hash % _table_size; 152 assert(h >= 0 && h < _table_size, "Illegal hash value"); 153 return h; 154 } 155 156 private: 157 // Instance variables 158 int _table_size; 159 HashtableBucket<F>* _buckets; 160 BasicHashtableEntry<F>* volatile _free_list; 161 char* _first_free_entry; 162 char* _end_block; 163 int _entry_size; 164 volatile int _number_of_entries; 165 166 protected: 167 168 void initialize(int table_size, int entry_size, int number_of_entries); 169 170 // Accessor 171 int entry_size() const { return _entry_size; } 172 173 // The following method is MT-safe and may be used with caution. 174 BasicHashtableEntry<F>* bucket(int i) const; 175 176 // The following method is not MT-safe and must be done under lock. 177 BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); } 178 179 // Attempt to get an entry from the free list 180 BasicHashtableEntry<F>* new_entry_free_list(); 181 182 // Table entry management 183 BasicHashtableEntry<F>* new_entry(unsigned int hashValue); 184 185 // Used when moving the entry to another table 186 // Clean up links, but do not add to free_list 187 void unlink_entry(BasicHashtableEntry<F>* entry) { 188 entry->set_next(NULL); 189 --_number_of_entries; 190 } 191 192 // Move over freelist and free block for allocation 193 void copy_freelist(BasicHashtable* src) { 194 _free_list = src->_free_list; 195 src->_free_list = NULL; 196 _first_free_entry = src->_first_free_entry; 197 src->_first_free_entry = NULL; 198 _end_block = src->_end_block; 199 src->_end_block = NULL; 200 } 201 202 // Free the buckets in this hashtable 203 void free_buckets(); 204 205 // Helper data structure containing context for the bucket entry unlink process, 206 // storing the unlinked buckets in a linked list. 207 // Also avoids the need to pass around these four members as parameters everywhere. 208 struct BucketUnlinkContext { 209 int _num_processed; 210 int _num_removed; 211 // Head and tail pointers for the linked list of removed entries. 212 BasicHashtableEntry<F>* _removed_head; 213 BasicHashtableEntry<F>* _removed_tail; 214 215 BucketUnlinkContext() : _num_processed(0), _num_removed(0), _removed_head(NULL), _removed_tail(NULL) { 216 } 217 218 void free_entry(BasicHashtableEntry<F>* entry); 219 }; 220 // Add of bucket entries linked together in the given context to the global free list. This method 221 // is mt-safe wrt. to other calls of this method. 222 void bulk_free_entries(BucketUnlinkContext* context); 223 public: 224 int table_size() const { return _table_size; } 225 void set_entry(int index, BasicHashtableEntry<F>* entry); 226 227 void add_entry(int index, BasicHashtableEntry<F>* entry); 228 229 void free_entry(BasicHashtableEntry<F>* entry); 230 231 int number_of_entries() const { return _number_of_entries; } 232 233 bool resize(int new_size); 234 235 // Grow the number of buckets if the average entries per bucket is over the load_factor 236 bool maybe_grow(int load_factor = 8) { 237 if (number_of_entries() / table_size() > load_factor) { 238 resize(table_size() * 2); 239 return true; 240 } else { 241 return false; 242 } 243 } 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, T (*literal_load_barrier)(HashtableEntry<T, F>*) = NULL); 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 // A subclass of BasicHashtable that allows you to do a simple K -> V mapping 292 // without using tons of boilerplate code. 293 template< 294 typename K, typename V, 295 MEMFLAGS F = mtInternal, 296 unsigned (*HASH) (K const&) = primitive_hash<K>, 297 bool (*EQUALS)(K const&, K const&) = primitive_equals<K> 298 > 299 class KVHashtable : public BasicHashtable<F> { 300 class KVHashtableEntry : public BasicHashtableEntry<F> { 301 public: 302 K _key; 303 V _value; 304 KVHashtableEntry* next() { 305 return (KVHashtableEntry*)BasicHashtableEntry<F>::next(); 306 } 307 }; 308 309 protected: 310 KVHashtableEntry* bucket(int i) const { 311 return (KVHashtableEntry*)BasicHashtable<F>::bucket(i); 312 } 313 314 KVHashtableEntry* new_entry(unsigned int hashValue, K key, V value) { 315 KVHashtableEntry* entry = (KVHashtableEntry*)BasicHashtable<F>::new_entry(hashValue); 316 entry->_key = key; 317 entry->_value = value; 318 return entry; 319 } 320 321 public: 322 KVHashtable(int table_size) : BasicHashtable<F>(table_size, sizeof(KVHashtableEntry)) {} 323 324 void add(K key, V value) { 325 unsigned int hash = HASH(key); 326 KVHashtableEntry* entry = new_entry(hash, key, value); 327 BasicHashtable<F>::add_entry(BasicHashtable<F>::hash_to_index(hash), entry); 328 } 329 330 V* lookup(K key) { 331 unsigned int hash = HASH(key); 332 int index = BasicHashtable<F>::hash_to_index(hash); 333 for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) { 334 if (e->hash() == hash && e->_key == key) { 335 return &(e->_value); 336 } 337 } 338 return NULL; 339 } 340 }; 341 342 343 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP