Wed, 15 Apr 2020 11:49:55 +0800
Merge
1 /*
2 * Copyright (c) 2012, 2015 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 */
25 #ifndef SHARE_VM_UTILITIES_RESOURCEHASH_HPP
26 #define SHARE_VM_UTILITIES_RESOURCEHASH_HPP
28 #include "memory/allocation.hpp"
29 #include "utilities/top.hpp"
31 template<typename K> struct ResourceHashtableFns {
32 typedef unsigned (*hash_fn)(K const&);
33 typedef bool (*equals_fn)(K const&, K const&);
34 };
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 }
41 template<typename K> bool primitive_equals(const K& k0, const K& k1) {
42 return k0 == k1;
43 }
45 template<
46 typename K, typename V,
47 // xlC does not compile this:
48 // http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c
49 //typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>,
50 //typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>,
51 unsigned (*HASH) (K const&) = primitive_hash<K>,
52 bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
53 unsigned SIZE = 256,
54 ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA,
55 MEMFLAGS MEM_TYPE = mtInternal
56 >
57 class ResourceHashtable : public ResourceObj {
58 private:
60 class Node : public ResourceObj {
61 public:
62 unsigned _hash;
63 K _key;
64 V _value;
65 Node* _next;
67 Node(unsigned hash, K const& key, V const& value) :
68 _hash(hash), _key(key), _value(value), _next(NULL) {}
69 };
71 Node* _table[SIZE];
73 // Returns a pointer to where the node where the value would reside if
74 // it's in the table.
75 Node** lookup_node(unsigned hash, K const& key) {
76 unsigned index = hash % SIZE;
77 Node** ptr = &_table[index];
78 while (*ptr != NULL) {
79 Node* node = *ptr;
80 if (node->_hash == hash && EQUALS(key, node->_key)) {
81 break;
82 }
83 ptr = &(node->_next);
84 }
85 return ptr;
86 }
88 Node const** lookup_node(unsigned hash, K const& key) const {
89 return const_cast<Node const**>(
90 const_cast<ResourceHashtable*>(this)->lookup_node(hash, key));
91 }
93 public:
94 ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); }
96 ~ResourceHashtable() {
97 if (ALLOC_TYPE == C_HEAP) {
98 Node* const* bucket = _table;
99 while (bucket < &_table[SIZE]) {
100 Node* node = *bucket;
101 while (node != NULL) {
102 Node* cur = node;
103 node = node->_next;
104 delete cur;
105 }
106 ++bucket;
107 }
108 }
109 }
111 bool contains(K const& key) const {
112 return get(key) != NULL;
113 }
115 V* get(K const& key) const {
116 unsigned hv = HASH(key);
117 Node const** ptr = lookup_node(hv, key);
118 if (*ptr != NULL) {
119 return const_cast<V*>(&(*ptr)->_value);
120 } else {
121 return NULL;
122 }
123 }
125 /**
126 * Inserts or replaces a value in the table.
127 * @return: true: if a new item is added
128 * false: if the item already existed and the value is updated
129 */
130 bool put(K const& key, V const& value) {
131 unsigned hv = HASH(key);
132 Node** ptr = lookup_node(hv, key);
133 if (*ptr != NULL) {
134 (*ptr)->_value = value;
135 return false;
136 } else {
137 *ptr = new (ALLOC_TYPE, MEM_TYPE) Node(hv, key, value);
138 return true;
139 }
140 }
142 bool remove(K const& key) {
143 unsigned hv = HASH(key);
144 Node** ptr = lookup_node(hv, key);
146 Node* node = *ptr;
147 if (node != NULL) {
148 *ptr = node->_next;
149 if (ALLOC_TYPE == C_HEAP) {
150 delete node;
151 }
152 return true;
153 }
154 return false;
155 }
157 // ITER contains bool do_entry(K const&, V const&), which will be
158 // called for each entry in the table. If do_entry() returns false,
159 // the iteration is cancelled.
160 template<class ITER>
161 void iterate(ITER* iter) const {
162 Node* const* bucket = _table;
163 while (bucket < &_table[SIZE]) {
164 Node* node = *bucket;
165 while (node != NULL) {
166 bool cont = iter->do_entry(node->_key, node->_value);
167 if (!cont) { return; }
168 node = node->_next;
169 }
170 ++bucket;
171 }
172 }
174 static size_t node_size() {
175 return sizeof(Node);
176 }
177 };
180 #endif // SHARE_VM_UTILITIES_RESOURCEHASH_HPP