duke@435: /* acorn@3491: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_UTILITIES_HASHTABLE_HPP stefank@2314: #define SHARE_VM_UTILITIES_HASHTABLE_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "oops/oop.hpp" coleenp@2497: #include "oops/symbol.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: duke@435: // This is a generic hashtable, designed to be used for the symbol duke@435: // and string tables. duke@435: // duke@435: // It is implemented as an open hash table with a fixed number of buckets. duke@435: // duke@435: // %note: duke@435: // - TableEntrys are allocated in blocks to reduce the space overhead. duke@435: duke@435: duke@435: zgu@3900: template class BasicHashtableEntry : public CHeapObj { duke@435: friend class VMStructs; duke@435: private: duke@435: unsigned int _hash; // 32-bit hash for item duke@435: duke@435: // Link to next element in the linked list for this bucket. EXCEPT duke@435: // bit 0 set indicates that this entry is shared and must not be duke@435: // unlinked from the table. Bit 0 is set during the dumping of the duke@435: // archive. Since shared entries are immutable, _next fields in the duke@435: // shared entries will not change. New entries will always be duke@435: // unshared and since pointers are align, bit 0 will always remain 0 duke@435: // with no extra effort. zgu@3900: BasicHashtableEntry* _next; duke@435: duke@435: // Windows IA64 compiler requires subclasses to be able to access these duke@435: protected: duke@435: // Entry objects should not be created, they should be taken from the duke@435: // free list with BasicHashtable.new_entry(). duke@435: BasicHashtableEntry() { ShouldNotReachHere(); } duke@435: // Entry objects should not be destroyed. They should be placed on duke@435: // the free list instead with BasicHashtable.free_entry(). duke@435: ~BasicHashtableEntry() { ShouldNotReachHere(); } duke@435: duke@435: public: duke@435: duke@435: unsigned int hash() const { return _hash; } duke@435: void set_hash(unsigned int hash) { _hash = hash; } duke@435: unsigned int* hash_addr() { return &_hash; } duke@435: zgu@3900: static BasicHashtableEntry* make_ptr(BasicHashtableEntry* p) { duke@435: return (BasicHashtableEntry*)((intptr_t)p & -2); duke@435: } duke@435: zgu@3900: BasicHashtableEntry* next() const { duke@435: return make_ptr(_next); duke@435: } duke@435: zgu@3900: void set_next(BasicHashtableEntry* next) { duke@435: _next = next; duke@435: } duke@435: zgu@3900: BasicHashtableEntry** next_addr() { duke@435: return &_next; duke@435: } duke@435: duke@435: bool is_shared() const { duke@435: return ((intptr_t)_next & 1) != 0; duke@435: } duke@435: duke@435: void set_shared() { zgu@3900: _next = (BasicHashtableEntry*)((intptr_t)_next | 1); duke@435: } duke@435: }; duke@435: duke@435: duke@435: zgu@3900: template class HashtableEntry : public BasicHashtableEntry { duke@435: friend class VMStructs; duke@435: private: coleenp@2497: T _literal; // ref to item in table. duke@435: duke@435: public: duke@435: // Literal coleenp@2497: T literal() const { return _literal; } coleenp@2497: T* literal_addr() { return &_literal; } coleenp@2497: void set_literal(T s) { _literal = s; } duke@435: duke@435: HashtableEntry* next() const { zgu@3900: return (HashtableEntry*)BasicHashtableEntry::next(); duke@435: } duke@435: HashtableEntry** next_addr() { zgu@3900: return (HashtableEntry**)BasicHashtableEntry::next_addr(); duke@435: } duke@435: }; duke@435: duke@435: duke@435: zgu@3900: template class HashtableBucket : public CHeapObj { duke@435: friend class VMStructs; duke@435: private: duke@435: // Instance variable zgu@3900: BasicHashtableEntry* _entry; duke@435: duke@435: public: duke@435: // Accessing duke@435: void clear() { _entry = NULL; } duke@435: duke@435: // The following methods use order access methods to avoid race duke@435: // conditions in multiprocessor systems. zgu@3900: BasicHashtableEntry* get_entry() const; zgu@3900: void set_entry(BasicHashtableEntry* l); duke@435: duke@435: // The following method is not MT-safe and must be done under lock. zgu@3900: BasicHashtableEntry** entry_addr() { return &_entry; } duke@435: }; duke@435: duke@435: zgu@3900: template class BasicHashtable : public CHeapObj { duke@435: friend class VMStructs; duke@435: duke@435: public: duke@435: BasicHashtable(int table_size, int entry_size); duke@435: BasicHashtable(int table_size, int entry_size, zgu@3900: HashtableBucket* buckets, int number_of_entries); duke@435: duke@435: // Sharing support. duke@435: void copy_buckets(char** top, char* end); duke@435: void copy_table(char** top, char* end); duke@435: duke@435: // Bucket handling duke@435: int hash_to_index(unsigned int full_hash) { duke@435: int h = full_hash % _table_size; duke@435: assert(h >= 0 && h < _table_size, "Illegal hash value"); duke@435: return h; duke@435: } duke@435: duke@435: // Reverse the order of elements in each of the buckets. duke@435: void reverse(); duke@435: duke@435: private: duke@435: // Instance variables duke@435: int _table_size; zgu@3900: HashtableBucket* _buckets; zgu@3900: BasicHashtableEntry* _free_list; duke@435: char* _first_free_entry; duke@435: char* _end_block; duke@435: int _entry_size; duke@435: int _number_of_entries; duke@435: duke@435: protected: duke@435: duke@435: #ifdef ASSERT duke@435: int _lookup_count; duke@435: int _lookup_length; duke@435: void verify_lookup_length(double load); duke@435: #endif duke@435: coleenp@3865: enum { coleenp@3865: rehash_count = 100, coleenp@3865: rehash_multiple = 60 coleenp@3865: }; coleenp@3865: duke@435: void initialize(int table_size, int entry_size, int number_of_entries); duke@435: duke@435: // Accessor duke@435: int entry_size() const { return _entry_size; } duke@435: duke@435: // The following method is MT-safe and may be used with caution. zgu@3900: BasicHashtableEntry* bucket(int i); duke@435: duke@435: // The following method is not MT-safe and must be done under lock. zgu@3900: BasicHashtableEntry** bucket_addr(int i) { return _buckets[i].entry_addr(); } duke@435: duke@435: // Table entry management zgu@3900: BasicHashtableEntry* new_entry(unsigned int hashValue); duke@435: coleenp@3865: // Check that the table is unbalanced coleenp@3865: bool check_rehash_table(int count); coleenp@3865: coleenp@3865: // Used when moving the entry to another table coleenp@3865: // Clean up links, but do not add to free_list zgu@3900: void unlink_entry(BasicHashtableEntry* entry) { coleenp@3865: entry->set_next(NULL); coleenp@3865: --_number_of_entries; coleenp@3865: } coleenp@3865: coleenp@3865: // Move over freelist and free block for allocation coleenp@3865: void copy_freelist(BasicHashtable* src) { coleenp@3865: _free_list = src->_free_list; coleenp@3865: src->_free_list = NULL; coleenp@3865: _first_free_entry = src->_first_free_entry; coleenp@3865: src->_first_free_entry = NULL; coleenp@3865: _end_block = src->_end_block; coleenp@3865: src->_end_block = NULL; coleenp@3865: } coleenp@3865: coleenp@3865: // Free the buckets in this hashtable coleenp@3875: void free_buckets(); coleenp@3865: duke@435: public: acorn@3491: int table_size() { return _table_size; } zgu@3900: void set_entry(int index, BasicHashtableEntry* entry); duke@435: zgu@3900: void add_entry(int index, BasicHashtableEntry* entry); duke@435: zgu@3900: void free_entry(BasicHashtableEntry* entry); duke@435: duke@435: int number_of_entries() { return _number_of_entries; } duke@435: duke@435: void verify() PRODUCT_RETURN; duke@435: }; duke@435: duke@435: zgu@3900: template class Hashtable : public BasicHashtable { duke@435: friend class VMStructs; duke@435: duke@435: public: duke@435: Hashtable(int table_size, int entry_size) zgu@3900: : BasicHashtable(table_size, entry_size) { } duke@435: duke@435: Hashtable(int table_size, int entry_size, zgu@3900: HashtableBucket* buckets, int number_of_entries) zgu@3900: : BasicHashtable(table_size, entry_size, buckets, number_of_entries) { } duke@435: duke@435: // Debugging duke@435: void print() PRODUCT_RETURN; duke@435: duke@435: // Reverse the order of elements in each of the buckets. Hashtable duke@435: // entries which refer to objects at a lower address than 'boundary' duke@435: // are separated from those which refer to objects at higher duke@435: // addresses, and appear first in the list. duke@435: void reverse(void* boundary = NULL); duke@435: duke@435: protected: duke@435: coleenp@2497: unsigned int compute_hash(Symbol* name) { duke@435: return (unsigned int) name->identity_hash(); duke@435: } duke@435: coleenp@2497: int index_for(Symbol* name) { duke@435: return hash_to_index(compute_hash(name)); duke@435: } duke@435: duke@435: // Table entry management zgu@3900: HashtableEntry* new_entry(unsigned int hashValue, T obj); duke@435: duke@435: // The following method is MT-safe and may be used with caution. zgu@3900: HashtableEntry* bucket(int i) { zgu@3900: return (HashtableEntry*)BasicHashtable::bucket(i); duke@435: } duke@435: duke@435: // The following method is not MT-safe and must be done under lock. zgu@3900: HashtableEntry** bucket_addr(int i) { zgu@3900: return (HashtableEntry**)BasicHashtable::bucket_addr(i); duke@435: } coleenp@3865: coleenp@3865: // Function to move these elements into the new table. zgu@3900: void move_to(Hashtable* new_table); coleenp@3904: static bool use_alternate_hashcode() { return _seed != 0; } coleenp@3904: static jint seed() { return _seed; } coleenp@3904: coleenp@3904: private: coleenp@3904: static jint _seed; coleenp@3904: coleenp@3904: unsigned int new_hash(Symbol* s); coleenp@3904: unsigned int new_hash(oop string); duke@435: }; duke@435: duke@435: duke@435: // Verions of hashtable where two handles are used to compute the index. duke@435: zgu@3900: template class TwoOopHashtable : public Hashtable { duke@435: friend class VMStructs; duke@435: protected: duke@435: TwoOopHashtable(int table_size, int entry_size) zgu@3900: : Hashtable(table_size, entry_size) {} duke@435: zgu@3900: TwoOopHashtable(int table_size, int entry_size, HashtableBucket* t, duke@435: int number_of_entries) zgu@3900: : Hashtable(table_size, entry_size, t, number_of_entries) {} duke@435: duke@435: public: coleenp@2497: unsigned int compute_hash(Symbol* name, Handle loader) { duke@435: // Be careful with identity_hash(), it can safepoint and if this duke@435: // were one expression, the compiler could choose to unhandle each duke@435: // oop before calling identity_hash() for either of them. If the first duke@435: // causes a GC, the next would fail. duke@435: unsigned int name_hash = name->identity_hash(); duke@435: unsigned int loader_hash = loader.is_null() ? 0 : loader->identity_hash(); duke@435: return name_hash ^ loader_hash; duke@435: } duke@435: coleenp@2497: int index_for(Symbol* name, Handle loader) { coleenp@2547: return this->hash_to_index(compute_hash(name, loader)); duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_HASHTABLE_HPP