Add an interface to Space, so Heap no longer needs to be friend of Space.

Also, fix and add comments with respect to utilization.

Change-Id: Ia9d27cc16d2b85ac97a48fee80567829f41224f4
diff --git a/src/heap.cc b/src/heap.cc
index 98552a7..eae772f 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -353,7 +353,7 @@
   ptr = space->AllocWithGrowth(size);
   if (ptr != NULL) {
     //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
-    size_t new_footprint = space->MaxAllowedFootprint();
+    size_t new_footprint = space->GetMaxAllowedFootprint();
     // TODO: may want to grow a little bit more so that the amount of
     //       free space is equal to the old free space + the
     //       utilization slop for the new allocation.
@@ -497,8 +497,8 @@
  *  3. Soft footprint: external allocation + spaces footprint + active space footprint
  *  4. Overhead: soft footprint excluding active.
  *
- * Layout: (Below might be incontiguous, but are lumped together to depict size.)
- * |---for external allocation---|---spaces footprint ("heap1")---|----active space footprint----|
+ * Layout: (The spaces below might not be contiguous, but are lumped together to depict size.)
+ * |----------------------spaces footprint--------- --------------|----active space footprint----|
  *                                                                |--active space allocated--|
  * |--------------------soft footprint (include active)--------------------------------------|
  * |----------------soft footprint excluding active---------------|
@@ -520,26 +520,14 @@
     max_allowed_footprint = Heap::maximum_size_;
   }
 
-  SetSoftLimit(max_allowed_footprint);
+  alloc_space_->SetMaxAllowedFootprint(max_allowed_footprint);
 }
 
-void Heap::SetSoftLimit(size_t soft_limit)
-{
-  // Compare against the actual footprint, rather than the
-  // max_allowed, because the heap may not have grown all the
-  // way to the allowed size yet.
-  //
-  size_t current_space_size = mspace_footprint(alloc_space_->mspace_);
-  if (soft_limit < current_space_size) {
-    // Don't let the space grow any more, and impose a soft limit.
-    mspace_set_max_allowed_footprint(alloc_space_->mspace_, current_space_size);
-  } else {
-    // Let the heap grow to the requested max
-    mspace_set_max_allowed_footprint(alloc_space_->mspace_, soft_limit);
-  }
-}
-
-static const size_t kHeapIdealFree = 1024 * 1024 * 2;
+// kHeapIdealFree is the ideal maximum free size, when we grow the heap for
+// utlization.
+static const size_t kHeapIdealFree = 2 * MB;
+// kHeapMinFree guarantees that you always have at least 512 KB free, when
+// you grow for utilization, regardless of target utilization ratio.
 static const size_t kHeapMinFree = kHeapIdealFree / 4;
 
 // Given the current contents of the active space, increase the allowed
diff --git a/src/heap.h b/src/heap.h
index d847a87..c25803f 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -92,8 +92,9 @@
   static void SetTargetHeapUtilization(float target) {
     target_utilization_ = target;
   }
+  // Sets the maximum number of bytes that the heap is allowed to allocate
+  // from the system.  Clamps to the appropriate maximum value.
   static void SetIdealFootprint(size_t max_allowed_footprint);
-  static void SetSoftLimit(size_t soft_limit);
 
   // Blocks the caller until the garbage collector becomes idle.
   static void WaitForConcurrentGcToComplete();
diff --git a/src/space.cc b/src/space.cc
index 1852b0d..1b887d8 100644
--- a/src/space.cc
+++ b/src/space.cc
@@ -203,11 +203,29 @@
   mspace_walk_free_pages(mspace_, DontNeed, &num_bytes_released);
 }
 
-size_t Space::MaxAllowedFootprint() {
+size_t Space::GetMaxAllowedFootprint() {
   DCHECK(mspace_ != NULL);
   return mspace_max_allowed_footprint(mspace_);
 }
 
+void Space::SetMaxAllowedFootprint(size_t limit)
+{
+  DCHECK(mspace_ != NULL);
+
+  // Compare against the actual footprint, rather than the
+  // max_allowed, because the heap may not have grown all the
+  // way to the allowed size yet.
+  //
+  size_t current_space_size = mspace_footprint(mspace_);
+  if (limit < current_space_size) {
+    // Don't let the space grow any more.
+    mspace_set_max_allowed_footprint(mspace_, current_space_size);
+  } else {
+    // Let the heap grow to the requested limit.
+    mspace_set_max_allowed_footprint(mspace_, limit);
+  }
+}
+
 void Space::Grow(size_t new_size) {
   UNIMPLEMENTED(FATAL);
 }
diff --git a/src/space.h b/src/space.h
index 83c94a0..758d80c 100644
--- a/src/space.h
+++ b/src/space.h
@@ -36,7 +36,8 @@
 
   void Trim();
 
-  size_t MaxAllowedFootprint();
+  size_t GetMaxAllowedFootprint();
+  void SetMaxAllowedFootprint(size_t limit);
 
   void Grow(size_t num_bytes);
 
@@ -102,8 +103,6 @@
 
   byte* limit_;
 
-  friend class Heap;
-
   DISALLOW_COPY_AND_ASSIGN(Space);
 };