src/share/vm/utilities/hashtable.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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 enum {
aoqi@0 182 rehash_count = 100,
aoqi@0 183 rehash_multiple = 60
aoqi@0 184 };
aoqi@0 185
aoqi@0 186 void initialize(int table_size, int entry_size, int number_of_entries);
aoqi@0 187
aoqi@0 188 // Accessor
aoqi@0 189 int entry_size() const { return _entry_size; }
aoqi@0 190
aoqi@0 191 // The following method is MT-safe and may be used with caution.
aoqi@0 192 BasicHashtableEntry<F>* bucket(int i);
aoqi@0 193
aoqi@0 194 // The following method is not MT-safe and must be done under lock.
aoqi@0 195 BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); }
aoqi@0 196
aoqi@0 197 // Table entry management
aoqi@0 198 BasicHashtableEntry<F>* new_entry(unsigned int hashValue);
aoqi@0 199
aoqi@0 200 // Check that the table is unbalanced
aoqi@0 201 bool check_rehash_table(int count);
aoqi@0 202
aoqi@0 203 // Used when moving the entry to another table
aoqi@0 204 // Clean up links, but do not add to free_list
aoqi@0 205 void unlink_entry(BasicHashtableEntry<F>* entry) {
aoqi@0 206 entry->set_next(NULL);
aoqi@0 207 --_number_of_entries;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 // Move over freelist and free block for allocation
aoqi@0 211 void copy_freelist(BasicHashtable* src) {
aoqi@0 212 _free_list = src->_free_list;
aoqi@0 213 src->_free_list = NULL;
aoqi@0 214 _first_free_entry = src->_first_free_entry;
aoqi@0 215 src->_first_free_entry = NULL;
aoqi@0 216 _end_block = src->_end_block;
aoqi@0 217 src->_end_block = NULL;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 // Free the buckets in this hashtable
aoqi@0 221 void free_buckets();
aoqi@0 222
aoqi@0 223 public:
aoqi@0 224 int table_size() { return _table_size; }
aoqi@0 225 void set_entry(int index, BasicHashtableEntry<F>* entry);
aoqi@0 226
aoqi@0 227 void add_entry(int index, BasicHashtableEntry<F>* entry);
aoqi@0 228
aoqi@0 229 void free_entry(BasicHashtableEntry<F>* entry);
aoqi@0 230
aoqi@0 231 int number_of_entries() { return _number_of_entries; }
aoqi@0 232
aoqi@0 233 void verify() PRODUCT_RETURN;
aoqi@0 234 };
aoqi@0 235
aoqi@0 236
aoqi@0 237 template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> {
aoqi@0 238 friend class VMStructs;
aoqi@0 239
aoqi@0 240 public:
aoqi@0 241 Hashtable(int table_size, int entry_size)
aoqi@0 242 : BasicHashtable<F>(table_size, entry_size) { }
aoqi@0 243
aoqi@0 244 Hashtable(int table_size, int entry_size,
aoqi@0 245 HashtableBucket<F>* buckets, int number_of_entries)
aoqi@0 246 : BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { }
aoqi@0 247
aoqi@0 248 // Debugging
aoqi@0 249 void print() PRODUCT_RETURN;
aoqi@0 250
aoqi@0 251 // Reverse the order of elements in each of the buckets. Hashtable
aoqi@0 252 // entries which refer to objects at a lower address than 'boundary'
aoqi@0 253 // are separated from those which refer to objects at higher
aoqi@0 254 // addresses, and appear first in the list.
aoqi@0 255 void reverse(void* boundary = NULL);
aoqi@0 256
aoqi@0 257 protected:
aoqi@0 258
aoqi@0 259 unsigned int compute_hash(Symbol* name) {
aoqi@0 260 return (unsigned int) name->identity_hash();
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 int index_for(Symbol* name) {
aoqi@0 264 return this->hash_to_index(compute_hash(name));
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 // Table entry management
aoqi@0 268 HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj);
aoqi@0 269
aoqi@0 270 // The following method is MT-safe and may be used with caution.
aoqi@0 271 HashtableEntry<T, F>* bucket(int i) {
aoqi@0 272 return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i);
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 // The following method is not MT-safe and must be done under lock.
aoqi@0 276 HashtableEntry<T, F>** bucket_addr(int i) {
aoqi@0 277 return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i);
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 // Function to move these elements into the new table.
aoqi@0 281 void move_to(Hashtable<T, F>* new_table);
aoqi@0 282 static bool use_alternate_hashcode() { return _seed != 0; }
aoqi@0 283 static juint seed() { return _seed; }
aoqi@0 284
aoqi@0 285 static int literal_size(Symbol *symbol);
aoqi@0 286 static int literal_size(oop oop);
aoqi@0 287
aoqi@0 288 // The following two are currently not used, but are needed anyway because some
aoqi@0 289 // C++ compilers (MacOS and Solaris) force the instantiation of
aoqi@0 290 // Hashtable<ConstantPool*, mtClass>::dump_table() even though we never call this function
aoqi@0 291 // in the VM code.
aoqi@0 292 static int literal_size(ConstantPool *cp) {Unimplemented(); return 0;}
aoqi@0 293 static int literal_size(Klass *k) {Unimplemented(); return 0;}
aoqi@0 294
aoqi@0 295 public:
aoqi@0 296 void dump_table(outputStream* st, const char *table_name);
aoqi@0 297
aoqi@0 298 private:
aoqi@0 299 static juint _seed;
aoqi@0 300 };
aoqi@0 301
aoqi@0 302
aoqi@0 303 // Verions of hashtable where two handles are used to compute the index.
aoqi@0 304
aoqi@0 305 template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> {
aoqi@0 306 friend class VMStructs;
aoqi@0 307 protected:
aoqi@0 308 TwoOopHashtable(int table_size, int entry_size)
aoqi@0 309 : Hashtable<T, F>(table_size, entry_size) {}
aoqi@0 310
aoqi@0 311 TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t,
aoqi@0 312 int number_of_entries)
aoqi@0 313 : Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {}
aoqi@0 314
aoqi@0 315 public:
aoqi@0 316 unsigned int compute_hash(Symbol* name, ClassLoaderData* loader_data) {
aoqi@0 317 unsigned int name_hash = name->identity_hash();
aoqi@0 318 // loader is null with CDS
aoqi@0 319 assert(loader_data != NULL || UseSharedSpaces || DumpSharedSpaces,
aoqi@0 320 "only allowed with shared spaces");
aoqi@0 321 unsigned int loader_hash = loader_data == NULL ? 0 : loader_data->identity_hash();
aoqi@0 322 return name_hash ^ loader_hash;
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 int index_for(Symbol* name, ClassLoaderData* loader_data) {
aoqi@0 326 return this->hash_to_index(compute_hash(name, loader_data));
aoqi@0 327 }
aoqi@0 328 };
aoqi@0 329
aoqi@0 330 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP

mercurial