Fix framework perf regression

Fix framework perf error caused by broken if statement in CanAllocate.

Add some additional checks to try and debug large object space related monkey crashes.

Change-Id: I49add14eef6f10ba0877b4d282fb6a1bbce75fa4
diff --git a/src/heap.cc b/src/heap.cc
index 74e91f5..0f9b65b 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -584,8 +584,14 @@
 }
 
 Object* Heap::TryToAllocate(AllocSpace* space, size_t alloc_size, bool grow) {
-  if (UNLIKELY(space == NULL) && CanAllocateBytes(alloc_size)) {
-    return large_object_space_->Alloc(alloc_size);
+  if (UNLIKELY(space == NULL)) {
+    if (CanAllocateBytes(alloc_size)) {
+      // TODO: This is racy, but is it worth fixing?
+      return large_object_space_->Alloc(alloc_size);
+    } else {
+      // Application ran out of heap space.
+      return NULL;
+    }
   } else if (grow) {
     return space->AllocWithGrowth(alloc_size);
   } else {
diff --git a/src/mark_sweep.cc b/src/mark_sweep.cc
index a2aa1c9..2c280a2 100644
--- a/src/mark_sweep.cc
+++ b/src/mark_sweep.cc
@@ -108,6 +108,7 @@
       LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
       SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
       if (!large_objects->Test(obj)) {
+        CHECK(large_object_space->Contains(obj)) << "Attempting to mark object " << obj << " not in large object space";
         large_objects->Set(obj);
         // Don't need to check finger since large objects never have any object references.
       }
diff --git a/src/space.cc b/src/space.cc
index 428c122..434c39e 100644
--- a/src/space.cc
+++ b/src/space.cc
@@ -519,7 +519,6 @@
 }
 
 void LargeObjectSpace::CopyLiveToMarked() {
-  MutexLock mu(lock_);
   mark_objects_->CopyFrom(*live_objects_.get());
 }
 
diff --git a/src/space.h b/src/space.h
index cf10ce4..c3c31a8 100644
--- a/src/space.h
+++ b/src/space.h
@@ -391,6 +391,11 @@
     return lock_;
   }
 
+  bool Contains(const Object* obj) const {
+    MutexLock mu(const_cast<Mutex&>(lock_));
+    return mem_maps_.find(const_cast<Object*>(obj)) != mem_maps_.end();
+  }
+
  private:
   LargeObjectSpace(const std::string& name);