blob: 1baec28e593d8f845ca84a44e01ee07b953237a5 [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartierc56057e2014-05-04 13:18:58 -070017#include "indirect_reference_table-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080018
Vladimir Marko3481ba22015-04-13 12:22:36 +010019#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_runtime_test.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070021#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070022#include "scoped_thread_state_change-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024namespace art {
25
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070027
Ian Rogers63818dc2012-09-26 12:23:04 -070028static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070029 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers63818dc2012-09-26 12:23:04 -070030 std::ostringstream oss;
31 irt->Dump(oss);
32 if (num_objects == 0) {
33 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
34 } else if (num_objects == 1) {
35 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
36 } else {
37 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
38 num_objects, num_unique)),
39 std::string::npos)
40 << "\n Expected number of objects: " << num_objects
41 << "\n Expected unique objects: " << num_unique << "\n"
42 << oss.str();
43 }
44}
45
Elliott Hughes6c1a3942011-08-17 15:00:06 -070046TEST_F(IndirectReferenceTableTest, BasicTest) {
Andreas Gampe369810a2015-01-14 19:53:31 -080047 // This will lead to error messages in the log.
48 ScopedLogSeverity sls(LogSeverity::FATAL);
49
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070051 static const size_t kTableMax = 20;
Richard Uhlerda0a69e2016-10-11 15:06:38 +010052 std::string error_msg;
53 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
54 ASSERT_TRUE(irt.IsValid()) << error_msg;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070055
Ian Rogers98379392014-02-24 16:53:16 -080056 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070057 StackHandleScope<4> hs(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070058 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070059 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
60 ASSERT_TRUE(obj0.Get() != nullptr);
61 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
62 ASSERT_TRUE(obj1.Get() != nullptr);
63 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
64 ASSERT_TRUE(obj2.Get() != nullptr);
65 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
66 ASSERT_TRUE(obj3.Get() != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067
Andreas Gampee03662b2016-10-13 17:12:56 -070068 const IRTSegmentState cookie = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070069
Ian Rogers63818dc2012-09-26 12:23:04 -070070 CheckDump(&irt, 0, 0);
71
Elliott Hughes6c1a3942011-08-17 15:00:06 -070072 IndirectRef iref0 = (IndirectRef) 0x11110;
73 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
74
75 // Add three, check, remove in the order in which they were added.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070076 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070077 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 1, 1);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070079 IndirectRef iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070080 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070081 CheckDump(&irt, 2, 2);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070082 IndirectRef iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070083 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070084 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070086 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
87 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
88 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070089
90 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070091 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070093 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070095 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096
97 // Table should be empty now.
98 EXPECT_EQ(0U, irt.Capacity());
99
100 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -0700101 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102
103 // Add three, remove in the opposite order.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700104 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700105 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700106 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700107 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700108 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700109 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700110 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700111
112 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700113 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700114 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700115 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700117 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700118
119 // Table should be empty now.
120 ASSERT_EQ(0U, irt.Capacity());
121
122 // Add three, remove middle / middle / bottom / top. (Second attempt
123 // to remove middle should fail.)
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700124 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700125 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700126 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700127 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700128 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700129 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700130 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131
132 ASSERT_EQ(3U, irt.Capacity());
133
134 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700135 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700136 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700137 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138
139 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700140 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700141
142 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700143 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700145 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700146
147 // Table should be empty now.
148 ASSERT_EQ(0U, irt.Capacity());
149
150 // Add four entries. Remove #1, add new entry, verify that table size
151 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
152 // that we delete one and don't hole-compact the other.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700153 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700154 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700155 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700156 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700157 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700158 EXPECT_TRUE(iref2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700159 IndirectRef iref3 = irt.Add(cookie, obj3.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700160 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700161 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700162
163 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700164 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700166 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700167 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168
169 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700170 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700171
172 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700173 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700174 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700175 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700176
177 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
178
179 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700180 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700182 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183
184 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
185
186 // Add an entry, remove it, add a new entry, and try to use the original
187 // iref. They have the same slot number but are for different objects.
188 // With the extended checks in place, this should fail.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700189 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700190 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700191 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700192 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700193 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700194 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700195 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700196 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700197 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700198 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700199 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
200 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202
203 // Same as above, but with the same object. A more rigorous checker
204 // (e.g. with slot serialization) will catch this.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700205 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700206 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700207 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700208 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700209 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700210 iref1 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700211 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700212 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700213 if (iref0 != iref1) {
214 // Try 0, should not work.
215 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
216 }
217 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
218 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700219 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700221 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700222 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700223
224 // Stale lookup.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700225 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700226 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700227 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700228 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700229 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700230 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700231
232 // Test table resizing.
233 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700234 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235 IndirectRef manyRefs[kTableInitial];
236 for (size_t i = 0; i < kTableInitial; i++) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700237 manyRefs[i] = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700238 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700239 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700240 }
241 // ...this one causes overflow.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700242 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700243 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700244 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700245 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700246
247 for (size_t i = 0; i < kTableInitial; i++) {
248 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700249 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700250 }
251 // Because of removal order, should have 11 entries, 10 of them holes.
252 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
253
254 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
255
256 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700257 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700258}
259
Andreas Gampee03662b2016-10-13 17:12:56 -0700260TEST_F(IndirectReferenceTableTest, Holes) {
261 // Test the explicitly named cases from the IRT implementation:
262 //
263 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
264 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
265 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
266 // reference
267 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
268 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
269 // reference
270
271 ScopedObjectAccess soa(Thread::Current());
272 static const size_t kTableMax = 10;
273
274 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
275 StackHandleScope<5> hs(soa.Self());
276 ASSERT_TRUE(c != nullptr);
277 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
278 ASSERT_TRUE(obj0.Get() != nullptr);
279 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
280 ASSERT_TRUE(obj1.Get() != nullptr);
281 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
282 ASSERT_TRUE(obj2.Get() != nullptr);
283 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
284 ASSERT_TRUE(obj3.Get() != nullptr);
285 Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self()));
286 ASSERT_TRUE(obj4.Get() != nullptr);
287
288 std::string error_msg;
289
290 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference.
291 {
292 IndirectReferenceTable irt(kTableMax, kLocal, &error_msg);
293 ASSERT_TRUE(irt.IsValid()) << error_msg;
294
295 const IRTSegmentState cookie0 = kIRTFirstSegment;
296
297 CheckDump(&irt, 0, 0);
298
299 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
300 IndirectRef iref1 = irt.Add(cookie0, obj1.Get());
301 IndirectRef iref2 = irt.Add(cookie0, obj2.Get());
302
303 EXPECT_TRUE(irt.Remove(cookie0, iref1));
304
305 // New segment.
306 const IRTSegmentState cookie1 = irt.GetSegmentState();
307
308 IndirectRef iref3 = irt.Add(cookie1, obj3.Get());
309
310 // Must not have filled the previous hole.
311 EXPECT_EQ(irt.Capacity(), 4u);
312 EXPECT_TRUE(irt.Get(iref1) == nullptr);
313 CheckDump(&irt, 3, 3);
314
315 UNUSED(iref0, iref1, iref2, iref3);
316 }
317
318 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
319 {
320 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
321 ASSERT_TRUE(irt.IsValid()) << error_msg;
322
323 const IRTSegmentState cookie0 = kIRTFirstSegment;
324
325 CheckDump(&irt, 0, 0);
326
327 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
328
329 // New segment.
330 const IRTSegmentState cookie1 = irt.GetSegmentState();
331
332 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
333 IndirectRef iref2 = irt.Add(cookie1, obj2.Get());
334 IndirectRef iref3 = irt.Add(cookie1, obj3.Get());
335
336 EXPECT_TRUE(irt.Remove(cookie1, iref2));
337
338 // Pop segment.
339 irt.SetSegmentState(cookie1);
340
341 IndirectRef iref4 = irt.Add(cookie1, obj4.Get());
342
343 EXPECT_EQ(irt.Capacity(), 2u);
344 EXPECT_TRUE(irt.Get(iref2) == nullptr);
345 CheckDump(&irt, 2, 2);
346
347 UNUSED(iref0, iref1, iref2, iref3, iref4);
348 }
349
350 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
351 // reference.
352 {
353 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
354 ASSERT_TRUE(irt.IsValid()) << error_msg;
355
356 const IRTSegmentState cookie0 = kIRTFirstSegment;
357
358 CheckDump(&irt, 0, 0);
359
360 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
361
362 // New segment.
363 const IRTSegmentState cookie1 = irt.GetSegmentState();
364
365 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
366 IndirectRef iref2 = irt.Add(cookie1, obj2.Get());
367
368 EXPECT_TRUE(irt.Remove(cookie1, iref1));
369
370 // New segment.
371 const IRTSegmentState cookie2 = irt.GetSegmentState();
372
373 IndirectRef iref3 = irt.Add(cookie2, obj3.Get());
374
375 // Pop segment.
376 irt.SetSegmentState(cookie2);
377
378 IndirectRef iref4 = irt.Add(cookie1, obj4.Get());
379
380 EXPECT_EQ(irt.Capacity(), 3u);
381 EXPECT_TRUE(irt.Get(iref1) == nullptr);
382 CheckDump(&irt, 3, 3);
383
384 UNUSED(iref0, iref1, iref2, iref3, iref4);
385 }
386
387 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference.
388 {
389 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
390 ASSERT_TRUE(irt.IsValid()) << error_msg;
391
392 const IRTSegmentState cookie0 = kIRTFirstSegment;
393
394 CheckDump(&irt, 0, 0);
395
396 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
397
398 // New segment.
399 const IRTSegmentState cookie1 = irt.GetSegmentState();
400
401 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
402 EXPECT_TRUE(irt.Remove(cookie1, iref1));
403
404 // Emptied segment, push new one.
405 const IRTSegmentState cookie2 = irt.GetSegmentState();
406
407 IndirectRef iref2 = irt.Add(cookie1, obj1.Get());
408 IndirectRef iref3 = irt.Add(cookie1, obj2.Get());
409 IndirectRef iref4 = irt.Add(cookie1, obj3.Get());
410
411 EXPECT_TRUE(irt.Remove(cookie1, iref3));
412
413 // Pop segment.
414 UNUSED(cookie2);
415 irt.SetSegmentState(cookie1);
416
417 IndirectRef iref5 = irt.Add(cookie1, obj4.Get());
418
419 EXPECT_EQ(irt.Capacity(), 2u);
420 EXPECT_TRUE(irt.Get(iref3) == nullptr);
421 CheckDump(&irt, 2, 2);
422
423 UNUSED(iref0, iref1, iref2, iref3, iref4, iref5);
424 }
425
426 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
427 // reference
428 {
429 IndirectReferenceTable irt(kTableMax, kGlobal, &error_msg);
430 ASSERT_TRUE(irt.IsValid()) << error_msg;
431
432 const IRTSegmentState cookie0 = kIRTFirstSegment;
433
434 CheckDump(&irt, 0, 0);
435
436 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
437
438 // New segment.
439 const IRTSegmentState cookie1 = irt.GetSegmentState();
440
441 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
442 IndirectRef iref2 = irt.Add(cookie1, obj1.Get());
443 IndirectRef iref3 = irt.Add(cookie1, obj2.Get());
444
445 EXPECT_TRUE(irt.Remove(cookie1, iref2));
446
447 // Pop segment.
448 irt.SetSegmentState(cookie1);
449
450 // Push segment.
451 const IRTSegmentState cookie1_second = irt.GetSegmentState();
452 UNUSED(cookie1_second);
453
454 IndirectRef iref4 = irt.Add(cookie1, obj3.Get());
455
456 EXPECT_EQ(irt.Capacity(), 2u);
457 EXPECT_TRUE(irt.Get(iref3) == nullptr);
458 CheckDump(&irt, 2, 2);
459
460 UNUSED(iref0, iref1, iref2, iref3, iref4);
461 }
462}
463
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700464} // namespace art