ART: Clean up includes.

Reduce dependencies to improve incremental build times.
Break up circular dependency involving class_linker-inl.h.

Change-Id: I4be742c5c2b5cd9855beea86630fd68aab76b0db
diff --git a/runtime/arch/stub_test.cc b/runtime/arch/stub_test.cc
index d7de119..9cccf7c 100644
--- a/runtime/arch/stub_test.cc
+++ b/runtime/arch/stub_test.cc
@@ -17,6 +17,7 @@
 #include <cstdio>
 
 #include "art_field-inl.h"
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "mirror/art_method-inl.h"
diff --git a/runtime/art_field-inl.h b/runtime/art_field-inl.h
index aeb1273..a2625e2 100644
--- a/runtime/art_field-inl.h
+++ b/runtime/art_field-inl.h
@@ -21,6 +21,7 @@
 
 #include "base/logging.h"
 #include "class_linker.h"
+#include "gc_root-inl.h"
 #include "gc/accounting/card_table-inl.h"
 #include "jvalue.h"
 #include "mirror/dex_cache.h"
@@ -289,15 +290,14 @@
   const uint32_t field_index = GetDexFieldIndex();
   auto* declaring_class = GetDeclaringClass();
   if (UNLIKELY(declaring_class->IsProxyClass())) {
-    return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
-                                                                 GetTypeDescriptor());
+    return ProxyFindSystemClass(GetTypeDescriptor());
   }
   auto* dex_cache = declaring_class->GetDexCache();
   const DexFile* const dex_file = dex_cache->GetDexFile();
   const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
   mirror::Class* type = dex_cache->GetResolvedType(field_id.type_idx_);
   if (kResolve && UNLIKELY(type == nullptr)) {
-    type = Runtime::Current()->GetClassLinker()->ResolveType(field_id.type_idx_, this);
+    type = ResolveGetType(field_id.type_idx_);
     CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
   }
   return type;
@@ -323,9 +323,7 @@
   const auto& field_id = dex_file->GetFieldId(dex_field_index);
   auto* name = dex_cache->GetResolvedString(field_id.name_idx_);
   if (resolve && name == nullptr) {
-    StackHandleScope<1> hs(self);
-    name = Runtime::Current()->GetClassLinker()->ResolveString(
-        *dex_file, field_id.name_idx_, hs.NewHandle(dex_cache));
+    name = ResolveGetStringName(self, *dex_file, field_id.name_idx_, dex_cache);
   }
   return name;
 }
diff --git a/runtime/art_field.cc b/runtime/art_field.cc
index cdf8967..2aed440 100644
--- a/runtime/art_field.cc
+++ b/runtime/art_field.cc
@@ -17,7 +17,9 @@
 #include "art_field.h"
 
 #include "art_field-inl.h"
+#include "class_linker-inl.h"
 #include "gc/accounting/card_table-inl.h"
+#include "handle_scope.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
 #include "runtime.h"
@@ -61,4 +63,20 @@
       FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset) : nullptr;
 }
 
+mirror::Class* ArtField::ProxyFindSystemClass(const char* descriptor) {
+  DCHECK(GetDeclaringClass()->IsProxyClass());
+  return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(), descriptor);
+}
+
+mirror::Class* ArtField::ResolveGetType(uint32_t type_idx) {
+  return Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
+}
+
+mirror::String* ArtField::ResolveGetStringName(Thread* self, const DexFile& dex_file,
+                                               uint32_t string_idx, mirror::DexCache* dex_cache) {
+  StackHandleScope<1> hs(self);
+  return Runtime::Current()->GetClassLinker()->ResolveString(
+      dex_file, string_idx, hs.NewHandle(dex_cache));
+}
+
 }  // namespace art
diff --git a/runtime/art_field.h b/runtime/art_field.h
index 5bdbe71..16c46f0 100644
--- a/runtime/art_field.h
+++ b/runtime/art_field.h
@@ -188,6 +188,13 @@
   }
 
  private:
+  mirror::Class* ProxyFindSystemClass(const char* descriptor)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+  mirror::Class* ResolveGetType(uint32_t type_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+  mirror::String* ResolveGetStringName(Thread* self, const DexFile& dex_file, uint32_t string_idx,
+                                       mirror::DexCache* dex_cache)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
   GcRoot<mirror::Class> declaring_class_;
 
   uint32_t access_flags_;
diff --git a/runtime/base/arena_allocator.cc b/runtime/base/arena_allocator.cc
index 59d38ad..b53fa84 100644
--- a/runtime/base/arena_allocator.cc
+++ b/runtime/base/arena_allocator.cc
@@ -20,6 +20,7 @@
 
 #include "arena_allocator.h"
 #include "logging.h"
+#include "mem_map.h"
 #include "mutex.h"
 #include "thread-inl.h"
 #include <memcheck/memcheck.h>
@@ -141,6 +142,10 @@
   size_ = map_->Size();
 }
 
+MemMapArena::~MemMapArena() {
+  // Destroys MemMap via std::unique_ptr<>.
+}
+
 void MemMapArena::Release() {
   if (bytes_allocated_ > 0) {
     map_->MadviseDontNeedAndZero();
diff --git a/runtime/base/arena_allocator.h b/runtime/base/arena_allocator.h
index 3a86b61..ab5968c 100644
--- a/runtime/base/arena_allocator.h
+++ b/runtime/base/arena_allocator.h
@@ -22,7 +22,6 @@
 
 #include "debug_stack.h"
 #include "macros.h"
-#include "mem_map.h"
 #include "mutex.h"
 #include "utils.h"
 
@@ -33,6 +32,7 @@
 class ArenaAllocator;
 class ArenaStack;
 class ScopedArenaAllocator;
+class MemMap;
 class MemStats;
 
 template <typename T>
@@ -166,7 +166,7 @@
 class MemMapArena FINAL : public Arena {
  public:
   explicit MemMapArena(size_t size, bool low_4gb);
-  virtual ~MemMapArena() { }
+  virtual ~MemMapArena();
   void Release() OVERRIDE;
 
  private:
diff --git a/runtime/gc/accounting/bitmap.cc b/runtime/gc/accounting/bitmap.cc
index 20984fd..13fcdb3 100644
--- a/runtime/gc/accounting/bitmap.cc
+++ b/runtime/gc/accounting/bitmap.cc
@@ -35,6 +35,10 @@
   CHECK_NE(bitmap_size, 0U);
 }
 
+Bitmap::~Bitmap() {
+  // Destroys MemMap via std::unique_ptr<>.
+}
+
 MemMap* Bitmap::AllocateMemMap(const std::string& name, size_t num_bits) {
   const size_t bitmap_size = RoundUp(
       RoundUp(num_bits, kBitsPerBitmapWord) / kBitsPerBitmapWord * sizeof(uintptr_t), kPageSize);
diff --git a/runtime/gc/accounting/bitmap.h b/runtime/gc/accounting/bitmap.h
index cf2c293..b294d49 100644
--- a/runtime/gc/accounting/bitmap.h
+++ b/runtime/gc/accounting/bitmap.h
@@ -103,6 +103,7 @@
   static constexpr size_t kBitsPerBitmapWord = sizeof(uintptr_t) * kBitsPerByte;
 
   Bitmap(MemMap* mem_map, size_t bitmap_size);
+  ~Bitmap();
 
   // Allocate the mem-map for a bitmap based on how many bits are required.
   static MemMap* AllocateMemMap(const std::string& name, size_t num_bits);
diff --git a/runtime/gc/accounting/card_table-inl.h b/runtime/gc/accounting/card_table-inl.h
index 83ad33e..b936d93 100644
--- a/runtime/gc/accounting/card_table-inl.h
+++ b/runtime/gc/accounting/card_table-inl.h
@@ -20,6 +20,7 @@
 #include "atomic.h"
 #include "base/logging.h"
 #include "card_table.h"
+#include "mem_map.h"
 #include "space_bitmap.h"
 #include "utils.h"
 
@@ -223,6 +224,12 @@
   return card_addr;
 }
 
+inline bool CardTable::IsValidCard(const uint8_t* card_addr) const {
+  uint8_t* begin = mem_map_->Begin() + offset_;
+  uint8_t* end = mem_map_->End();
+  return card_addr >= begin && card_addr < end;
+}
+
 inline void CardTable::CheckCardValid(uint8_t* card) const {
   DCHECK(IsValidCard(card))
       << " card_addr: " << reinterpret_cast<const void*>(card)
diff --git a/runtime/gc/accounting/card_table.cc b/runtime/gc/accounting/card_table.cc
index ad1f192..7879632 100644
--- a/runtime/gc/accounting/card_table.cc
+++ b/runtime/gc/accounting/card_table.cc
@@ -21,6 +21,7 @@
 #include "gc/heap.h"
 #include "gc/space/space.h"
 #include "heap_bitmap.h"
+#include "mem_map.h"
 #include "runtime.h"
 #include "utils.h"
 
@@ -90,6 +91,10 @@
     : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) {
 }
 
+CardTable::~CardTable() {
+  // Destroys MemMap via std::unique_ptr<>.
+}
+
 void CardTable::ClearSpaceCards(space::ContinuousSpace* space) {
   // TODO: clear just the range of the table that has been modified
   uint8_t* card_start = CardFromAddr(space->Begin());
diff --git a/runtime/gc/accounting/card_table.h b/runtime/gc/accounting/card_table.h
index 3ea7651..896cce5 100644
--- a/runtime/gc/accounting/card_table.h
+++ b/runtime/gc/accounting/card_table.h
@@ -21,10 +21,11 @@
 
 #include "base/mutex.h"
 #include "globals.h"
-#include "mem_map.h"
 
 namespace art {
 
+class MemMap;
+
 namespace mirror {
   class Object;
 }  // namespace mirror
@@ -52,6 +53,7 @@
   static constexpr uint8_t kCardDirty = 0x70;
 
   static CardTable* Create(const uint8_t* heap_begin, size_t heap_capacity);
+  ~CardTable();
 
   // Set the card associated with the given address to GC_CARD_DIRTY.
   ALWAYS_INLINE void MarkCard(const void *addr) {
@@ -130,11 +132,7 @@
   CardTable(MemMap* begin, uint8_t* biased_begin, size_t offset);
 
   // Returns true iff the card table address is within the bounds of the card table.
-  bool IsValidCard(const uint8_t* card_addr) const {
-    uint8_t* begin = mem_map_->Begin() + offset_;
-    uint8_t* end = mem_map_->End();
-    return card_addr >= begin && card_addr < end;
-  }
+  bool IsValidCard(const uint8_t* card_addr) const ALWAYS_INLINE;
 
   void CheckCardValid(uint8_t* card) const ALWAYS_INLINE;
 
diff --git a/runtime/gc/accounting/mod_union_table_test.cc b/runtime/gc/accounting/mod_union_table_test.cc
index 94bb3f5..043b558 100644
--- a/runtime/gc/accounting/mod_union_table_test.cc
+++ b/runtime/gc/accounting/mod_union_table_test.cc
@@ -16,6 +16,7 @@
 
 #include "mod_union_table-inl.h"
 
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "gc/space/space-inl.h"
 #include "mirror/array-inl.h"
diff --git a/runtime/gc/allocator/rosalloc.cc b/runtime/gc/allocator/rosalloc.cc
index f64a4ff..515f124 100644
--- a/runtime/gc/allocator/rosalloc.cc
+++ b/runtime/gc/allocator/rosalloc.cc
@@ -18,6 +18,7 @@
 
 #include "base/mutex-inl.h"
 #include "gc/space/valgrind_settings.h"
+#include "mem_map.h"
 #include "mirror/class-inl.h"
 #include "mirror/object.h"
 #include "mirror/object-inl.h"
diff --git a/runtime/gc/allocator/rosalloc.h b/runtime/gc/allocator/rosalloc.h
index d1e7ad9..a54edcc 100644
--- a/runtime/gc/allocator/rosalloc.h
+++ b/runtime/gc/allocator/rosalloc.h
@@ -30,11 +30,13 @@
 #include "base/mutex.h"
 #include "base/logging.h"
 #include "globals.h"
-#include "mem_map.h"
 #include "thread.h"
 #include "utils.h"
 
 namespace art {
+
+class MemMap;
+
 namespace gc {
 namespace allocator {
 
diff --git a/runtime/gc/heap_test.cc b/runtime/gc/heap_test.cc
index 14d78d8..a3cefd9 100644
--- a/runtime/gc/heap_test.cc
+++ b/runtime/gc/heap_test.cc
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "gc/accounting/card_table-inl.h"
 #include "gc/accounting/space_bitmap-inl.h"
diff --git a/runtime/indirect_reference_table_test.cc b/runtime/indirect_reference_table_test.cc
index 1156cf5..fe1b8f0 100644
--- a/runtime/indirect_reference_table_test.cc
+++ b/runtime/indirect_reference_table_test.cc
@@ -16,6 +16,7 @@
 
 #include "indirect_reference_table-inl.h"
 
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "mirror/object-inl.h"
 #include "scoped_thread_state_change.h"
diff --git a/runtime/mirror/class-inl.h b/runtime/mirror/class-inl.h
index 5b72e5a..f4656ec 100644
--- a/runtime/mirror/class-inl.h
+++ b/runtime/mirror/class-inl.h
@@ -21,7 +21,6 @@
 
 #include "art_field-inl.h"
 #include "art_method-inl.h"
-#include "class_linker-inl.h"
 #include "class_loader.h"
 #include "common_throws.h"
 #include "dex_cache.h"
@@ -39,12 +38,8 @@
 
 template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
 inline uint32_t Class::GetObjectSize() {
-  if (kIsDebugBuild) {
-    // Use a local variable as (D)CHECK can't handle the space between
-    // the two template params.
-    bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>();
-    CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this);
-  }
+  // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator.
+  DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this);
   return GetField32(ObjectSizeOffset());
 }
 
@@ -706,7 +701,7 @@
   } else if (IsPrimitive()) {
     return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
   } else if (IsProxyClass()) {
-    return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
+    return ProxyDescriptorEquals(match);
   } else {
     const DexFile& dex_file = GetDexFile();
     const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc
index 8fb8147..2afb4af 100644
--- a/runtime/mirror/class.cc
+++ b/runtime/mirror/class.cc
@@ -18,7 +18,7 @@
 
 #include "art_field-inl.h"
 #include "art_method-inl.h"
-#include "class_linker.h"
+#include "class_linker-inl.h"
 #include "class_loader.h"
 #include "class-inl.h"
 #include "dex_cache.h"
@@ -871,5 +871,10 @@
   return new_class->AsClass();
 }
 
+bool Class::ProxyDescriptorEquals(const char* match) {
+  DCHECK(IsProxyClass());
+  return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
+}
+
 }  // namespace mirror
 }  // namespace art
diff --git a/runtime/mirror/class.h b/runtime/mirror/class.h
index 92493bc..20f2387 100644
--- a/runtime/mirror/class.h
+++ b/runtime/mirror/class.h
@@ -1095,6 +1095,8 @@
   ArtField* GetSFieldsUnchecked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
   ArtField* GetIFieldsUnchecked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
+  bool ProxyDescriptorEquals(const char* match) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
   // defining class loader, or NULL for the "bootstrap" system loader
   HeapReference<ClassLoader> class_loader_;
 
diff --git a/runtime/mirror/field-inl.h b/runtime/mirror/field-inl.h
index 7db1811..9820db7 100644
--- a/runtime/mirror/field-inl.h
+++ b/runtime/mirror/field-inl.h
@@ -20,6 +20,7 @@
 #include "field.h"
 
 #include "art_field-inl.h"
+#include "mirror/dex_cache-inl.h"
 #include "runtime-inl.h"
 
 namespace art {
diff --git a/runtime/monitor_test.cc b/runtime/monitor_test.cc
index 6d1e721..2351463 100644
--- a/runtime/monitor_test.cc
+++ b/runtime/monitor_test.cc
@@ -20,6 +20,7 @@
 #include <string>
 
 #include "atomic.h"
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "handle_scope-inl.h"
 #include "mirror/class-inl.h"
diff --git a/runtime/native/java_lang_DexCache.cc b/runtime/native/java_lang_DexCache.cc
index 8944270..1198c2e 100644
--- a/runtime/native/java_lang_DexCache.cc
+++ b/runtime/native/java_lang_DexCache.cc
@@ -18,7 +18,7 @@
 
 #include "dex_file.h"
 #include "jni_internal.h"
-#include "mirror/dex_cache.h"
+#include "mirror/dex_cache-inl.h"
 #include "mirror/object-inl.h"
 #include "scoped_fast_native_object_access.h"
 #include "well_known_classes.h"
diff --git a/runtime/oat_file_assistant_test.cc b/runtime/oat_file_assistant_test.cc
index 37c6353..0c942d2 100644
--- a/runtime/oat_file_assistant_test.cc
+++ b/runtime/oat_file_assistant_test.cc
@@ -26,7 +26,7 @@
 #include <gtest/gtest.h>
 
 #include "art_field-inl.h"
-#include "class_linker.h"
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "compiler_callbacks.h"
 #include "mem_map.h"
diff --git a/runtime/proxy_test.cc b/runtime/proxy_test.cc
index a3156b4..6061f73 100644
--- a/runtime/proxy_test.cc
+++ b/runtime/proxy_test.cc
@@ -18,6 +18,7 @@
 #include <vector>
 
 #include "art_field-inl.h"
+#include "class_linker-inl.h"
 #include "common_compiler_test.h"
 #include "scoped_thread_state_change.h"
 
diff --git a/runtime/quick/inline_method_analyser.cc b/runtime/quick/inline_method_analyser.cc
index efaa0ac..9cf4b16 100644
--- a/runtime/quick/inline_method_analyser.cc
+++ b/runtime/quick/inline_method_analyser.cc
@@ -17,6 +17,7 @@
 #include "inline_method_analyser.h"
 
 #include "art_field-inl.h"
+#include "class_linker-inl.h"
 #include "dex_file-inl.h"
 #include "dex_instruction.h"
 #include "dex_instruction-inl.h"
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 66d38ce..543b9dc 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -54,7 +54,7 @@
 #include "base/arena_allocator.h"
 #include "base/dumpable.h"
 #include "base/unix_file/fd_file.h"
-#include "class_linker.h"
+#include "class_linker-inl.h"
 #include "compiler_callbacks.h"
 #include "debugger.h"
 #include "elf_file.h"
diff --git a/runtime/transaction_test.cc b/runtime/transaction_test.cc
index 9792eca..aee2c54 100644
--- a/runtime/transaction_test.cc
+++ b/runtime/transaction_test.cc
@@ -17,6 +17,7 @@
 #include "transaction.h"
 
 #include "art_field-inl.h"
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "mirror/array-inl.h"
 #include "mirror/art_method-inl.h"
diff --git a/runtime/utils_test.cc b/runtime/utils_test.cc
index 623a3ec..aa7bc64 100644
--- a/runtime/utils_test.cc
+++ b/runtime/utils_test.cc
@@ -16,6 +16,7 @@
 
 #include "utils.h"
 
+#include "class_linker-inl.h"
 #include "common_runtime_test.h"
 #include "mirror/array.h"
 #include "mirror/array-inl.h"