Make dvmCardTableStartup be more independant of HeapSource startup.

Also added a call to dvmCardTableShutdown. Also removed the dependency
on GcHeap from CardTable.h.

Change-Id: Icf0293572371cc8a30b55672816fdd75a151e82c
diff --git a/vm/alloc/CardTable.c b/vm/alloc/CardTable.c
index c8d7f08..1537f7c 100644
--- a/vm/alloc/CardTable.c
+++ b/vm/alloc/CardTable.c
@@ -64,11 +64,15 @@
  * Initializes the card table; must be called before any other
  * dvmCardTable*() functions.
  */
-bool dvmCardTableStartup(GcHeap *gcHeap, void *heapBase)
+bool dvmCardTableStartup(void)
 {
     size_t length;
     void *allocBase;
     u1 *biasedBase;
+    GcHeap *gcHeap = gDvm.gcHeap;
+    void *heapBase = dvmHeapSourceGetBase();
+    assert(gcHeap != NULL);
+    assert(heapBase != NULL);
 
     /* Set up the card table */
     length = gDvm.heapSizeMax / GC_CARD_SIZE;
@@ -101,6 +105,7 @@
  */
 void dvmCardTableShutdown()
 {
+    gDvm.biasedCardTableBase = NULL;
     munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength);
 }
 
diff --git a/vm/alloc/CardTable.h b/vm/alloc/CardTable.h
index b960ec5..86995b6 100644
--- a/vm/alloc/CardTable.h
+++ b/vm/alloc/CardTable.h
@@ -32,14 +32,13 @@
 #define GC_CARD_CLEAN 0
 #define GC_CARD_DIRTY 0x70
 
-struct GcHeap;
 struct HeapBitmap;
 
 /*
  * Initializes the card table; must be called before any other
  * dvmCardTable*() functions.
  */
-bool dvmCardTableStartup(struct GcHeap *gcHeap, void *heapBase);
+bool dvmCardTableStartup(void);
 
 /*
  * Tears down the entire CardTable structure.
diff --git a/vm/alloc/Heap.c b/vm/alloc/Heap.c
index f3a4e2d..b94f106 100644
--- a/vm/alloc/Heap.c
+++ b/vm/alloc/Heap.c
@@ -84,6 +84,11 @@
     gcHeap->pendingFinalizationRefs = NULL;
     gcHeap->referenceOperations = NULL;
 
+    if (!dvmCardTableStartup()) {
+        LOGE_HEAP("card table startup failed.");
+        return false;
+    }
+
     /* Initialize the HeapWorker locks and other state
      * that the GC uses.
      */
@@ -105,10 +110,10 @@
 {
 //TODO: make sure we're locked
     if (gDvm.gcHeap != NULL) {
-        /* Tables are allocated on the native heap;
-         * they need to be cleaned up explicitly.
-         * The process may stick around, so we don't
-         * want to leak any native memory.
+        dvmCardTableShutdown();
+         /* Tables are allocated on the native heap; they need to be
+         * cleaned up explicitly.  The process may stick around, so we
+         * don't want to leak any native memory.
          */
         dvmHeapFreeLargeTable(gDvm.gcHeap->finalizableRefs);
         gDvm.gcHeap->finalizableRefs = NULL;
@@ -119,10 +124,9 @@
         dvmHeapFreeLargeTable(gDvm.gcHeap->referenceOperations);
         gDvm.gcHeap->referenceOperations = NULL;
 
-        /* Destroy the heap.  Any outstanding pointers
-         * will point to unmapped memory (unless/until
-         * someone else maps it).  This frees gDvm.gcHeap
-         * as a side-effect.
+        /* Destroy the heap.  Any outstanding pointers will point to
+         * unmapped memory (unless/until someone else maps it).  This
+         * frees gDvm.gcHeap as a side-effect.
          */
         dvmHeapSourceShutdown(&gDvm.gcHeap);
     }
diff --git a/vm/alloc/HeapSource.c b/vm/alloc/HeapSource.c
index bb27f31..f685fe9 100644
--- a/vm/alloc/HeapSource.c
+++ b/vm/alloc/HeapSource.c
@@ -547,10 +547,6 @@
     countAllocation(hs2heap(hs), hs, false);
 
     gHs = hs;
-    if (!dvmCardTableStartup(gcHeap, base)) {
-        LOGE_HEAP("card table allocation failed.");
-        goto fail;
-    }
     return gcHeap;
 
 fail:
@@ -621,6 +617,14 @@
 }
 
 /*
+ * Gets the begining of the allocation for the HeapSource.
+ */
+void *dvmHeapSourceGetBase(void)
+{
+    return gHs->heapBase;
+}
+
+/*
  * Returns the requested value. If the per-heap stats are requested, fill
  * them as well.
  *
diff --git a/vm/alloc/HeapSource.h b/vm/alloc/HeapSource.h
index fb8fdf1..be81220 100644
--- a/vm/alloc/HeapSource.h
+++ b/vm/alloc/HeapSource.h
@@ -74,6 +74,11 @@
 HeapBitmap *dvmHeapSourceGetLiveBits(void);
 
 /*
+ * Gets the begining of the allocation for the HeapSource.
+ */
+void *dvmHeapSourceGetBase(void);
+
+/*
  * Returns the requested value. If the per-heap stats are requested, fill
  * them as well.
  */