Make allocations report usable size.

Work-in-progress to allow arrays to fill usable size. Bug: 13028925.
Use C++11's override keyword on GCC >= 2.7 to ensure that we override GC and
allocator methods.
Move initial mirror::Class set up into a Functor so that all allocated objects
have non-zero sizes. Use this property to assert that all objects are never
larger than their usable size.
Other bits of GC related clean-up, missing initialization, missing use of
const, hot methods in .cc files, "unimplemented" functions that fail at
runtime in header files, reducing header file includes, move valgrind's space
into its own files, reduce number of array allocation routines.

Change-Id: Id5760041a2d7f94dcaf17ec760f6095ec75dadaa
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index ff6f9de..87323f9 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1185,27 +1185,47 @@
   return dex_cache.get();
 }
 
+// Used to initialize a class in the allocation code path to ensure it is guarded by a StoreStore
+// fence.
+class InitializeClassVisitor {
+ public:
+  explicit InitializeClassVisitor(uint32_t class_size) : class_size_(class_size) {
+  }
+
+  void operator()(mirror::Object* obj, size_t usable_size) const
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    DCHECK_LE(class_size_, usable_size);
+    // Avoid AsClass as object is not yet in live bitmap or allocation stack.
+    mirror::Class* klass = down_cast<mirror::Class*>(obj);
+    // DCHECK(klass->IsClass());
+    klass->SetClassSize(class_size_);
+    klass->SetPrimitiveType(Primitive::kPrimNot);  // Default to not being primitive.
+    klass->SetDexClassDefIndex(DexFile::kDexNoIndex16);  // Default to no valid class def index.
+    klass->SetDexTypeIndex(DexFile::kDexNoIndex16);  // Default to no valid type index.
+  }
+
+ private:
+  const uint32_t class_size_;
+
+  DISALLOW_COPY_AND_ASSIGN(InitializeClassVisitor);
+};
+
 mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class,
-                                       size_t class_size) {
+                                       uint32_t class_size) {
   DCHECK_GE(class_size, sizeof(mirror::Class));
   gc::Heap* heap = Runtime::Current()->GetHeap();
+  InitializeClassVisitor visitor(class_size);
   mirror::Object* k =
-      kMovingClasses ?
-          heap->AllocObject<true>(self, java_lang_Class, class_size) :
-          heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size);
-  if (UNLIKELY(k == NULL)) {
+      kMovingClasses ? heap->AllocObject<true>(self, java_lang_Class, class_size, visitor)
+                     : heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor);
+  if (UNLIKELY(k == nullptr)) {
     CHECK(self->IsExceptionPending());  // OOME.
-    return NULL;
+    return nullptr;
   }
-  mirror::Class* klass = k->AsClass();
-  klass->SetPrimitiveType(Primitive::kPrimNot);  // Default to not being primitive.
-  klass->SetClassSize(class_size);
-  klass->SetDexClassDefIndex(DexFile::kDexNoIndex16);  // Default to no valid class def index.
-  klass->SetDexTypeIndex(DexFile::kDexNoIndex16);  // Default to no valid type index.
-  return klass;
+  return k->AsClass();
 }
 
-mirror::Class* ClassLinker::AllocClass(Thread* self, size_t class_size) {
+mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) {
   return AllocClass(self, GetClassRoot(kJavaLangClass), class_size);
 }
 
@@ -1419,7 +1439,7 @@
 }
 
 // Precomputes size that will be needed for Class, matching LinkStaticFields
-size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
+uint32_t ClassLinker::SizeOfClass(const DexFile& dex_file,
                                 const DexFile::ClassDef& dex_class_def) {
   const byte* class_data = dex_file.GetClassData(dex_class_def);
   size_t num_ref = 0;
@@ -1440,7 +1460,7 @@
     }
   }
   // start with generic class data
-  size_t size = sizeof(mirror::Class);
+  uint32_t size = sizeof(mirror::Class);
   // follow with reference fields which must be contiguous at start
   size += (num_ref * sizeof(uint32_t));
   // if there are 64-bit fields to add, make sure they are aligned