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