blob: cff3ea7ecd7179c20648539d6dde92ea74b2d83c [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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070021#include "java_vm_ext.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070022#include "jni_internal.h"
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070023#include "nth_caller_visitor.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024#include "reference_table.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070025#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Ian Rogers5a7a74a2011-09-26 16:32:29 -070027#include "thread.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070028#include "utils.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 Gampe0ece10d2017-05-31 20:09:28 -070037// Maximum table size we allow.
38static constexpr size_t kMaxTableSizeInBytes = 128 * MB;
39
Andreas Gampef1e86302016-10-03 11:42:31 -070040const char* GetIndirectRefKindString(const IndirectRefKind& kind) {
41 switch (kind) {
42 case kHandleScopeOrInvalid:
43 return "HandleScopeOrInvalid";
44 case kLocal:
45 return "Local";
46 case kGlobal:
47 return "Global";
48 case kWeakGlobal:
49 return "WeakGlobal";
50 }
51 return "IndirectRefKind Error";
52}
53
Andreas Gampef1e86302016-10-03 11:42:31 -070054void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) {
Elliott Hughesa2501992011-08-26 19:39:54 -070055 // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
Ian Rogers68d8b422014-07-17 11:09:10 -070056 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
57 if (!vm->IsCheckJniEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -070058 // Otherwise, we want to abort rather than hand back a bad reference.
Andreas Gampef1e86302016-10-03 11:42:31 -070059 LOG(FATAL) << msg;
60 } else {
61 LOG(ERROR) << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070062 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -070063}
64
Andreas Gampea8e3b862016-10-17 20:12:52 -070065IndirectReferenceTable::IndirectReferenceTable(size_t max_count,
66 IndirectRefKind desired_kind,
Andreas Gampe9d7ef622016-10-24 19:35:19 -070067 ResizableCapacity resizable,
Richard Uhlerda0a69e2016-10-11 15:06:38 +010068 std::string* error_msg)
Andreas Gampee03662b2016-10-13 17:12:56 -070069 : segment_state_(kIRTFirstSegment),
70 kind_(desired_kind),
71 max_entries_(max_count),
Andreas Gampe9d7ef622016-10-24 19:35:19 -070072 current_num_holes_(0),
73 resizable_(resizable) {
Richard Uhlerda0a69e2016-10-11 15:06:38 +010074 CHECK(error_msg != nullptr);
Andreas Gampea8e3b862016-10-17 20:12:52 -070075 CHECK_NE(desired_kind, kHandleScopeOrInvalid);
Elliott Hughes6c1a3942011-08-17 15:00:06 -070076
Andreas Gampe0ece10d2017-05-31 20:09:28 -070077 // Overflow and maximum check.
78 CHECK_LE(max_count, kMaxTableSizeInBytes / sizeof(IrtEntry));
79
Andreas Gampea8e3b862016-10-17 20:12:52 -070080 const size_t table_bytes = max_count * sizeof(IrtEntry);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 table_mem_map_.reset(MemMap::MapAnonymous("indirect ref table", nullptr, table_bytes,
Richard Uhlerda0a69e2016-10-11 15:06:38 +010082 PROT_READ | PROT_WRITE, false, false, error_msg));
83 if (table_mem_map_.get() == nullptr && error_msg->empty()) {
84 *error_msg = "Unable to map memory for indirect ref table";
Andreas Gampe3f5881f2015-04-08 10:26:16 -070085 }
Richard Uhlerda0a69e2016-10-11 15:06:38 +010086
87 if (table_mem_map_.get() != nullptr) {
88 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
89 } else {
90 table_ = nullptr;
91 }
Andreas Gampee03662b2016-10-13 17:12:56 -070092 segment_state_ = kIRTFirstSegment;
Andreas Gampe94a52022016-10-25 12:01:48 -070093 last_known_previous_state_ = kIRTFirstSegment;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070094}
95
96IndirectReferenceTable::~IndirectReferenceTable() {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070097}
98
Andreas Gampedc061d02016-10-24 13:19:37 -070099void IndirectReferenceTable::ConstexprChecks() {
100 // Use this for some assertions. They can't be put into the header as C++ wants the class
101 // to be complete.
102
103 // Check kind.
104 static_assert((EncodeIndirectRefKind(kLocal) & (~kKindMask)) == 0, "Kind encoding error");
105 static_assert((EncodeIndirectRefKind(kGlobal) & (~kKindMask)) == 0, "Kind encoding error");
106 static_assert((EncodeIndirectRefKind(kWeakGlobal) & (~kKindMask)) == 0, "Kind encoding error");
107 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kLocal)) == kLocal,
108 "Kind encoding error");
109 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kGlobal)) == kGlobal,
110 "Kind encoding error");
111 static_assert(DecodeIndirectRefKind(EncodeIndirectRefKind(kWeakGlobal)) == kWeakGlobal,
112 "Kind encoding error");
113
114 // Check serial.
115 static_assert(DecodeSerial(EncodeSerial(0u)) == 0u, "Serial encoding error");
116 static_assert(DecodeSerial(EncodeSerial(1u)) == 1u, "Serial encoding error");
117 static_assert(DecodeSerial(EncodeSerial(2u)) == 2u, "Serial encoding error");
118 static_assert(DecodeSerial(EncodeSerial(3u)) == 3u, "Serial encoding error");
119
120 // Table index.
121 static_assert(DecodeIndex(EncodeIndex(0u)) == 0u, "Index encoding error");
122 static_assert(DecodeIndex(EncodeIndex(1u)) == 1u, "Index encoding error");
123 static_assert(DecodeIndex(EncodeIndex(2u)) == 2u, "Index encoding error");
124 static_assert(DecodeIndex(EncodeIndex(3u)) == 3u, "Index encoding error");
125}
126
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700127bool IndirectReferenceTable::IsValid() const {
128 return table_mem_map_.get() != nullptr;
129}
130
Andreas Gampee03662b2016-10-13 17:12:56 -0700131// Holes:
132//
133// To keep the IRT compact, we want to fill "holes" created by non-stack-discipline Add & Remove
134// operation sequences. For simplicity and lower memory overhead, we do not use a free list or
135// similar. Instead, we scan for holes, with the expectation that we will find holes fast as they
136// are usually near the end of the table (see the header, TODO: verify this assumption). To avoid
137// scans when there are no holes, the number of known holes should be tracked.
138//
139// A previous implementation stored the top index and the number of holes as the segment state.
140// This constraints the maximum number of references to 16-bit. We want to relax this, as it
141// is easy to require more references (e.g., to list all classes in large applications). Thus,
142// the implicitly stack-stored state, the IRTSegmentState, is only the top index.
143//
144// Thus, hole count is a local property of the current segment, and needs to be recovered when
145// (or after) a frame is pushed or popped. To keep JNI transitions simple (and inlineable), we
146// cannot do work when the segment changes. Thus, Add and Remove need to ensure the current
147// hole count is correct.
148//
149// To be able to detect segment changes, we require an additional local field that can describe
150// the known segment. This is last_known_previous_state_. The requirement will become clear with
151// the following (some non-trivial) cases that have to be supported:
152//
153// 1) Segment with holes (current_num_holes_ > 0), push new segment, add/remove reference
154// 2) Segment with holes (current_num_holes_ > 0), pop segment, add/remove reference
155// 3) Segment with holes (current_num_holes_ > 0), push new segment, pop segment, add/remove
156// reference
157// 4) Empty segment, push new segment, create a hole, pop a segment, add/remove a reference
158// 5) Base segment, push new segment, create a hole, pop a segment, push new segment, add/remove
159// reference
160//
161// Storing the last known *previous* state (bottom index) allows conservatively detecting all the
162// segment changes above. The condition is simply that the last known state is greater than or
163// equal to the current previous state, and smaller than the current state (top index). The
164// condition is conservative as it adds O(1) overhead to operations on an empty segment.
165
166static size_t CountNullEntries(const IrtEntry* table, size_t from, size_t to) {
167 size_t count = 0;
168 for (size_t index = from; index != to; ++index) {
169 if (table[index].GetReference()->IsNull()) {
170 count++;
171 }
172 }
173 return count;
174}
175
176void IndirectReferenceTable::RecoverHoles(IRTSegmentState prev_state) {
177 if (last_known_previous_state_.top_index >= segment_state_.top_index ||
178 last_known_previous_state_.top_index < prev_state.top_index) {
179 const size_t top_index = segment_state_.top_index;
180 size_t count = CountNullEntries(table_, prev_state.top_index, top_index);
181
182 if (kDebugIRT) {
183 LOG(INFO) << "+++ Recovered holes: "
184 << " Current prev=" << prev_state.top_index
185 << " Current top_index=" << top_index
186 << " Old num_holes=" << current_num_holes_
187 << " New num_holes=" << count;
188 }
189
190 current_num_holes_ = count;
191 last_known_previous_state_ = prev_state;
192 } else if (kDebugIRT) {
193 LOG(INFO) << "No need to recover holes";
194 }
195}
196
197ALWAYS_INLINE
198static inline void CheckHoleCount(IrtEntry* table,
199 size_t exp_num_holes,
200 IRTSegmentState prev_state,
201 IRTSegmentState cur_state) {
202 if (kIsDebugBuild) {
203 size_t count = CountNullEntries(table, prev_state.top_index, cur_state.top_index);
204 CHECK_EQ(exp_num_holes, count) << "prevState=" << prev_state.top_index
205 << " topIndex=" << cur_state.top_index;
206 }
207}
208
209bool IndirectReferenceTable::Resize(size_t new_size, std::string* error_msg) {
210 CHECK_GT(new_size, max_entries_);
211
Andreas Gampe0ece10d2017-05-31 20:09:28 -0700212 constexpr size_t kMaxEntries = kMaxTableSizeInBytes / sizeof(IrtEntry);
213 if (new_size > kMaxEntries) {
214 *error_msg = android::base::StringPrintf("Requested size exceeds maximum: %zu", new_size);
215 return false;
216 }
217 // Note: the above check also ensures that there is no overflow below.
218
Andreas Gampee03662b2016-10-13 17:12:56 -0700219 const size_t table_bytes = new_size * sizeof(IrtEntry);
220 std::unique_ptr<MemMap> new_map(MemMap::MapAnonymous("indirect ref table",
221 nullptr,
222 table_bytes,
223 PROT_READ | PROT_WRITE,
224 false,
225 false,
226 error_msg));
227 if (new_map == nullptr) {
228 return false;
229 }
230
231 memcpy(new_map->Begin(), table_mem_map_->Begin(), table_mem_map_->Size());
232 table_mem_map_ = std::move(new_map);
233 table_ = reinterpret_cast<IrtEntry*>(table_mem_map_->Begin());
234 max_entries_ = new_size;
235
236 return true;
237}
238
239IndirectRef IndirectReferenceTable::Add(IRTSegmentState previous_state,
240 ObjPtr<mirror::Object> obj) {
241 if (kDebugIRT) {
242 LOG(INFO) << "+++ Add: previous_state=" << previous_state.top_index
243 << " top_index=" << segment_state_.top_index
244 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
245 << " holes=" << current_num_holes_;
246 }
247
248 size_t top_index = segment_state_.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700249
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700250 CHECK(obj != nullptr);
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700251 VerifyObject(obj);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700252 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700253
Andreas Gampee03662b2016-10-13 17:12:56 -0700254 if (top_index == max_entries_) {
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700255 if (resizable_ == ResizableCapacity::kNo) {
256 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
257 << "(max=" << max_entries_ << ")\n"
258 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
259 UNREACHABLE();
260 }
261
262 // Try to double space.
Andreas Gampe0ece10d2017-05-31 20:09:28 -0700263 if (std::numeric_limits<size_t>::max() / 2 < max_entries_) {
264 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
265 << "(max=" << max_entries_ << ")" << std::endl
266 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
267 << " Resizing failed: exceeds size_t";
268 UNREACHABLE();
269 }
270
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700271 std::string error_msg;
272 if (!Resize(max_entries_ * 2, &error_msg)) {
273 LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
274 << "(max=" << max_entries_ << ")" << std::endl
275 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
276 << " Resizing failed: " << error_msg;
277 UNREACHABLE();
278 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700279 }
280
Andreas Gampee03662b2016-10-13 17:12:56 -0700281 RecoverHoles(previous_state);
282 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
283
Elliott Hughes73e66f72012-05-09 09:34:45 -0700284 // We know there's enough room in the table. Now we just need to find
285 // the right spot. If there's a hole, find it and fill it; otherwise,
286 // add to the end of the list.
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700287 IndirectRef result;
Mathieu Chartier4838d662014-09-25 15:27:43 -0700288 size_t index;
Andreas Gampee03662b2016-10-13 17:12:56 -0700289 if (current_num_holes_ > 0) {
290 DCHECK_GT(top_index, 1U);
Elliott Hughes73e66f72012-05-09 09:34:45 -0700291 // Find the first hole; likely to be near the end of the list.
Andreas Gampee03662b2016-10-13 17:12:56 -0700292 IrtEntry* p_scan = &table_[top_index - 1];
293 DCHECK(!p_scan->GetReference()->IsNull());
294 --p_scan;
295 while (!p_scan->GetReference()->IsNull()) {
296 DCHECK_GE(p_scan, table_ + previous_state.top_index);
297 --p_scan;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700298 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700299 index = p_scan - table_;
300 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700301 } else {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700302 // Add to the end.
Andreas Gampee03662b2016-10-13 17:12:56 -0700303 index = top_index++;
304 segment_state_.top_index = top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700305 }
Mathieu Chartier4838d662014-09-25 15:27:43 -0700306 table_[index].Add(obj);
307 result = ToIndirectRef(index);
Andreas Gampee03662b2016-10-13 17:12:56 -0700308 if (kDebugIRT) {
309 LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.top_index
310 << " holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700311 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700312
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 DCHECK(result != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700314 return result;
315}
316
Elliott Hughes726079d2011-10-07 18:43:44 -0700317void IndirectReferenceTable::AssertEmpty() {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700318 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700319 if (!table_[i].GetReference()->IsNull()) {
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700320 LOG(FATAL) << "Internal Error: non-empty local reference table\n"
321 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700322 UNREACHABLE();
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -0700323 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700324 }
325}
326
Elliott Hughes73e66f72012-05-09 09:34:45 -0700327// Removes an object. We extract the table offset bits from "iref"
328// and zap the corresponding entry, leaving a hole if it's not at the top.
329// If the entry is not between the current top index and the bottom index
330// specified by the cookie, we don't remove anything. This is the behavior
331// required by JNI's DeleteLocalRef function.
332// This method is not called when a local frame is popped; this is only used
333// for explicit single removals.
334// Returns "false" if nothing was removed.
Andreas Gampee03662b2016-10-13 17:12:56 -0700335bool IndirectReferenceTable::Remove(IRTSegmentState previous_state, IndirectRef iref) {
336 if (kDebugIRT) {
337 LOG(INFO) << "+++ Remove: previous_state=" << previous_state.top_index
338 << " top_index=" << segment_state_.top_index
339 << " last_known_prev_top_index=" << last_known_previous_state_.top_index
340 << " holes=" << current_num_holes_;
341 }
342
343 const uint32_t top_index = segment_state_.top_index;
344 const uint32_t bottom_index = previous_state.top_index;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700345
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700346 DCHECK(table_ != nullptr);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700347
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700348 if (GetIndirectRefKind(iref) == kHandleScopeOrInvalid) {
349 auto* self = Thread::Current();
350 if (self->HandleScopeContains(reinterpret_cast<jobject>(iref))) {
351 auto* env = self->GetJniEnv();
352 DCHECK(env != nullptr);
353 if (env->check_jni) {
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700354 ScopedObjectAccess soa(self);
355 LOG(WARNING) << "Attempt to remove non-JNI local reference, dumping thread";
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700356 if (kDumpStackOnNonLocalReference) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700357 self->Dump(LOG_STREAM(WARNING));
Mathieu Chartier2ada67b2015-07-30 11:41:04 -0700358 }
Mathieu Chartierc263bf82015-04-29 09:57:48 -0700359 }
360 return true;
361 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700362 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700363 const uint32_t idx = ExtractIndex(iref);
364 if (idx < bottom_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700365 // Wrong segment.
366 LOG(WARNING) << "Attempt to remove index outside index area (" << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700367 << " vs " << bottom_index << "-" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700368 return false;
369 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700370 if (idx >= top_index) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700371 // Bad --- stale reference?
372 LOG(WARNING) << "Attempt to remove invalid index " << idx
Andreas Gampee03662b2016-10-13 17:12:56 -0700373 << " (bottom=" << bottom_index << " top=" << top_index << ")";
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700374 return false;
375 }
376
Andreas Gampee03662b2016-10-13 17:12:56 -0700377 RecoverHoles(previous_state);
378 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
379
380 if (idx == top_index - 1) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700381 // Top-most entry. Scan up and consume holes.
382
Ian Rogers987560f2014-04-22 11:42:59 -0700383 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700384 return false;
385 }
386
Mathieu Chartier4838d662014-09-25 15:27:43 -0700387 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700388 if (current_num_holes_ != 0) {
389 uint32_t collapse_top_index = top_index;
390 while (--collapse_top_index > bottom_index && current_num_holes_ != 0) {
391 if (kDebugIRT) {
392 ScopedObjectAccess soa(Thread::Current());
393 LOG(INFO) << "+++ checking for hole at " << collapse_top_index - 1
394 << " (previous_state=" << bottom_index << ") val="
395 << table_[collapse_top_index - 1].GetReference()->Read<kWithoutReadBarrier>();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700396 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700397 if (!table_[collapse_top_index - 1].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700398 break;
399 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700400 if (kDebugIRT) {
401 LOG(INFO) << "+++ ate hole at " << (collapse_top_index - 1);
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700402 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700403 current_num_holes_--;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700404 }
Andreas Gampee03662b2016-10-13 17:12:56 -0700405 segment_state_.top_index = collapse_top_index;
406
407 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700408 } else {
Andreas Gampee03662b2016-10-13 17:12:56 -0700409 segment_state_.top_index = top_index - 1;
410 if (kDebugIRT) {
411 LOG(INFO) << "+++ ate last entry " << top_index - 1;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700412 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700413 }
414 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700415 // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody
416 // from deleting it twice and screwing up the hole count.
Mathieu Chartier4838d662014-09-25 15:27:43 -0700417 if (table_[idx].GetReference()->IsNull()) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700418 LOG(INFO) << "--- WEIRD: removing null entry " << idx;
419 return false;
420 }
Ian Rogers987560f2014-04-22 11:42:59 -0700421 if (!CheckEntry("remove", iref, idx)) {
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700422 return false;
423 }
424
Mathieu Chartier4838d662014-09-25 15:27:43 -0700425 *table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -0700426 current_num_holes_++;
427 CheckHoleCount(table_, current_num_holes_, previous_state, segment_state_);
428 if (kDebugIRT) {
429 LOG(INFO) << "+++ left hole at " << idx << ", holes=" << current_num_holes_;
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700430 }
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700431 }
432
433 return true;
434}
435
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800436void IndirectReferenceTable::Trim() {
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800437 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800438 const size_t top_index = Capacity();
439 auto* release_start = AlignUp(reinterpret_cast<uint8_t*>(&table_[top_index]), kPageSize);
440 uint8_t* release_end = table_mem_map_->End();
441 madvise(release_start, release_end - release_start, MADV_DONTNEED);
442}
443
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700444void IndirectReferenceTable::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
Mathieu Chartier4809d0a2015-04-07 10:39:04 -0700445 BufferedRootVisitor<kDefaultBufferedRootCount> root_visitor(visitor, root_info);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700446 for (auto ref : *this) {
Mathieu Chartier9086b652015-04-14 09:35:18 -0700447 if (!ref->IsNull()) {
448 root_visitor.VisitRoot(*ref);
449 DCHECK(!ref->IsNull());
450 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700451 }
452}
453
Elliott Hughes73e66f72012-05-09 09:34:45 -0700454void IndirectReferenceTable::Dump(std::ostream& os) const {
455 os << kind_ << " table dump:\n";
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -0700456 ReferenceTable::Table entries;
457 for (size_t i = 0; i < Capacity(); ++i) {
Mathieu Chartier8778c522016-10-04 19:06:30 -0700458 ObjPtr<mirror::Object> obj = table_[i].GetReference()->Read<kWithoutReadBarrier>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700459 if (obj != nullptr) {
Mathieu Chartier4838d662014-09-25 15:27:43 -0700460 obj = table_[i].GetReference()->Read();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700461 entries.push_back(GcRoot<mirror::Object>(obj));
Ian Rogers63818dc2012-09-26 12:23:04 -0700462 }
463 }
Elliott Hughes73e66f72012-05-09 09:34:45 -0700464 ReferenceTable::Dump(os, entries);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700465}
466
Andreas Gampee03662b2016-10-13 17:12:56 -0700467void IndirectReferenceTable::SetSegmentState(IRTSegmentState new_state) {
468 if (kDebugIRT) {
469 LOG(INFO) << "Setting segment state: "
470 << segment_state_.top_index
471 << " -> "
472 << new_state.top_index;
473 }
474 segment_state_ = new_state;
475}
476
Andreas Gampe88831082017-05-31 19:46:03 -0700477bool IndirectReferenceTable::EnsureFreeCapacity(size_t free_capacity, std::string* error_msg) {
478 size_t top_index = segment_state_.top_index;
479 if (top_index < max_entries_ && top_index + free_capacity <= max_entries_) {
480 return true;
481 }
482
483 // We're only gonna do a simple best-effort here, ensuring the asked-for capacity at the end.
484 if (resizable_ == ResizableCapacity::kNo) {
485 *error_msg = "Table is not resizable";
486 return false;
487 }
488
489 // Try to increase the table size.
490
491 // Would this overflow?
492 if (std::numeric_limits<size_t>::max() - free_capacity < top_index) {
493 *error_msg = "Cannot resize table, overflow.";
494 return false;
495 }
496
497 if (!Resize(top_index + free_capacity, error_msg)) {
498 LOG(WARNING) << "JNI ERROR: Unable to reserve space in EnsureFreeCapacity (" << free_capacity
499 << "): " << std::endl
500 << MutatorLockedDumpable<IndirectReferenceTable>(*this)
501 << " Resizing failed: " << *error_msg;
502 return false;
503 }
504 return true;
505}
506
507size_t IndirectReferenceTable::FreeCapacity() {
508 return max_entries_ - segment_state_.top_index;
509}
510
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700511} // namespace art