Revert "Revert "Trim pre-Zygote fork, fix under-estimate of heap limit.""

This reverts commit 4c7e27186d87ccd40646fd4f514dc982ff5b40b1.
diff --git a/vm/alloc/HeapSource.cpp b/vm/alloc/HeapSource.cpp
index c62cde1..cd2f4df 100644
--- a/vm/alloc/HeapSource.cpp
+++ b/vm/alloc/HeapSource.cpp
@@ -639,6 +639,9 @@
     assert(gDvm.zygote);
 
     if (!gDvm.newZygoteHeapAllocated) {
+       /* Ensure heaps are trimmed to minimize footprint pre-fork.
+        */
+        trimHeaps();
         /* Create a new heap for post-fork zygote allocations.  We only
          * try once, even if it fails.
          */
@@ -686,6 +689,25 @@
 }
 
 /*
+ * Returns a high water mark, between base and limit all objects must have been
+ * allocated.
+ */
+void *dvmHeapSourceGetLimit()
+{
+    HeapSource *hs = gHs;
+    void *max_brk = hs->heaps[0].brk;
+
+#ifndef NDEBUG
+    for (size_t i = 1; i < hs->numHeaps; i++) {
+        Heap *const heap = &hs->heaps[i];
+        void *heap_brk = heap->brk;
+        assert (max_brk > heap_brk);
+    }
+#endif
+    return max_brk;
+}
+
+/*
  * Returns the requested value. If the per-heap stats are requested, fill
  * them as well.
  *
@@ -706,7 +728,8 @@
 
         switch (spec) {
         case HS_FOOTPRINT:
-            value = mspace_footprint(heap->msp);
+            value = heap->brk - heap->base;
+            assert(value == mspace_footprint(heap->msp));
             break;
         case HS_ALLOWED_FOOTPRINT:
             value = mspace_footprint_limit(heap->msp);
@@ -979,7 +1002,7 @@
 {
     HS_BOILERPLATE();
 
-    return (dvmHeapBitmapCoversAddress(&gHs->liveBits, ptr));
+    return (dvmHeapSourceGetBase() <= ptr) && (ptr <= dvmHeapSourceGetLimit());
 }
 
 /*
diff --git a/vm/alloc/HeapSource.h b/vm/alloc/HeapSource.h
index 4bd11cd..e1f6820 100644
--- a/vm/alloc/HeapSource.h
+++ b/vm/alloc/HeapSource.h
@@ -92,6 +92,12 @@
 void *dvmHeapSourceGetBase(void);
 
 /*
+ * Returns a high water mark, between base and limit all objects must have been
+ * allocated.
+ */
+void *dvmHeapSourceGetLimit(void);
+
+/*
  * Returns the requested value. If the per-heap stats are requested, fill
  * them as well.
  */
diff --git a/vm/alloc/MarkSweep.cpp b/vm/alloc/MarkSweep.cpp
index d4f4669..268d880 100644
--- a/vm/alloc/MarkSweep.cpp
+++ b/vm/alloc/MarkSweep.cpp
@@ -560,7 +560,7 @@
 
     footprint = dvmHeapSourceGetValue(HS_FOOTPRINT, NULL, 0);
     base = &h->cardTableBase[0];
-    limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetBase() + footprint);
+    limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetLimit());
     assert(limit <= &h->cardTableBase[h->cardTableLength]);
 
     ptr = base;