hotspot/src/share/vm/utilities/hashtable.hpp
author coleenp
Wed, 13 Jun 2012 19:52:59 -0400
changeset 13087 673ea6efaf18
parent 11628 13155c0c00b4
child 13097 c146b608d91f
permissions -rw-r--r--
7158800: Improve storage of symbol tables Summary: Use an alternate version of hashing algorithm for symbol string tables and after a certain bucket size to improve performance Reviewed-by: pbk, kamg, dlong, kvn, fparain
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     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
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_UTILITIES_HASHTABLE_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    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
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/handles.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// This is a generic hashtable, designed to be used for the symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// and string tables.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// It is implemented as an open hash table with a fixed number of buckets.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// %note:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
//  - TableEntrys are allocated in blocks to reduce the space overhead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
class BasicHashtableEntry : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  unsigned int         _hash;           // 32-bit hash for item
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // Link to next element in the linked list for this bucket.  EXCEPT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  // bit 0 set indicates that this entry is shared and must not be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  // unlinked from the table. Bit 0 is set during the dumping of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  // archive. Since shared entries are immutable, _next fields in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // shared entries will not change.  New entries will always be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // unshared and since pointers are align, bit 0 will always remain 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // with no extra effort.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  BasicHashtableEntry* _next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // Windows IA64 compiler requires subclasses to be able to access these
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // Entry objects should not be created, they should be taken from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  // free list with BasicHashtable.new_entry().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  BasicHashtableEntry() { ShouldNotReachHere(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // Entry objects should not be destroyed.  They should be placed on
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  // the free list instead with BasicHashtable.free_entry().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  ~BasicHashtableEntry() { ShouldNotReachHere(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  unsigned int hash() const             { return _hash; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  void set_hash(unsigned int hash)      { _hash = hash; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  unsigned int* hash_addr()             { return &_hash; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  static BasicHashtableEntry* make_ptr(BasicHashtableEntry* p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    return (BasicHashtableEntry*)((intptr_t)p & -2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  BasicHashtableEntry* next() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    return make_ptr(_next);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  void set_next(BasicHashtableEntry* next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    _next = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  BasicHashtableEntry** next_addr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    return &_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  bool is_shared() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    return ((intptr_t)_next & 1) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  void set_shared() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    _next = (BasicHashtableEntry*)((intptr_t)_next | 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
    99
template <class T> class HashtableEntry : public BasicHashtableEntry {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   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
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   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
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  HashtableEntry* next() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    return (HashtableEntry*)BasicHashtableEntry::next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  HashtableEntry** next_addr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    return (HashtableEntry**)BasicHashtableEntry::next_addr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
class HashtableBucket : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // Instance variable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  BasicHashtableEntry*       _entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  // Accessing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  void clear()                        { _entry = NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  // The following methods use order access methods to avoid race
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // conditions in multiprocessor systems.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  BasicHashtableEntry* get_entry() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  void set_entry(BasicHashtableEntry* l);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // The following method is not MT-safe and must be done under lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  BasicHashtableEntry** entry_addr()  { return &_entry; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
class BasicHashtable : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  BasicHashtable(int table_size, int entry_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  BasicHashtable(int table_size, int entry_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
                 HashtableBucket* buckets, int number_of_entries);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  // Sharing support.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  void copy_buckets(char** top, char* end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  void copy_table(char** top, char* end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // Bucket handling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  int hash_to_index(unsigned int full_hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    int h = full_hash % _table_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    assert(h >= 0 && h < _table_size, "Illegal hash value");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    return h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // Reverse the order of elements in each of the buckets.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  void reverse();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  // Instance variables
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  int               _table_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  HashtableBucket*  _buckets;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  BasicHashtableEntry* _free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  char*             _first_free_entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  char*             _end_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  int               _entry_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  int               _number_of_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  int               _lookup_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  int               _lookup_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  void verify_lookup_length(double load);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
13087
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   180
  enum {
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   181
    rehash_count = 100,
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   182
    rehash_multiple = 60
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   183
  };
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   184
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  void initialize(int table_size, int entry_size, int number_of_entries);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  // Accessor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  int entry_size() const { return _entry_size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  // The following method is MT-safe and may be used with caution.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  BasicHashtableEntry* bucket(int i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  // The following method is not MT-safe and must be done under lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  BasicHashtableEntry** bucket_addr(int i) { return _buckets[i].entry_addr(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  // Table entry management
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  BasicHashtableEntry* new_entry(unsigned int hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
13087
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   199
  // Check that the table is unbalanced
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   200
  bool check_rehash_table(int count);
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   201
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   202
  // Used when moving the entry to another table
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   203
  // Clean up links, but do not add to free_list
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   204
  void unlink_entry(BasicHashtableEntry* entry) {
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   205
    entry->set_next(NULL);
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   206
    --_number_of_entries;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   207
  }
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   208
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   209
  // Move over freelist and free block for allocation
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   210
  void copy_freelist(BasicHashtable* src) {
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   211
    _free_list = src->_free_list;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   212
    src->_free_list = NULL;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   213
    _first_free_entry = src->_first_free_entry;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   214
    src->_first_free_entry = NULL;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   215
    _end_block = src->_end_block;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   216
    src->_end_block = NULL;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   217
  }
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   218
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   219
  // Free the buckets in this hashtable
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   220
  void free_buckets() {
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   221
    if (NULL != _buckets) {
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   222
      FREE_C_HEAP_ARRAY(HashtableBucket, _buckets);
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   223
      _buckets = NULL;
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   224
    }
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   225
  }
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   226
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
public:
11628
13155c0c00b4 7114376: Make system dictionary hashtable bucket array size configurable
acorn
parents: 8921
diff changeset
   228
  int table_size() { return _table_size; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  void set_entry(int index, BasicHashtableEntry* entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  void add_entry(int index, BasicHashtableEntry* entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  void free_entry(BasicHashtableEntry* entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  int number_of_entries() { return _number_of_entries; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  void verify() PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   241
template <class T> class Hashtable : public BasicHashtable {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  Hashtable(int table_size, int entry_size)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    : BasicHashtable(table_size, entry_size) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  Hashtable(int table_size, int entry_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
                   HashtableBucket* buckets, int number_of_entries)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    : BasicHashtable(table_size, entry_size, buckets, number_of_entries) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  // Debugging
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  void print()               PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  // Reverse the order of elements in each of the buckets. Hashtable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  // entries which refer to objects at a lower address than 'boundary'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  // are separated from those which refer to objects at higher
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  // addresses, and appear first in the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  void reverse(void* boundary = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   263
  unsigned int compute_hash(Symbol* name) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    return (unsigned int) name->identity_hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   267
  int index_for(Symbol* name) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    return hash_to_index(compute_hash(name));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  // Table entry management
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   272
  HashtableEntry<T>* new_entry(unsigned int hashValue, T obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // The following method is MT-safe and may be used with caution.
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   275
  HashtableEntry<T>* bucket(int i) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   276
    return (HashtableEntry<T>*)BasicHashtable::bucket(i);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // The following method is not MT-safe and must be done under lock.
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   280
  HashtableEntry<T>** bucket_addr(int i) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   281
    return (HashtableEntry<T>**)BasicHashtable::bucket_addr(i);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  }
13087
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   283
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   284
  // Function to move these elements into the new table.
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   285
  void move_to(Hashtable<T>* new_table);
673ea6efaf18 7158800: Improve storage of symbol tables
coleenp
parents: 11628
diff changeset
   286
  virtual unsigned int new_hash(T) { ShouldNotReachHere(); return 0; } // should be overridden
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
//  Verions of hashtable where two handles are used to compute the index.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   292
template <class T> class TwoOopHashtable : public Hashtable<T> {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  TwoOopHashtable(int table_size, int entry_size)
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   296
    : Hashtable<T>(table_size, entry_size) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  TwoOopHashtable(int table_size, int entry_size, HashtableBucket* t,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
                  int number_of_entries)
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   300
    : Hashtable<T>(table_size, entry_size, t, number_of_entries) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
public:
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   303
  unsigned int compute_hash(Symbol* name, Handle loader) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    // Be careful with identity_hash(), it can safepoint and if this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    // were one expression, the compiler could choose to unhandle each
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    // oop before calling identity_hash() for either of them.  If the first
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    // causes a GC, the next would fail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    unsigned int name_hash = name->identity_hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    unsigned int loader_hash = loader.is_null() ? 0 : loader->identity_hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    return name_hash ^ loader_hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   313
  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
   314
    return this->hash_to_index(compute_hash(name, loader));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   317
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   318
#endif // SHARE_VM_UTILITIES_HASHTABLE_HPP