Fix valgrind gtests and memory leaks.

All tests pass other than image_test which passes if some bad reads
are disabled (buzbee working on this).

Change-Id: Ifd6b6e3aed0bc867703b6e818353a9f296609422
diff --git a/runtime/instrumentation.cc b/runtime/instrumentation.cc
index e10d881..89a63ac 100644
--- a/runtime/instrumentation.cc
+++ b/runtime/instrumentation.cc
@@ -457,6 +457,22 @@
   thread->ResetQuickAllocEntryPointsForThread();
 }
 
+void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
+  Runtime* runtime = Runtime::Current();
+  ThreadList* tl = runtime->GetThreadList();
+  if (runtime->IsStarted()) {
+    tl->SuspendAll();
+  }
+  {
+    MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
+    SetQuickAllocEntryPointsInstrumented(instrumented);
+    ResetQuickAllocEntryPoints();
+  }
+  if (runtime->IsStarted()) {
+    tl->ResumeAll();
+  }
+}
+
 void Instrumentation::InstrumentQuickAllocEntryPoints() {
   // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
   //       should be guarded by a lock.
@@ -464,15 +480,7 @@
   const bool enable_instrumentation =
       quick_alloc_entry_points_instrumentation_counter_.FetchAndAdd(1) == 0;
   if (enable_instrumentation) {
-    // Instrumentation wasn't enabled so enable it.
-    ThreadList* tl = Runtime::Current()->GetThreadList();
-    tl->SuspendAll();
-    {
-      MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
-      SetQuickAllocEntryPointsInstrumented(true);
-      ResetQuickAllocEntryPoints();
-    }
-    tl->ResumeAll();
+    SetEntrypointsInstrumented(true);
   }
 }
 
@@ -483,14 +491,7 @@
   const bool disable_instrumentation =
       quick_alloc_entry_points_instrumentation_counter_.FetchAndSub(1) == 1;
   if (disable_instrumentation) {
-    ThreadList* tl = Runtime::Current()->GetThreadList();
-    tl->SuspendAll();
-    {
-      MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
-      SetQuickAllocEntryPointsInstrumented(false);
-      ResetQuickAllocEntryPoints();
-    }
-    tl->ResumeAll();
+    SetEntrypointsInstrumented(false);
   }
 }