annotate src/share/vm/utilities/hashtable.inline.hpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 // Inline function definitions for hashtable.hpp.
duke@0 26
duke@0 27
duke@0 28 // --------------------------------------------------------------------------
duke@0 29 // Hash function
duke@0 30
duke@0 31 // We originally used hashpjw, but hash P(31) gives just as good results
duke@0 32 // and is slighly faster. We would like a hash function that looks at every
duke@0 33 // character, since package names have large common prefixes, and also because
duke@0 34 // hash_or_fail does error checking while iterating.
duke@0 35
duke@0 36 // hash P(31) from Kernighan & Ritchie
duke@0 37
duke@0 38 inline unsigned int Hashtable::hash_symbol(const char* s, int len) {
duke@0 39 unsigned int h = 0;
duke@0 40 while (len-- > 0) {
duke@0 41 h = 31*h + (unsigned) *s;
duke@0 42 s++;
duke@0 43 }
duke@0 44 return h;
duke@0 45 }
duke@0 46
duke@0 47
duke@0 48 // --------------------------------------------------------------------------
duke@0 49
duke@0 50 // Initialize a table.
duke@0 51
duke@0 52 inline BasicHashtable::BasicHashtable(int table_size, int entry_size) {
duke@0 53 // Called on startup, no locking needed
duke@0 54 initialize(table_size, entry_size, 0);
duke@0 55 _buckets = NEW_C_HEAP_ARRAY(HashtableBucket, table_size);
duke@0 56 for (int index = 0; index < _table_size; index++) {
duke@0 57 _buckets[index].clear();
duke@0 58 }
duke@0 59 }
duke@0 60
duke@0 61
duke@0 62 inline BasicHashtable::BasicHashtable(int table_size, int entry_size,
duke@0 63 HashtableBucket* buckets,
duke@0 64 int number_of_entries) {
duke@0 65 // Called on startup, no locking needed
duke@0 66 initialize(table_size, entry_size, number_of_entries);
duke@0 67 _buckets = buckets;
duke@0 68 }
duke@0 69
duke@0 70
duke@0 71 inline void BasicHashtable::initialize(int table_size, int entry_size,
duke@0 72 int number_of_entries) {
duke@0 73 // Called on startup, no locking needed
duke@0 74 _table_size = table_size;
duke@0 75 _entry_size = entry_size;
duke@0 76 _free_list = NULL;
duke@0 77 _first_free_entry = NULL;
duke@0 78 _end_block = NULL;
duke@0 79 _number_of_entries = number_of_entries;
duke@0 80 #ifdef ASSERT
duke@0 81 _lookup_count = 0;
duke@0 82 _lookup_length = 0;
duke@0 83 #endif
duke@0 84 }
duke@0 85
duke@0 86
duke@0 87 // The following method is MT-safe and may be used with caution.
duke@0 88 inline BasicHashtableEntry* BasicHashtable::bucket(int i) {
duke@0 89 return _buckets[i].get_entry();
duke@0 90 }
duke@0 91
duke@0 92
duke@0 93 inline void HashtableBucket::set_entry(BasicHashtableEntry* l) {
duke@0 94 // Warning: Preserve store ordering. The SystemDictionary is read
duke@0 95 // without locks. The new SystemDictionaryEntry must be
duke@0 96 // complete before other threads can be allowed to see it
duke@0 97 // via a store to _buckets[index].
duke@0 98 OrderAccess::release_store_ptr(&_entry, l);
duke@0 99 }
duke@0 100
duke@0 101
duke@0 102 inline BasicHashtableEntry* HashtableBucket::get_entry() const {
duke@0 103 // Warning: Preserve load ordering. The SystemDictionary is read
duke@0 104 // without locks. The new SystemDictionaryEntry must be
duke@0 105 // complete before other threads can be allowed to see it
duke@0 106 // via a store to _buckets[index].
duke@0 107 return (BasicHashtableEntry*) OrderAccess::load_ptr_acquire(&_entry);
duke@0 108 }
duke@0 109
duke@0 110
duke@0 111 inline void BasicHashtable::set_entry(int index, BasicHashtableEntry* entry) {
duke@0 112 _buckets[index].set_entry(entry);
duke@0 113 }
duke@0 114
duke@0 115
duke@0 116 inline void BasicHashtable::add_entry(int index, BasicHashtableEntry* entry) {
duke@0 117 entry->set_next(bucket(index));
duke@0 118 _buckets[index].set_entry(entry);
duke@0 119 ++_number_of_entries;
duke@0 120 }
duke@0 121
duke@0 122 inline void BasicHashtable::free_entry(BasicHashtableEntry* entry) {
duke@0 123 entry->set_next(_free_list);
duke@0 124 _free_list = entry;
duke@0 125 --_number_of_entries;
duke@0 126 }