hotspot/src/share/vm/utilities/hashtable.inline.hpp
author apetrusenko
Tue, 10 Feb 2009 18:39:09 +0300
changeset 2013 49e915da0905
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6700941: G1: allocation spec missing for some G1 classes Reviewed-by: tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Inline function definitions for hashtable.hpp.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// --------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Hash function
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// We originally used hashpjw, but hash P(31) gives just as good results
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// and is slighly faster. We would like a hash function that looks at every
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// character, since package names have large common prefixes, and also because
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// hash_or_fail does error checking while iterating.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// hash P(31) from Kernighan & Ritchie
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
inline unsigned int Hashtable::hash_symbol(const char* s, int len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  unsigned int h = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  while (len-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    h = 31*h + (unsigned) *s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    s++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  return h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
// --------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// Initialize a table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
inline BasicHashtable::BasicHashtable(int table_size, int entry_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // Called on startup, no locking needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  initialize(table_size, entry_size, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  _buckets = NEW_C_HEAP_ARRAY(HashtableBucket, table_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  for (int index = 0; index < _table_size; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    _buckets[index].clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
inline BasicHashtable::BasicHashtable(int table_size, int entry_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
                                      HashtableBucket* buckets,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
                                      int number_of_entries) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // Called on startup, no locking needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  initialize(table_size, entry_size, number_of_entries);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  _buckets = buckets;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
inline void BasicHashtable::initialize(int table_size, int entry_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
                                       int number_of_entries) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Called on startup, no locking needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  _table_size = table_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  _entry_size = entry_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  _free_list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  _first_free_entry = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  _end_block = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  _number_of_entries = number_of_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  _lookup_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  _lookup_length = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// The following method is MT-safe and may be used with caution.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
inline BasicHashtableEntry* BasicHashtable::bucket(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  return _buckets[i].get_entry();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
inline void HashtableBucket::set_entry(BasicHashtableEntry* l) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // Warning: Preserve store ordering.  The SystemDictionary is read
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  //          without locks.  The new SystemDictionaryEntry must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  //          complete before other threads can be allowed to see it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  //          via a store to _buckets[index].
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  OrderAccess::release_store_ptr(&_entry, l);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
inline BasicHashtableEntry* HashtableBucket::get_entry() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // Warning: Preserve load ordering.  The SystemDictionary is read
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  //          without locks.  The new SystemDictionaryEntry must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  //          complete before other threads can be allowed to see it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  //          via a store to _buckets[index].
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  return (BasicHashtableEntry*) OrderAccess::load_ptr_acquire(&_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
inline void BasicHashtable::set_entry(int index, BasicHashtableEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  _buckets[index].set_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
inline void BasicHashtable::add_entry(int index, BasicHashtableEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  entry->set_next(bucket(index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  _buckets[index].set_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  ++_number_of_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
inline void BasicHashtable::free_entry(BasicHashtableEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  entry->set_next(_free_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  _free_list = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  --_number_of_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
}