1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_HASHTABLE_INLINE_HPP
  26 #define SHARE_VM_UTILITIES_HASHTABLE_INLINE_HPP
  27 
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/orderAccess.inline.hpp"
  30 #include "utilities/hashtable.hpp"
  31 #include "utilities/dtrace.hpp"
  32 
  33 // Inline function definitions for hashtable.hpp.
  34 
  35 // --------------------------------------------------------------------------
  36 
  37 // Initialize a table.
  38 
  39 template <MEMFLAGS F> inline BasicHashtable<F>::BasicHashtable(int table_size, int entry_size) {
  40   // Called on startup, no locking needed
  41   initialize(table_size, entry_size, 0);
  42   _buckets = NEW_C_HEAP_ARRAY2(HashtableBucket<F>, table_size, F, CURRENT_PC);
  43   for (int index = 0; index < _table_size; index++) {
  44     _buckets[index].clear();
  45   }
  46   _stats_rate = TableRateStatistics();
  47 }
  48 
  49 
  50 template <MEMFLAGS F> inline BasicHashtable<F>::BasicHashtable(int table_size, int entry_size,
  51                                       HashtableBucket<F>* buckets,
  52                                       int number_of_entries) {
  53   // Called on startup, no locking needed
  54   initialize(table_size, entry_size, number_of_entries);
  55   _buckets = buckets;
  56   _stats_rate = TableRateStatistics();
  57 }
  58 
  59 
  60 template <MEMFLAGS F> inline void BasicHashtable<F>::initialize(int table_size, int entry_size,
  61                                        int number_of_entries) {
  62   // Called on startup, no locking needed
  63   _table_size = table_size;
  64   _entry_size = entry_size;
  65   _free_list = NULL;
  66   _first_free_entry = NULL;
  67   _end_block = NULL;
  68   _number_of_entries = number_of_entries;
  69 #ifdef ASSERT
  70   _lookup_count = 0;
  71   _lookup_length = 0;
  72 #endif
  73 }
  74 
  75 
  76 // The following method is MT-safe and may be used with caution.
  77 template <MEMFLAGS F> inline BasicHashtableEntry<F>* BasicHashtable<F>::bucket(int i) {
  78   return _buckets[i].get_entry();
  79 }
  80 
  81 
  82 template <MEMFLAGS F> inline void HashtableBucket<F>::set_entry(BasicHashtableEntry<F>* l) {
  83   // Warning: Preserve store ordering.  The SystemDictionary is read
  84   //          without locks.  The new SystemDictionaryEntry must be
  85   //          complete before other threads can be allowed to see it
  86   //          via a store to _buckets[index].
  87   OrderAccess::release_store_ptr(&_entry, l);
  88 }
  89 
  90 
  91 template <MEMFLAGS F> inline BasicHashtableEntry<F>* HashtableBucket<F>::get_entry() const {
  92   // Warning: Preserve load ordering.  The SystemDictionary is read
  93   //          without locks.  The new SystemDictionaryEntry must be
  94   //          complete before other threads can be allowed to see it
  95   //          via a store to _buckets[index].
  96   return (BasicHashtableEntry<F>*) OrderAccess::load_ptr_acquire(&_entry);
  97 }
  98 
  99 
 100 template <MEMFLAGS F> inline void BasicHashtable<F>::set_entry(int index, BasicHashtableEntry<F>* entry) {
 101   _buckets[index].set_entry(entry);
 102   if (entry != NULL) {
 103     JFR_ONLY(_stats_rate.add();)
 104   } else {
 105     JFR_ONLY(_stats_rate.remove();)
 106   }
 107 }
 108 
 109 
 110 template <MEMFLAGS F> inline void BasicHashtable<F>::add_entry(int index, BasicHashtableEntry<F>* entry) {
 111   entry->set_next(bucket(index));
 112   _buckets[index].set_entry(entry);
 113   ++_number_of_entries;
 114   JFR_ONLY(_stats_rate.add();)
 115 }
 116 
 117 template <MEMFLAGS F> inline void BasicHashtable<F>::free_entry(BasicHashtableEntry<F>* entry) {
 118   entry->set_next(_free_list);
 119   _free_list = entry;
 120   --_number_of_entries;
 121   JFR_ONLY(_stats_rate.remove();)
 122 }
 123 
 124 #endif // SHARE_VM_UTILITIES_HASHTABLE_INLINE_HPP