src/share/vm/utilities/hashtable.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7207
152cf4afc11f
parent 6876
710a3c8b516e
child 8856
ac27a9c85bea
permissions
-rw-r--r--

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;
aoqi@0 167 BasicHashtableEntry<F>* _free_list;
aoqi@0 168 char* _first_free_entry;
aoqi@0 169 char* _end_block;
aoqi@0 170 int _entry_size;
aoqi@0 171 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
aoqi@0 218 public:
aoqi@0 219 int table_size() { return _table_size; }
aoqi@0 220 void set_entry(int index, BasicHashtableEntry<F>* entry);
aoqi@0 221
aoqi@0 222 void add_entry(int index, BasicHashtableEntry<F>* entry);
aoqi@0 223
aoqi@0 224 void free_entry(BasicHashtableEntry<F>* entry);
aoqi@0 225
aoqi@0 226 int number_of_entries() { return _number_of_entries; }
aoqi@0 227
aoqi@0 228 void verify() PRODUCT_RETURN;
aoqi@0 229 };
aoqi@0 230
aoqi@0 231
aoqi@0 232 template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> {
aoqi@0 233 friend class VMStructs;
aoqi@0 234
aoqi@0 235 public:
aoqi@0 236 Hashtable(int table_size, int entry_size)
aoqi@0 237 : BasicHashtable<F>(table_size, entry_size) { }
aoqi@0 238
aoqi@0 239 Hashtable(int table_size, int entry_size,
aoqi@0 240 HashtableBucket<F>* buckets, int number_of_entries)
aoqi@0 241 : BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { }
aoqi@0 242
aoqi@0 243 // Debugging
aoqi@0 244 void print() PRODUCT_RETURN;
aoqi@0 245
aoqi@0 246 // Reverse the order of elements in each of the buckets. Hashtable
aoqi@0 247 // entries which refer to objects at a lower address than 'boundary'
aoqi@0 248 // are separated from those which refer to objects at higher
aoqi@0 249 // addresses, and appear first in the list.
aoqi@0 250 void reverse(void* boundary = NULL);
aoqi@0 251
aoqi@0 252 protected:
aoqi@0 253
aoqi@0 254 unsigned int compute_hash(Symbol* name) {
aoqi@0 255 return (unsigned int) name->identity_hash();
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 int index_for(Symbol* name) {
aoqi@0 259 return this->hash_to_index(compute_hash(name));
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 // Table entry management
aoqi@0 263 HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj);
aoqi@0 264
aoqi@0 265 // The following method is MT-safe and may be used with caution.
aoqi@0 266 HashtableEntry<T, F>* bucket(int i) {
aoqi@0 267 return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i);
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 // The following method is not MT-safe and must be done under lock.
aoqi@0 271 HashtableEntry<T, F>** bucket_addr(int i) {
aoqi@0 272 return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i);
aoqi@0 273 }
aoqi@0 274
mgerdin@7207 275 };
mgerdin@7207 276
mgerdin@7207 277 template <class T, MEMFLAGS F> class RehashableHashtable : public Hashtable<T, F> {
mgerdin@7207 278 protected:
mgerdin@7207 279
mgerdin@7207 280 enum {
mgerdin@7207 281 rehash_count = 100,
mgerdin@7207 282 rehash_multiple = 60
mgerdin@7207 283 };
mgerdin@7207 284
mgerdin@7207 285 // Check that the table is unbalanced
mgerdin@7207 286 bool check_rehash_table(int count);
mgerdin@7207 287
mgerdin@7207 288 public:
mgerdin@7207 289 RehashableHashtable(int table_size, int entry_size)
mgerdin@7207 290 : Hashtable<T, F>(table_size, entry_size) { }
mgerdin@7207 291
mgerdin@7207 292 RehashableHashtable(int table_size, int entry_size,
mgerdin@7207 293 HashtableBucket<F>* buckets, int number_of_entries)
mgerdin@7207 294 : Hashtable<T, F>(table_size, entry_size, buckets, number_of_entries) { }
mgerdin@7207 295
mgerdin@7207 296
aoqi@0 297 // Function to move these elements into the new table.
mgerdin@7207 298 void move_to(RehashableHashtable<T, F>* new_table);
aoqi@0 299 static bool use_alternate_hashcode() { return _seed != 0; }
aoqi@0 300 static juint seed() { return _seed; }
aoqi@0 301
aoqi@0 302 static int literal_size(Symbol *symbol);
aoqi@0 303 static int literal_size(oop oop);
aoqi@0 304
aoqi@0 305 // The following two are currently not used, but are needed anyway because some
aoqi@0 306 // C++ compilers (MacOS and Solaris) force the instantiation of
aoqi@0 307 // Hashtable<ConstantPool*, mtClass>::dump_table() even though we never call this function
aoqi@0 308 // in the VM code.
aoqi@0 309 static int literal_size(ConstantPool *cp) {Unimplemented(); return 0;}
aoqi@0 310 static int literal_size(Klass *k) {Unimplemented(); return 0;}
aoqi@0 311
aoqi@0 312 void dump_table(outputStream* st, const char *table_name);
aoqi@0 313
aoqi@0 314 private:
aoqi@0 315 static juint _seed;
aoqi@0 316 };
aoqi@0 317
aoqi@0 318
aoqi@0 319 // Verions of hashtable where two handles are used to compute the index.
aoqi@0 320
aoqi@0 321 template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> {
aoqi@0 322 friend class VMStructs;
aoqi@0 323 protected:
aoqi@0 324 TwoOopHashtable(int table_size, int entry_size)
aoqi@0 325 : Hashtable<T, F>(table_size, entry_size) {}
aoqi@0 326
aoqi@0 327 TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t,
aoqi@0 328 int number_of_entries)
aoqi@0 329 : Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {}
aoqi@0 330
aoqi@0 331 public:
aoqi@0 332 unsigned int compute_hash(Symbol* name, ClassLoaderData* loader_data) {
aoqi@0 333 unsigned int name_hash = name->identity_hash();
aoqi@0 334 // loader is null with CDS
aoqi@0 335 assert(loader_data != NULL || UseSharedSpaces || DumpSharedSpaces,
aoqi@0 336 "only allowed with shared spaces");
aoqi@0 337 unsigned int loader_hash = loader_data == NULL ? 0 : loader_data->identity_hash();
aoqi@0 338 return name_hash ^ loader_hash;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 int index_for(Symbol* name, ClassLoaderData* loader_data) {
aoqi@0 342 return this->hash_to_index(compute_hash(name, loader_data));
aoqi@0 343 }
aoqi@0 344 };
aoqi@0 345
aoqi@0 346 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP

mercurial