Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 17 | #include "indirect_reference_table-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "common_runtime_test.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 23 | #include "mirror/class-alloc-inl.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 24 | #include "mirror/object-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 29 | using android::base::StringPrintf; |
| 30 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 31 | class IndirectReferenceTableTest : public CommonRuntimeTest {}; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 33 | static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 34 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 35 | std::ostringstream oss; |
| 36 | irt->Dump(oss); |
| 37 | if (num_objects == 0) { |
| 38 | EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str(); |
| 39 | } else if (num_objects == 1) { |
| 40 | EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str(); |
| 41 | } else { |
| 42 | EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)", |
| 43 | num_objects, num_unique)), |
| 44 | std::string::npos) |
| 45 | << "\n Expected number of objects: " << num_objects |
| 46 | << "\n Expected unique objects: " << num_unique << "\n" |
| 47 | << oss.str(); |
| 48 | } |
| 49 | } |
| 50 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 51 | TEST_F(IndirectReferenceTableTest, BasicTest) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 52 | // This will lead to error messages in the log. |
| 53 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 54 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 55 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 56 | static const size_t kTableMax = 20; |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 57 | std::string error_msg; |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 58 | IndirectReferenceTable irt(kTableMax, |
| 59 | kGlobal, |
| 60 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 61 | &error_msg); |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 62 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 63 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 64 | StackHandleScope<5> hs(soa.Self()); |
| 65 | Handle<mirror::Class> c = |
| 66 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(c != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 68 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 69 | ASSERT_TRUE(obj0 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 70 | Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 71 | ASSERT_TRUE(obj1 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 72 | Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 73 | ASSERT_TRUE(obj2 != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 74 | Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 75 | ASSERT_TRUE(obj3 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 77 | const IRTSegmentState cookie = kIRTFirstSegment; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 78 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 79 | CheckDump(&irt, 0, 0); |
| 80 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 81 | IndirectRef iref0 = (IndirectRef) 0x11110; |
| 82 | EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal"; |
| 83 | |
| 84 | // Add three, check, remove in the order in which they were added. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 85 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 87 | CheckDump(&irt, 1, 1); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 88 | IndirectRef iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 90 | CheckDump(&irt, 2, 2); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 91 | IndirectRef iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 93 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 94 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 95 | EXPECT_OBJ_PTR_EQ(obj0.Get(), irt.Get(iref0)); |
| 96 | EXPECT_OBJ_PTR_EQ(obj1.Get(), irt.Get(iref1)); |
| 97 | EXPECT_OBJ_PTR_EQ(obj2.Get(), irt.Get(iref2)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 98 | |
| 99 | EXPECT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 100 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 102 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 103 | EXPECT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 104 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 105 | |
| 106 | // Table should be empty now. |
| 107 | EXPECT_EQ(0U, irt.Capacity()); |
| 108 | |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 109 | // Check that the entry off the end of the list is not valid. |
| 110 | // (CheckJNI shall abort for such entries.) |
| 111 | EXPECT_FALSE(irt.IsValidReference(iref0, &error_msg)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 112 | |
| 113 | // Add three, remove in the opposite order. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 114 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 116 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 118 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 119 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 120 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 121 | |
| 122 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 123 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 124 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 125 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 126 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 127 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 128 | |
| 129 | // Table should be empty now. |
| 130 | ASSERT_EQ(0U, irt.Capacity()); |
| 131 | |
| 132 | // Add three, remove middle / middle / bottom / top. (Second attempt |
| 133 | // to remove middle should fail.) |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 134 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 136 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 138 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 140 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 141 | |
| 142 | ASSERT_EQ(3U, irt.Capacity()); |
| 143 | |
| 144 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 145 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 146 | ASSERT_FALSE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 147 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 148 | |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 149 | // Check that the reference to the hole is not valid. |
| 150 | EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg)); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 151 | |
| 152 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 153 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 154 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 155 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 156 | |
| 157 | // Table should be empty now. |
| 158 | ASSERT_EQ(0U, irt.Capacity()); |
| 159 | |
| 160 | // Add four entries. Remove #1, add new entry, verify that table size |
| 161 | // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 162 | // that we delete one and don't hole-compact the other. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 163 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 165 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 166 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 167 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(iref2 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 169 | IndirectRef iref3 = irt.Add(cookie, obj3.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 170 | EXPECT_TRUE(iref3 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 171 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 172 | |
| 173 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 174 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 175 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 176 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 177 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 178 | |
| 179 | ASSERT_EQ(4U, irt.Capacity()) << "hole not filled"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 180 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 181 | |
| 182 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 183 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(irt.Remove(cookie, iref3)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 185 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 186 | |
| 187 | ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions"; |
| 188 | |
| 189 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 190 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 191 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 192 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 193 | |
| 194 | ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove"; |
| 195 | |
| 196 | // Add an entry, remove it, add a new entry, and try to use the original |
| 197 | // iref. They have the same slot number but are for different objects. |
| 198 | // With the extended checks in place, this should fail. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 199 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 200 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 201 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 202 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 203 | CheckDump(&irt, 0, 0); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 204 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 205 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 206 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 207 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 208 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 209 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed"; |
| 210 | ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 211 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 212 | |
| 213 | // Same as above, but with the same object. A more rigorous checker |
| 214 | // (e.g. with slot serialization) will catch this. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 215 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 216 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 217 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 218 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 219 | CheckDump(&irt, 0, 0); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 220 | iref1 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 221 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 222 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 223 | if (iref0 != iref1) { |
| 224 | // Try 0, should not work. |
| 225 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded"; |
| 226 | } |
| 227 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed"; |
| 228 | ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 229 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 230 | |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 231 | // Stale reference is not valid. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 232 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 233 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 234 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 235 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 236 | EXPECT_FALSE(irt.IsValidReference(iref0, &error_msg)) << "stale lookup succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 237 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 238 | |
| 239 | // Test table resizing. |
| 240 | // These ones fit... |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 241 | static const size_t kTableInitial = kTableMax / 2; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 242 | IndirectRef manyRefs[kTableInitial]; |
| 243 | for (size_t i = 0; i < kTableInitial; i++) { |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 244 | manyRefs[i] = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 246 | CheckDump(&irt, i + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 247 | } |
| 248 | // ...this one causes overflow. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 249 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 250 | ASSERT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 251 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 252 | CheckDump(&irt, kTableInitial + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 253 | |
| 254 | for (size_t i = 0; i < kTableInitial; i++) { |
| 255 | ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 256 | CheckDump(&irt, kTableInitial - i, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 257 | } |
| 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 Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 264 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 267 | TEST_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 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 281 | StackHandleScope<6> hs(soa.Self()); |
| 282 | Handle<mirror::Class> c = hs.NewHandle( |
| 283 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 284 | ASSERT_TRUE(c != nullptr); |
| 285 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 286 | ASSERT_TRUE(obj0 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 287 | Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 288 | ASSERT_TRUE(obj1 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 289 | Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 290 | ASSERT_TRUE(obj2 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 291 | Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 292 | ASSERT_TRUE(obj3 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 293 | Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 294 | ASSERT_TRUE(obj4 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 295 | |
| 296 | std::string error_msg; |
| 297 | |
| 298 | // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference. |
| 299 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 300 | IndirectReferenceTable irt(kTableMax, |
| 301 | kGlobal, |
| 302 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 303 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 304 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 305 | |
| 306 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 307 | |
| 308 | CheckDump(&irt, 0, 0); |
| 309 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 310 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
| 311 | IndirectRef iref1 = irt.Add(cookie0, obj1.Get(), &error_msg); |
| 312 | IndirectRef iref2 = irt.Add(cookie0, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 313 | |
| 314 | EXPECT_TRUE(irt.Remove(cookie0, iref1)); |
| 315 | |
| 316 | // New segment. |
| 317 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 318 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 319 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 320 | |
| 321 | // Must not have filled the previous hole. |
| 322 | EXPECT_EQ(irt.Capacity(), 4u); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 323 | EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg)); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 324 | CheckDump(&irt, 3, 3); |
| 325 | |
| 326 | UNUSED(iref0, iref1, iref2, iref3); |
| 327 | } |
| 328 | |
| 329 | // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference |
| 330 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 331 | IndirectReferenceTable irt(kTableMax, |
| 332 | kGlobal, |
| 333 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 334 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 335 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 336 | |
| 337 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 338 | |
| 339 | CheckDump(&irt, 0, 0); |
| 340 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 341 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 342 | |
| 343 | // New segment. |
| 344 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 345 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 346 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 347 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg); |
| 348 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 349 | |
| 350 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 351 | |
| 352 | // Pop segment. |
| 353 | irt.SetSegmentState(cookie1); |
| 354 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 355 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 356 | |
| 357 | EXPECT_EQ(irt.Capacity(), 2u); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 358 | EXPECT_FALSE(irt.IsValidReference(iref2, &error_msg)); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 359 | CheckDump(&irt, 2, 2); |
| 360 | |
| 361 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 362 | } |
| 363 | |
| 364 | // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove |
| 365 | // reference. |
| 366 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 367 | IndirectReferenceTable irt(kTableMax, |
| 368 | kGlobal, |
| 369 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 370 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 371 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 372 | |
| 373 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 374 | |
| 375 | CheckDump(&irt, 0, 0); |
| 376 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 377 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 378 | |
| 379 | // New segment. |
| 380 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 381 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 382 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 383 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 384 | |
| 385 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 386 | |
| 387 | // New segment. |
| 388 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 389 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 390 | IndirectRef iref3 = irt.Add(cookie2, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 391 | |
| 392 | // Pop segment. |
| 393 | irt.SetSegmentState(cookie2); |
| 394 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 395 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 396 | |
| 397 | EXPECT_EQ(irt.Capacity(), 3u); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 398 | EXPECT_FALSE(irt.IsValidReference(iref1, &error_msg)); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 399 | CheckDump(&irt, 3, 3); |
| 400 | |
| 401 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 402 | } |
| 403 | |
| 404 | // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference. |
| 405 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 406 | IndirectReferenceTable irt(kTableMax, |
| 407 | kGlobal, |
| 408 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 409 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 410 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 411 | |
| 412 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 413 | |
| 414 | CheckDump(&irt, 0, 0); |
| 415 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 416 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 417 | |
| 418 | // New segment. |
| 419 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 420 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 421 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 422 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 423 | |
| 424 | // Emptied segment, push new one. |
| 425 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 426 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 427 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 428 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg); |
| 429 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 430 | |
| 431 | EXPECT_TRUE(irt.Remove(cookie1, iref3)); |
| 432 | |
| 433 | // Pop segment. |
| 434 | UNUSED(cookie2); |
| 435 | irt.SetSegmentState(cookie1); |
| 436 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 437 | IndirectRef iref5 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 438 | |
| 439 | EXPECT_EQ(irt.Capacity(), 2u); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 440 | EXPECT_FALSE(irt.IsValidReference(iref3, &error_msg)); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 441 | CheckDump(&irt, 2, 2); |
| 442 | |
| 443 | UNUSED(iref0, iref1, iref2, iref3, iref4, iref5); |
| 444 | } |
| 445 | |
| 446 | // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove |
| 447 | // reference |
| 448 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 449 | IndirectReferenceTable irt(kTableMax, |
| 450 | kGlobal, |
| 451 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 452 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 453 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 454 | |
| 455 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 456 | |
| 457 | CheckDump(&irt, 0, 0); |
| 458 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 459 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 460 | |
| 461 | // New segment. |
| 462 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 463 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 464 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 465 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 466 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 467 | |
| 468 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 469 | |
| 470 | // Pop segment. |
| 471 | irt.SetSegmentState(cookie1); |
| 472 | |
| 473 | // Push segment. |
| 474 | const IRTSegmentState cookie1_second = irt.GetSegmentState(); |
| 475 | UNUSED(cookie1_second); |
| 476 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 477 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 478 | |
| 479 | EXPECT_EQ(irt.Capacity(), 2u); |
Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 480 | EXPECT_FALSE(irt.IsValidReference(iref3, &error_msg)); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 481 | CheckDump(&irt, 2, 2); |
| 482 | |
| 483 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 484 | } |
| 485 | } |
| 486 | |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 487 | TEST_F(IndirectReferenceTableTest, Resize) { |
| 488 | ScopedObjectAccess soa(Thread::Current()); |
| 489 | static const size_t kTableMax = 512; |
| 490 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 491 | StackHandleScope<2> hs(soa.Self()); |
| 492 | Handle<mirror::Class> c = hs.NewHandle( |
| 493 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 494 | ASSERT_TRUE(c != nullptr); |
| 495 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 496 | ASSERT_TRUE(obj0 != nullptr); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 497 | |
| 498 | std::string error_msg; |
| 499 | IndirectReferenceTable irt(kTableMax, |
| 500 | kLocal, |
| 501 | IndirectReferenceTable::ResizableCapacity::kYes, |
| 502 | &error_msg); |
| 503 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 504 | |
| 505 | CheckDump(&irt, 0, 0); |
| 506 | const IRTSegmentState cookie = kIRTFirstSegment; |
| 507 | |
| 508 | for (size_t i = 0; i != kTableMax + 1; ++i) { |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 509 | irt.Add(cookie, obj0.Get(), &error_msg); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | EXPECT_EQ(irt.Capacity(), kTableMax + 1); |
| 513 | } |
| 514 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 515 | } // namespace art |