Revert "Hold dex caches live in class table"

This reverts commit d6d49e56c2b7b11f474acb80cb02bb1fe9b7861e.

Bug: 29083330

(cherry picked from commit f102faf1bcbdb2149e3e7bf27b1819f621b7894b)

Change-Id: Id0c921bd6fe422159f8daa598dc8e7c9ed6eca47
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index e789db6..9e144dd 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -2469,7 +2469,9 @@
     self->AssertPendingOOMException();
     return nullptr;
   }
-  mirror::DexCache* dex_cache = RegisterDexFile(dex_file, class_loader.Get());
+  mirror::DexCache* dex_cache = RegisterDexFile(
+      dex_file,
+      GetOrCreateAllocatorForClassLoader(class_loader.Get()));
   if (dex_cache == nullptr) {
     self->AssertPendingOOMException();
     return nullptr;
@@ -3228,8 +3230,7 @@
   dex_caches_.push_back(data);
 }
 
-mirror::DexCache* ClassLinker::RegisterDexFile(const DexFile& dex_file,
-                                               mirror::ClassLoader* class_loader) {
+mirror::DexCache* ClassLinker::RegisterDexFile(const DexFile& dex_file, LinearAlloc* linear_alloc) {
   Thread* self = Thread::Current();
   {
     ReaderMutexLock mu(self, dex_lock_);
@@ -3238,31 +3239,21 @@
       return dex_cache;
     }
   }
-  LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader);
-  DCHECK(linear_alloc != nullptr);
-  ClassTable* table;
-  {
-    WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
-    table = InsertClassTableForClassLoader(class_loader);
-  }
   // Don't alloc while holding the lock, since allocation may need to
   // suspend all threads and another thread may need the dex_lock_ to
   // get to a suspend point.
   StackHandleScope<1> hs(self);
   Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file, linear_alloc)));
-  {
-    WriterMutexLock mu(self, dex_lock_);
-    mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true);
-    if (dex_cache != nullptr) {
-      return dex_cache;
-    }
-    if (h_dex_cache.Get() == nullptr) {
-      self->AssertPendingOOMException();
-      return nullptr;
-    }
-    RegisterDexFileLocked(dex_file, h_dex_cache);
+  WriterMutexLock mu(self, dex_lock_);
+  mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true);
+  if (dex_cache != nullptr) {
+    return dex_cache;
   }
-  table->InsertStrongRoot(h_dex_cache.Get());
+  if (h_dex_cache.Get() == nullptr) {
+    self->AssertPendingOOMException();
+    return nullptr;
+  }
+  RegisterDexFileLocked(dex_file, h_dex_cache);
   return h_dex_cache.Get();
 }
 
@@ -8000,7 +7991,7 @@
   WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
   ClassTable* const table = ClassTableForClassLoader(class_loader);
   DCHECK(table != nullptr);
-  if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) {
+  if (table->InsertDexFile(dex_file) && class_loader != nullptr) {
     // It was not already inserted, perform the write barrier to let the GC know the class loader's
     // class table was modified.
     Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader);