aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_RESOURCEHASH_HPP aoqi@0: #define SHARE_VM_UTILITIES_RESOURCEHASH_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: aoqi@0: template struct ResourceHashtableFns { aoqi@0: typedef unsigned (*hash_fn)(K const&); aoqi@0: typedef bool (*equals_fn)(K const&, K const&); aoqi@0: }; aoqi@0: aoqi@0: template unsigned primitive_hash(const K& k) { aoqi@0: unsigned hash = (unsigned)((uintptr_t)k); aoqi@0: return hash ^ (hash > 3); // just in case we're dealing with aligned ptrs aoqi@0: } aoqi@0: aoqi@0: template bool primitive_equals(const K& k0, const K& k1) { aoqi@0: return k0 == k1; aoqi@0: } aoqi@0: aoqi@0: template< aoqi@0: typename K, typename V, aoqi@0: // xlC does not compile this: aoqi@0: // http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c aoqi@0: //typename ResourceHashtableFns::hash_fn HASH = primitive_hash, aoqi@0: //typename ResourceHashtableFns::equals_fn EQUALS = primitive_equals, aoqi@0: unsigned (*HASH) (K const&) = primitive_hash, aoqi@0: bool (*EQUALS)(K const&, K const&) = primitive_equals, aoqi@0: unsigned SIZE = 256 aoqi@0: > aoqi@0: class ResourceHashtable : public ResourceObj { aoqi@0: private: aoqi@0: aoqi@0: class Node : public ResourceObj { aoqi@0: public: aoqi@0: unsigned _hash; aoqi@0: K _key; aoqi@0: V _value; aoqi@0: Node* _next; aoqi@0: aoqi@0: Node(unsigned hash, K const& key, V const& value) : aoqi@0: _hash(hash), _key(key), _value(value), _next(NULL) {} aoqi@0: }; aoqi@0: aoqi@0: Node* _table[SIZE]; aoqi@0: aoqi@0: // Returns a pointer to where the node where the value would reside if aoqi@0: // it's in the table. aoqi@0: Node** lookup_node(unsigned hash, K const& key) { aoqi@0: unsigned index = hash % SIZE; aoqi@0: Node** ptr = &_table[index]; aoqi@0: while (*ptr != NULL) { aoqi@0: Node* node = *ptr; aoqi@0: if (node->_hash == hash && EQUALS(key, node->_key)) { aoqi@0: break; aoqi@0: } aoqi@0: ptr = &(node->_next); aoqi@0: } aoqi@0: return ptr; aoqi@0: } aoqi@0: aoqi@0: Node const** lookup_node(unsigned hash, K const& key) const { aoqi@0: return const_cast( aoqi@0: const_cast(this)->lookup_node(hash, key)); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); } aoqi@0: aoqi@0: bool contains(K const& key) const { aoqi@0: return get(key) != NULL; aoqi@0: } aoqi@0: aoqi@0: V* get(K const& key) const { aoqi@0: unsigned hv = HASH(key); aoqi@0: Node const** ptr = lookup_node(hv, key); aoqi@0: if (*ptr != NULL) { aoqi@0: return const_cast(&(*ptr)->_value); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Inserts or replaces a value in the table aoqi@0: void put(K const& key, V const& value) { aoqi@0: unsigned hv = HASH(key); aoqi@0: Node** ptr = lookup_node(hv, key); aoqi@0: if (*ptr != NULL) { aoqi@0: (*ptr)->_value = value; aoqi@0: } else { aoqi@0: *ptr = new Node(hv, key, value); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ITER contains bool do_entry(K const&, V const&), which will be aoqi@0: // called for each entry in the table. If do_entry() returns false, aoqi@0: // the iteration is cancelled. aoqi@0: template aoqi@0: void iterate(ITER* iter) const { aoqi@0: Node* const* bucket = _table; aoqi@0: while (bucket < &_table[SIZE]) { aoqi@0: Node* node = *bucket; aoqi@0: while (node != NULL) { aoqi@0: bool cont = iter->do_entry(node->_key, node->_value); aoqi@0: if (!cont) { return; } aoqi@0: node = node->_next; aoqi@0: } aoqi@0: ++bucket; aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_RESOURCEHASH_HPP