6934
|
1 /*
|
|
2 * Copyright (c) 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_RESOURCEHASH_HPP
|
|
26 #define SHARE_VM_UTILITIES_RESOURCEHASH_HPP
|
|
27
|
|
28 #include "memory/allocation.hpp"
|
|
29 #include "utilities/top.hpp"
|
|
30
|
|
31 template<typename K> struct ResourceHashtableFns {
|
|
32 typedef unsigned (*hash_fn)(K const&);
|
|
33 typedef bool (*equals_fn)(K const&, K const&);
|
|
34 };
|
|
35
|
|
36 template<typename K> unsigned primitive_hash(const K& k) {
|
|
37 unsigned hash = (unsigned)((uintptr_t)k);
|
|
38 return hash ^ (hash > 3); // just in case we're dealing with aligned ptrs
|
|
39 }
|
|
40
|
|
41 template<typename K> bool primitive_equals(const K& k0, const K& k1) {
|
|
42 return k0 == k1;
|
|
43 }
|
|
44
|
|
45 template<
|
|
46 typename K, typename V,
|
|
47 typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>,
|
|
48 typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>,
|
|
49 unsigned SIZE = 256
|
|
50 >
|
|
51 class ResourceHashtable : public ResourceObj {
|
|
52 private:
|
|
53
|
|
54 class Node : public ResourceObj {
|
|
55 public:
|
|
56 unsigned _hash;
|
|
57 K _key;
|
|
58 V _value;
|
|
59 Node* _next;
|
|
60
|
|
61 Node(unsigned hash, K const& key, V const& value) :
|
|
62 _hash(hash), _key(key), _value(value), _next(NULL) {}
|
|
63 };
|
|
64
|
|
65 Node* _table[SIZE];
|
|
66
|
|
67 // Returns a pointer to where the node where the value would reside if
|
|
68 // it's in the table.
|
|
69 Node** lookup_node(unsigned hash, K const& key) {
|
|
70 unsigned index = hash % SIZE;
|
|
71 Node** ptr = &_table[index];
|
|
72 while (*ptr != NULL) {
|
|
73 Node* node = *ptr;
|
|
74 if (node->_hash == hash && EQUALS(key, node->_key)) {
|
|
75 break;
|
|
76 }
|
|
77 ptr = &(node->_next);
|
|
78 }
|
|
79 return ptr;
|
|
80 }
|
|
81
|
|
82 Node const** lookup_node(unsigned hash, K const& key) const {
|
|
83 return const_cast<Node const**>(
|
|
84 const_cast<ResourceHashtable*>(this)->lookup_node(hash, key));
|
|
85 }
|
|
86
|
|
87 public:
|
|
88 ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); }
|
|
89
|
|
90 bool contains(K const& key) const {
|
|
91 return get(key) != NULL;
|
|
92 }
|
|
93
|
|
94 V* get(K const& key) const {
|
|
95 unsigned hv = HASH(key);
|
|
96 Node const** ptr = lookup_node(hv, key);
|
|
97 if (*ptr != NULL) {
|
|
98 return const_cast<V*>(&(*ptr)->_value);
|
|
99 } else {
|
|
100 return NULL;
|
|
101 }
|
|
102 }
|
|
103
|
|
104 // Inserts or replaces a value in the table
|
|
105 void put(K const& key, V const& value) {
|
|
106 unsigned hv = HASH(key);
|
|
107 Node** ptr = lookup_node(hv, key);
|
|
108 if (*ptr != NULL) {
|
|
109 (*ptr)->_value = value;
|
|
110 } else {
|
|
111 *ptr = new Node(hv, key, value);
|
|
112 }
|
|
113 }
|
|
114
|
|
115 // ITER contains bool do_entry(K const&, V const&), which will be
|
|
116 // called for each entry in the table. If do_entry() returns false,
|
|
117 // the iteration is cancelled.
|
|
118 template<class ITER>
|
|
119 void iterate(ITER* iter) const {
|
|
120 Node* const* bucket = _table;
|
|
121 while (bucket < &_table[SIZE]) {
|
|
122 Node* node = *bucket;
|
|
123 while (node != NULL) {
|
|
124 bool cont = iter->do_entry(node->_key, node->_value);
|
|
125 if (!cont) { return; }
|
|
126 node = node->_next;
|
|
127 }
|
|
128 ++bucket;
|
|
129 }
|
|
130 }
|
|
131 };
|
|
132
|
|
133
|
|
134 #endif // SHARE_VM_UTILITIES_RESOURCEHASH_HPP
|