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" |
| 18 | |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 19 | #include "base/dumpable-inl.h" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 20 | #include "base/systrace.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
Mathieu Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 22 | #include "nth_caller_visitor.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 23 | #include "reference_table.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 24 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 26 | #include "thread.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 27 | #include "utils.h" |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 28 | #include "verify_object-inl.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 29 | |
| 30 | #include <cstdlib> |
| 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | 2ada67b | 2015-07-30 11:41:04 -0700 | [diff] [blame] | 34 | static constexpr bool kDumpStackOnNonLocalReference = false; |
| 35 | |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 36 | const char* GetIndirectRefKindString(const IndirectRefKind& kind) { |
| 37 | switch (kind) { |
| 38 | case kHandleScopeOrInvalid: |
| 39 | return "HandleScopeOrInvalid"; |
| 40 | case kLocal: |
| 41 | return "Local"; |
| 42 | case kGlobal: |
| 43 | return "Global"; |
| 44 | case kWeakGlobal: |
| 45 | return "WeakGlobal"; |
| 46 | } |
| 47 | return "IndirectRefKind Error"; |
| 48 | } |
| 49 | |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 50 | void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 51 | // If -Xcheck:jni is on, it'll give a more detailed error before aborting. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 52 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 53 | if (!vm->IsCheckJniEnabled()) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 54 | // Otherwise, we want to abort rather than hand back a bad reference. |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 55 | LOG(FATAL) << msg; |
| 56 | } else { |
| 57 | LOG(ERROR) << msg; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 58 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 61 | IndirectReferenceTable::IndirectReferenceTable(size_t max_count, |
| 62 | IndirectRefKind desired_kind, |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 63 | std::string* error_msg) |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 64 | : kind_(desired_kind), |
| 65 | max_entries_(max_count) { |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 66 | CHECK(error_msg != nullptr); |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 67 | CHECK_NE(desired_kind, kHandleScopeOrInvalid); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 68 | |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 69 | const size_t table_bytes = max_count * sizeof(IrtEntry); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 70 | table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes, |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 71 | PROT_READ | PROT_WRITE, false, false, error_msg)); |
| 72 | if (table_mem_map_.get() == nullptr && error_msg->empty()) { |
| 73 | *error_msg = "Unable to map memory for indirect ref table"; |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 74 | } |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 75 | |
| 76 | if (table_mem_map_.get() != nullptr) { |
| 77 | table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin()); |
| 78 | } else { |
| 79 | table_ = nullptr; |
| 80 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 81 | segment_state_.all = IRT_FIRST_SEGMENT; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | IndirectReferenceTable::~IndirectReferenceTable() { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame^] | 87 | void IndirectReferenceTable::ConstexprChecks() { |
| 88 | // Use this for some assertions. They can't be put into the header as C++ wants the class |
| 89 | // to be complete. |
| 90 | |
| 91 | // Check kind. |
| 92 | static_assert((EncodeIndirectRefKind(kLocal) & (~kKindMask)) == 0, "Kind encoding error"); |
| 93 | static_assert((EncodeIndirectRefKind(kGlobal) & (~kKindMask)) == 0, "Kind encoding error"); |
| 94 | static_assert((EncodeIndirectRefKind(kWeakGlobal) & (~kKindMask)) == 0, "Kind encoding error"); |
| 95 | static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kLocal)) == kLocal, |
| 96 | "Kind encoding error"); |
| 97 | static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kGlobal)) == kGlobal, |
| 98 | "Kind encoding error"); |
| 99 | static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kWeakGlobal)) == kWeakGlobal, |
| 100 | "Kind encoding error"); |
| 101 | |
| 102 | // Check serial. |
| 103 | static_assert(DecodeSerial(EncodeSerial(0u)) == 0u, "Serial encoding error"); |
| 104 | static_assert(DecodeSerial(EncodeSerial(1u)) == 1u, "Serial encoding error"); |
| 105 | static_assert(DecodeSerial(EncodeSerial(2u)) == 2u, "Serial encoding error"); |
| 106 | static_assert(DecodeSerial(EncodeSerial(3u)) == 3u, "Serial encoding error"); |
| 107 | |
| 108 | // Table index. |
| 109 | static_assert(DecodeIndex(EncodeIndex(0u)) == 0u, "Index encoding error"); |
| 110 | static_assert(DecodeIndex(EncodeIndex(1u)) == 1u, "Index encoding error"); |
| 111 | static_assert(DecodeIndex(EncodeIndex(2u)) == 2u, "Index encoding error"); |
| 112 | static_assert(DecodeIndex(EncodeIndex(3u)) == 3u, "Index encoding error"); |
| 113 | } |
| 114 | |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 115 | bool IndirectReferenceTable::IsValid() const { |
| 116 | return table_mem_map_.get() != nullptr; |
| 117 | } |
| 118 | |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 119 | IndirectRef IndirectReferenceTable::Add(uint32_t cookie, ObjPtr<mirror::Object> obj) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 120 | IRTSegmentState prevState; |
| 121 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 122 | size_t topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 123 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 124 | CHECK(obj != nullptr); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 125 | VerifyObject(obj); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | DCHECK(table_ != nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 127 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 128 | |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 129 | if (topIndex == max_entries_) { |
Andreas Gampe | 90a32b1 | 2016-10-03 19:47:08 -0700 | [diff] [blame] | 130 | LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow " |
| 131 | << "(max=" << max_entries_ << ")\n" |
| 132 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 135 | // We know there's enough room in the table. Now we just need to find |
| 136 | // the right spot. If there's a hole, find it and fill it; otherwise, |
| 137 | // add to the end of the list. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 138 | IndirectRef result; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 139 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 140 | size_t index; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 141 | if (numHoles > 0) { |
| 142 | DCHECK_GT(topIndex, 1U); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 143 | // Find the first hole; likely to be near the end of the list. |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 144 | IrtEntry* pScan = &table_[topIndex - 1]; |
| 145 | DCHECK(!pScan->GetReference()->IsNull()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 146 | --pScan; |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 147 | while (!pScan->GetReference()->IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 148 | DCHECK_GE(pScan, table_ + prevState.parts.topIndex); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 149 | --pScan; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 150 | } |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 151 | index = pScan - table_; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 152 | segment_state_.parts.numHoles--; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 153 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 154 | // Add to the end. |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 155 | index = topIndex++; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 156 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 157 | } |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 158 | table_[index].Add(obj); |
| 159 | result = ToIndirectRef(index); |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 160 | if ((false)) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 161 | LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex |
| 162 | << " holes=" << segment_state_.parts.numHoles; |
| 163 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 164 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 165 | DCHECK(result != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 166 | return result; |
| 167 | } |
| 168 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 169 | void IndirectReferenceTable::AssertEmpty() { |
Hiroshi Yamauchi | 8a74117 | 2014-09-08 13:22:56 -0700 | [diff] [blame] | 170 | for (size_t i = 0; i < Capacity(); ++i) { |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 171 | if (!table_[i].GetReference()->IsNull()) { |
Hiroshi Yamauchi | 8a74117 | 2014-09-08 13:22:56 -0700 | [diff] [blame] | 172 | LOG(FATAL) << "Internal Error: non-empty local reference table\n" |
| 173 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 174 | UNREACHABLE(); |
Hiroshi Yamauchi | 8a74117 | 2014-09-08 13:22:56 -0700 | [diff] [blame] | 175 | } |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 179 | // Removes an object. We extract the table offset bits from "iref" |
| 180 | // and zap the corresponding entry, leaving a hole if it's not at the top. |
| 181 | // If the entry is not between the current top index and the bottom index |
| 182 | // specified by the cookie, we don't remove anything. This is the behavior |
| 183 | // required by JNI's DeleteLocalRef function. |
| 184 | // This method is not called when a local frame is popped; this is only used |
| 185 | // for explicit single removals. |
| 186 | // Returns "false" if nothing was removed. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 187 | bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) { |
| 188 | IRTSegmentState prevState; |
| 189 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 190 | int topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 191 | int bottomIndex = prevState.parts.topIndex; |
| 192 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 193 | DCHECK(table_ != nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 194 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 195 | |
Mathieu Chartier | c263bf8 | 2015-04-29 09:57:48 -0700 | [diff] [blame] | 196 | if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) { |
| 197 | auto* self = Thread::Current(); |
| 198 | if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) { |
| 199 | auto* env = self->GetJniEnv(); |
| 200 | DCHECK(env != nullptr); |
| 201 | if (env->check_jni) { |
Mathieu Chartier | ff6d8cf | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 202 | ScopedObjectAccess soa(self); |
| 203 | LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread"; |
Mathieu Chartier | 2ada67b | 2015-07-30 11:41:04 -0700 | [diff] [blame] | 204 | if (kDumpStackOnNonLocalReference) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 205 | self->Dump(LOG_STREAM(WARNING)); |
Mathieu Chartier | 2ada67b | 2015-07-30 11:41:04 -0700 | [diff] [blame] | 206 | } |
Mathieu Chartier | c263bf8 | 2015-04-29 09:57:48 -0700 | [diff] [blame] | 207 | } |
| 208 | return true; |
| 209 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 210 | } |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 211 | const int idx = ExtractIndex(iref); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 212 | if (idx < bottomIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 213 | // Wrong segment. |
| 214 | LOG(WARNING) << "Attempt to remove index outside index area (" << idx |
| 215 | << " vs " << bottomIndex << "-" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | if (idx >= topIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 219 | // Bad --- stale reference? |
| 220 | LOG(WARNING) << "Attempt to remove invalid index " << idx |
| 221 | << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 225 | if (idx == topIndex - 1) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 226 | // Top-most entry. Scan up and consume holes. |
| 227 | |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 228 | if (!CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 232 | *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 233 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 234 | if (numHoles != 0) { |
| 235 | while (--topIndex > bottomIndex && numHoles != 0) { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 236 | if ((false)) { |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 237 | LOG(INFO) << "+++ checking for hole at " << topIndex - 1 |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 238 | << " (cookie=" << cookie << ") val=" |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 239 | << table_[topIndex - 1].GetReference()->Read<kWithoutReadBarrier>(); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 240 | } |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 241 | if (!table_[topIndex - 1].GetReference()->IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 242 | break; |
| 243 | } |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 244 | if ((false)) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 245 | LOG(INFO) << "+++ ate hole at " << (topIndex - 1); |
| 246 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 247 | numHoles--; |
| 248 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 249 | segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles; |
| 250 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 251 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 252 | segment_state_.parts.topIndex = topIndex-1; |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 253 | if ((false)) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 254 | LOG(INFO) << "+++ ate last entry " << topIndex - 1; |
| 255 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 256 | } |
| 257 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 258 | // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody |
| 259 | // from deleting it twice and screwing up the hole count. |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 260 | if (table_[idx].GetReference()->IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 261 | LOG(INFO) << "--- WEIRD: removing null entry " << idx; |
| 262 | return false; |
| 263 | } |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 264 | if (!CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 268 | *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 269 | segment_state_.parts.numHoles++; |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 270 | if ((false)) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 271 | LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles; |
| 272 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 278 | void IndirectReferenceTable::Trim() { |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 279 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 280 | const size_t top_index = Capacity(); |
| 281 | auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize); |
| 282 | uint8_t* release_end = table_mem_map_->End(); |
| 283 | madvise(release_start, release_end - release_start, MADV_DONTNEED); |
| 284 | } |
| 285 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 286 | void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 287 | BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 288 | for (auto ref : *this) { |
Mathieu Chartier | 9086b65 | 2015-04-14 09:35:18 -0700 | [diff] [blame] | 289 | if (!ref->IsNull()) { |
| 290 | root_visitor.VisitRoot(*ref); |
| 291 | DCHECK(!ref->IsNull()); |
| 292 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 296 | void IndirectReferenceTable::Dump(std::ostream& os) const { |
| 297 | os << kind_ << " table dump:\n"; |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 298 | ReferenceTable::Table entries; |
| 299 | for (size_t i = 0; i < Capacity(); ++i) { |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 300 | ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 301 | if (obj != nullptr) { |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 302 | obj = table_[i].GetReference()->Read(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 303 | entries.push_back(GcRoot<mirror::Object>(obj)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 306 | ReferenceTable::Dump(os, entries); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 309 | } // namespace art |