// No need to verify the dex file when we have a vdex file, which means it was already
// verified.
const bool verify =
(input_vdex_file_ == nullptr) && !compiler_options_->AssumeDexFilesAreVerified();
if (!oat_writers_[i]->WriteAndOpenDexFiles(
vdex_files_[i].get(),
verify,
update_input_vdex_,
copy_dex_files_,
&opened_dex_files_map,
&opened_dex_files)) {
return dex2oat::ReturnCode::kOther;
}
// C++ mirror of java.lang.Class
class MANAGED Class final : public Object {
// Defining class loader, or null for the "bootstrap" system loader.
HeapReference<ClassLoader> class_loader_;
// 数组元素的类型
// (for String[][][], this will be String[][]). null for non-array classes.
HeapReference<Class> component_type_;
// 这个类对应的 DexCache 对象,虚拟机直接创建的类没有这个值(数组、基本类型)
HeapReference<DexCache> dex_cache_;
//接口表,包括自己实现的和继承的
HeapReference<IfTable> iftable_;
// 类名,"java.lang.Class" or "[C"
HeapReference<String> name_;
HeapReference<Class> super_class_;
//虚函数表,invoke-virtual 调用的函数,包括父类的和当前类的
HeapReference<PointerArray> vtable_;
//本类定义的非静态成员,不包括父类的。
uint64_t ifields_;
/* [0,virtual_methods_offset_):本类的direct函数
[virtual_methods_offset_,copied_methods_offset_):本类的virtual函数
[copied_methods_offset_, ...) 诸如miranda函数等 */
uint64_t methods_;
// Static fields length-prefixed array.
uint64_t sfields_;
uint32_t access_flags_;
uint32_t class_flags_;
// Total size of the Class instance; used when allocating storage on gc heap
uint32_t class_size_;
// Tid used to check for recursive <clinit> invocation.
pid_t clinit_thread_id_;
static_assert(sizeof(pid_t) == sizeof(int32_t), "java.lang.Class.clinitThreadId size check");
// ClassDef index in dex file, -1 if no class definition such as an array.
int32_t dex_class_def_idx_;
// Type index in dex file.
int32_t dex_type_idx_;
};
ClassTable:
// Lock to guard inserting and removing.
mutable ReaderWriterMutex lock_;
// We have a vector to help prevent dirty pages after the zygote forks by calling FreezeSnapshot.
std::vector<ClassSet> classes_ GUARDED_BY(lock_);
// Hash set that hashes class descriptor, and compares descriptors and class loaders. Results
// should be compared for a matching class descriptor and class loader.
typedef HashSet<TableSlot,
TableSlotEmptyFn,
ClassDescriptorHashEquals,
ClassDescriptorHashEquals,
TrackingAllocator<TableSlot, kAllocatorTagClassTable>> ClassSet;
通过 ClassLinker::InsertClass 插入到 ClassTable 中
ClassLinker::InsertClassTableForClassLoader
ClassLinker::RegisterClassLoader 创建 ClassTable
void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) {
CHECK(class_loader->GetAllocator() == nullptr);
CHECK(class_loader->GetClassTable() == nullptr);
Thread* const self = Thread::Current();
ClassLoaderData data;
data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader);
// Create and set the class table.
data.class_table = new ClassTable;
class_loader->SetClassTable(data.class_table);
// Create and set the linear allocator.
data.allocator = Runtime::Current()->CreateLinearAlloc();
class_loader->SetAllocator(data.allocator);
// Add to the list so that we know to free the data later.
class_loaders_.push_back(data);
}