author | zgu |
Thu, 28 Jun 2012 17:03:16 -0400 | |
changeset 13195 | be27e1b6a4b9 |
parent 13097 | c146b608d91f |
child 13199 | 025b0984feea |
permissions | -rw-r--r-- |
1 | 1 |
/* |
11628
13155c0c00b4
7114376: Make system dictionary hashtable bucket array size configurable
acorn
parents:
8921
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP |
26 |
#define SHARE_VM_UTILITIES_HASHTABLE_HPP |
|
27 |
||
28 |
#include "memory/allocation.hpp" |
|
29 |
#include "oops/oop.hpp" |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
30 |
#include "oops/symbol.hpp" |
7397 | 31 |
#include "runtime/handles.hpp" |
32 |
||
1 | 33 |
// This is a generic hashtable, designed to be used for the symbol |
34 |
// and string tables. |
|
35 |
// |
|
36 |
// It is implemented as an open hash table with a fixed number of buckets. |
|
37 |
// |
|
38 |
// %note: |
|
39 |
// - TableEntrys are allocated in blocks to reduce the space overhead. |
|
40 |
||
41 |
||
42 |
||
13195 | 43 |
template <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> { |
1 | 44 |
friend class VMStructs; |
45 |
private: |
|
46 |
unsigned int _hash; // 32-bit hash for item |
|
47 |
||
48 |
// Link to next element in the linked list for this bucket. EXCEPT |
|
49 |
// bit 0 set indicates that this entry is shared and must not be |
|
50 |
// unlinked from the table. Bit 0 is set during the dumping of the |
|
51 |
// archive. Since shared entries are immutable, _next fields in the |
|
52 |
// shared entries will not change. New entries will always be |
|
53 |
// unshared and since pointers are align, bit 0 will always remain 0 |
|
54 |
// with no extra effort. |
|
13195 | 55 |
BasicHashtableEntry<F>* _next; |
1 | 56 |
|
57 |
// Windows IA64 compiler requires subclasses to be able to access these |
|
58 |
protected: |
|
59 |
// Entry objects should not be created, they should be taken from the |
|
60 |
// free list with BasicHashtable.new_entry(). |
|
61 |
BasicHashtableEntry() { ShouldNotReachHere(); } |
|
62 |
// Entry objects should not be destroyed. They should be placed on |
|
63 |
// the free list instead with BasicHashtable.free_entry(). |
|
64 |
~BasicHashtableEntry() { ShouldNotReachHere(); } |
|
65 |
||
66 |
public: |
|
67 |
||
68 |
unsigned int hash() const { return _hash; } |
|
69 |
void set_hash(unsigned int hash) { _hash = hash; } |
|
70 |
unsigned int* hash_addr() { return &_hash; } |
|
71 |
||
13195 | 72 |
static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) { |
1 | 73 |
return (BasicHashtableEntry*)((intptr_t)p & -2); |
74 |
} |
|
75 |
||
13195 | 76 |
BasicHashtableEntry<F>* next() const { |
1 | 77 |
return make_ptr(_next); |
78 |
} |
|
79 |
||
13195 | 80 |
void set_next(BasicHashtableEntry<F>* next) { |
1 | 81 |
_next = next; |
82 |
} |
|
83 |
||
13195 | 84 |
BasicHashtableEntry<F>** next_addr() { |
1 | 85 |
return &_next; |
86 |
} |
|
87 |
||
88 |
bool is_shared() const { |
|
89 |
return ((intptr_t)_next & 1) != 0; |
|
90 |
} |
|
91 |
||
92 |
void set_shared() { |
|
13195 | 93 |
_next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1); |
1 | 94 |
} |
95 |
}; |
|
96 |
||
97 |
||
98 |
||
13195 | 99 |
template <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> { |
1 | 100 |
friend class VMStructs; |
101 |
private: |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
102 |
T _literal; // ref to item in table. |
1 | 103 |
|
104 |
public: |
|
105 |
// Literal |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
106 |
T literal() const { return _literal; } |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
107 |
T* literal_addr() { return &_literal; } |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
108 |
void set_literal(T s) { _literal = s; } |
1 | 109 |
|
110 |
HashtableEntry* next() const { |
|
13195 | 111 |
return (HashtableEntry*)BasicHashtableEntry<F>::next(); |
1 | 112 |
} |
113 |
HashtableEntry** next_addr() { |
|
13195 | 114 |
return (HashtableEntry**)BasicHashtableEntry<F>::next_addr(); |
1 | 115 |
} |
116 |
}; |
|
117 |
||
118 |
||
119 |
||
13195 | 120 |
template <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> { |
1 | 121 |
friend class VMStructs; |
122 |
private: |
|
123 |
// Instance variable |
|
13195 | 124 |
BasicHashtableEntry<F>* _entry; |
1 | 125 |
|
126 |
public: |
|
127 |
// Accessing |
|
128 |
void clear() { _entry = NULL; } |
|
129 |
||
130 |
// The following methods use order access methods to avoid race |
|
131 |
// conditions in multiprocessor systems. |
|
13195 | 132 |
BasicHashtableEntry<F>* get_entry() const; |
133 |
void set_entry(BasicHashtableEntry<F>* l); |
|
1 | 134 |
|
135 |
// The following method is not MT-safe and must be done under lock. |
|
13195 | 136 |
BasicHashtableEntry<F>** entry_addr() { return &_entry; } |
1 | 137 |
}; |
138 |
||
139 |
||
13195 | 140 |
template <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> { |
1 | 141 |
friend class VMStructs; |
142 |
||
143 |
public: |
|
144 |
BasicHashtable(int table_size, int entry_size); |
|
145 |
BasicHashtable(int table_size, int entry_size, |
|
13195 | 146 |
HashtableBucket<F>* buckets, int number_of_entries); |
1 | 147 |
|
148 |
// Sharing support. |
|
149 |
void copy_buckets(char** top, char* end); |
|
150 |
void copy_table(char** top, char* end); |
|
151 |
||
152 |
// Bucket handling |
|
153 |
int hash_to_index(unsigned int full_hash) { |
|
154 |
int h = full_hash % _table_size; |
|
155 |
assert(h >= 0 && h < _table_size, "Illegal hash value"); |
|
156 |
return h; |
|
157 |
} |
|
158 |
||
159 |
// Reverse the order of elements in each of the buckets. |
|
160 |
void reverse(); |
|
161 |
||
162 |
private: |
|
163 |
// Instance variables |
|
164 |
int _table_size; |
|
13195 | 165 |
HashtableBucket<F>* _buckets; |
166 |
BasicHashtableEntry<F>* _free_list; |
|
1 | 167 |
char* _first_free_entry; |
168 |
char* _end_block; |
|
169 |
int _entry_size; |
|
170 |
int _number_of_entries; |
|
171 |
||
172 |
protected: |
|
173 |
||
174 |
#ifdef ASSERT |
|
175 |
int _lookup_count; |
|
176 |
int _lookup_length; |
|
177 |
void verify_lookup_length(double load); |
|
178 |
#endif |
|
179 |
||
13087 | 180 |
enum { |
181 |
rehash_count = 100, |
|
182 |
rehash_multiple = 60 |
|
183 |
}; |
|
184 |
||
1 | 185 |
void initialize(int table_size, int entry_size, int number_of_entries); |
186 |
||
187 |
// Accessor |
|
188 |
int entry_size() const { return _entry_size; } |
|
189 |
||
190 |
// The following method is MT-safe and may be used with caution. |
|
13195 | 191 |
BasicHashtableEntry<F>* bucket(int i); |
1 | 192 |
|
193 |
// The following method is not MT-safe and must be done under lock. |
|
13195 | 194 |
BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); } |
1 | 195 |
|
196 |
// Table entry management |
|
13195 | 197 |
BasicHashtableEntry<F>* new_entry(unsigned int hashValue); |
1 | 198 |
|
13087 | 199 |
// Check that the table is unbalanced |
200 |
bool check_rehash_table(int count); |
|
201 |
||
202 |
// Used when moving the entry to another table |
|
203 |
// Clean up links, but do not add to free_list |
|
13195 | 204 |
void unlink_entry(BasicHashtableEntry<F>* entry) { |
13087 | 205 |
entry->set_next(NULL); |
206 |
--_number_of_entries; |
|
207 |
} |
|
208 |
||
209 |
// Move over freelist and free block for allocation |
|
210 |
void copy_freelist(BasicHashtable* src) { |
|
211 |
_free_list = src->_free_list; |
|
212 |
src->_free_list = NULL; |
|
213 |
_first_free_entry = src->_first_free_entry; |
|
214 |
src->_first_free_entry = NULL; |
|
215 |
_end_block = src->_end_block; |
|
216 |
src->_end_block = NULL; |
|
217 |
} |
|
218 |
||
219 |
// Free the buckets in this hashtable |
|
13097
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
220 |
void free_buckets(); |
13087 | 221 |
|
1 | 222 |
public: |
11628
13155c0c00b4
7114376: Make system dictionary hashtable bucket array size configurable
acorn
parents:
8921
diff
changeset
|
223 |
int table_size() { return _table_size; } |
13195 | 224 |
void set_entry(int index, BasicHashtableEntry<F>* entry); |
1 | 225 |
|
13195 | 226 |
void add_entry(int index, BasicHashtableEntry<F>* entry); |
1 | 227 |
|
13195 | 228 |
void free_entry(BasicHashtableEntry<F>* entry); |
1 | 229 |
|
230 |
int number_of_entries() { return _number_of_entries; } |
|
231 |
||
232 |
void verify() PRODUCT_RETURN; |
|
233 |
}; |
|
234 |
||
235 |
||
13195 | 236 |
template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> { |
1 | 237 |
friend class VMStructs; |
238 |
||
239 |
public: |
|
240 |
Hashtable(int table_size, int entry_size) |
|
13195 | 241 |
: BasicHashtable<F>(table_size, entry_size) { } |
1 | 242 |
|
243 |
Hashtable(int table_size, int entry_size, |
|
13195 | 244 |
HashtableBucket<F>* buckets, int number_of_entries) |
245 |
: BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { } |
|
1 | 246 |
|
247 |
// Debugging |
|
248 |
void print() PRODUCT_RETURN; |
|
249 |
||
250 |
// Reverse the order of elements in each of the buckets. Hashtable |
|
251 |
// entries which refer to objects at a lower address than 'boundary' |
|
252 |
// are separated from those which refer to objects at higher |
|
253 |
// addresses, and appear first in the list. |
|
254 |
void reverse(void* boundary = NULL); |
|
255 |
||
256 |
protected: |
|
257 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
258 |
unsigned int compute_hash(Symbol* name) { |
1 | 259 |
return (unsigned int) name->identity_hash(); |
260 |
} |
|
261 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
262 |
int index_for(Symbol* name) { |
1 | 263 |
return hash_to_index(compute_hash(name)); |
264 |
} |
|
265 |
||
266 |
// Table entry management |
|
13195 | 267 |
HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj); |
1 | 268 |
|
269 |
// The following method is MT-safe and may be used with caution. |
|
13195 | 270 |
HashtableEntry<T, F>* bucket(int i) { |
271 |
return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i); |
|
1 | 272 |
} |
273 |
||
274 |
// The following method is not MT-safe and must be done under lock. |
|
13195 | 275 |
HashtableEntry<T, F>** bucket_addr(int i) { |
276 |
return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i); |
|
1 | 277 |
} |
13087 | 278 |
|
279 |
// Function to move these elements into the new table. |
|
13195 | 280 |
void move_to(Hashtable<T, F>* new_table); |
13087 | 281 |
virtual unsigned int new_hash(T) { ShouldNotReachHere(); return 0; } // should be overridden |
1 | 282 |
}; |
283 |
||
284 |
||
285 |
// Verions of hashtable where two handles are used to compute the index. |
|
286 |
||
13195 | 287 |
template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> { |
1 | 288 |
friend class VMStructs; |
289 |
protected: |
|
290 |
TwoOopHashtable(int table_size, int entry_size) |
|
13195 | 291 |
: Hashtable<T, F>(table_size, entry_size) {} |
1 | 292 |
|
13195 | 293 |
TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t, |
1 | 294 |
int number_of_entries) |
13195 | 295 |
: Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {} |
1 | 296 |
|
297 |
public: |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
298 |
unsigned int compute_hash(Symbol* name, Handle loader) { |
1 | 299 |
// Be careful with identity_hash(), it can safepoint and if this |
300 |
// were one expression, the compiler could choose to unhandle each |
|
301 |
// oop before calling identity_hash() for either of them. If the first |
|
302 |
// causes a GC, the next would fail. |
|
303 |
unsigned int name_hash = name->identity_hash(); |
|
304 |
unsigned int loader_hash = loader.is_null() ? 0 : loader->identity_hash(); |
|
305 |
return name_hash ^ loader_hash; |
|
306 |
} |
|
307 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
308 |
int index_for(Symbol* name, Handle loader) { |
8310
d7c285113f48
7019689: Non-dependent name is found in dependent base class although it should be rejected
coleenp
parents:
8076
diff
changeset
|
309 |
return this->hash_to_index(compute_hash(name, loader)); |
1 | 310 |
} |
311 |
}; |
|
7397 | 312 |
|
313 |
#endif // SHARE_VM_UTILITIES_HASHTABLE_HPP |