Move reference processing next to its implementation.

With this change, all of the reference processing now happens in the
same module.  As such, we can make all of the implementation routines
static.

Change-Id: If3f1ff2e48f49b57b75cfc9d0af61ad892435c47
diff --git a/vm/alloc/Heap.c b/vm/alloc/Heap.c
index d6e08ae..7e47e73 100644
--- a/vm/alloc/Heap.c
+++ b/vm/alloc/Heap.c
@@ -676,35 +676,13 @@
         dvmHeapReScanMarkedObjects();
     }
 
-    /* All strongly-reachable objects have now been marked.
+    /*
+     * All strongly-reachable objects have now been marked.  Process
+     * weakly-reachable objects discovered while tracing.
      */
-    LOGD_HEAP("Handling soft references...");
-    if (!clearSoftRefs) {
-        dvmHandleSoftRefs(&gcHeap->softReferences);
-    }
-    dvmClearWhiteRefs(&gcHeap->softReferences);
-
-    LOGD_HEAP("Handling weak references...");
-    dvmClearWhiteRefs(&gcHeap->weakReferences);
-
-    /* Once all weak-reachable objects have been taken
-     * care of, any remaining unmarked objects can be finalized.
-     */
-    LOGD_HEAP("Finding finalizations...");
-    dvmHeapScheduleFinalizations();
-
-    LOGD_HEAP("Handling f-reachable soft references...");
-    dvmClearWhiteRefs(&gcHeap->softReferences);
-
-    LOGD_HEAP("Handling f-reachable weak references...");
-    dvmClearWhiteRefs(&gcHeap->weakReferences);
-
-    /* Any remaining objects that are not pending finalization
-     * could be phantom-reachable.  This will mark any phantom-reachable
-     * objects, as well as enqueue their references.
-     */
-    LOGD_HEAP("Handling phantom references...");
-    dvmClearWhiteRefs(&gcHeap->phantomReferences);
+    dvmHeapProcessReferences(&gcHeap->softReferences, clearSoftRefs,
+                             &gcHeap->weakReferences,
+                             &gcHeap->phantomReferences);
 
 #if defined(WITH_JIT)
     /*
diff --git a/vm/alloc/MarkSweep.c b/vm/alloc/MarkSweep.c
index e0ff3a6..ccbb663 100644
--- a/vm/alloc/MarkSweep.c
+++ b/vm/alloc/MarkSweep.c
@@ -811,7 +811,7 @@
  * removed from the list.  References with white referents biased
  * toward saving are blackened and also removed from the list.
  */
-void dvmHandleSoftRefs(Object **list)
+static void preserveSomeSoftReferences(Object **list)
 {
     GcMarkContext *ctx;
     Object *ref, *referent;
@@ -852,7 +852,7 @@
  * referents.  Cleared references registered to a reference queue are
  * scheduled for appending by the heap worker thread.
  */
-void dvmClearWhiteRefs(Object **list)
+static void clearWhiteReferences(Object **list)
 {
     GcMarkContext *ctx;
     Object *ref, *referent;
@@ -888,7 +888,7 @@
 /* Find unreachable objects that need to be finalized,
  * and schedule them for finalization.
  */
-void dvmHeapScheduleFinalizations()
+static void scheduleFinalizations(void)
 {
     HeapRefTable newPendingRefs;
     LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs;
@@ -910,8 +910,8 @@
         //      we can schedule them next time.  Watch out,
         //      because we may be expecting to free up space
         //      by calling finalizers.
-        LOGE_GC("dvmHeapScheduleFinalizations(): no room for "
-                "pending finalizations\n");
+        LOGE_GC("scheduleFinalizations(): no room for "
+                "pending finalizations");
         dvmAbort();
     }
 
@@ -930,8 +930,8 @@
                 if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) {
                     //TODO: add the current table and allocate
                     //      a new, smaller one.
-                    LOGE_GC("dvmHeapScheduleFinalizations(): "
-                            "no room for any more pending finalizations: %zd\n",
+                    LOGE_GC("scheduleFinalizations(): "
+                            "no room for any more pending finalizations: %zd",
                             dvmHeapNumHeapRefTableEntries(&newPendingRefs));
                     dvmAbort();
                 }
@@ -956,7 +956,7 @@
         totalPendCount += newPendCount;
         finRefs = finRefs->next;
     }
-    LOGD_GC("dvmHeapScheduleFinalizations(): %zd finalizers triggered.\n",
+    LOGD_GC("scheduleFinalizations(): %zd finalizers triggered.",
             totalPendCount);
     if (totalPendCount == 0) {
         /* No objects required finalization.
@@ -971,8 +971,8 @@
     if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs,
                 &newPendingRefs))
     {
-        LOGE_GC("dvmHeapScheduleFinalizations(): can't insert new "
-                "pending finalizations\n");
+        LOGE_GC("scheduleFinalizations(): can't insert new "
+                "pending finalizations");
         dvmAbort();
     }
 
@@ -993,6 +993,52 @@
     dvmSignalHeapWorker(false);
 }
 
+/*
+ * Process reference class instances and schedule finalizations.
+ */
+void dvmHeapProcessReferences(Object **softReferences, bool clearSoftRefs,
+                              Object **weakReferences,
+                              Object **phantomReferences)
+{
+    assert(softReferences != NULL);
+    assert(weakReferences != NULL);
+    assert(phantomReferences != NULL);
+    /*
+     * Unless we are required to clear soft references with white
+     * references, preserve some white referents.
+     */
+    if (!clearSoftRefs) {
+        preserveSomeSoftReferences(softReferences);
+    }
+    /*
+     * Clear all remaining soft and weak references with white
+     * referents.
+     */
+    clearWhiteReferences(softReferences);
+    clearWhiteReferences(weakReferences);
+    /*
+     * Preserve all white objects with finalize methods and schedule
+     * them for finalization.
+     */
+    scheduleFinalizations();
+    /*
+     * Clear all f-reachable soft and weak references with white
+     * referents.
+     */
+    clearWhiteReferences(softReferences);
+    clearWhiteReferences(weakReferences);
+    /*
+     * Clear all phantom references with white referents.
+     */
+    clearWhiteReferences(phantomReferences);
+    /*
+     * At this point all reference lists should be empty.
+     */
+    assert(*softReferences == NULL);
+    assert(*weakReferences == NULL);
+    assert(*phantomReferences == NULL);
+}
+
 void dvmHeapFinishMarkStep()
 {
     GcMarkContext *ctx;
diff --git a/vm/alloc/MarkSweep.h b/vm/alloc/MarkSweep.h
index 59b34e1..ece12e0 100644
--- a/vm/alloc/MarkSweep.h
+++ b/vm/alloc/MarkSweep.h
@@ -51,9 +51,9 @@
 void dvmHeapReMarkRootSet(void);
 void dvmHeapScanMarkedObjects(void);
 void dvmHeapReScanMarkedObjects(void);
-void dvmHandleSoftRefs(Object **list);
-void dvmClearWhiteRefs(Object **list);
-void dvmHeapScheduleFinalizations(void);
+void dvmHeapProcessReferences(Object **softReferences, bool clearSoftRefs,
+                              Object **weakReferences,
+                              Object **phantomReferences);
 void dvmHeapFinishMarkStep(void);
 void dvmHeapSweepSystemWeaks(void);
 void dvmHeapSweepUnmarkedObjects(GcMode mode, bool isConcurrent,