author | alanb |
Thu, 01 Dec 2016 15:54:39 +0000 | |
changeset 42309 | 21f9f3fbc302 |
parent 41668 | e9c6bbf513e5 |
permissions | -rw-r--r-- |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
1 |
/* |
41668 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
4 |
* |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
8 |
* |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
13 |
* accompanied this code). |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
14 |
* |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
18 |
* |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
21 |
* questions. |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
22 |
*/ |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
23 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
24 |
#include "precompiled.hpp" |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
25 |
#include "memory/allocation.hpp" |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
26 |
#include "memory/resourceArea.hpp" |
41668 | 27 |
#include "unittest.hpp" |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
28 |
#include "utilities/debug.hpp" |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
29 |
#include "utilities/resourceHash.hpp" |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
30 |
|
41668 | 31 |
class CommonResourceHashtableTest : public ::testing::Test { |
32 |
protected: |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
33 |
typedef void* K; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
34 |
typedef int V; |
41668 | 35 |
const static MEMFLAGS MEM_TYPE = mtInternal; |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
36 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
37 |
static unsigned identity_hash(const K& k) { |
41668 | 38 |
return (unsigned) (uintptr_t) k; |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
39 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
40 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
41 |
static unsigned bad_hash(const K& k) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
42 |
return 1; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
43 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
44 |
|
41668 | 45 |
static void* as_K(uintptr_t val) { |
46 |
return (void*) val; |
|
47 |
} |
|
48 |
||
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
49 |
class EqualityTestIter { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
50 |
public: |
41668 | 51 |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
52 |
bool do_entry(K const& k, V const& v) { |
41668 | 53 |
if ((uintptr_t) k != (uintptr_t) v) { |
54 |
EXPECT_EQ((uintptr_t) k, (uintptr_t) v); |
|
55 |
return false; |
|
56 |
} else { |
|
57 |
return true; // continue iteration |
|
58 |
} |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
59 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
60 |
}; |
41668 | 61 |
}; |
62 |
||
63 |
class SmallResourceHashtableTest : public CommonResourceHashtableTest { |
|
64 |
protected: |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
65 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
66 |
template< |
41668 | 67 |
unsigned (*HASH) (K const&) = primitive_hash<K>, |
68 |
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:
diff
changeset
|
69 |
unsigned SIZE = 256, |
41668 | 70 |
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
71 |
> |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
72 |
class Runner : public AllStatic { |
41668 | 73 |
public: |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
74 |
|
41668 | 75 |
static void test(V step) { |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
76 |
EqualityTestIter et; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
77 |
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
78 |
|
41668 | 79 |
ASSERT_FALSE(rh.contains(as_K(step))); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
80 |
|
41668 | 81 |
ASSERT_TRUE(rh.put(as_K(step), step)); |
82 |
ASSERT_TRUE(rh.contains(as_K(step))); |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
83 |
|
41668 | 84 |
ASSERT_FALSE(rh.put(as_K(step), step)); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
85 |
|
41668 | 86 |
ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step)); |
87 |
ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step)); |
|
88 |
ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step)); |
|
89 |
ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step)); |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
90 |
|
41668 | 91 |
ASSERT_FALSE(rh.remove(as_K(0x0))); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
92 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
93 |
rh.iterate(&et); |
41668 | 94 |
if (::testing::Test::HasFailure()) { |
95 |
return; |
|
96 |
} |
|
97 |
||
98 |
ASSERT_TRUE(rh.remove(as_K(step))); |
|
99 |
rh.iterate(&et); |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
100 |
} |
41668 | 101 |
}; |
102 |
}; |
|
103 |
||
104 |
TEST_VM_F(SmallResourceHashtableTest, default) { |
|
105 |
ResourceMark rm; |
|
106 |
Runner<>::test(0x1); |
|
107 |
} |
|
108 |
||
109 |
TEST_VM_F(SmallResourceHashtableTest, default_shifted) { |
|
110 |
ResourceMark rm; |
|
111 |
Runner<>::test(0x10); |
|
112 |
} |
|
113 |
||
114 |
TEST_VM_F(SmallResourceHashtableTest, bad_hash) { |
|
115 |
ResourceMark rm; |
|
116 |
Runner<bad_hash>::test(0x1); |
|
117 |
} |
|
118 |
||
119 |
TEST_VM_F(SmallResourceHashtableTest, bad_hash_shifted) { |
|
120 |
ResourceMark rm; |
|
121 |
Runner<bad_hash>::test(0x10); |
|
122 |
} |
|
123 |
||
124 |
TEST_VM_F(SmallResourceHashtableTest, identity_hash) { |
|
125 |
ResourceMark rm; |
|
126 |
Runner<identity_hash>::test(0x1); |
|
127 |
} |
|
128 |
||
129 |
TEST_VM_F(SmallResourceHashtableTest, identity_hash_shifted) { |
|
130 |
ResourceMark rm; |
|
131 |
Runner<identity_hash>::test(0x10); |
|
132 |
} |
|
133 |
||
134 |
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm) { |
|
135 |
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1); |
|
136 |
} |
|
137 |
||
138 |
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) { |
|
139 |
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10); |
|
140 |
} |
|
141 |
||
142 |
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm) { |
|
143 |
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1); |
|
144 |
} |
|
145 |
||
146 |
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) { |
|
147 |
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10); |
|
148 |
} |
|
149 |
||
150 |
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm) { |
|
151 |
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1); |
|
152 |
} |
|
153 |
||
154 |
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) { |
|
155 |
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10); |
|
156 |
} |
|
157 |
||
158 |
class GenericResourceHashtableTest : public CommonResourceHashtableTest { |
|
159 |
protected: |
|
160 |
||
161 |
template< |
|
162 |
unsigned (*HASH) (K const&) = primitive_hash<K>, |
|
163 |
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, |
|
164 |
unsigned SIZE = 256, |
|
165 |
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA |
|
166 |
> |
|
167 |
class Runner : public AllStatic { |
|
168 |
public: |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
169 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
170 |
static void test(unsigned num_elements = SIZE) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
171 |
EqualityTestIter et; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
172 |
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
173 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
174 |
for (uintptr_t i = 0; i < num_elements; ++i) { |
41668 | 175 |
ASSERT_TRUE(rh.put(as_K(i), i)); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
176 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
177 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
178 |
rh.iterate(&et); |
41668 | 179 |
if (::testing::Test::HasFailure()) { |
180 |
return; |
|
181 |
} |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
182 |
|
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
183 |
for (uintptr_t i = num_elements; i > 0; --i) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
184 |
uintptr_t index = i - 1; |
41668 | 185 |
ASSERT_TRUE((rh.remove(as_K(index)))); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
186 |
} |
41668 | 187 |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
188 |
rh.iterate(&et); |
41668 | 189 |
if (::testing::Test::HasFailure()) { |
190 |
return; |
|
191 |
} |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
192 |
for (uintptr_t i = num_elements; i > 0; --i) { |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
193 |
uintptr_t index = i - 1; |
41668 | 194 |
ASSERT_FALSE(rh.remove(as_K(index))); |
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
195 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
196 |
rh.iterate(&et); |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
197 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
198 |
}; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
199 |
}; |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
200 |
|
41668 | 201 |
TEST_VM_F(GenericResourceHashtableTest, default) { |
202 |
ResourceMark rm; |
|
203 |
Runner<>::test(); |
|
204 |
} |
|
205 |
||
206 |
TEST_VM_F(GenericResourceHashtableTest, bad_hash) { |
|
207 |
ResourceMark rm; |
|
208 |
Runner<bad_hash>::test(); |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
209 |
} |
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
210 |
|
41668 | 211 |
TEST_VM_F(GenericResourceHashtableTest, identity_hash) { |
212 |
ResourceMark rm; |
|
213 |
Runner<identity_hash>::test(); |
|
214 |
} |
|
33754
49614940dafc
8055283: Expand ResourceHashtable with C_HEAP allocation, removal and some unit tests
mgerdin
parents:
diff
changeset
|
215 |
|
41668 | 216 |
TEST_VM_F(GenericResourceHashtableTest, primitive_hash_no_rm) { |
217 |
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); |
|
218 |
} |
|
219 |
||
220 |
TEST_VM_F(GenericResourceHashtableTest, bad_hash_no_rm) { |
|
221 |
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); |
|
222 |
} |
|
223 |
||
224 |
TEST_VM_F(GenericResourceHashtableTest, identity_hash_no_rm) { |
|
225 |
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512); |
|
226 |
} |