blob: b6c6cb4be372b76fa6831919f1c9a74bd061f2fa [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
17#include "common_test.h"
18
19#include "indirect_reference_table.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070020#include "mirror/object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070021
Elliott Hughes6c1a3942011-08-17 15:00:06 -070022namespace art {
23
Brian Carlstromf734cf52011-08-17 16:28:14 -070024class IndirectReferenceTableTest : public CommonTest {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070025};
26
Ian Rogers63818dc2012-09-26 12:23:04 -070027static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
28 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29 std::ostringstream oss;
30 irt->Dump(oss);
31 if (num_objects == 0) {
32 EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
33 } else if (num_objects == 1) {
34 EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
35 } else {
36 EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
37 num_objects, num_unique)),
38 std::string::npos)
39 << "\n Expected number of objects: " << num_objects
40 << "\n Expected unique objects: " << num_unique << "\n"
41 << oss.str();
42 }
43}
44
Elliott Hughes6c1a3942011-08-17 15:00:06 -070045TEST_F(IndirectReferenceTableTest, BasicTest) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070047 static const size_t kTableInitial = 10;
48 static const size_t kTableMax = 20;
49 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
50
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 mirror::Class* c = class_linker_->FindSystemClass("Ljava/lang/Object;");
Elliott Hughes6c1a3942011-08-17 15:00:06 -070052 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 mirror::Object* obj0 = c->AllocObject(soa.Self());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070054 ASSERT_TRUE(obj0 != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 mirror::Object* obj1 = c->AllocObject(soa.Self());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070056 ASSERT_TRUE(obj1 != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::Object* obj2 = c->AllocObject(soa.Self());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070058 ASSERT_TRUE(obj2 != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Object* obj3 = c->AllocObject(soa.Self());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070060 ASSERT_TRUE(obj3 != NULL);
61
62 const uint32_t cookie = IRT_FIRST_SEGMENT;
63
Ian Rogers63818dc2012-09-26 12:23:04 -070064 CheckDump(&irt, 0, 0);
65
Elliott Hughes6c1a3942011-08-17 15:00:06 -070066 IndirectRef iref0 = (IndirectRef) 0x11110;
67 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
68
69 // Add three, check, remove in the order in which they were added.
70 iref0 = irt.Add(cookie, obj0);
71 EXPECT_TRUE(iref0 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -070072 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070073 IndirectRef iref1 = irt.Add(cookie, obj1);
74 EXPECT_TRUE(iref1 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -070075 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076 IndirectRef iref2 = irt.Add(cookie, obj2);
77 EXPECT_TRUE(iref2 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -070078 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070079
80 EXPECT_EQ(obj0, irt.Get(iref0));
81 EXPECT_EQ(obj1, irt.Get(iref1));
82 EXPECT_EQ(obj2, irt.Get(iref2));
83
84 EXPECT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -070085 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070086 EXPECT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -070087 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088 EXPECT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -070089 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070090
91 // Table should be empty now.
92 EXPECT_EQ(0U, irt.Capacity());
93
94 // Get invalid entry (off the end of the list).
95 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0));
96
97 // Add three, remove in the opposite order.
98 iref0 = irt.Add(cookie, obj0);
99 EXPECT_TRUE(iref0 != NULL);
100 iref1 = irt.Add(cookie, obj1);
101 EXPECT_TRUE(iref1 != NULL);
102 iref2 = irt.Add(cookie, obj2);
103 EXPECT_TRUE(iref2 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700104 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700105
106 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700107 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700108 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700109 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700111 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112
113 // Table should be empty now.
114 ASSERT_EQ(0U, irt.Capacity());
115
116 // Add three, remove middle / middle / bottom / top. (Second attempt
117 // to remove middle should fail.)
118 iref0 = irt.Add(cookie, obj0);
119 EXPECT_TRUE(iref0 != NULL);
120 iref1 = irt.Add(cookie, obj1);
121 EXPECT_TRUE(iref1 != NULL);
122 iref2 = irt.Add(cookie, obj2);
123 EXPECT_TRUE(iref2 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700124 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700125
126 ASSERT_EQ(3U, irt.Capacity());
127
128 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700129 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130 ASSERT_FALSE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700131 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700132
133 // Get invalid entry (from hole).
134 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1));
135
136 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700137 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700138 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700139 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140
141 // Table should be empty now.
142 ASSERT_EQ(0U, irt.Capacity());
143
144 // Add four entries. Remove #1, add new entry, verify that table size
145 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
146 // that we delete one and don't hole-compact the other.
147 iref0 = irt.Add(cookie, obj0);
148 EXPECT_TRUE(iref0 != NULL);
149 iref1 = irt.Add(cookie, obj1);
150 EXPECT_TRUE(iref1 != NULL);
151 iref2 = irt.Add(cookie, obj2);
152 EXPECT_TRUE(iref2 != NULL);
153 IndirectRef iref3 = irt.Add(cookie, obj3);
154 EXPECT_TRUE(iref3 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700155 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700156
157 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700158 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700159
160 iref1 = irt.Add(cookie, obj1);
161 EXPECT_TRUE(iref1 != NULL);
162
163 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
Ian Rogers63818dc2012-09-26 12:23:04 -0700164 CheckDump(&irt, 4, 4);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700165
166 ASSERT_TRUE(irt.Remove(cookie, iref1));
Ian Rogers63818dc2012-09-26 12:23:04 -0700167 CheckDump(&irt, 3, 3);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168 ASSERT_TRUE(irt.Remove(cookie, iref3));
Ian Rogers63818dc2012-09-26 12:23:04 -0700169 CheckDump(&irt, 2, 2);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700170
171 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
172
173 ASSERT_TRUE(irt.Remove(cookie, iref2));
Ian Rogers63818dc2012-09-26 12:23:04 -0700174 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700175 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700176 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700177
178 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
179
180 // Add an entry, remove it, add a new entry, and try to use the original
181 // iref. They have the same slot number but are for different objects.
182 // With the extended checks in place, this should fail.
183 iref0 = irt.Add(cookie, obj0);
184 EXPECT_TRUE(iref0 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700185 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700186 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700187 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700188 iref1 = irt.Add(cookie, obj1);
189 EXPECT_TRUE(iref1 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700190 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700191 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700192 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
194 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700195 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700196
197 // Same as above, but with the same object. A more rigorous checker
198 // (e.g. with slot serialization) will catch this.
199 iref0 = irt.Add(cookie, obj0);
200 EXPECT_TRUE(iref0 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700201 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700202 ASSERT_TRUE(irt.Remove(cookie, iref0));
Ian Rogers63818dc2012-09-26 12:23:04 -0700203 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700204 iref1 = irt.Add(cookie, obj0);
205 EXPECT_TRUE(iref1 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700206 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700207 if (iref0 != iref1) {
208 // Try 0, should not work.
209 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
210 }
211 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
212 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700213 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700214
215 // NULL isn't a valid iref.
216 ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL));
217
218 // Stale lookup.
219 iref0 = irt.Add(cookie, obj0);
220 EXPECT_TRUE(iref0 != NULL);
Ian Rogers63818dc2012-09-26 12:23:04 -0700221 CheckDump(&irt, 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700222 ASSERT_TRUE(irt.Remove(cookie, iref0));
223 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded";
Ian Rogers63818dc2012-09-26 12:23:04 -0700224 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700225
226 // Test table resizing.
227 // These ones fit...
228 IndirectRef manyRefs[kTableInitial];
229 for (size_t i = 0; i < kTableInitial; i++) {
230 manyRefs[i] = irt.Add(cookie, obj0);
231 ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700232 CheckDump(&irt, i + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233 }
234 // ...this one causes overflow.
235 iref0 = irt.Add(cookie, obj0);
236 ASSERT_TRUE(iref0 != NULL);
237 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
Ian Rogers63818dc2012-09-26 12:23:04 -0700238 CheckDump(&irt, kTableInitial + 1, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700239
240 for (size_t i = 0; i < kTableInitial; i++) {
241 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
Ian Rogers63818dc2012-09-26 12:23:04 -0700242 CheckDump(&irt, kTableInitial - i, 1);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243 }
244 // Because of removal order, should have 11 entries, 10 of them holes.
245 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
246
247 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
248
249 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
Ian Rogers63818dc2012-09-26 12:23:04 -0700250 CheckDump(&irt, 0, 0);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251}
252
253} // namespace art