hashtable.hpp revision 13059:cb443f7d564f
165285Siwasaki/*
265285Siwasaki * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
365285Siwasaki * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
465285Siwasaki *
565285Siwasaki * This code is free software; you can redistribute it and/or modify it
665285Siwasaki * under the terms of the GNU General Public License version 2 only, as
765285Siwasaki * published by the Free Software Foundation.
865285Siwasaki *
965285Siwasaki * This code is distributed in the hope that it will be useful, but WITHOUT
1065285Siwasaki * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1165285Siwasaki * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1265285Siwasaki * version 2 for more details (a copy is included in the LICENSE file that
1365285Siwasaki * accompanied this code).
1465285Siwasaki *
1565285Siwasaki * You should have received a copy of the GNU General Public License version
1665285Siwasaki * 2 along with this work; if not, write to the Free Software Foundation,
1765285Siwasaki * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1865285Siwasaki *
1965285Siwasaki * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2065285Siwasaki * or visit www.oracle.com if you need additional information or have any
2165285Siwasaki * questions.
2265285Siwasaki *
2365285Siwasaki */
2465285Siwasaki
2565285Siwasaki#ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP
2665285Siwasaki#define SHARE_VM_UTILITIES_HASHTABLE_HPP
2765285Siwasaki
2865285Siwasaki#include "classfile/classLoaderData.hpp"
2965285Siwasaki#include "memory/allocation.hpp"
3065285Siwasaki#include "oops/oop.hpp"
31196555Sjhb#include "oops/symbol.hpp"
3265285Siwasaki#include "runtime/handles.hpp"
33196555Sjhb
34196555Sjhb// This is a generic hashtable, designed to be used for the symbol
35196555Sjhb// and string tables.
3668475Siwasaki//
37196555Sjhb// It is implemented as an open hash table with a fixed number of buckets.
38196555Sjhb//
39196555Sjhb// %note:
40196555Sjhb//  - TableEntrys are allocated in blocks to reduce the space overhead.
41196555Sjhb
42196555Sjhb
43196555Sjhb
44196555Sjhbtemplate <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> {
45196555Sjhb  friend class VMStructs;
46196555Sjhbprivate:
4768475Siwasaki  unsigned int         _hash;           // 32-bit hash for item
48196555Sjhb
49196555Sjhb  // Link to next element in the linked list for this bucket.  EXCEPT
50196555Sjhb  // bit 0 set indicates that this entry is shared and must not be
51196555Sjhb  // unlinked from the table. Bit 0 is set during the dumping of the
52196555Sjhb  // archive. Since shared entries are immutable, _next fields in the
53196555Sjhb  // shared entries will not change.  New entries will always be
5468475Siwasaki  // unshared and since pointers are align, bit 0 will always remain 0
55119515Snjl  // with no extra effort.
56196555Sjhb  BasicHashtableEntry<F>* _next;
57119515Snjl
58211196Stakawata  // Windows IA64 compiler requires subclasses to be able to access these
59211196Stakawataprotected:
60211196Stakawata  // Entry objects should not be created, they should be taken from the
61211196Stakawata  // free list with BasicHashtable.new_entry().
62211196Stakawata  BasicHashtableEntry() { ShouldNotReachHere(); }
63211196Stakawata  // Entry objects should not be destroyed.  They should be placed on
64211196Stakawata  // the free list instead with BasicHashtable.free_entry().
65211196Stakawata  ~BasicHashtableEntry() { ShouldNotReachHere(); }
66211196Stakawata
67211196Stakawatapublic:
68211196Stakawata
69211196Stakawata  unsigned int hash() const             { return _hash; }
70211196Stakawata  void set_hash(unsigned int hash)      { _hash = hash; }
71211196Stakawata  unsigned int* hash_addr()             { return &_hash; }
72211196Stakawata
73211196Stakawata  static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) {
74211196Stakawata    return (BasicHashtableEntry*)((intptr_t)p & -2);
75211196Stakawata  }
76211196Stakawata
77211196Stakawata  BasicHashtableEntry<F>* next() const {
78211196Stakawata    return make_ptr(_next);
79211196Stakawata  }
80211196Stakawata
81211196Stakawata  void set_next(BasicHashtableEntry<F>* next) {
82211196Stakawata    _next = next;
83211196Stakawata  }
84211196Stakawata
85211196Stakawata  BasicHashtableEntry<F>** next_addr() {
86211196Stakawata    return &_next;
87211196Stakawata  }
88211196Stakawata
89211196Stakawata  bool is_shared() const {
90211196Stakawata    return ((intptr_t)_next & 1) != 0;
91211196Stakawata  }
92211196Stakawata
93211196Stakawata  void set_shared() {
94211196Stakawata    _next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1);
95211196Stakawata  }
96211196Stakawata};
97211196Stakawata
98211196Stakawata
99211196Stakawata
100211196Stakawatatemplate <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> {
101211196Stakawata  friend class VMStructs;
102211196Stakawataprivate:
103211196Stakawata  T               _literal;          // ref to item in table.
104211196Stakawata
105211196Stakawatapublic:
106211196Stakawata  // Literal
107211196Stakawata  T literal() const                   { return _literal; }
108211196Stakawata  T* literal_addr()                   { return &_literal; }
109211196Stakawata  void set_literal(T s)               { _literal = s; }
110211196Stakawata
111211196Stakawata  HashtableEntry* next() const {
112211196Stakawata    return (HashtableEntry*)BasicHashtableEntry<F>::next();
113211196Stakawata  }
114211196Stakawata  HashtableEntry** next_addr() {
115211196Stakawata    return (HashtableEntry**)BasicHashtableEntry<F>::next_addr();
116211196Stakawata  }
117211196Stakawata};
118211196Stakawata
119211196Stakawata
120211196Stakawata
121211196Stakawatatemplate <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> {
122211196Stakawata  friend class VMStructs;
123211196Stakawataprivate:
124211196Stakawata  // Instance variable
125211196Stakawata  BasicHashtableEntry<F>*       _entry;
126211196Stakawata
127211196Stakawata#ifdef ASSERT
128211196Stakawataprivate:
129211196Stakawata  unsigned _hits;
130119515Snjlpublic:
131119515Snjl  unsigned hits()   { return _hits; }
132119515Snjl  void count_hit()  { _hits++; }
133119515Snjl#endif
134196555Sjhb
135119515Snjlpublic:
136119515Snjl  // Accessing
137196555Sjhb  void clear()                        { _entry = NULL; DEBUG_ONLY(_hits = 0); }
138119515Snjl
139119515Snjl  // The following methods use order access methods to avoid race
140196555Sjhb  // conditions in multiprocessor systems.
141119515Snjl  BasicHashtableEntry<F>* get_entry() const;
142119515Snjl  void set_entry(BasicHashtableEntry<F>* l);
143196555Sjhb
144119515Snjl  // The following method is not MT-safe and must be done under lock.
145119515Snjl  BasicHashtableEntry<F>** entry_addr()  { return &_entry; }
146196555Sjhb
147196555Sjhb};
148196555Sjhb
149196555Sjhb
150196555Sjhbtemplate <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> {
151196555Sjhb  friend class VMStructs;
152119515Snjl
153119515Snjlpublic:
154119515Snjl  BasicHashtable(int table_size, int entry_size);
155119515Snjl  BasicHashtable(int table_size, int entry_size,
156119515Snjl                 HashtableBucket<F>* buckets, int number_of_entries);
157119515Snjl
15865285Siwasaki  // Sharing support.
159  void copy_buckets(char** top, char* end);
160  void copy_table(char** top, char* end);
161
162  // Bucket handling
163  int hash_to_index(unsigned int full_hash) const {
164    int h = full_hash % _table_size;
165    assert(h >= 0 && h < _table_size, "Illegal hash value");
166    return h;
167  }
168
169private:
170  // Instance variables
171  int               _table_size;
172  HashtableBucket<F>*     _buckets;
173  BasicHashtableEntry<F>* _free_list;
174  char*             _first_free_entry;
175  char*             _end_block;
176  int               _entry_size;
177  int               _number_of_entries;
178
179protected:
180
181#ifdef ASSERT
182  bool              _lookup_warning;
183  mutable int       _lookup_count;
184  mutable int       _lookup_length;
185  bool verify_lookup_length(double load, const char *table_name);
186#endif
187
188  void initialize(int table_size, int entry_size, int number_of_entries);
189
190  // Accessor
191  int entry_size() const { return _entry_size; }
192
193  // The following method is MT-safe and may be used with caution.
194  BasicHashtableEntry<F>* bucket(int i) const;
195
196  // The following method is not MT-safe and must be done under lock.
197  BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); }
198
199  // Attempt to get an entry from the free list
200  BasicHashtableEntry<F>* new_entry_free_list();
201
202  // Table entry management
203  BasicHashtableEntry<F>* new_entry(unsigned int hashValue);
204
205  // Used when moving the entry to another table
206  // Clean up links, but do not add to free_list
207  void unlink_entry(BasicHashtableEntry<F>* entry) {
208    entry->set_next(NULL);
209    --_number_of_entries;
210  }
211
212  // Move over freelist and free block for allocation
213  void copy_freelist(BasicHashtable* src) {
214    _free_list = src->_free_list;
215    src->_free_list = NULL;
216    _first_free_entry = src->_first_free_entry;
217    src->_first_free_entry = NULL;
218    _end_block = src->_end_block;
219    src->_end_block = NULL;
220  }
221
222  // Free the buckets in this hashtable
223  void free_buckets();
224
225public:
226  int table_size() { return _table_size; }
227  void set_entry(int index, BasicHashtableEntry<F>* entry);
228
229  void add_entry(int index, BasicHashtableEntry<F>* entry);
230
231  void free_entry(BasicHashtableEntry<F>* entry);
232
233  int number_of_entries() { return _number_of_entries; }
234
235  void verify() PRODUCT_RETURN;
236
237#ifdef ASSERT
238  void bucket_count_hit(int i) const {
239    _buckets[i].count_hit();
240  }
241  unsigned bucket_hits(int i) const {
242    return _buckets[i].hits();
243  }
244#endif
245};
246
247
248template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> {
249  friend class VMStructs;
250
251public:
252  Hashtable(int table_size, int entry_size)
253    : BasicHashtable<F>(table_size, entry_size) { }
254
255  Hashtable(int table_size, int entry_size,
256                   HashtableBucket<F>* buckets, int number_of_entries)
257    : BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { }
258
259  // Debugging
260  void print()               PRODUCT_RETURN;
261
262protected:
263
264  unsigned int compute_hash(Symbol* name) {
265    return (unsigned int) name->identity_hash();
266  }
267
268  int index_for(Symbol* name) {
269    return this->hash_to_index(compute_hash(name));
270  }
271
272  // Table entry management
273  HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj);
274
275  // The following method is MT-safe and may be used with caution.
276  HashtableEntry<T, F>* bucket(int i) const {
277    return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i);
278  }
279
280  // The following method is not MT-safe and must be done under lock.
281  HashtableEntry<T, F>** bucket_addr(int i) {
282    return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i);
283  }
284
285};
286
287template <class T, MEMFLAGS F> class RehashableHashtable : public Hashtable<T, F> {
288 friend class VMStructs;
289 protected:
290
291  enum {
292    rehash_count = 100,
293    rehash_multiple = 60
294  };
295
296  // Check that the table is unbalanced
297  bool check_rehash_table(int count);
298
299 public:
300  RehashableHashtable(int table_size, int entry_size)
301    : Hashtable<T, F>(table_size, entry_size) { }
302
303  RehashableHashtable(int table_size, int entry_size,
304                   HashtableBucket<F>* buckets, int number_of_entries)
305    : Hashtable<T, F>(table_size, entry_size, buckets, number_of_entries) { }
306
307
308  // Function to move these elements into the new table.
309  void move_to(RehashableHashtable<T, F>* new_table);
310  static bool use_alternate_hashcode()  { return _seed != 0; }
311  static juint seed()                    { return _seed; }
312
313  static int literal_size(Symbol *symbol);
314  static int literal_size(oop oop);
315
316  // The following two are currently not used, but are needed anyway because some
317  // C++ compilers (MacOS and Solaris) force the instantiation of
318  // Hashtable<ConstantPool*, mtClass>::dump_table() even though we never call this function
319  // in the VM code.
320  static int literal_size(ConstantPool *cp) {Unimplemented(); return 0;}
321  static int literal_size(Klass *k)         {Unimplemented(); return 0;}
322
323  void dump_table(outputStream* st, const char *table_name);
324
325 private:
326  static juint _seed;
327};
328
329
330// Versions of hashtable where two handles are used to compute the index.
331
332template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> {
333  friend class VMStructs;
334protected:
335  TwoOopHashtable(int table_size, int entry_size)
336    : Hashtable<T, F>(table_size, entry_size) {}
337
338  TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t,
339                  int number_of_entries)
340    : Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {}
341
342public:
343  unsigned int compute_hash(const Symbol* name, const ClassLoaderData* loader_data) const {
344    unsigned int name_hash = name->identity_hash();
345    // loader is null with CDS
346    assert(loader_data != NULL || UseSharedSpaces || DumpSharedSpaces,
347           "only allowed with shared spaces");
348    unsigned int loader_hash = loader_data == NULL ? 0 : loader_data->identity_hash();
349    return name_hash ^ loader_hash;
350  }
351
352  int index_for(Symbol* name, ClassLoaderData* loader_data) {
353    return this->hash_to_index(compute_hash(name, loader_data));
354  }
355};
356
357#endif // SHARE_VM_UTILITIES_HASHTABLE_HPP
358