expose AssertNoAllocation to api
authordcarney@chromium.org <dcarney@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 2 May 2013 20:28:02 +0000 (20:28 +0000)
committerdcarney@chromium.org <dcarney@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 2 May 2013 20:28:02 +0000 (20:28 +0000)
R=svenpanne@chromium.org
BUG=

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14531 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

include/v8.h
src/api.cc
src/heap-inl.h
src/heap.h

index cb06a78..43ebbf4 100644 (file)
@@ -3877,6 +3877,24 @@ class V8EXPORT PersistentHandleVisitor {  // NOLINT
 
 
 /**
+ * Asserts that no action is performed that could cause a handle's value
+ * to be modified. Useful when otherwise unsafe handle operations need to
+ * be performed.
+ */
+class V8EXPORT AssertNoGCScope {
+#ifndef DEBUG
+  V8_INLINE(AssertNoGCScope(Isolate* isolate)) {}
+#else
+  AssertNoGCScope(Isolate* isolate);
+  ~AssertNoGCScope();
+ private:
+  Isolate* isolate_;
+  bool last_state_;
+#endif
+};
+
+
+/**
  * Container class for static utility functions.
  */
 class V8EXPORT V8 {
index d995e4f..107c9ba 100644 (file)
@@ -6077,6 +6077,19 @@ Local<Integer> v8::Integer::NewFromUnsigned(uint32_t value, Isolate* isolate) {
 }
 
 
+#ifdef DEBUG
+v8::AssertNoGCScope::AssertNoGCScope(v8::Isolate* isolate)
+  : isolate_(isolate),
+    last_state_(i::EnterAllocationScope(
+        reinterpret_cast<i::Isolate*>(isolate), false)) {
+}
+
+v8::AssertNoGCScope::~AssertNoGCScope() {
+  i::ExitAllocationScope(reinterpret_cast<i::Isolate*>(isolate_), last_state_);
+}
+#endif
+
+
 void V8::IgnoreOutOfMemoryException() {
   EnterIsolateIfNeeded()->set_ignore_out_of_memory(true);
 }
index f937426..18a2cf3 100644 (file)
@@ -650,6 +650,10 @@ inline bool Heap::allow_allocation(bool new_state) {
   return old;
 }
 
+inline void Heap::set_allow_allocation(bool allocation_allowed) {
+  allocation_allowed_ = allocation_allowed;
+}
+
 #endif
 
 
@@ -864,33 +868,36 @@ DisallowAllocationFailure::~DisallowAllocationFailure() {
 
 
 #ifdef DEBUG
-AssertNoAllocation::AssertNoAllocation() {
-  Isolate* isolate = ISOLATE;
-  active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread();
-  if (active_) {
-    old_state_ = isolate->heap()->allow_allocation(false);
+bool EnterAllocationScope(Isolate* isolate, bool allow_allocation) {
+  bool active = !isolate->optimizing_compiler_thread()->IsOptimizerThread();
+  bool last_state = isolate->heap()->IsAllocationAllowed();
+  if (active) {
+    isolate->heap()->set_allow_allocation(allow_allocation);
   }
+  return last_state;
 }
 
 
-AssertNoAllocation::~AssertNoAllocation() {
-  if (active_) HEAP->allow_allocation(old_state_);
+void ExitAllocationScope(Isolate* isolate, bool last_state) {
+  isolate->heap()->set_allow_allocation(last_state);
 }
 
 
-DisableAssertNoAllocation::DisableAssertNoAllocation() {
-  Isolate* isolate = ISOLATE;
-  active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread();
-  if (active_) {
-    old_state_ = isolate->heap()->allow_allocation(true);
-  }
+AssertNoAllocation::AssertNoAllocation()
+    : last_state_(EnterAllocationScope(ISOLATE, false)) {
 }
 
+AssertNoAllocation::~AssertNoAllocation() {
+  ExitAllocationScope(ISOLATE, last_state_);
+}
 
-DisableAssertNoAllocation::~DisableAssertNoAllocation() {
-  if (active_) HEAP->allow_allocation(old_state_);
+DisableAssertNoAllocation::DisableAssertNoAllocation()
+  : last_state_(EnterAllocationScope(ISOLATE, true)) {
 }
 
+DisableAssertNoAllocation::~DisableAssertNoAllocation() {
+  ExitAllocationScope(ISOLATE, last_state_);
+}
 #else
 
 AssertNoAllocation::AssertNoAllocation() { }
index 7722079..b25aa7f 100644 (file)
@@ -1476,6 +1476,7 @@ class Heap {
 
 #ifdef DEBUG
   bool IsAllocationAllowed() { return allocation_allowed_; }
+  inline void set_allow_allocation(bool allocation_allowed);
   inline bool allow_allocation(bool enable);
 
   bool disallow_allocation_failure() {
@@ -2691,6 +2692,13 @@ class DescriptorLookupCache {
 // { AssertNoAllocation nogc;
 //   ...
 // }
+
+#ifdef DEBUG
+inline bool EnterAllocationScope(Isolate* isolate, bool allow_allocation);
+inline void ExitAllocationScope(Isolate* isolate, bool last_state);
+#endif
+
+
 class AssertNoAllocation {
  public:
   inline AssertNoAllocation();
@@ -2698,8 +2706,7 @@ class AssertNoAllocation {
 
 #ifdef DEBUG
  private:
-  bool old_state_;
-  bool active_;
+  bool last_state_;
 #endif
 };
 
@@ -2711,8 +2718,7 @@ class DisableAssertNoAllocation {
 
 #ifdef DEBUG
  private:
-  bool old_state_;
-  bool active_;
+  bool last_state_;
 #endif
 };