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 | |
| 109 | // Get invalid entry (off the end of the list). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 110 | EXPECT_TRUE(irt.Get(iref0) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 111 | |
| 112 | // Add three, remove in the opposite order. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 113 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 115 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 117 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 119 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 120 | |
| 121 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 122 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 124 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 125 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 126 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 127 | |
| 128 | // Table should be empty now. |
| 129 | ASSERT_EQ(0U, irt.Capacity()); |
| 130 | |
| 131 | // Add three, remove middle / middle / bottom / top. (Second attempt |
| 132 | // to remove middle should fail.) |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 133 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 135 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 137 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 138 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 139 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 140 | |
| 141 | ASSERT_EQ(3U, irt.Capacity()); |
| 142 | |
| 143 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 144 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 145 | ASSERT_FALSE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 146 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 147 | |
| 148 | // Get invalid entry (from hole). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 149 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 150 | |
| 151 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 152 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 153 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 154 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 155 | |
| 156 | // Table should be empty now. |
| 157 | ASSERT_EQ(0U, irt.Capacity()); |
| 158 | |
| 159 | // Add four entries. Remove #1, add new entry, verify that table size |
| 160 | // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 161 | // that we delete one and don't hole-compact the other. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 162 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(iref0 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 164 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(iref1 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 166 | iref2 = irt.Add(cookie, obj2.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 167 | EXPECT_TRUE(iref2 != nullptr); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 168 | IndirectRef iref3 = irt.Add(cookie, obj3.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 169 | EXPECT_TRUE(iref3 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 170 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 171 | |
| 172 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 173 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 174 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 175 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 176 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 177 | |
| 178 | ASSERT_EQ(4U, irt.Capacity()) << "hole not filled"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 179 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 180 | |
| 181 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 182 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 183 | ASSERT_TRUE(irt.Remove(cookie, iref3)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 184 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 185 | |
| 186 | ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions"; |
| 187 | |
| 188 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 189 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 190 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 191 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 192 | |
| 193 | ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove"; |
| 194 | |
| 195 | // Add an entry, remove it, add a new entry, and try to use the original |
| 196 | // iref. They have the same slot number but are for different objects. |
| 197 | // With the extended checks in place, this should fail. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 198 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 199 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 200 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 201 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 202 | CheckDump(&irt, 0, 0); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 203 | iref1 = irt.Add(cookie, obj1.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 204 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 205 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 206 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 207 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 208 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed"; |
| 209 | ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 210 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 211 | |
| 212 | // Same as above, but with the same object. A more rigorous checker |
| 213 | // (e.g. with slot serialization) will catch this. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 214 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 215 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 216 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 217 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 218 | CheckDump(&irt, 0, 0); |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 219 | iref1 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 221 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 222 | if (iref0 != iref1) { |
| 223 | // Try 0, should not work. |
| 224 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded"; |
| 225 | } |
| 226 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed"; |
| 227 | ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 228 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 229 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 230 | // null isn't a valid iref. |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 231 | ASSERT_TRUE(irt.Get(nullptr) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 232 | |
| 233 | // Stale lookup. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 234 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 235 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 236 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 237 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 238 | EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 239 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 240 | |
| 241 | // Test table resizing. |
| 242 | // These ones fit... |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 243 | static const size_t kTableInitial = kTableMax / 2; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 244 | IndirectRef manyRefs[kTableInitial]; |
| 245 | for (size_t i = 0; i < kTableInitial; i++) { |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 246 | manyRefs[i] = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 247 | ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 248 | CheckDump(&irt, i + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 249 | } |
| 250 | // ...this one causes overflow. |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 251 | iref0 = irt.Add(cookie, obj0.Get(), &error_msg); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 252 | ASSERT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 253 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 254 | CheckDump(&irt, kTableInitial + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 255 | |
| 256 | for (size_t i = 0; i < kTableInitial; i++) { |
| 257 | ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 258 | CheckDump(&irt, kTableInitial - i, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 259 | } |
| 260 | // Because of removal order, should have 11 entries, 10 of them holes. |
| 261 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
| 262 | |
| 263 | ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed"; |
| 264 | |
| 265 | ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 266 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 269 | TEST_F(IndirectReferenceTableTest, Holes) { |
| 270 | // Test the explicitly named cases from the IRT implementation: |
| 271 | // |
| 272 | // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference |
| 273 | // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference |
| 274 | // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove |
| 275 | // reference |
| 276 | // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference |
| 277 | // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove |
| 278 | // reference |
| 279 | |
| 280 | ScopedObjectAccess soa(Thread::Current()); |
| 281 | static const size_t kTableMax = 10; |
| 282 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 283 | StackHandleScope<6> hs(soa.Self()); |
| 284 | Handle<mirror::Class> c = hs.NewHandle( |
| 285 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 286 | ASSERT_TRUE(c != nullptr); |
| 287 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 288 | ASSERT_TRUE(obj0 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 289 | Handle<mirror::Object> obj1 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 290 | ASSERT_TRUE(obj1 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 291 | Handle<mirror::Object> obj2 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 292 | ASSERT_TRUE(obj2 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 293 | Handle<mirror::Object> obj3 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 294 | ASSERT_TRUE(obj3 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 295 | Handle<mirror::Object> obj4 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 296 | ASSERT_TRUE(obj4 != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 297 | |
| 298 | std::string error_msg; |
| 299 | |
| 300 | // 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference. |
| 301 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 302 | IndirectReferenceTable irt(kTableMax, |
| 303 | kGlobal, |
| 304 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 305 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 306 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 307 | |
| 308 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 309 | |
| 310 | CheckDump(&irt, 0, 0); |
| 311 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 312 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
| 313 | IndirectRef iref1 = irt.Add(cookie0, obj1.Get(), &error_msg); |
| 314 | IndirectRef iref2 = irt.Add(cookie0, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 315 | |
| 316 | EXPECT_TRUE(irt.Remove(cookie0, iref1)); |
| 317 | |
| 318 | // New segment. |
| 319 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 320 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 321 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 322 | |
| 323 | // Must not have filled the previous hole. |
| 324 | EXPECT_EQ(irt.Capacity(), 4u); |
| 325 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
| 326 | CheckDump(&irt, 3, 3); |
| 327 | |
| 328 | UNUSED(iref0, iref1, iref2, iref3); |
| 329 | } |
| 330 | |
| 331 | // 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference |
| 332 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 333 | IndirectReferenceTable irt(kTableMax, |
| 334 | kGlobal, |
| 335 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 336 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 338 | |
| 339 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 340 | |
| 341 | CheckDump(&irt, 0, 0); |
| 342 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 343 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 344 | |
| 345 | // New segment. |
| 346 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 347 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 348 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 349 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg); |
| 350 | IndirectRef iref3 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 351 | |
| 352 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 353 | |
| 354 | // Pop segment. |
| 355 | irt.SetSegmentState(cookie1); |
| 356 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 357 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 358 | |
| 359 | EXPECT_EQ(irt.Capacity(), 2u); |
| 360 | EXPECT_TRUE(irt.Get(iref2) == nullptr); |
| 361 | CheckDump(&irt, 2, 2); |
| 362 | |
| 363 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 364 | } |
| 365 | |
| 366 | // 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove |
| 367 | // reference. |
| 368 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 369 | IndirectReferenceTable irt(kTableMax, |
| 370 | kGlobal, |
| 371 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 372 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 373 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 374 | |
| 375 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 376 | |
| 377 | CheckDump(&irt, 0, 0); |
| 378 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 379 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 380 | |
| 381 | // New segment. |
| 382 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 383 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 384 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 385 | IndirectRef iref2 = irt.Add(cookie1, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 386 | |
| 387 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 388 | |
| 389 | // New segment. |
| 390 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 391 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 392 | IndirectRef iref3 = irt.Add(cookie2, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 393 | |
| 394 | // Pop segment. |
| 395 | irt.SetSegmentState(cookie2); |
| 396 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 397 | IndirectRef iref4 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 398 | |
| 399 | EXPECT_EQ(irt.Capacity(), 3u); |
| 400 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
| 401 | CheckDump(&irt, 3, 3); |
| 402 | |
| 403 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 404 | } |
| 405 | |
| 406 | // 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference. |
| 407 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 408 | IndirectReferenceTable irt(kTableMax, |
| 409 | kGlobal, |
| 410 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 411 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 412 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 413 | |
| 414 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 415 | |
| 416 | CheckDump(&irt, 0, 0); |
| 417 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 418 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 419 | |
| 420 | // New segment. |
| 421 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 422 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 423 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 424 | EXPECT_TRUE(irt.Remove(cookie1, iref1)); |
| 425 | |
| 426 | // Emptied segment, push new one. |
| 427 | const IRTSegmentState cookie2 = irt.GetSegmentState(); |
| 428 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 429 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 430 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg); |
| 431 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 432 | |
| 433 | EXPECT_TRUE(irt.Remove(cookie1, iref3)); |
| 434 | |
| 435 | // Pop segment. |
| 436 | UNUSED(cookie2); |
| 437 | irt.SetSegmentState(cookie1); |
| 438 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 439 | IndirectRef iref5 = irt.Add(cookie1, obj4.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 440 | |
| 441 | EXPECT_EQ(irt.Capacity(), 2u); |
| 442 | EXPECT_TRUE(irt.Get(iref3) == nullptr); |
| 443 | CheckDump(&irt, 2, 2); |
| 444 | |
| 445 | UNUSED(iref0, iref1, iref2, iref3, iref4, iref5); |
| 446 | } |
| 447 | |
| 448 | // 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove |
| 449 | // reference |
| 450 | { |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 451 | IndirectReferenceTable irt(kTableMax, |
| 452 | kGlobal, |
| 453 | IndirectReferenceTable::ResizableCapacity::kNo, |
| 454 | &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 455 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 456 | |
| 457 | const IRTSegmentState cookie0 = kIRTFirstSegment; |
| 458 | |
| 459 | CheckDump(&irt, 0, 0); |
| 460 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 461 | IndirectRef iref0 = irt.Add(cookie0, obj0.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 462 | |
| 463 | // New segment. |
| 464 | const IRTSegmentState cookie1 = irt.GetSegmentState(); |
| 465 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 466 | IndirectRef iref1 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 467 | IndirectRef iref2 = irt.Add(cookie1, obj1.Get(), &error_msg); |
| 468 | IndirectRef iref3 = irt.Add(cookie1, obj2.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 469 | |
| 470 | EXPECT_TRUE(irt.Remove(cookie1, iref2)); |
| 471 | |
| 472 | // Pop segment. |
| 473 | irt.SetSegmentState(cookie1); |
| 474 | |
| 475 | // Push segment. |
| 476 | const IRTSegmentState cookie1_second = irt.GetSegmentState(); |
| 477 | UNUSED(cookie1_second); |
| 478 | |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 479 | IndirectRef iref4 = irt.Add(cookie1, obj3.Get(), &error_msg); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 480 | |
| 481 | EXPECT_EQ(irt.Capacity(), 2u); |
| 482 | EXPECT_TRUE(irt.Get(iref3) == nullptr); |
| 483 | CheckDump(&irt, 2, 2); |
| 484 | |
| 485 | UNUSED(iref0, iref1, iref2, iref3, iref4); |
| 486 | } |
| 487 | } |
| 488 | |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 489 | TEST_F(IndirectReferenceTableTest, Resize) { |
| 490 | ScopedObjectAccess soa(Thread::Current()); |
| 491 | static const size_t kTableMax = 512; |
| 492 | |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 493 | StackHandleScope<2> hs(soa.Self()); |
| 494 | Handle<mirror::Class> c = hs.NewHandle( |
| 495 | class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 496 | ASSERT_TRUE(c != nullptr); |
| 497 | Handle<mirror::Object> obj0 = hs.NewHandle(c->AllocObject(soa.Self())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 498 | ASSERT_TRUE(obj0 != nullptr); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 499 | |
| 500 | std::string error_msg; |
| 501 | IndirectReferenceTable irt(kTableMax, |
| 502 | kLocal, |
| 503 | IndirectReferenceTable::ResizableCapacity::kYes, |
| 504 | &error_msg); |
| 505 | ASSERT_TRUE(irt.IsValid()) << error_msg; |
| 506 | |
| 507 | CheckDump(&irt, 0, 0); |
| 508 | const IRTSegmentState cookie = kIRTFirstSegment; |
| 509 | |
| 510 | for (size_t i = 0; i != kTableMax + 1; ++i) { |
Andreas Gampe | 2565112 | 2017-09-25 14:50:23 -0700 | [diff] [blame] | 511 | irt.Add(cookie, obj0.Get(), &error_msg); |
Andreas Gampe | 9d7ef62 | 2016-10-24 19:35:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | EXPECT_EQ(irt.Capacity(), kTableMax + 1); |
| 515 | } |
| 516 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 517 | } // namespace art |