author | mr |
Tue, 29 Oct 2019 13:52:04 -0700 | |
changeset 58850 | f4290bf1cc21 |
parent 53547 | 9d1a788dea3d |
permissions | -rw-r--r-- |
14385 | 1 |
/* |
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
52631
diff
changeset
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
14385 | 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 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
52631
diff
changeset
|
25 |
#ifndef SHARE_UTILITIES_RESOURCEHASH_HPP |
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
52631
diff
changeset
|
26 |
#define SHARE_UTILITIES_RESOURCEHASH_HPP |
14385 | 27 |
|
28 |
#include "memory/allocation.hpp" |
|
29 |
||
30 |
template< |
|
31 |
typename K, typename V, |
|
22827 | 32 |
// xlC does not compile this: |
33 |
// http://stackoverflow.com/questions/8532961/template-argument-of-type-that-is-defined-by-inner-typedef-from-other-template-c |
|
34 |
//typename ResourceHashtableFns<K>::hash_fn HASH = primitive_hash<K>, |
|
35 |
//typename ResourceHashtableFns<K>::equals_fn EQUALS = primitive_equals<K>, |
|
36 |
unsigned (*HASH) (K const&) = primitive_hash<K>, |
|
37 |
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
38 |
unsigned SIZE = 256, |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
39 |
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA, |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
40 |
MEMFLAGS MEM_TYPE = mtInternal |
14385 | 41 |
> |
42 |
class ResourceHashtable : public ResourceObj { |
|
43 |
private: |
|
44 |
||
45 |
class Node : public ResourceObj { |
|
46 |
public: |
|
47 |
unsigned _hash; |
|
48 |
K _key; |
|
49 |
V _value; |
|
50 |
Node* _next; |
|
51 |
||
52 |
Node(unsigned hash, K const& key, V const& value) : |
|
53 |
_hash(hash), _key(key), _value(value), _next(NULL) {} |
|
54 |
}; |
|
55 |
||
56 |
Node* _table[SIZE]; |
|
57 |
||
58 |
// Returns a pointer to where the node where the value would reside if |
|
59 |
// it's in the table. |
|
60 |
Node** lookup_node(unsigned hash, K const& key) { |
|
61 |
unsigned index = hash % SIZE; |
|
62 |
Node** ptr = &_table[index]; |
|
63 |
while (*ptr != NULL) { |
|
64 |
Node* node = *ptr; |
|
65 |
if (node->_hash == hash && EQUALS(key, node->_key)) { |
|
66 |
break; |
|
67 |
} |
|
68 |
ptr = &(node->_next); |
|
69 |
} |
|
70 |
return ptr; |
|
71 |
} |
|
72 |
||
73 |
Node const** lookup_node(unsigned hash, K const& key) const { |
|
74 |
return const_cast<Node const**>( |
|
75 |
const_cast<ResourceHashtable*>(this)->lookup_node(hash, key)); |
|
76 |
} |
|
77 |
||
78 |
public: |
|
79 |
ResourceHashtable() { memset(_table, 0, SIZE * sizeof(Node*)); } |
|
80 |
||
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
81 |
~ResourceHashtable() { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
82 |
if (ALLOC_TYPE == C_HEAP) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
83 |
Node* const* bucket = _table; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
84 |
while (bucket < &_table[SIZE]) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
85 |
Node* node = *bucket; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
86 |
while (node != NULL) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
87 |
Node* cur = node; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
88 |
node = node->_next; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
89 |
delete cur; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
90 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
91 |
++bucket; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
92 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
93 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
94 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
95 |
|
14385 | 96 |
bool contains(K const& key) const { |
97 |
return get(key) != NULL; |
|
98 |
} |
|
99 |
||
100 |
V* get(K const& key) const { |
|
101 |
unsigned hv = HASH(key); |
|
102 |
Node const** ptr = lookup_node(hv, key); |
|
103 |
if (*ptr != NULL) { |
|
104 |
return const_cast<V*>(&(*ptr)->_value); |
|
105 |
} else { |
|
106 |
return NULL; |
|
107 |
} |
|
108 |
} |
|
109 |
||
23187
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
110 |
/** |
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
111 |
* Inserts or replaces a value in the table. |
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
112 |
* @return: true: if a new item is added |
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
113 |
* false: if the item already existed and the value is updated |
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
114 |
*/ |
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
115 |
bool put(K const& key, V const& value) { |
14385 | 116 |
unsigned hv = HASH(key); |
117 |
Node** ptr = lookup_node(hv, key); |
|
118 |
if (*ptr != NULL) { |
|
119 |
(*ptr)->_value = value; |
|
23187
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
120 |
return false; |
14385 | 121 |
} else { |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
122 |
*ptr = new (ALLOC_TYPE, MEM_TYPE) Node(hv, key, value); |
23187
0f438571f278
8035946: Use ResourceHashtable for dependency checking
anoll
parents:
22827
diff
changeset
|
123 |
return true; |
14385 | 124 |
} |
125 |
} |
|
126 |
||
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
127 |
bool remove(K const& key) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
128 |
unsigned hv = HASH(key); |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
129 |
Node** ptr = lookup_node(hv, key); |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
130 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
131 |
Node* node = *ptr; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
132 |
if (node != NULL) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
133 |
*ptr = node->_next; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
134 |
if (ALLOC_TYPE == C_HEAP) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
135 |
delete node; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
136 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
137 |
return true; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
138 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
139 |
return false; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
140 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
23187
diff
changeset
|
141 |
|
14385 | 142 |
// ITER contains bool do_entry(K const&, V const&), which will be |
143 |
// called for each entry in the table. If do_entry() returns false, |
|
144 |
// the iteration is cancelled. |
|
145 |
template<class ITER> |
|
146 |
void iterate(ITER* iter) const { |
|
147 |
Node* const* bucket = _table; |
|
148 |
while (bucket < &_table[SIZE]) { |
|
149 |
Node* node = *bucket; |
|
150 |
while (node != NULL) { |
|
151 |
bool cont = iter->do_entry(node->_key, node->_value); |
|
152 |
if (!cont) { return; } |
|
153 |
node = node->_next; |
|
154 |
} |
|
155 |
++bucket; |
|
156 |
} |
|
157 |
} |
|
158 |
}; |
|
159 |
||
160 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
52631
diff
changeset
|
161 |
#endif // SHARE_UTILITIES_RESOURCEHASH_HPP |