Propagate failures from VirtualMemory::Commit.

BUG=23314
R=iposva@google.com

Review URL: https://codereview.chromium.org//1119403003

git-svn-id: http://dart.googlecode.com/svn/trunk@45501 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/runtime/vm/pages.cc b/runtime/vm/pages.cc
index 19da567..1cc8e95 100644
--- a/runtime/vm/pages.cc
+++ b/runtime/vm/pages.cc
@@ -46,11 +46,14 @@
 DEFINE_FLAG(bool, log_growth, false, "Log PageSpace growth policy decisions.");
 
 HeapPage* HeapPage::Initialize(VirtualMemory* memory, PageType type) {
+  ASSERT(memory != NULL);
   ASSERT(memory->size() > VirtualMemory::PageSize());
   bool is_executable = (type == kExecutable);
-  memory->Commit(is_executable);
-
+  if (!memory->Commit(is_executable)) {
+    return NULL;
+  }
   HeapPage* result = reinterpret_cast<HeapPage*>(memory->address());
+  ASSERT(result != NULL);
   result->memory_ = memory;
   result->next_ = NULL;
   result->executable_ = is_executable;
@@ -64,7 +67,12 @@
   if (memory == NULL) {
     return NULL;
   }
-  return Initialize(memory, type);
+  HeapPage* result = Initialize(memory, type);
+  if (result == NULL) {
+    delete memory;  // Release reservation to OS.
+    return NULL;
+  }
+  return result;
 }
 
 
diff --git a/runtime/vm/pages.h b/runtime/vm/pages.h
index c6b4f38..79d5343 100644
--- a/runtime/vm/pages.h
+++ b/runtime/vm/pages.h
@@ -69,6 +69,7 @@
     object_end_ = val;
   }
 
+  // These return NULL on OOM.
   static HeapPage* Initialize(VirtualMemory* memory, PageType type);
   static HeapPage* Allocate(intptr_t size_in_words, PageType type);
 
diff --git a/runtime/vm/virtual_memory.h b/runtime/vm/virtual_memory.h
index 844ff5e..607e6b2 100644
--- a/runtime/vm/virtual_memory.h
+++ b/runtime/vm/virtual_memory.h
@@ -35,7 +35,8 @@
     return region_.Contains(addr);
   }
 
-  // Commits the virtual memory area, which is guaranteed to be zeroed.
+  // Commits the virtual memory area, which is guaranteed to be zeroed. Returns
+  // true on success and false on failure (e.g., out-of-memory).
   bool Commit(bool is_executable) {
     return Commit(start(), size(), is_executable);
   }