Upgrade to latest dlmalloc. Refactor Heap and related APIs to use STL like naming.

We fail assertions in the existing heap code, as does Dalvik. This refactoring
is to clean the heap and space APIs and to reduce duplication of data
and thereby solve a failing assertion in the card table.

This change also wires up clearing of soft references including before
out-of-memory errors are reported.

In doing this change it was made clear that mspaces are buggy (and
violating invariants with the garbage collector). This
change upgrades to an un-Android molested version of dlmalloc-2.8.5 and
implements a version of the mspace morecore routine under ART control.

run-test 061-out-of-memory is updated for current heap sizes.

Change-Id: I377e83ab2a8c78afb9b1881f03356929e2c9dc64
diff --git a/src/heap.h b/src/heap.h
index 2e9aa35..678f876 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -21,6 +21,7 @@
 
 #include "card_table.h"
 #include "globals.h"
+#include "gtest/gtest.h"
 #include "heap_bitmap.h"
 #include "mutex.h"
 #include "offsets.h"
@@ -29,17 +30,19 @@
 
 namespace art {
 
+class AllocSpace;
 class Class;
 class Object;
 class Space;
 class Thread;
 class HeapBitmap;
+class SpaceTest;
 
 class Heap {
  public:
-  static const size_t kInitialSize = 4 * MB;
+  static const size_t kInitialSize = 2 * MB;
 
-  static const size_t kMaximumSize = 16 * MB;
+  static const size_t kMaximumSize = 32 * MB;
 
   typedef void (RootVisitor)(const Object* root, void* arg);
   typedef bool (IsMarkedTester)(const Object* object, void* arg);
@@ -47,7 +50,7 @@
   // Create a heap with the requested sizes. The possible empty
   // image_file_names names specify Spaces to load based on
   // ImageWriter output.
-  static void Init(size_t starting_size, size_t maximum_size, size_t growth_size,
+  static void Init(size_t starting_size, size_t growth_limit, size_t capacity,
                    const std::vector<std::string>& image_file_names);
 
   static void Destroy();
@@ -74,7 +77,7 @@
   static bool IsLiveObjectLocked(const Object* obj);
 
   // Initiates an explicit garbage collection.
-  static void CollectGarbage();
+  static void CollectGarbage(bool clear_soft_references);
 
   // Implements java.lang.Runtime.maxMemory.
   static int64_t GetMaxMemory();
@@ -89,12 +92,16 @@
   // Implements dalvik.system.VMRuntime.clearGrowthLimit.
   static void ClearGrowthLimit();
 
-  // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
+  // Target ideal heap utilization ratio, implements
+  // dalvik.system.VMRuntime.getTargetHeapUtilization.
   static float GetTargetHeapUtilization() {
     return target_utilization_;
   }
-  // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
+  // Set target ideal heap utilization ratio, implements
+  // dalvik.system.VMRuntime.setTargetHeapUtilization.
   static void SetTargetHeapUtilization(float target) {
+    DCHECK_GT(target, 0.0f);  // asserted in Java code
+    DCHECK_LT(target, 1.0f);
     target_utilization_ = target;
   }
   // Sets the maximum number of bytes that the heap is allowed to allocate
@@ -155,6 +162,9 @@
   }
 
   static void EnableObjectValidation() {
+#if VERIFY_OBJECT_ENABLED
+    Heap::VerifyHeap();
+#endif
     verify_objects_ = true;
   }
 
@@ -189,33 +199,34 @@
     card_marking_disabled_ = true;
   }
 
-  // dlmalloc_walk_heap-compatible heap walker.
-  static void WalkHeap(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg);
-
   static void AddFinalizerReference(Thread* self, Object* object);
 
   static size_t GetBytesAllocated() { return num_bytes_allocated_; }
   static size_t GetObjectsAllocated() { return num_objects_allocated_; }
 
-  static Space* GetAllocSpace() {
+  static AllocSpace* GetAllocSpace() {
     return alloc_space_;
   }
 
  private:
   // Allocates uninitialized storage.
   static Object* AllocateLocked(size_t num_bytes);
-  static Object* AllocateLocked(Space* space, size_t num_bytes);
+  static Object* AllocateLocked(AllocSpace* space, size_t num_bytes);
 
   // Pushes a list of cleared references out to the managed heap.
   static void EnqueueClearedReferences(Object** cleared_references);
 
-  static void RecordAllocationLocked(Space* space, const Object* object);
+  static void RecordAllocationLocked(AllocSpace* space, const Object* object);
   static void RecordImageAllocations(Space* space);
 
-  static void CollectGarbageInternal();
+  static void CollectGarbageInternal(bool clear_soft_references);
 
   static void GrowForUtilization();
 
+  static void AddSpace(Space* space) {
+    spaces_.push_back(space);
+  }
+
   static void VerifyObjectLocked(const Object *obj);
 
   static void VerificationCallback(Object* obj, void* arg);
@@ -225,7 +236,7 @@
   static std::vector<Space*> spaces_;
 
   // default Space for allocations
-  static Space* alloc_space_;
+  static AllocSpace* alloc_space_;
 
   static HeapBitmap* mark_bitmap_;
 
@@ -237,16 +248,6 @@
   // TODO: remove
   static bool card_marking_disabled_;
 
-  // The maximum size of the heap in bytes.
-  static size_t maximum_size_;
-
-  // The largest size the heap may grow. This value allows the app to limit the
-  // growth below the maximum size. This is a work around until we can
-  // dynamically set the maximum size. This value can range between the starting
-  // size and the maximum size but should never be set below the current
-  // footprint of the heap.
-  static size_t growth_size_;
-
   // True while the garbage collector is running.
   static bool is_gc_running_;
 
@@ -281,6 +282,8 @@
 
   static bool verify_objects_;
 
+  FRIEND_TEST(SpaceTest, AllocAndFree);
+
   DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
 };