src/share/vm/utilities/resourceHash.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_RESOURCEHASH_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_RESOURCEHASH_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/top.hpp"
aoqi@0 30
aoqi@0 31 template<typename K> struct ResourceHashtableFns {
aoqi@0 32 typedef unsigned (*hash_fn)(K const&);
aoqi@0 33 typedef bool (*equals_fn)(K const&, K const&);
aoqi@0 34 };
aoqi@0 35
aoqi@0 36 template<typename K> unsigned primitive_hash(const K& k) {
aoqi@0 37 unsigned hash = (unsigned)((uintptr_t)k);
aoqi@0 38 return hash ^ (hash > 3); // just in case we're dealing with aligned ptrs
aoqi@0 39 }
aoqi@0 40
aoqi@0 41 template<typename K> bool primitive_equals(const K& k0, const K& k1) {
aoqi@0 42 return k0 == k1;
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 template<
aoqi@0 46 typename K, typename V,
aoqi@0 47 // xlC does not compile this:
aoqi@0 48 // http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c
aoqi@0 49 //typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>,
aoqi@0 50 //typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>,
aoqi@0 51 unsigned (*HASH) (K const&) = primitive_hash<K>,
aoqi@0 52 bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
aoqi@0 53 unsigned SIZE = 256
aoqi@0 54 >
aoqi@0 55 class ResourceHashtable : public ResourceObj {
aoqi@0 56 private:
aoqi@0 57
aoqi@0 58 class Node : public ResourceObj {
aoqi@0 59 public:
aoqi@0 60 unsigned _hash;
aoqi@0 61 K _key;
aoqi@0 62 V _value;
aoqi@0 63 Node* _next;
aoqi@0 64
aoqi@0 65 Node(unsigned hash, K const& key, V const& value) :
aoqi@0 66 _hash(hash), _key(key), _value(value), _next(NULL) {}
aoqi@0 67 };
aoqi@0 68
aoqi@0 69 Node* _table[SIZE];
aoqi@0 70
aoqi@0 71 // Returns a pointer to where the node where the value would reside if
aoqi@0 72 // it's in the table.
aoqi@0 73 Node** lookup_node(unsigned hash, K const& key) {
aoqi@0 74 unsigned index = hash % SIZE;
aoqi@0 75 Node** ptr = &_table[index];
aoqi@0 76 while (*ptr != NULL) {
aoqi@0 77 Node* node = *ptr;
aoqi@0 78 if (node->_hash == hash && EQUALS(key, node->_key)) {
aoqi@0 79 break;
aoqi@0 80 }
aoqi@0 81 ptr = &(node->_next);
aoqi@0 82 }
aoqi@0 83 return ptr;
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 Node const** lookup_node(unsigned hash, K const& key) const {
aoqi@0 87 return const_cast<Node const**>(
aoqi@0 88 const_cast<ResourceHashtable*>(this)->lookup_node(hash, key));
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 public:
aoqi@0 92 ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); }
aoqi@0 93
aoqi@0 94 bool contains(K const& key) const {
aoqi@0 95 return get(key) != NULL;
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 V* get(K const& key) const {
aoqi@0 99 unsigned hv = HASH(key);
aoqi@0 100 Node const** ptr = lookup_node(hv, key);
aoqi@0 101 if (*ptr != NULL) {
aoqi@0 102 return const_cast<V*>(&(*ptr)->_value);
aoqi@0 103 } else {
aoqi@0 104 return NULL;
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 // Inserts or replaces a value in the table
aoqi@0 109 void put(K const& key, V const& value) {
aoqi@0 110 unsigned hv = HASH(key);
aoqi@0 111 Node** ptr = lookup_node(hv, key);
aoqi@0 112 if (*ptr != NULL) {
aoqi@0 113 (*ptr)->_value = value;
aoqi@0 114 } else {
aoqi@0 115 *ptr = new Node(hv, key, value);
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 // ITER contains bool do_entry(K const&, V const&), which will be
aoqi@0 120 // called for each entry in the table. If do_entry() returns false,
aoqi@0 121 // the iteration is cancelled.
aoqi@0 122 template<class ITER>
aoqi@0 123 void iterate(ITER* iter) const {
aoqi@0 124 Node* const* bucket = _table;
aoqi@0 125 while (bucket < &_table[SIZE]) {
aoqi@0 126 Node* node = *bucket;
aoqi@0 127 while (node != NULL) {
aoqi@0 128 bool cont = iter->do_entry(node->_key, node->_value);
aoqi@0 129 if (!cont) { return; }
aoqi@0 130 node = node->_next;
aoqi@0 131 }
aoqi@0 132 ++bucket;
aoqi@0 133 }
aoqi@0 134 }
aoqi@0 135 };
aoqi@0 136
aoqi@0 137
aoqi@0 138 #endif // SHARE_VM_UTILITIES_RESOURCEHASH_HPP

mercurial