blob: 3ed51f7b8f9d7bed1ef71ea823d0e12fb73921ea [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
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 Chartierc56057e2014-05-04 13:18:58 -070017#include "indirect_reference_table-inl.h"
18
Mathieu Chartier8778c522016-10-04 19:06:30 -070019#include "base/dumpable-inl.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080020#include "base/systrace.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070021#include "jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070022#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070024#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070026#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070027#include "utils.h"
Mathieu Chartier6dda8982014-03-06 11:11:48 -080028#include "verify_object-inl.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070029
30#include <cstdlib>
31
32namespace art {
33
Mathieu Chartier2ada67b2015-07-30 11:41:04 -070034static constexpr bool kDumpStackOnNonLocalReference = false;
Andreas Gampee03662b2016-10-13 17:12:56 -070035static constexpr bool kDebugIRT = false;
Mathieu Chartier2ada67b2015-07-30 11:41:04 -070036
Andreas Gampef1e86302016-10-03 11:42:31 -070037const char* GetIndirectRefKindString(const IndirectRefKind& kind) {
38 switch (kind) {
39 case kHandleScopeOrInvalid:
40 return "HandleScopeOrInvalid";
41 case kLocal:
42 return "Local";
43 case kGlobal:
44 return "Global";
45 case kWeakGlobal:
46 return "WeakGlobal";
47 }
48 return "IndirectRefKind Error";
49}
50
Andreas Gampef1e86302016-10-03 11:42:31 -070051void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
Elliott Hughesa2501992011-08-26 19:39:54 -070052 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070053 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
54 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070055 // Otherwise, we want to abort rather than hand back a bad reference.
Andreas Gampef1e86302016-10-03 11:42:31 -070056 LOG(FATAL) << msg;
57 } else {
58 LOG(ERROR) << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070059 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070060}
61
Andreas Gampea8e3b862016-10-17 20:12:52 -070062IndirectReferenceTable::IndirectReferenceTable(size_t max_count,
63 IndirectRefKind desired_kind,
Richard Uhlerda0a69e2016-10-11 15:06:38 +010064 std::string* error_msg)
Andreas Gampee03662b2016-10-13 17:12:56 -070065 : segment_state_(kIRTFirstSegment),
66 kind_(desired_kind),
67 max_entries_(max_count),
68 current_num_holes_(0) {
Richard Uhlerda0a69e2016-10-11 15:06:38 +010069 CHECK(error_msg != nullptr);
Andreas Gampea8e3b862016-10-17 20:12:52 -070070 CHECK_NE(desired_kind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071
Andreas Gampea8e3b862016-10-17 20:12:52 -070072 const size_t table_bytes = max_count * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070073 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Richard Uhlerda0a69e2016-10-11 15:06:38 +010074 PROT_READ | PROT_WRITE, false, false, error_msg));
75 if (table_mem_map_.get() == nullptr && error_msg->empty()) {
76 *error_msg = "Unable to map memory for indirect ref table";
Andreas Gampe3f5881f2015-04-08 10:26:16 -070077 }
Richard Uhlerda0a69e2016-10-11 15:06:38 +010078
79 if (table_mem_map_.get() != nullptr) {
80 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
81 } else {
82 table_ = nullptr;
83 }
Andreas Gampee03662b2016-10-13 17:12:56 -070084 segment_state_ = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070085}
86
87IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070088}
89
Andreas Gampedc061d02016-10-24 13:19:37 -070090void IndirectReferenceTable::ConstexprChecks() {
91 // Use this for some assertions. They can't be put into the header as C++ wants the class
92 // to be complete.
93
94 // Check kind.
95 static_assert((EncodeIndirectRefKind(kLocal) & (~kKindMask)) == 0, "Kind encoding error");
96 static_assert((EncodeIndirectRefKind(kGlobal) & (~kKindMask)) == 0, "Kind encoding error");
97 static_assert((EncodeIndirectRefKind(kWeakGlobal) & (~kKindMask)) == 0, "Kind encoding error");
98 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kLocal)) == kLocal,
99 "Kind encoding error");
100 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kGlobal)) == kGlobal,
101 "Kind encoding error");
102 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kWeakGlobal)) == kWeakGlobal,
103 "Kind encoding error");
104
105 // Check serial.
106 static_assert(DecodeSerial(EncodeSerial(0u)) == 0u, "Serial encoding error");
107 static_assert(DecodeSerial(EncodeSerial(1u)) == 1u, "Serial encoding error");
108 static_assert(DecodeSerial(EncodeSerial(2u)) == 2u, "Serial encoding error");
109 static_assert(DecodeSerial(EncodeSerial(3u)) == 3u, "Serial encoding error");
110
111 // Table index.
112 static_assert(DecodeIndex(EncodeIndex(0u)) == 0u, "Index encoding error");
113 static_assert(DecodeIndex(EncodeIndex(1u)) == 1u, "Index encoding error");
114 static_assert(DecodeIndex(EncodeIndex(2u)) == 2u, "Index encoding error");
115 static_assert(DecodeIndex(EncodeIndex(3u)) == 3u, "Index encoding error");
116}
117
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700118bool IndirectReferenceTable::IsValid() const {
119 return table_mem_map_.get() != nullptr;
120}
121
Andreas Gampee03662b2016-10-13 17:12:56 -0700122// Holes:
123//
124// To keep the IRT compact, we want to fill "holes" created by non-stack-discipline Add & Remove
125// operation sequences. For simplicity and lower memory overhead, we do not use a free list or
126// similar. Instead, we scan for holes, with the expectation that we will find holes fast as they
127// are usually near the end of the table (see the header, TODO: verify this assumption). To avoid
128// scans when there are no holes, the number of known holes should be tracked.
129//
130// A previous implementation stored the top index and the number of holes as the segment state.
131// This constraints the maximum number of references to 16-bit. We want to relax this, as it
132// is easy to require more references (e.g., to list all classes in large applications). Thus,
133// the implicitly stack-stored state, the IRTSegmentState, is only the top index.
134//
135// Thus, hole count is a local property of the current segment, and needs to be recovered when
136// (or after) a frame is pushed or popped. To keep JNI transitions simple (and inlineable), we
137// cannot do work when the segment changes. Thus, Add and Remove need to ensure the current
138// hole count is correct.
139//
140// To be able to detect segment changes, we require an additional local field that can describe
141// the known segment. This is last_known_previous_state_. The requirement will become clear with
142// the following (some non-trivial) cases that have to be supported:
143//
144// 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
145// 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
146// 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
147// reference
148// 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
149// 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
150// reference
151//
152// Storing the last known *previous* state (bottom index) allows conservatively detecting all the
153// segment changes above. The condition is simply that the last known state is greater than or
154// equal to the current previous state, and smaller than the current state (top index). The
155// condition is conservative as it adds O(1) overhead to operations on an empty segment.
156
157static size_t CountNullEntries(const IrtEntry* table, size_t from, size_t to) {
158 size_t count = 0;
159 for (size_t index = from; index != to; ++index) {
160 if (table[index].GetReference()->IsNull()) {
161 count++;
162 }
163 }
164 return count;
165}
166
167void IndirectReferenceTable::RecoverHoles(IRTSegmentState prev_state) {
168 if (last_known_previous_state_.top_index >= segment_state_.top_index ||
169 last_known_previous_state_.top_index < prev_state.top_index) {
170 const size_t top_index = segment_state_.top_index;
171 size_t count = CountNullEntries(table_, prev_state.top_index, top_index);
172
173 if (kDebugIRT) {
174 LOG(INFO) << "+++ Recovered holes: "
175 << " Current prev=" << prev_state.top_index
176 << " Current top_index=" << top_index
177 << " Old num_holes=" << current_num_holes_
178 << " New num_holes=" << count;
179 }
180
181 current_num_holes_ = count;
182 last_known_previous_state_ = prev_state;
183 } else if (kDebugIRT) {
184 LOG(INFO) << "No need to recover holes";
185 }
186}
187
188ALWAYS_INLINE
189static inline void CheckHoleCount(IrtEntry* table,
190 size_t exp_num_holes,
191 IRTSegmentState prev_state,
192 IRTSegmentState cur_state) {
193 if (kIsDebugBuild) {
194 size_t count = CountNullEntries(table, prev_state.top_index, cur_state.top_index);
195 CHECK_EQ(exp_num_holes, count) << "prevState=" << prev_state.top_index
196 << " topIndex=" << cur_state.top_index;
197 }
198}
199
200bool IndirectReferenceTable::Resize(size_t new_size, std::string* error_msg) {
201 CHECK_GT(new_size, max_entries_);
202
203 const size_t table_bytes = new_size * sizeof(IrtEntry);
204 std::unique_ptr<MemMap> new_map(MemMap::MapAnonymous("indirect ref table",
205 nullptr,
206 table_bytes,
207 PROT_READ | PROT_WRITE,
208 false,
209 false,
210 error_msg));
211 if (new_map == nullptr) {
212 return false;
213 }
214
215 memcpy(new_map->Begin(), table_mem_map_->Begin(), table_mem_map_->Size());
216 table_mem_map_ = std::move(new_map);
217 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
218 max_entries_ = new_size;
219
220 return true;
221}
222
223IndirectRef IndirectReferenceTable::Add(IRTSegmentState previous_state,
224 ObjPtr<mirror::Object> obj) {
225 if (kDebugIRT) {
226 LOG(INFO) << "+++ Add: previous_state=" << previous_state.top_index
227 << " top_index=" << segment_state_.top_index
228 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
229 << " holes=" << current_num_holes_;
230 }
231
232 size_t top_index = segment_state_.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700233
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 CHECK(obj != nullptr);
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700235 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700236 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700237
Andreas Gampee03662b2016-10-13 17:12:56 -0700238 if (top_index == max_entries_) {
Andreas Gampe90a32b12016-10-03 19:47:08 -0700239 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
240 << "(max=" << max_entries_ << ")\n"
241 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Andreas Gampee03662b2016-10-13 17:12:56 -0700242 UNREACHABLE();
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700243 }
244
Andreas Gampee03662b2016-10-13 17:12:56 -0700245 RecoverHoles(previous_state);
246 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
247
Elliott Hughes73e66f72012-05-09 09:34:45 -0700248 // We know there's enough room in the table. Now we just need to find
249 // the right spot. If there's a hole, find it and fill it; otherwise,
250 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700251 IndirectRef result;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700252 size_t index;
Andreas Gampee03662b2016-10-13 17:12:56 -0700253 if (current_num_holes_ > 0) {
254 DCHECK_GT(top_index, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700255 // Find the first hole; likely to be near the end of the list.
Andreas Gampee03662b2016-10-13 17:12:56 -0700256 IrtEntry* p_scan = &table_[top_index - 1];
257 DCHECK(!p_scan->GetReference()->IsNull());
258 --p_scan;
259 while (!p_scan->GetReference()->IsNull()) {
260 DCHECK_GE(p_scan, table_ + previous_state.top_index);
261 --p_scan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700262 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700263 index = p_scan - table_;
264 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700265 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700266 // Add to the end.
Andreas Gampee03662b2016-10-13 17:12:56 -0700267 index = top_index++;
268 segment_state_.top_index = top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700269 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700270 table_[index].Add(obj);
271 result = ToIndirectRef(index);
Andreas Gampee03662b2016-10-13 17:12:56 -0700272 if (kDebugIRT) {
273 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.top_index
274 << " holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700275 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700276
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700277 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700278 return result;
279}
280
Elliott Hughes726079d2011-10-07 18:43:44 -0700281void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700282 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700283 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700284 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
285 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700286 UNREACHABLE();
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700287 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700288 }
289}
290
Elliott Hughes73e66f72012-05-09 09:34:45 -0700291// Removes an object. We extract the table offset bits from "iref"
292// and zap the corresponding entry, leaving a hole if it's not at the top.
293// If the entry is not between the current top index and the bottom index
294// specified by the cookie, we don't remove anything. This is the behavior
295// required by JNI's DeleteLocalRef function.
296// This method is not called when a local frame is popped; this is only used
297// for explicit single removals.
298// Returns "false" if nothing was removed.
Andreas Gampee03662b2016-10-13 17:12:56 -0700299bool IndirectReferenceTable::Remove(IRTSegmentState previous_state, IndirectRef iref) {
300 if (kDebugIRT) {
301 LOG(INFO) << "+++ Remove: previous_state=" << previous_state.top_index
302 << " top_index=" << segment_state_.top_index
303 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
304 << " holes=" << current_num_holes_;
305 }
306
307 const uint32_t top_index = segment_state_.top_index;
308 const uint32_t bottom_index = previous_state.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700309
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700310 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700311
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700312 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
313 auto* self = Thread::Current();
314 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
315 auto* env = self->GetJniEnv();
316 DCHECK(env != nullptr);
317 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700318 ScopedObjectAccess soa(self);
319 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700320 if (kDumpStackOnNonLocalReference) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700321 self->Dump(LOG_STREAM(WARNING));
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700322 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700323 }
324 return true;
325 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700326 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700327 const uint32_t idx = ExtractIndex(iref);
328 if (idx < bottom_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700329 // Wrong segment.
330 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700331 << " vs " << bottom_index << "-" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700332 return false;
333 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700334 if (idx >= top_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700335 // Bad --- stale reference?
336 LOG(WARNING) << "Attempt to remove invalid index " << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700337 << " (bottom=" << bottom_index << " top=" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700338 return false;
339 }
340
Andreas Gampee03662b2016-10-13 17:12:56 -0700341 RecoverHoles(previous_state);
342 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
343
344 if (idx == top_index - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700345 // Top-most entry. Scan up and consume holes.
346
Ian Rogers987560f2014-04-22 11:42:59 -0700347 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700348 return false;
349 }
350
Mathieu Chartier4838d662014-09-25 15:27:43 -0700351 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700352 if (current_num_holes_ != 0) {
353 uint32_t collapse_top_index = top_index;
354 while (--collapse_top_index > bottom_index && current_num_holes_ != 0) {
355 if (kDebugIRT) {
356 ScopedObjectAccess soa(Thread::Current());
357 LOG(INFO) << "+++ checking for hole at " << collapse_top_index - 1
358 << " (previous_state=" << bottom_index << ") val="
359 << table_[collapse_top_index - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700360 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700361 if (!table_[collapse_top_index - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700362 break;
363 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700364 if (kDebugIRT) {
365 LOG(INFO) << "+++ ate hole at " << (collapse_top_index - 1);
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700366 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700367 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700368 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700369 segment_state_.top_index = collapse_top_index;
370
371 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700372 } else {
Andreas Gampee03662b2016-10-13 17:12:56 -0700373 segment_state_.top_index = top_index - 1;
374 if (kDebugIRT) {
375 LOG(INFO) << "+++ ate last entry " << top_index - 1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700376 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700377 }
378 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
380 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700381 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700382 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
383 return false;
384 }
Ian Rogers987560f2014-04-22 11:42:59 -0700385 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700386 return false;
387 }
388
Mathieu Chartier4838d662014-09-25 15:27:43 -0700389 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700390 current_num_holes_++;
391 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
392 if (kDebugIRT) {
393 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700394 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700395 }
396
397 return true;
398}
399
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800400void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800401 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800402 const size_t top_index = Capacity();
403 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
404 uint8_t* release_end = table_mem_map_->End();
405 madvise(release_start, release_end - release_start, MADV_DONTNEED);
406}
407
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700408void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700409 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700410 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700411 if (!ref->IsNull()) {
412 root_visitor.VisitRoot(*ref);
413 DCHECK(!ref->IsNull());
414 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700415 }
416}
417
Elliott Hughes73e66f72012-05-09 09:34:45 -0700418void IndirectReferenceTable::Dump(std::ostream& os) const {
419 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700420 ReferenceTable::Table entries;
421 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier8778c522016-10-04 19:06:30 -0700422 ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700423 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700424 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700425 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700426 }
427 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700428 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700429}
430
Andreas Gampee03662b2016-10-13 17:12:56 -0700431void IndirectReferenceTable::SetSegmentState(IRTSegmentState new_state) {
432 if (kDebugIRT) {
433 LOG(INFO) << "Setting segment state: "
434 << segment_state_.top_index
435 << " -> "
436 << new_state.top_index;
437 }
438 segment_state_ = new_state;
439}
440
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700441} // namespace art