// Maximum table size we allow. static constexpr size_t kMaxTableSizeInBytes = 128 * MB;
constchar* GetIndirectRefKindString(IndirectRefKind kind) { switch (kind) { case kJniTransition: return"JniTransition"; case kLocal: return"Local"; case kGlobal: return"Global"; case kWeakGlobal: return"WeakGlobal";
} return"IndirectRefKind Error";
}
void IndirectReferenceTable::AbortIfNoCheckJNI(const std::string& msg) { // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
JavaVMExt* vm = Runtime::Current()->GetJavaVM(); if (!vm->IsCheckJniEnabled()) { // Otherwise, we want to abort rather than hand back a bad reference.
LOG(FATAL) << msg;
} else {
LOG(ERROR) << msg;
}
}
// Mmap an "indirect ref table region. Table_bytes is a multiple of a page size. staticinline MemMap NewIRTMap(size_t table_bytes, std::string* error_msg) {
MemMap result = MemMap::MapAnonymous("indirect ref table",
table_bytes,
PROT_READ | PROT_WRITE, /*low_4gb=*/ false,
error_msg); if (!result.IsValid() && error_msg->empty()) {
*error_msg = "Unable to map memory for indirect ref table";
} return result;
}
table_ = reinterpret_cast<IrtEntry*>(table_mem_map_.Begin()); // Take into account the actual length.
max_entries_ = table_bytes / sizeof(IrtEntry); returntrue;
}
void IndirectReferenceTable::ConstexprChecks() { // Use this for some assertions. They can't be put into the header as C++ wants the class // to be complete.
// Distinguishing between local and (weak) global references.
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kJniTransition)) == 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kLocal)) == 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kGlobal)) != 0u);
static_assert((GetGlobalOrWeakGlobalMask() & EncodeIndirectRefKind(kWeakGlobal)) != 0u);
}
// Holes: // // To keep the IRT compact, we want to fill "holes" created by non-stack-discipline Add & Remove // operation sequences. For simplicity and lower memory overhead, we do not use a free list or // similar. Instead, we scan for holes, with the expectation that we will find holes fast as they // are usually near the end of the table (see the header, TODO: verify this assumption). To avoid // scans when there are no holes, the number of known holes should be tracked.
static size_t CountNullEntries(const IrtEntry* table, size_t to) {
size_t count = 0; for (size_t index = 0u; index != to; ++index) { if (table[index].GetReference()->IsNull()) {
count++;
}
} return count;
}
// We know there's enough room in the table. Now we just need to find // the right spot. If there's a hole, find it and fill it; otherwise, // add to the end of the list.
IndirectRef result;
size_t index; if (current_num_holes_ > 0) {
DCHECK_GT(top_index_, 1U); // Find the first hole; likely to be near the end of the list.
IrtEntry* p_scan = &table_[top_index_ - 1];
DCHECK(!p_scan->GetReference()->IsNull());
--p_scan; while (!p_scan->GetReference()->IsNull()) {
DCHECK_GT(p_scan, table_);
--p_scan;
}
index = p_scan - table_;
current_num_holes_--;
} else { // Add to the end.
index = top_index_;
++top_index_;
}
table_[index].Add(obj);
result = ToIndirectRef(index); if (kDebugIRT) {
LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << top_index_
<< " holes=" << current_num_holes_;
}
DCHECK(result != nullptr); return result;
}
// Removes an object. We extract the table offset bits from "iref" // and zap the corresponding entry, leaving a hole if it's not at the top. // Returns "false" if nothing was removed. bool IndirectReferenceTable::Remove(IndirectRef iref) { if (kDebugIRT) {
LOG(INFO) << "+++ Remove: top_index=" << top_index_
<< " holes=" << current_num_holes_;
}
// TODO: We should eagerly check the ref kind against the `kind_` instead of postponing until // `CheckEntry()` below. Passing the wrong kind shall currently result in misleading warnings.
const uint32_t top_index = top_index_;
DCHECK(table_ != nullptr);
const uint32_t idx = ExtractIndex(iref); if (idx >= top_index) { // Bad --- stale reference?
LOG(WARNING) << "Attempt to remove invalid index " << idx
<< " (top=" << top_index << ")"; returnfalse;
}
if (idx == top_index - 1) { // Top-most entry. Scan up and consume holes.
if (!CheckEntry("remove", iref, idx)) { returnfalse;
}
*table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr); if (current_num_holes_ != 0) {
uint32_t collapse_top_index = top_index; while (--collapse_top_index > 0u && current_num_holes_ != 0) { if (kDebugIRT) {
ScopedObjectAccess soa(Thread::Current());
LOG(INFO) << "+++ checking for hole at " << collapse_top_index - 1 << " val="
<< table_[collapse_top_index - 1].GetReference()->Read<kWithoutReadBarrier>();
} if (!table_[collapse_top_index - 1].GetReference()->IsNull()) { break;
} if (kDebugIRT) {
LOG(INFO) << "+++ ate hole at " << (collapse_top_index - 1);
}
current_num_holes_--;
}
top_index_ = collapse_top_index;
CheckHoleCount(table_, current_num_holes_, top_index_);
} else {
top_index_ = top_index - 1; if (kDebugIRT) {
LOG(INFO) << "+++ ate last entry " << top_index - 1;
}
}
} else { // Not the top-most entry. This creates a hole. We null out the entry to prevent somebody // from deleting it twice and screwing up the hole count. if (table_[idx].GetReference()->IsNull()) {
LOG(INFO) << "--- WEIRD: removing null entry " << idx; returnfalse;
} if (!CheckEntry("remove", iref, idx)) { returnfalse;
}
*table_[idx].GetReference() = GcRoot<mirror::Object>(nullptr);
current_num_holes_++;
CheckHoleCount(table_, current_num_holes_, top_index_); if (kDebugIRT) {
LOG(INFO) << "+++ left hole at " << idx << ", holes=" << current_num_holes_;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.