src/share/vm/utilities/hashtable.hpp

Mon, 30 Jan 2012 23:27:30 -0500

author
acorn
date
Mon, 30 Jan 2012 23:27:30 -0500
changeset 3491
b2cd0ee8f778
parent 2708
1d1603768966
child 3865
e9140bf80b4a
permissions
-rw-r--r--

7114376: Make system dictionary hashtable bucket array size configurable
Summary: 7u4 new experimental flag -XX:PredictedClassLoadedCount=#
Reviewed-by: dholmes, phh, dcubed

duke@435 1 /*
acorn@3491 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_HASHTABLE_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "oops/oop.hpp"
coleenp@2497 30 #include "oops/symbol.hpp"
stefank@2314 31 #include "runtime/handles.hpp"
stefank@2314 32
duke@435 33 // This is a generic hashtable, designed to be used for the symbol
duke@435 34 // and string tables.
duke@435 35 //
duke@435 36 // It is implemented as an open hash table with a fixed number of buckets.
duke@435 37 //
duke@435 38 // %note:
duke@435 39 // - TableEntrys are allocated in blocks to reduce the space overhead.
duke@435 40
duke@435 41
duke@435 42
duke@435 43 class BasicHashtableEntry : public CHeapObj {
duke@435 44 friend class VMStructs;
duke@435 45 private:
duke@435 46 unsigned int _hash; // 32-bit hash for item
duke@435 47
duke@435 48 // Link to next element in the linked list for this bucket. EXCEPT
duke@435 49 // bit 0 set indicates that this entry is shared and must not be
duke@435 50 // unlinked from the table. Bit 0 is set during the dumping of the
duke@435 51 // archive. Since shared entries are immutable, _next fields in the
duke@435 52 // shared entries will not change. New entries will always be
duke@435 53 // unshared and since pointers are align, bit 0 will always remain 0
duke@435 54 // with no extra effort.
duke@435 55 BasicHashtableEntry* _next;
duke@435 56
duke@435 57 // Windows IA64 compiler requires subclasses to be able to access these
duke@435 58 protected:
duke@435 59 // Entry objects should not be created, they should be taken from the
duke@435 60 // free list with BasicHashtable.new_entry().
duke@435 61 BasicHashtableEntry() { ShouldNotReachHere(); }
duke@435 62 // Entry objects should not be destroyed. They should be placed on
duke@435 63 // the free list instead with BasicHashtable.free_entry().
duke@435 64 ~BasicHashtableEntry() { ShouldNotReachHere(); }
duke@435 65
duke@435 66 public:
duke@435 67
duke@435 68 unsigned int hash() const { return _hash; }
duke@435 69 void set_hash(unsigned int hash) { _hash = hash; }
duke@435 70 unsigned int* hash_addr() { return &_hash; }
duke@435 71
duke@435 72 static BasicHashtableEntry* make_ptr(BasicHashtableEntry* p) {
duke@435 73 return (BasicHashtableEntry*)((intptr_t)p & -2);
duke@435 74 }
duke@435 75
duke@435 76 BasicHashtableEntry* next() const {
duke@435 77 return make_ptr(_next);
duke@435 78 }
duke@435 79
duke@435 80 void set_next(BasicHashtableEntry* next) {
duke@435 81 _next = next;
duke@435 82 }
duke@435 83
duke@435 84 BasicHashtableEntry** next_addr() {
duke@435 85 return &_next;
duke@435 86 }
duke@435 87
duke@435 88 bool is_shared() const {
duke@435 89 return ((intptr_t)_next & 1) != 0;
duke@435 90 }
duke@435 91
duke@435 92 void set_shared() {
duke@435 93 _next = (BasicHashtableEntry*)((intptr_t)_next | 1);
duke@435 94 }
duke@435 95 };
duke@435 96
duke@435 97
duke@435 98
coleenp@2497 99 template <class T> class HashtableEntry : public BasicHashtableEntry {
duke@435 100 friend class VMStructs;
duke@435 101 private:
coleenp@2497 102 T _literal; // ref to item in table.
duke@435 103
duke@435 104 public:
duke@435 105 // Literal
coleenp@2497 106 T literal() const { return _literal; }
coleenp@2497 107 T* literal_addr() { return &_literal; }
coleenp@2497 108 void set_literal(T s) { _literal = s; }
duke@435 109
duke@435 110 HashtableEntry* next() const {
duke@435 111 return (HashtableEntry*)BasicHashtableEntry::next();
duke@435 112 }
duke@435 113 HashtableEntry** next_addr() {
duke@435 114 return (HashtableEntry**)BasicHashtableEntry::next_addr();
duke@435 115 }
duke@435 116 };
duke@435 117
duke@435 118
duke@435 119
duke@435 120 class HashtableBucket : public CHeapObj {
duke@435 121 friend class VMStructs;
duke@435 122 private:
duke@435 123 // Instance variable
duke@435 124 BasicHashtableEntry* _entry;
duke@435 125
duke@435 126 public:
duke@435 127 // Accessing
duke@435 128 void clear() { _entry = NULL; }
duke@435 129
duke@435 130 // The following methods use order access methods to avoid race
duke@435 131 // conditions in multiprocessor systems.
duke@435 132 BasicHashtableEntry* get_entry() const;
duke@435 133 void set_entry(BasicHashtableEntry* l);
duke@435 134
duke@435 135 // The following method is not MT-safe and must be done under lock.
duke@435 136 BasicHashtableEntry** entry_addr() { return &_entry; }
duke@435 137 };
duke@435 138
duke@435 139
duke@435 140 class BasicHashtable : public CHeapObj {
duke@435 141 friend class VMStructs;
duke@435 142
duke@435 143 public:
duke@435 144 BasicHashtable(int table_size, int entry_size);
duke@435 145 BasicHashtable(int table_size, int entry_size,
duke@435 146 HashtableBucket* buckets, int number_of_entries);
duke@435 147
duke@435 148 // Sharing support.
duke@435 149 void copy_buckets(char** top, char* end);
duke@435 150 void copy_table(char** top, char* end);
duke@435 151
duke@435 152 // Bucket handling
duke@435 153 int hash_to_index(unsigned int full_hash) {
duke@435 154 int h = full_hash % _table_size;
duke@435 155 assert(h >= 0 && h < _table_size, "Illegal hash value");
duke@435 156 return h;
duke@435 157 }
duke@435 158
duke@435 159 // Reverse the order of elements in each of the buckets.
duke@435 160 void reverse();
duke@435 161
coleenp@2497 162 static unsigned int hash_symbol(const char* s, int len);
coleenp@2497 163
duke@435 164 private:
duke@435 165 // Instance variables
duke@435 166 int _table_size;
duke@435 167 HashtableBucket* _buckets;
duke@435 168 BasicHashtableEntry* _free_list;
duke@435 169 char* _first_free_entry;
duke@435 170 char* _end_block;
duke@435 171 int _entry_size;
duke@435 172 int _number_of_entries;
duke@435 173
duke@435 174 protected:
duke@435 175
duke@435 176 #ifdef ASSERT
duke@435 177 int _lookup_count;
duke@435 178 int _lookup_length;
duke@435 179 void verify_lookup_length(double load);
duke@435 180 #endif
duke@435 181
duke@435 182 void initialize(int table_size, int entry_size, int number_of_entries);
duke@435 183
duke@435 184 // Accessor
duke@435 185 int entry_size() const { return _entry_size; }
duke@435 186
duke@435 187 // The following method is MT-safe and may be used with caution.
duke@435 188 BasicHashtableEntry* bucket(int i);
duke@435 189
duke@435 190 // The following method is not MT-safe and must be done under lock.
duke@435 191 BasicHashtableEntry** bucket_addr(int i) { return _buckets[i].entry_addr(); }
duke@435 192
duke@435 193 // Table entry management
duke@435 194 BasicHashtableEntry* new_entry(unsigned int hashValue);
duke@435 195
duke@435 196 public:
acorn@3491 197 int table_size() { return _table_size; }
duke@435 198 void set_entry(int index, BasicHashtableEntry* entry);
duke@435 199
duke@435 200 void add_entry(int index, BasicHashtableEntry* entry);
duke@435 201
duke@435 202 void free_entry(BasicHashtableEntry* entry);
duke@435 203
duke@435 204 int number_of_entries() { return _number_of_entries; }
duke@435 205
duke@435 206 void verify() PRODUCT_RETURN;
duke@435 207 };
duke@435 208
duke@435 209
coleenp@2497 210 template <class T> class Hashtable : public BasicHashtable {
duke@435 211 friend class VMStructs;
duke@435 212
duke@435 213 public:
duke@435 214 Hashtable(int table_size, int entry_size)
duke@435 215 : BasicHashtable(table_size, entry_size) { }
duke@435 216
duke@435 217 Hashtable(int table_size, int entry_size,
duke@435 218 HashtableBucket* buckets, int number_of_entries)
duke@435 219 : BasicHashtable(table_size, entry_size, buckets, number_of_entries) { }
duke@435 220
duke@435 221 // Debugging
duke@435 222 void print() PRODUCT_RETURN;
duke@435 223
duke@435 224 // Reverse the order of elements in each of the buckets. Hashtable
duke@435 225 // entries which refer to objects at a lower address than 'boundary'
duke@435 226 // are separated from those which refer to objects at higher
duke@435 227 // addresses, and appear first in the list.
duke@435 228 void reverse(void* boundary = NULL);
duke@435 229
duke@435 230 protected:
duke@435 231
coleenp@2497 232 unsigned int compute_hash(Symbol* name) {
duke@435 233 return (unsigned int) name->identity_hash();
duke@435 234 }
duke@435 235
coleenp@2497 236 int index_for(Symbol* name) {
duke@435 237 return hash_to_index(compute_hash(name));
duke@435 238 }
duke@435 239
duke@435 240 // Table entry management
coleenp@2497 241 HashtableEntry<T>* new_entry(unsigned int hashValue, T obj);
duke@435 242
duke@435 243 // The following method is MT-safe and may be used with caution.
coleenp@2497 244 HashtableEntry<T>* bucket(int i) {
coleenp@2497 245 return (HashtableEntry<T>*)BasicHashtable::bucket(i);
duke@435 246 }
duke@435 247
duke@435 248 // The following method is not MT-safe and must be done under lock.
coleenp@2497 249 HashtableEntry<T>** bucket_addr(int i) {
coleenp@2497 250 return (HashtableEntry<T>**)BasicHashtable::bucket_addr(i);
duke@435 251 }
duke@435 252 };
duke@435 253
duke@435 254
duke@435 255 // Verions of hashtable where two handles are used to compute the index.
duke@435 256
coleenp@2497 257 template <class T> class TwoOopHashtable : public Hashtable<T> {
duke@435 258 friend class VMStructs;
duke@435 259 protected:
duke@435 260 TwoOopHashtable(int table_size, int entry_size)
coleenp@2497 261 : Hashtable<T>(table_size, entry_size) {}
duke@435 262
duke@435 263 TwoOopHashtable(int table_size, int entry_size, HashtableBucket* t,
duke@435 264 int number_of_entries)
coleenp@2497 265 : Hashtable<T>(table_size, entry_size, t, number_of_entries) {}
duke@435 266
duke@435 267 public:
coleenp@2497 268 unsigned int compute_hash(Symbol* name, Handle loader) {
duke@435 269 // Be careful with identity_hash(), it can safepoint and if this
duke@435 270 // were one expression, the compiler could choose to unhandle each
duke@435 271 // oop before calling identity_hash() for either of them. If the first
duke@435 272 // causes a GC, the next would fail.
duke@435 273 unsigned int name_hash = name->identity_hash();
duke@435 274 unsigned int loader_hash = loader.is_null() ? 0 : loader->identity_hash();
duke@435 275 return name_hash ^ loader_hash;
duke@435 276 }
duke@435 277
coleenp@2497 278 int index_for(Symbol* name, Handle loader) {
coleenp@2547 279 return this->hash_to_index(compute_hash(name, loader));
duke@435 280 }
duke@435 281 };
stefank@2314 282
stefank@2314 283 #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP

mercurial