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 #include "utilities/tableStatistics.hpp" 34 35 // This is a generic hashtable, designed to be used for the symbol 36 // and string tables. 37 // 38 // It is implemented as an open hash table with a fixed number of buckets. 39 // 40 // %note: 41 // - TableEntrys are allocated in blocks to reduce the space overhead. 42 43 44 45 template <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> { 46 friend class VMStructs; 47 private: 48 unsigned int _hash; // 32-bit hash for item 49 50 // Link to next element in the linked list for this bucket. EXCEPT 51 // bit 0 set indicates that this entry is shared and must not be 52 // unlinked from the table. Bit 0 is set during the dumping of the 53 // archive. Since shared entries are immutable, _next fields in the 54 // shared entries will not change. New entries will always be 55 // unshared and since pointers are align, bit 0 will always remain 0 56 // with no extra effort. 57 BasicHashtableEntry<F>* _next; 58 59 // Windows IA64 compiler requires subclasses to be able to access these 60 protected: 61 // Entry objects should not be created, they should be taken from the 62 // free list with BasicHashtable.new_entry(). 63 BasicHashtableEntry() { ShouldNotReachHere(); } 64 // Entry objects should not be destroyed. They should be placed on 65 // the free list instead with BasicHashtable.free_entry(). 66 ~BasicHashtableEntry() { ShouldNotReachHere(); } 67 68 public: 69 70 unsigned int hash() const { return _hash; } 71 void set_hash(unsigned int hash) { _hash = hash; } 72 unsigned int* hash_addr() { return &_hash; } 73 74 static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) { 75 return (BasicHashtableEntry*)((intptr_t)p & -2); 76 } 77 78 BasicHashtableEntry<F>* next() const { 79 return make_ptr(_next); 80 } 81 82 void set_next(BasicHashtableEntry<F>* next) { 83 _next = next; 84 } 85 86 BasicHashtableEntry<F>** next_addr() { 87 return &_next; 88 } 89 90 bool is_shared() const { 91 return ((intptr_t)_next & 1) != 0; 92 } 93 94 void set_shared() { 95 _next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1); 96 } 97 }; 98 99 100 101 template <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> { 102 friend class VMStructs; 103 private: 104 T _literal; // ref to item in table. 105 106 public: 107 // Literal 108 T literal() const { return _literal; } 109 T* literal_addr() { return &_literal; } 110 void set_literal(T s) { _literal = s; } 111 112 HashtableEntry* next() const { 113 return (HashtableEntry*)BasicHashtableEntry<F>::next(); 114 } 115 HashtableEntry** next_addr() { 116 return (HashtableEntry**)BasicHashtableEntry<F>::next_addr(); 117 } 118 }; 119 120 121 122 template <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> { 123 friend class VMStructs; 124 private: 125 // Instance variable 126 BasicHashtableEntry<F>* _entry; 127 128 public: 129 // Accessing 130 void clear() { _entry = NULL; } 131 132 // The following methods use order access methods to avoid race 133 // conditions in multiprocessor systems. 134 BasicHashtableEntry<F>* get_entry() const; 135 void set_entry(BasicHashtableEntry<F>* l); 136 137 // The following method is not MT-safe and must be done under lock. 138 BasicHashtableEntry<F>** entry_addr() { return &_entry; } 139 140 }; 141 142 143 template <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> { 144 friend class VMStructs; 145 146 public: 147 BasicHashtable(int table_size, int entry_size); 148 BasicHashtable(int table_size, int entry_size, 149 HashtableBucket<F>* buckets, int number_of_entries); 150 151 // Sharing support. 152 size_t count_bytes_for_buckets(); 153 size_t count_bytes_for_table(); 154 void copy_buckets(char* top, char* end); 155 void copy_table(char* top, char* end); 156 157 // Bucket handling 158 int hash_to_index(unsigned int full_hash) const { 159 int h = full_hash % _table_size; 160 assert(h >= 0 && h < _table_size, "Illegal hash value"); 161 return h; 162 } 163 164 private: 165 // Instance variables 166 int _table_size; 167 HashtableBucket<F>* _buckets; 168 BasicHashtableEntry<F>* volatile _free_list; 169 char* _first_free_entry; 170 char* _end_block; 171 int _entry_size; 172 volatile int _number_of_entries; 173 174 protected: 175 176 TableRateStatistics _stats_rate; 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 TableStatistics statistics_calculate(T (*literal_load_barrier)(HashtableEntry<T, F>*) = NULL); 272 void print_table_statistics(outputStream* st, const char *table_name, T (*literal_load_barrier)(HashtableEntry<T, F>*) = NULL); 273 274 protected: 275 276 // Table entry management 277 HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj); 278 // Don't create and use freelist of HashtableEntry. 279 HashtableEntry<T, F>* allocate_new_entry(unsigned int hashValue, T obj); 280 281 // The following method is MT-safe and may be used with caution. 282 HashtableEntry<T, F>* bucket(int i) const { 283 return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i); 284 } 285 286 // The following method is not MT-safe and must be done under lock. 287 HashtableEntry<T, F>** bucket_addr(int i) { 288 return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i); 289 } 290 }; 291 292 template <class T, MEMFLAGS F> class RehashableHashtable : public Hashtable<T, F> { 293 friend class VMStructs; 294 protected: 295 296 enum { 297 rehash_count = 100, 298 rehash_multiple = 60 299 }; 300 301 // Check that the table is unbalanced 302 bool check_rehash_table(int count); 303 304 public: 305 RehashableHashtable(int table_size, int entry_size) 306 : Hashtable<T, F>(table_size, entry_size) { } 307 308 RehashableHashtable(int table_size, int entry_size, 309 HashtableBucket<F>* buckets, int number_of_entries) 310 : Hashtable<T, F>(table_size, entry_size, buckets, number_of_entries) { } 311 312 313 // Function to move these elements into the new table. 314 void move_to(RehashableHashtable<T, F>* new_table); 315 static bool use_alternate_hashcode(); 316 static juint seed(); 317 318 private: 319 static juint _seed; 320 }; 321 322 template <class T, MEMFLAGS F> juint RehashableHashtable<T, F>::_seed = 0; 323 template <class T, MEMFLAGS F> juint RehashableHashtable<T, F>::seed() { return _seed; }; 324 template <class T, MEMFLAGS F> bool RehashableHashtable<T, F>::use_alternate_hashcode() { return _seed != 0; }; 325 326 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP