hashtable.hpp 10.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP
#define SHARE_VM_UTILITIES_HASHTABLE_HPP

28
#include "classfile/classLoaderData.hpp"
29 30
#include "memory/allocation.hpp"
#include "oops/oop.hpp"
31
#include "oops/symbol.hpp"
32 33
#include "runtime/handles.hpp"

D
duke 已提交
34 35 36 37 38 39 40 41 42 43
// This is a generic hashtable, designed to be used for the symbol
// and string tables.
//
// It is implemented as an open hash table with a fixed number of buckets.
//
// %note:
//  - TableEntrys are allocated in blocks to reduce the space overhead.



Z
zgu 已提交
44
template <MEMFLAGS F> class BasicHashtableEntry : public CHeapObj<F> {
D
duke 已提交
45 46 47 48 49 50 51 52 53 54 55
  friend class VMStructs;
private:
  unsigned int         _hash;           // 32-bit hash for item

  // Link to next element in the linked list for this bucket.  EXCEPT
  // bit 0 set indicates that this entry is shared and must not be
  // unlinked from the table. Bit 0 is set during the dumping of the
  // archive. Since shared entries are immutable, _next fields in the
  // shared entries will not change.  New entries will always be
  // unshared and since pointers are align, bit 0 will always remain 0
  // with no extra effort.
Z
zgu 已提交
56
  BasicHashtableEntry<F>* _next;
D
duke 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  // Windows IA64 compiler requires subclasses to be able to access these
protected:
  // Entry objects should not be created, they should be taken from the
  // free list with BasicHashtable.new_entry().
  BasicHashtableEntry() { ShouldNotReachHere(); }
  // Entry objects should not be destroyed.  They should be placed on
  // the free list instead with BasicHashtable.free_entry().
  ~BasicHashtableEntry() { ShouldNotReachHere(); }

public:

  unsigned int hash() const             { return _hash; }
  void set_hash(unsigned int hash)      { _hash = hash; }
  unsigned int* hash_addr()             { return &_hash; }

Z
zgu 已提交
73
  static BasicHashtableEntry<F>* make_ptr(BasicHashtableEntry<F>* p) {
D
duke 已提交
74 75 76
    return (BasicHashtableEntry*)((intptr_t)p & -2);
  }

Z
zgu 已提交
77
  BasicHashtableEntry<F>* next() const {
D
duke 已提交
78 79 80
    return make_ptr(_next);
  }

Z
zgu 已提交
81
  void set_next(BasicHashtableEntry<F>* next) {
D
duke 已提交
82 83 84
    _next = next;
  }

Z
zgu 已提交
85
  BasicHashtableEntry<F>** next_addr() {
D
duke 已提交
86 87 88 89 90 91 92 93
    return &_next;
  }

  bool is_shared() const {
    return ((intptr_t)_next & 1) != 0;
  }

  void set_shared() {
Z
zgu 已提交
94
    _next = (BasicHashtableEntry<F>*)((intptr_t)_next | 1);
D
duke 已提交
95 96 97 98 99
  }
};



Z
zgu 已提交
100
template <class T, MEMFLAGS F> class HashtableEntry : public BasicHashtableEntry<F> {
D
duke 已提交
101 102
  friend class VMStructs;
private:
103
  T               _literal;          // ref to item in table.
D
duke 已提交
104 105 106

public:
  // Literal
107 108 109
  T literal() const                   { return _literal; }
  T* literal_addr()                   { return &_literal; }
  void set_literal(T s)               { _literal = s; }
D
duke 已提交
110 111

  HashtableEntry* next() const {
Z
zgu 已提交
112
    return (HashtableEntry*)BasicHashtableEntry<F>::next();
D
duke 已提交
113 114
  }
  HashtableEntry** next_addr() {
Z
zgu 已提交
115
    return (HashtableEntry**)BasicHashtableEntry<F>::next_addr();
D
duke 已提交
116 117 118 119 120
  }
};



Z
zgu 已提交
121
template <MEMFLAGS F> class HashtableBucket : public CHeapObj<F> {
D
duke 已提交
122 123 124
  friend class VMStructs;
private:
  // Instance variable
Z
zgu 已提交
125
  BasicHashtableEntry<F>*       _entry;
D
duke 已提交
126 127 128 129 130 131 132

public:
  // Accessing
  void clear()                        { _entry = NULL; }

  // The following methods use order access methods to avoid race
  // conditions in multiprocessor systems.
Z
zgu 已提交
133 134
  BasicHashtableEntry<F>* get_entry() const;
  void set_entry(BasicHashtableEntry<F>* l);
D
duke 已提交
135 136

  // The following method is not MT-safe and must be done under lock.
Z
zgu 已提交
137
  BasicHashtableEntry<F>** entry_addr()  { return &_entry; }
D
duke 已提交
138 139 140
};


Z
zgu 已提交
141
template <MEMFLAGS F> class BasicHashtable : public CHeapObj<F> {
D
duke 已提交
142 143 144 145 146
  friend class VMStructs;

public:
  BasicHashtable(int table_size, int entry_size);
  BasicHashtable(int table_size, int entry_size,
Z
zgu 已提交
147
                 HashtableBucket<F>* buckets, int number_of_entries);
D
duke 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  // Sharing support.
  void copy_buckets(char** top, char* end);
  void copy_table(char** top, char* end);

  // Bucket handling
  int hash_to_index(unsigned int full_hash) {
    int h = full_hash % _table_size;
    assert(h >= 0 && h < _table_size, "Illegal hash value");
    return h;
  }

  // Reverse the order of elements in each of the buckets.
  void reverse();

private:
  // Instance variables
  int               _table_size;
Z
zgu 已提交
166 167
  HashtableBucket<F>*     _buckets;
  BasicHashtableEntry<F>* _free_list;
D
duke 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  char*             _first_free_entry;
  char*             _end_block;
  int               _entry_size;
  int               _number_of_entries;

protected:

#ifdef ASSERT
  int               _lookup_count;
  int               _lookup_length;
  void verify_lookup_length(double load);
#endif

  void initialize(int table_size, int entry_size, int number_of_entries);

  // Accessor
  int entry_size() const { return _entry_size; }

  // The following method is MT-safe and may be used with caution.
Z
zgu 已提交
187
  BasicHashtableEntry<F>* bucket(int i);
D
duke 已提交
188 189

  // The following method is not MT-safe and must be done under lock.
Z
zgu 已提交
190
  BasicHashtableEntry<F>** bucket_addr(int i) { return _buckets[i].entry_addr(); }
D
duke 已提交
191

192 193 194
  // Attempt to get an entry from the free list
  BasicHashtableEntry<F>* new_entry_free_list();

D
duke 已提交
195
  // Table entry management
Z
zgu 已提交
196
  BasicHashtableEntry<F>* new_entry(unsigned int hashValue);
D
duke 已提交
197

198 199
  // Used when moving the entry to another table
  // Clean up links, but do not add to free_list
Z
zgu 已提交
200
  void unlink_entry(BasicHashtableEntry<F>* entry) {
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    entry->set_next(NULL);
    --_number_of_entries;
  }

  // Move over freelist and free block for allocation
  void copy_freelist(BasicHashtable* src) {
    _free_list = src->_free_list;
    src->_free_list = NULL;
    _first_free_entry = src->_first_free_entry;
    src->_first_free_entry = NULL;
    _end_block = src->_end_block;
    src->_end_block = NULL;
  }

  // Free the buckets in this hashtable
216
  void free_buckets();
217

D
duke 已提交
218
public:
219
  int table_size() { return _table_size; }
Z
zgu 已提交
220
  void set_entry(int index, BasicHashtableEntry<F>* entry);
D
duke 已提交
221

Z
zgu 已提交
222
  void add_entry(int index, BasicHashtableEntry<F>* entry);
D
duke 已提交
223

Z
zgu 已提交
224
  void free_entry(BasicHashtableEntry<F>* entry);
D
duke 已提交
225 226 227 228 229 230 231

  int number_of_entries() { return _number_of_entries; }

  void verify() PRODUCT_RETURN;
};


Z
zgu 已提交
232
template <class T, MEMFLAGS F> class Hashtable : public BasicHashtable<F> {
D
duke 已提交
233 234 235 236
  friend class VMStructs;

public:
  Hashtable(int table_size, int entry_size)
Z
zgu 已提交
237
    : BasicHashtable<F>(table_size, entry_size) { }
D
duke 已提交
238 239

  Hashtable(int table_size, int entry_size,
Z
zgu 已提交
240 241
                   HashtableBucket<F>* buckets, int number_of_entries)
    : BasicHashtable<F>(table_size, entry_size, buckets, number_of_entries) { }
D
duke 已提交
242 243 244 245 246 247 248 249 250 251 252 253

  // Debugging
  void print()               PRODUCT_RETURN;

  // Reverse the order of elements in each of the buckets. Hashtable
  // entries which refer to objects at a lower address than 'boundary'
  // are separated from those which refer to objects at higher
  // addresses, and appear first in the list.
  void reverse(void* boundary = NULL);

protected:

254
  unsigned int compute_hash(Symbol* name) {
D
duke 已提交
255 256 257
    return (unsigned int) name->identity_hash();
  }

258
  int index_for(Symbol* name) {
259
    return this->hash_to_index(compute_hash(name));
D
duke 已提交
260 261 262
  }

  // Table entry management
Z
zgu 已提交
263
  HashtableEntry<T, F>* new_entry(unsigned int hashValue, T obj);
D
duke 已提交
264 265

  // The following method is MT-safe and may be used with caution.
Z
zgu 已提交
266 267
  HashtableEntry<T, F>* bucket(int i) {
    return (HashtableEntry<T, F>*)BasicHashtable<F>::bucket(i);
D
duke 已提交
268 269 270
  }

  // The following method is not MT-safe and must be done under lock.
Z
zgu 已提交
271 272
  HashtableEntry<T, F>** bucket_addr(int i) {
    return (HashtableEntry<T, F>**)BasicHashtable<F>::bucket_addr(i);
D
duke 已提交
273
  }
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
};

template <class T, MEMFLAGS F> class RehashableHashtable : public Hashtable<T, F> {
 protected:

  enum {
    rehash_count = 100,
    rehash_multiple = 60
  };

  // Check that the table is unbalanced
  bool check_rehash_table(int count);

 public:
  RehashableHashtable(int table_size, int entry_size)
    : Hashtable<T, F>(table_size, entry_size) { }

  RehashableHashtable(int table_size, int entry_size,
                   HashtableBucket<F>* buckets, int number_of_entries)
    : Hashtable<T, F>(table_size, entry_size, buckets, number_of_entries) { }


297
  // Function to move these elements into the new table.
298
  void move_to(RehashableHashtable<T, F>* new_table);
299
  static bool use_alternate_hashcode()  { return _seed != 0; }
300
  static juint seed()                    { return _seed; }
301

302 303 304 305 306 307 308 309 310 311 312 313
  static int literal_size(Symbol *symbol);
  static int literal_size(oop oop);

  // The following two are currently not used, but are needed anyway because some
  // C++ compilers (MacOS and Solaris) force the instantiation of
  // Hashtable<ConstantPool*, mtClass>::dump_table() even though we never call this function
  // in the VM code.
  static int literal_size(ConstantPool *cp) {Unimplemented(); return 0;}
  static int literal_size(Klass *k)         {Unimplemented(); return 0;}

  void dump_table(outputStream* st, const char *table_name);

314
 private:
315
  static juint _seed;
D
duke 已提交
316 317 318 319 320
};


//  Verions of hashtable where two handles are used to compute the index.

Z
zgu 已提交
321
template <class T, MEMFLAGS F> class TwoOopHashtable : public Hashtable<T, F> {
D
duke 已提交
322 323 324
  friend class VMStructs;
protected:
  TwoOopHashtable(int table_size, int entry_size)
Z
zgu 已提交
325
    : Hashtable<T, F>(table_size, entry_size) {}
D
duke 已提交
326

Z
zgu 已提交
327
  TwoOopHashtable(int table_size, int entry_size, HashtableBucket<F>* t,
D
duke 已提交
328
                  int number_of_entries)
Z
zgu 已提交
329
    : Hashtable<T, F>(table_size, entry_size, t, number_of_entries) {}
D
duke 已提交
330 331

public:
332
  unsigned int compute_hash(Symbol* name, ClassLoaderData* loader_data) {
D
duke 已提交
333
    unsigned int name_hash = name->identity_hash();
334 335 336 337
    // loader is null with CDS
    assert(loader_data != NULL || UseSharedSpaces || DumpSharedSpaces,
           "only allowed with shared spaces");
    unsigned int loader_hash = loader_data == NULL ? 0 : loader_data->identity_hash();
D
duke 已提交
338 339 340
    return name_hash ^ loader_hash;
  }

341 342
  int index_for(Symbol* name, ClassLoaderData* loader_data) {
    return this->hash_to_index(compute_hash(name, loader_data));
D
duke 已提交
343 344
  }
};
345 346

#endif // SHARE_VM_UTILITIES_HASHTABLE_HPP