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 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 20 | #include "reference_table.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 21 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 24 | #include "utils.h" |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 25 | #include "verify_object-inl.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 26 | |
| 27 | #include <cstdlib> |
| 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 31 | template<typename T> |
| 32 | class MutatorLockedDumpable { |
| 33 | public: |
| 34 | explicit MutatorLockedDumpable(T& value) |
| 35 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) { |
| 36 | } |
| 37 | |
| 38 | void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 39 | value_.Dump(os); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | T& value_; |
| 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable); |
| 46 | }; |
| 47 | |
| 48 | template<typename T> |
| 49 | std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs) |
| 50 | // TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis |
| 51 | // currently fails for this. |
| 52 | NO_THREAD_SAFETY_ANALYSIS { |
| 53 | rhs.Dump(os); |
| 54 | return os; |
| 55 | } |
| 56 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 57 | void IndirectReferenceTable::AbortIfNoCheckJNI() { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 58 | // If -Xcheck:jni is on, it'll give a more detailed error before aborting. |
| 59 | if (!Runtime::Current()->GetJavaVM()->check_jni) { |
| 60 | // Otherwise, we want to abort rather than hand back a bad reference. |
| 61 | LOG(FATAL) << "JNI ERROR (app bug): see above."; |
| 62 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | IndirectReferenceTable::IndirectReferenceTable(size_t initialCount, |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 66 | size_t maxCount, IndirectRefKind desiredKind) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 67 | CHECK_GT(initialCount, 0U); |
| 68 | CHECK_LE(initialCount, maxCount); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 69 | CHECK_NE(desiredKind, kHandleScopeOrInvalid); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 70 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 71 | std::string error_str; |
| 72 | const size_t initial_bytes = initialCount * sizeof(const mirror::Object*); |
| 73 | const size_t table_bytes = maxCount * sizeof(const mirror::Object*); |
| 74 | table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes, |
| 75 | PROT_READ | PROT_WRITE, false, &error_str)); |
| 76 | CHECK(table_mem_map_.get() != nullptr) << error_str; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 77 | CHECK_EQ(table_mem_map_->Size(), table_bytes); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 78 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 79 | table_ = reinterpret_cast<GcRoot<mirror::Object>*>(table_mem_map_->Begin()); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 80 | CHECK(table_ != nullptr); |
| 81 | memset(table_, 0xd1, initial_bytes); |
| 82 | |
| 83 | const size_t slot_bytes = maxCount * sizeof(IndirectRefSlot); |
| 84 | slot_mem_map_.reset(MemMap::MapAnonymous("indirect ref table slots", nullptr, slot_bytes, |
| 85 | PROT_READ | PROT_WRITE, false, &error_str)); |
| 86 | CHECK(slot_mem_map_.get() != nullptr) << error_str; |
| 87 | slot_data_ = reinterpret_cast<IndirectRefSlot*>(slot_mem_map_->Begin()); |
| 88 | CHECK(slot_data_ != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 89 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 90 | segment_state_.all = IRT_FIRST_SEGMENT; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 91 | alloc_entries_ = initialCount; |
| 92 | max_entries_ = maxCount; |
| 93 | kind_ = desiredKind; |
| 94 | } |
| 95 | |
| 96 | IndirectReferenceTable::~IndirectReferenceTable() { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 99 | IndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 100 | IRTSegmentState prevState; |
| 101 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 102 | size_t topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 103 | |
Mathieu Chartier | cd2cfff | 2013-12-19 15:46:55 -0800 | [diff] [blame] | 104 | CHECK(obj != NULL); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 105 | VerifyObject(obj); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 106 | DCHECK(table_ != NULL); |
| 107 | DCHECK_LE(alloc_entries_, max_entries_); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 108 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 109 | |
| 110 | if (topIndex == alloc_entries_) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 111 | // reached end of allocated space; did we hit buffer max? |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 112 | if (topIndex == max_entries_) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 113 | LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow " |
| 114 | << "(max=" << max_entries_ << ")\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 115 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | size_t newSize = alloc_entries_ * 2; |
| 119 | if (newSize > max_entries_) { |
| 120 | newSize = max_entries_; |
| 121 | } |
| 122 | DCHECK_GT(newSize, alloc_entries_); |
| 123 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 124 | alloc_entries_ = newSize; |
| 125 | } |
| 126 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 127 | // We know there's enough room in the table. Now we just need to find |
| 128 | // the right spot. If there's a hole, find it and fill it; otherwise, |
| 129 | // add to the end of the list. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 130 | IndirectRef result; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 131 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 132 | if (numHoles > 0) { |
| 133 | DCHECK_GT(topIndex, 1U); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 134 | // Find the first hole; likely to be near the end of the list. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 135 | GcRoot<mirror::Object>* pScan = &table_[topIndex - 1]; |
| 136 | DCHECK(!pScan->IsNull()); |
| 137 | --pScan; |
| 138 | while (!pScan->IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 139 | DCHECK_GE(pScan, table_ + prevState.parts.topIndex); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 140 | --pScan; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 141 | } |
| 142 | UpdateSlotAdd(obj, pScan - table_); |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 143 | result = ToIndirectRef(pScan - table_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 144 | *pScan = GcRoot<mirror::Object>(obj); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 145 | segment_state_.parts.numHoles--; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 146 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 147 | // Add to the end. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 148 | UpdateSlotAdd(obj, topIndex); |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 149 | result = ToIndirectRef(topIndex); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 150 | table_[topIndex++] = GcRoot<mirror::Object>(obj); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 151 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 152 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 153 | if (false) { |
| 154 | LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex |
| 155 | << " holes=" << segment_state_.parts.numHoles; |
| 156 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 157 | |
| 158 | DCHECK(result != NULL); |
| 159 | return result; |
| 160 | } |
| 161 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 162 | void IndirectReferenceTable::AssertEmpty() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 163 | if (UNLIKELY(begin() != end())) { |
| 164 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 165 | LOG(FATAL) << "Internal Error: non-empty local reference table\n" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 166 | << MutatorLockedDumpable<IndirectReferenceTable>(*this); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 170 | // Removes an object. We extract the table offset bits from "iref" |
| 171 | // and zap the corresponding entry, leaving a hole if it's not at the top. |
| 172 | // If the entry is not between the current top index and the bottom index |
| 173 | // specified by the cookie, we don't remove anything. This is the behavior |
| 174 | // required by JNI's DeleteLocalRef function. |
| 175 | // This method is not called when a local frame is popped; this is only used |
| 176 | // for explicit single removals. |
| 177 | // Returns "false" if nothing was removed. |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 178 | bool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) { |
| 179 | IRTSegmentState prevState; |
| 180 | prevState.all = cookie; |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 181 | int topIndex = segment_state_.parts.topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 182 | int bottomIndex = prevState.parts.topIndex; |
| 183 | |
| 184 | DCHECK(table_ != NULL); |
| 185 | DCHECK_LE(alloc_entries_, max_entries_); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 186 | DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 187 | |
| 188 | int idx = ExtractIndex(iref); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 189 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 190 | if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid && |
| 191 | Thread::Current()->HandleScopeContains(reinterpret_cast<jobject>(iref))) { |
| 192 | LOG(WARNING) << "Attempt to remove local handle scope entry from IRT, ignoring"; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 195 | |
| 196 | if (idx < bottomIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 197 | // Wrong segment. |
| 198 | LOG(WARNING) << "Attempt to remove index outside index area (" << idx |
| 199 | << " vs " << bottomIndex << "-" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | if (idx >= topIndex) { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 203 | // Bad --- stale reference? |
| 204 | LOG(WARNING) << "Attempt to remove invalid index " << idx |
| 205 | << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (idx == topIndex-1) { |
| 210 | // Top-most entry. Scan up and consume holes. |
| 211 | |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 212 | if (!CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 216 | table_[idx] = GcRoot<mirror::Object>(nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 217 | int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 218 | if (numHoles != 0) { |
| 219 | while (--topIndex > bottomIndex && numHoles != 0) { |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 220 | if (false) { |
| 221 | LOG(INFO) << "+++ checking for hole at " << topIndex-1 |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 222 | << " (cookie=" << cookie << ") val=" |
| 223 | << table_[topIndex - 1].Read<kWithoutReadBarrier>(); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 224 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 225 | if (!table_[topIndex-1].IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 228 | if (false) { |
| 229 | LOG(INFO) << "+++ ate hole at " << (topIndex - 1); |
| 230 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 231 | numHoles--; |
| 232 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 233 | segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles; |
| 234 | segment_state_.parts.topIndex = topIndex; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 235 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 236 | segment_state_.parts.topIndex = topIndex-1; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 237 | if (false) { |
| 238 | LOG(INFO) << "+++ ate last entry " << topIndex - 1; |
| 239 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 240 | } |
| 241 | } else { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 242 | // Not the top-most entry. This creates a hole. We NULL out the |
| 243 | // entry to prevent somebody from deleting it twice and screwing up |
| 244 | // the hole count. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 245 | if (table_[idx].IsNull()) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 246 | LOG(INFO) << "--- WEIRD: removing null entry " << idx; |
| 247 | return false; |
| 248 | } |
Ian Rogers | 987560f | 2014-04-22 11:42:59 -0700 | [diff] [blame] | 249 | if (!CheckEntry("remove", iref, idx)) { |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 253 | table_[idx] = GcRoot<mirror::Object>(nullptr); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 254 | segment_state_.parts.numHoles++; |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 255 | if (false) { |
| 256 | LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles; |
| 257 | } |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 263 | void IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid, |
| 264 | RootType root_type) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 265 | for (auto ref : *this) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 266 | callback(ref, arg, tid, root_type); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 267 | DCHECK(*ref != nullptr); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 271 | void IndirectReferenceTable::Dump(std::ostream& os) const { |
| 272 | os << kind_ << " table dump:\n"; |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 273 | ReferenceTable::Table entries; |
| 274 | for (size_t i = 0; i < Capacity(); ++i) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 275 | mirror::Object* obj = table_[i].Read<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 276 | if (UNLIKELY(obj == nullptr)) { |
| 277 | // Remove NULLs. |
| 278 | } else if (UNLIKELY(obj == kClearedJniWeakGlobal)) { |
| 279 | // ReferenceTable::Dump() will handle kClearedJniWeakGlobal |
| 280 | // while the read barrier won't. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 281 | entries.push_back(GcRoot<mirror::Object>(obj)); |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 282 | } else { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 283 | obj = table_[i].Read(); |
| 284 | entries.push_back(GcRoot<mirror::Object>(obj)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 287 | ReferenceTable::Dump(os, entries); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 290 | } // namespace art |