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