src/share/vm/utilities/hashtable.inline.hpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/hashtable.inline.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,126 @@
     1.4 +/*
     1.5 + * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// Inline function definitions for hashtable.hpp.
    1.29 +
    1.30 +
    1.31 +// --------------------------------------------------------------------------
    1.32 +// Hash function
    1.33 +
    1.34 +// We originally used hashpjw, but hash P(31) gives just as good results
    1.35 +// and is slighly faster. We would like a hash function that looks at every
    1.36 +// character, since package names have large common prefixes, and also because
    1.37 +// hash_or_fail does error checking while iterating.
    1.38 +
    1.39 +// hash P(31) from Kernighan & Ritchie
    1.40 +
    1.41 +inline unsigned int Hashtable::hash_symbol(const char* s, int len) {
    1.42 +  unsigned int h = 0;
    1.43 +  while (len-- > 0) {
    1.44 +    h = 31*h + (unsigned) *s;
    1.45 +    s++;
    1.46 +  }
    1.47 +  return h;
    1.48 +}
    1.49 +
    1.50 +
    1.51 +// --------------------------------------------------------------------------
    1.52 +
    1.53 +// Initialize a table.
    1.54 +
    1.55 +inline BasicHashtable::BasicHashtable(int table_size, int entry_size) {
    1.56 +  // Called on startup, no locking needed
    1.57 +  initialize(table_size, entry_size, 0);
    1.58 +  _buckets = NEW_C_HEAP_ARRAY(HashtableBucket, table_size);
    1.59 +  for (int index = 0; index < _table_size; index++) {
    1.60 +    _buckets[index].clear();
    1.61 +  }
    1.62 +}
    1.63 +
    1.64 +
    1.65 +inline BasicHashtable::BasicHashtable(int table_size, int entry_size,
    1.66 +                                      HashtableBucket* buckets,
    1.67 +                                      int number_of_entries) {
    1.68 +  // Called on startup, no locking needed
    1.69 +  initialize(table_size, entry_size, number_of_entries);
    1.70 +  _buckets = buckets;
    1.71 +}
    1.72 +
    1.73 +
    1.74 +inline void BasicHashtable::initialize(int table_size, int entry_size,
    1.75 +                                       int number_of_entries) {
    1.76 +  // Called on startup, no locking needed
    1.77 +  _table_size = table_size;
    1.78 +  _entry_size = entry_size;
    1.79 +  _free_list = NULL;
    1.80 +  _first_free_entry = NULL;
    1.81 +  _end_block = NULL;
    1.82 +  _number_of_entries = number_of_entries;
    1.83 +#ifdef ASSERT
    1.84 +  _lookup_count = 0;
    1.85 +  _lookup_length = 0;
    1.86 +#endif
    1.87 +}
    1.88 +
    1.89 +
    1.90 +// The following method is MT-safe and may be used with caution.
    1.91 +inline BasicHashtableEntry* BasicHashtable::bucket(int i) {
    1.92 +  return _buckets[i].get_entry();
    1.93 +}
    1.94 +
    1.95 +
    1.96 +inline void HashtableBucket::set_entry(BasicHashtableEntry* l) {
    1.97 +  // Warning: Preserve store ordering.  The SystemDictionary is read
    1.98 +  //          without locks.  The new SystemDictionaryEntry must be
    1.99 +  //          complete before other threads can be allowed to see it
   1.100 +  //          via a store to _buckets[index].
   1.101 +  OrderAccess::release_store_ptr(&_entry, l);
   1.102 +}
   1.103 +
   1.104 +
   1.105 +inline BasicHashtableEntry* HashtableBucket::get_entry() const {
   1.106 +  // Warning: Preserve load ordering.  The SystemDictionary is read
   1.107 +  //          without locks.  The new SystemDictionaryEntry must be
   1.108 +  //          complete before other threads can be allowed to see it
   1.109 +  //          via a store to _buckets[index].
   1.110 +  return (BasicHashtableEntry*) OrderAccess::load_ptr_acquire(&_entry);
   1.111 +}
   1.112 +
   1.113 +
   1.114 +inline void BasicHashtable::set_entry(int index, BasicHashtableEntry* entry) {
   1.115 +  _buckets[index].set_entry(entry);
   1.116 +}
   1.117 +
   1.118 +
   1.119 +inline void BasicHashtable::add_entry(int index, BasicHashtableEntry* entry) {
   1.120 +  entry->set_next(bucket(index));
   1.121 +  _buckets[index].set_entry(entry);
   1.122 +  ++_number_of_entries;
   1.123 +}
   1.124 +
   1.125 +inline void BasicHashtable::free_entry(BasicHashtableEntry* entry) {
   1.126 +  entry->set_next(_free_list);
   1.127 +  _free_list = entry;
   1.128 +  --_number_of_entries;
   1.129 +}

mercurial