Merge r3971 and r3984 to trunk forming V8 version 2.1.2.4. This adds
the new context disposal notification API.
Review URL: http://codereview.chromium.org/660268

git-svn-id: http://v8.googlecode.com/svn/trunk@3985 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
diff --git a/src/heap.cc b/src/heap.cc
index cfb786a..a15c6c0 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -115,7 +115,10 @@
 
 int Heap::always_allocate_scope_depth_ = 0;
 int Heap::linear_allocation_scope_depth_ = 0;
-bool Heap::context_disposed_pending_ = false;
+
+int Heap::contexts_disposed_ = 0;
+bool Heap::context_disposed_use_deprecated_heuristic_ = true;
+bool Heap::context_disposed_deprecated_pending_ = false;
 
 #ifdef DEBUG
 bool Heap::allocation_allowed_ = true;
@@ -371,21 +374,29 @@
 }
 
 
-void Heap::CollectAllGarbageIfContextDisposed() {
+void Heap::CollectAllGarbageIfContextDisposedDeprecated() {
+  if (!context_disposed_use_deprecated_heuristic_) return;
   // If the garbage collector interface is exposed through the global
   // gc() function, we avoid being clever about forcing GCs when
   // contexts are disposed and leave it to the embedder to make
   // informed decisions about when to force a collection.
-  if (!FLAG_expose_gc && context_disposed_pending_) {
+  if (!FLAG_expose_gc && context_disposed_deprecated_pending_) {
     HistogramTimerScope scope(&Counters::gc_context);
     CollectAllGarbage(false);
   }
-  context_disposed_pending_ = false;
+  context_disposed_deprecated_pending_ = false;
 }
 
 
 void Heap::NotifyContextDisposed() {
-  context_disposed_pending_ = true;
+  context_disposed_use_deprecated_heuristic_ = false;
+  contexts_disposed_++;
+}
+
+
+void Heap::NotifyContextDisposedDeprecated() {
+  if (!context_disposed_use_deprecated_heuristic_) return;
+  context_disposed_deprecated_pending_ = true;
 }
 
 
@@ -620,7 +631,9 @@
   Shrink();
 
   Counters::objs_since_last_full.Set(0);
-  context_disposed_pending_ = false;
+
+  contexts_disposed_ = 0;
+  context_disposed_deprecated_pending_ = false;
 }
 
 
@@ -3072,6 +3085,13 @@
   static int number_idle_notifications = 0;
   static int last_gc_count = gc_count_;
 
+  if (!FLAG_expose_gc && (contexts_disposed_ > 0)) {
+    HistogramTimerScope scope(&Counters::gc_context);
+    CollectAllGarbage(false);
+    ASSERT(contexts_disposed_ == 0);
+    return false;
+  }
+
   bool finished = false;
 
   if (last_gc_count == gc_count_) {