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