blob: 6aefe239a95264b604f14b691591eaca7681ad53 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_runtime_test.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070023#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025
Elliott Hughes6c1a3942011-08-17 15:00:06 -070026namespace art {
27
Andreas Gampe46ee31b2016-12-14 10:11:49 -080028using android::base::StringPrintf;
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class IndirectReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes6c1a3942011-08-17 15:00:06 -070031
Ian Rogers63818dc2012-09-26 12:23:04 -070032static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070033 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers63818dc2012-09-26 12:23:04 -070034 std::ostringstream oss;
35 irt->Dump(oss);
36 if (num_objects == 0) {
37 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
38 } else if (num_objects == 1) {
39 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
40 } else {
41 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
42 num_objects, num_unique)),
43 std::string::npos)
44 << "\n Expected number of objects: " << num_objects
45 << "\n Expected unique objects: " << num_unique << "\n"
46 << oss.str();
47 }
48}
49
Elliott Hughes6c1a3942011-08-17 15:00:06 -070050TEST_F(IndirectReferenceTableTest, BasicTest) {
Andreas Gampe369810a2015-01-14 19:53:31 -080051 // This will lead to error messages in the log.
52 ScopedLogSeverity sls(LogSeverity::FATAL);
53
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070055 static const size_t kTableMax = 20;
Richard Uhlerda0a69e2016-10-11 15:06:38 +010056 std::string error_msg;
Andreas Gampe9d7ef622016-10-24 19:35:19 -070057 IndirectReferenceTable irt(kTableMax,
58 kGlobal,
59 IndirectReferenceTable::ResizableCapacity::kNo,
60 &error_msg);
Richard Uhlerda0a69e2016-10-11 15:06:38 +010061 ASSERT_TRUE(irt.IsValid()) << error_msg;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070062
Ian Rogers98379392014-02-24 16:53:16 -080063 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070064 StackHandleScope<4> hs(soa.Self());
Ian Rogersc0542af2014-09-03 16:16:56 -070065 ASSERT_TRUE(c != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070066 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080067 ASSERT_TRUE(obj0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070068 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080069 ASSERT_TRUE(obj1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070070 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080071 ASSERT_TRUE(obj2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070072 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -080073 ASSERT_TRUE(obj3 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070074
Andreas Gampee03662b2016-10-13 17:12:56 -070075 const IRTSegmentState cookie = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076
Ian Rogers63818dc2012-09-26 12:23:04 -070077 CheckDump(&irt, 0, 0);
78
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079 IndirectRef iref0 = (IndirectRef) 0x11110;
80 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
81
82 // Add three, check, remove in the order in which they were added.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070083 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070084 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070085 CheckDump(&irt, 1, 1);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070086 IndirectRef iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070087 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070088 CheckDump(&irt, 2, 2);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070089 IndirectRef iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -070090 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -070091 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070092
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070093 EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0));
94 EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1));
95 EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070096
97 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070098 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070099 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700100 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700101 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700102 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700103
104 // Table should be empty now.
105 EXPECT_EQ(0U, irt.Capacity());
106
107 // Get invalid entry (off the end of the list).
Ian Rogersc0542af2014-09-03 16:16:56 -0700108 EXPECT_TRUE(irt.Get(iref0) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
110 // Add three, remove in the opposite order.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700111 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700112 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700113 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700114 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700115 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700116 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700117 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700118
119 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700120 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700121 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700122 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700123 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700124 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125
126 // Table should be empty now.
127 ASSERT_EQ(0U, irt.Capacity());
128
129 // Add three, remove middle / middle / bottom / top. (Second attempt
130 // to remove middle should fail.)
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700131 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700132 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700133 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700134 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700135 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700136 EXPECT_TRUE(iref2 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700137 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138
139 ASSERT_EQ(3U, irt.Capacity());
140
141 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700142 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700143 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700144 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700145
146 // Get invalid entry (from hole).
Ian Rogersc0542af2014-09-03 16:16:56 -0700147 EXPECT_TRUE(irt.Get(iref1) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700148
149 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700150 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700151 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700152 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700153
154 // Table should be empty now.
155 ASSERT_EQ(0U, irt.Capacity());
156
157 // Add four entries. Remove #1, add new entry, verify that table size
158 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
159 // that we delete one and don't hole-compact the other.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700160 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700161 EXPECT_TRUE(iref0 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700162 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700163 EXPECT_TRUE(iref1 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700164 iref2 = irt.Add(cookie, obj2.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700165 EXPECT_TRUE(iref2 != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700166 IndirectRef iref3 = irt.Add(cookie, obj3.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700167 EXPECT_TRUE(iref3 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700168 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700169
170 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700171 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700172
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700173 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700174 EXPECT_TRUE(iref1 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175
176 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700177 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700178
179 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700180 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700181 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700182 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700183
184 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
185
186 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700187 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700188 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700189 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700190
191 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
192
193 // Add an entry, remove it, add a new entry, and try to use the original
194 // iref. They have the same slot number but are for different objects.
195 // With the extended checks in place, this should fail.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700196 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700197 EXPECT_TRUE(iref0 != nullptr);
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, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700200 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700201 iref1 = irt.Add(cookie, obj1.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700202 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700203 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700204 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700205 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700206 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
207 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700208 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700209
210 // Same as above, but with the same object. A more rigorous checker
211 // (e.g. with slot serialization) will catch this.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700212 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700213 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700214 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700215 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700216 CheckDump(&irt, 0, 0);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700217 iref1 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700218 EXPECT_TRUE(iref1 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700219 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700220 if (iref0 != iref1) {
221 // Try 0, should not work.
222 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
223 }
224 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
225 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700226 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700227
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700228 // null isn't a valid iref.
Ian Rogersc0542af2014-09-03 16:16:56 -0700229 ASSERT_TRUE(irt.Get(nullptr) == nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700230
231 // Stale lookup.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700232 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700233 EXPECT_TRUE(iref0 != nullptr);
Ian Rogers63818dc2012-09-26 12:23:04 -0700234 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700235 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogersc0542af2014-09-03 16:16:56 -0700236 EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700237 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700238
239 // Test table resizing.
240 // These ones fit...
Andreas Gampea8e3b862016-10-17 20:12:52 -0700241 static const size_t kTableInitial = kTableMax / 2;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700242 IndirectRef manyRefs[kTableInitial];
243 for (size_t i = 0; i < kTableInitial; i++) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700244 manyRefs[i] = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700245 ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700246 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700247 }
248 // ...this one causes overflow.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700249 iref0 = irt.Add(cookie, obj0.Get());
Ian Rogersc0542af2014-09-03 16:16:56 -0700250 ASSERT_TRUE(iref0 != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700252 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253
254 for (size_t i = 0; i < kTableInitial; i++) {
255 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700256 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700257 }
258 // Because of removal order, should have 11 entries, 10 of them holes.
259 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
260
261 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
262
263 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700264 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700265}
266
Andreas Gampee03662b2016-10-13 17:12:56 -0700267TEST_F(IndirectReferenceTableTest, Holes) {
268 // Test the explicitly named cases from the IRT implementation:
269 //
270 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
271 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
272 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
273 // reference
274 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
275 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
276 // reference
277
278 ScopedObjectAccess soa(Thread::Current());
279 static const size_t kTableMax = 10;
280
281 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
282 StackHandleScope<5> hs(soa.Self());
283 ASSERT_TRUE(c != nullptr);
284 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800285 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700286 Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800287 ASSERT_TRUE(obj1 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700288 Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800289 ASSERT_TRUE(obj2 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700290 Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800291 ASSERT_TRUE(obj3 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700292 Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800293 ASSERT_TRUE(obj4 != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700294
295 std::string error_msg;
296
297 // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference.
298 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700299 IndirectReferenceTable irt(kTableMax,
300 kGlobal,
301 IndirectReferenceTable::ResizableCapacity::kNo,
302 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700303 ASSERT_TRUE(irt.IsValid()) << error_msg;
304
305 const IRTSegmentState cookie0 = kIRTFirstSegment;
306
307 CheckDump(&irt, 0, 0);
308
309 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
310 IndirectRef iref1 = irt.Add(cookie0, obj1.Get());
311 IndirectRef iref2 = irt.Add(cookie0, obj2.Get());
312
313 EXPECT_TRUE(irt.Remove(cookie0, iref1));
314
315 // New segment.
316 const IRTSegmentState cookie1 = irt.GetSegmentState();
317
318 IndirectRef iref3 = irt.Add(cookie1, obj3.Get());
319
320 // Must not have filled the previous hole.
321 EXPECT_EQ(irt.Capacity(), 4u);
322 EXPECT_TRUE(irt.Get(iref1) == nullptr);
323 CheckDump(&irt, 3, 3);
324
325 UNUSED(iref0, iref1, iref2, iref3);
326 }
327
328 // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
329 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700330 IndirectReferenceTable irt(kTableMax,
331 kGlobal,
332 IndirectReferenceTable::ResizableCapacity::kNo,
333 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700334 ASSERT_TRUE(irt.IsValid()) << error_msg;
335
336 const IRTSegmentState cookie0 = kIRTFirstSegment;
337
338 CheckDump(&irt, 0, 0);
339
340 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
341
342 // New segment.
343 const IRTSegmentState cookie1 = irt.GetSegmentState();
344
345 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
346 IndirectRef iref2 = irt.Add(cookie1, obj2.Get());
347 IndirectRef iref3 = irt.Add(cookie1, obj3.Get());
348
349 EXPECT_TRUE(irt.Remove(cookie1, iref2));
350
351 // Pop segment.
352 irt.SetSegmentState(cookie1);
353
354 IndirectRef iref4 = irt.Add(cookie1, obj4.Get());
355
356 EXPECT_EQ(irt.Capacity(), 2u);
357 EXPECT_TRUE(irt.Get(iref2) == nullptr);
358 CheckDump(&irt, 2, 2);
359
360 UNUSED(iref0, iref1, iref2, iref3, iref4);
361 }
362
363 // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
364 // reference.
365 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700366 IndirectReferenceTable irt(kTableMax,
367 kGlobal,
368 IndirectReferenceTable::ResizableCapacity::kNo,
369 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700370 ASSERT_TRUE(irt.IsValid()) << error_msg;
371
372 const IRTSegmentState cookie0 = kIRTFirstSegment;
373
374 CheckDump(&irt, 0, 0);
375
376 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
377
378 // New segment.
379 const IRTSegmentState cookie1 = irt.GetSegmentState();
380
381 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
382 IndirectRef iref2 = irt.Add(cookie1, obj2.Get());
383
384 EXPECT_TRUE(irt.Remove(cookie1, iref1));
385
386 // New segment.
387 const IRTSegmentState cookie2 = irt.GetSegmentState();
388
389 IndirectRef iref3 = irt.Add(cookie2, obj3.Get());
390
391 // Pop segment.
392 irt.SetSegmentState(cookie2);
393
394 IndirectRef iref4 = irt.Add(cookie1, obj4.Get());
395
396 EXPECT_EQ(irt.Capacity(), 3u);
397 EXPECT_TRUE(irt.Get(iref1) == nullptr);
398 CheckDump(&irt, 3, 3);
399
400 UNUSED(iref0, iref1, iref2, iref3, iref4);
401 }
402
403 // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference.
404 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700405 IndirectReferenceTable irt(kTableMax,
406 kGlobal,
407 IndirectReferenceTable::ResizableCapacity::kNo,
408 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700409 ASSERT_TRUE(irt.IsValid()) << error_msg;
410
411 const IRTSegmentState cookie0 = kIRTFirstSegment;
412
413 CheckDump(&irt, 0, 0);
414
415 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
416
417 // New segment.
418 const IRTSegmentState cookie1 = irt.GetSegmentState();
419
420 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
421 EXPECT_TRUE(irt.Remove(cookie1, iref1));
422
423 // Emptied segment, push new one.
424 const IRTSegmentState cookie2 = irt.GetSegmentState();
425
426 IndirectRef iref2 = irt.Add(cookie1, obj1.Get());
427 IndirectRef iref3 = irt.Add(cookie1, obj2.Get());
428 IndirectRef iref4 = irt.Add(cookie1, obj3.Get());
429
430 EXPECT_TRUE(irt.Remove(cookie1, iref3));
431
432 // Pop segment.
433 UNUSED(cookie2);
434 irt.SetSegmentState(cookie1);
435
436 IndirectRef iref5 = irt.Add(cookie1, obj4.Get());
437
438 EXPECT_EQ(irt.Capacity(), 2u);
439 EXPECT_TRUE(irt.Get(iref3) == nullptr);
440 CheckDump(&irt, 2, 2);
441
442 UNUSED(iref0, iref1, iref2, iref3, iref4, iref5);
443 }
444
445 // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
446 // reference
447 {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700448 IndirectReferenceTable irt(kTableMax,
449 kGlobal,
450 IndirectReferenceTable::ResizableCapacity::kNo,
451 &error_msg);
Andreas Gampee03662b2016-10-13 17:12:56 -0700452 ASSERT_TRUE(irt.IsValid()) << error_msg;
453
454 const IRTSegmentState cookie0 = kIRTFirstSegment;
455
456 CheckDump(&irt, 0, 0);
457
458 IndirectRef iref0 = irt.Add(cookie0, obj0.Get());
459
460 // New segment.
461 const IRTSegmentState cookie1 = irt.GetSegmentState();
462
463 IndirectRef iref1 = irt.Add(cookie1, obj1.Get());
464 IndirectRef iref2 = irt.Add(cookie1, obj1.Get());
465 IndirectRef iref3 = irt.Add(cookie1, obj2.Get());
466
467 EXPECT_TRUE(irt.Remove(cookie1, iref2));
468
469 // Pop segment.
470 irt.SetSegmentState(cookie1);
471
472 // Push segment.
473 const IRTSegmentState cookie1_second = irt.GetSegmentState();
474 UNUSED(cookie1_second);
475
476 IndirectRef iref4 = irt.Add(cookie1, obj3.Get());
477
478 EXPECT_EQ(irt.Capacity(), 2u);
479 EXPECT_TRUE(irt.Get(iref3) == nullptr);
480 CheckDump(&irt, 2, 2);
481
482 UNUSED(iref0, iref1, iref2, iref3, iref4);
483 }
484}
485
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700486TEST_F(IndirectReferenceTableTest, Resize) {
487 ScopedObjectAccess soa(Thread::Current());
488 static const size_t kTableMax = 512;
489
490 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
491 StackHandleScope<1> hs(soa.Self());
492 ASSERT_TRUE(c != nullptr);
493 Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800494 ASSERT_TRUE(obj0 != nullptr);
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700495
496 std::string error_msg;
497 IndirectReferenceTable irt(kTableMax,
498 kLocal,
499 IndirectReferenceTable::ResizableCapacity::kYes,
500 &error_msg);
501 ASSERT_TRUE(irt.IsValid()) << error_msg;
502
503 CheckDump(&irt, 0, 0);
504 const IRTSegmentState cookie = kIRTFirstSegment;
505
506 for (size_t i = 0; i != kTableMax + 1; ++i) {
507 irt.Add(cookie, obj0.Get());
508 }
509
510 EXPECT_EQ(irt.Capacity(), kTableMax + 1);
511}
512
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700513} // namespace art