OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_ASSERT_SCOPE_H_ |
| 29 #define V8_ASSERT_SCOPE_H_ |
| 30 |
| 31 #include "allocation.h" |
| 32 #include "platform.h" |
| 33 |
| 34 namespace v8 { |
| 35 namespace internal { |
| 36 |
| 37 class Isolate; |
| 38 |
| 39 enum PerThreadAssertType { |
| 40 HEAP_ALLOCATION_ASSERT, |
| 41 HANDLE_ALLOCATION_ASSERT, |
| 42 HANDLE_DEREFERENCE_ASSERT, |
| 43 DEFERRED_HANDLE_DEREFERENCE_ASSERT, |
| 44 LAST_PER_THREAD_ASSERT_TYPE |
| 45 }; |
| 46 |
| 47 |
| 48 #ifdef DEBUG |
| 49 class PerThreadAssertData { |
| 50 public: |
| 51 PerThreadAssertData() { |
| 52 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
| 53 assert_states_[i] = true; |
| 54 } |
| 55 } |
| 56 |
| 57 void set(PerThreadAssertType type, bool allow) { |
| 58 assert_states_[type] = allow; |
| 59 } |
| 60 |
| 61 bool get(PerThreadAssertType type) const { |
| 62 return assert_states_[type]; |
| 63 } |
| 64 |
| 65 private: |
| 66 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); |
| 69 }; |
| 70 #endif // DEBUG |
| 71 |
| 72 |
| 73 class PerThreadAssertScopeBase { |
| 74 #ifdef DEBUG |
| 75 protected: |
| 76 static PerThreadAssertData* AssertData() { |
| 77 PerThreadAssertData* data = reinterpret_cast<PerThreadAssertData*>( |
| 78 Thread::GetThreadLocal(thread_local_key)); |
| 79 if (data == NULL) { |
| 80 data = new PerThreadAssertData(); |
| 81 Thread::SetThreadLocal(thread_local_key, data); |
| 82 } |
| 83 return data; |
| 84 } |
| 85 |
| 86 static Thread::LocalStorageKey thread_local_key; |
| 87 friend class Isolate; |
| 88 #endif // DEBUG |
| 89 }; |
| 90 |
| 91 |
| 92 |
| 93 template <PerThreadAssertType type, bool allow> |
| 94 class PerThreadAssertScope : public PerThreadAssertScopeBase { |
| 95 public: |
| 96 #ifndef DEBUG |
| 97 PerThreadAssertScope() { } |
| 98 static void SetIsAllowed(bool is_allowed) { } |
| 99 #else |
| 100 PerThreadAssertScope() { |
| 101 PerThreadAssertData* data = AssertData(); |
| 102 old_state_ = data->get(type); |
| 103 data->set(type, allow); |
| 104 } |
| 105 |
| 106 ~PerThreadAssertScope() { AssertData()->set(type, old_state_); } |
| 107 |
| 108 static bool IsAllowed() { return AssertData()->get(type); } |
| 109 |
| 110 private: |
| 111 bool old_state_; |
| 112 #endif |
| 113 }; |
| 114 |
| 115 // Scope to document where we do not expect handles to be created. |
| 116 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> |
| 117 DisallowHandleAllocation; |
| 118 |
| 119 // Scope to introduce an exception to DisallowHandleAllocation. |
| 120 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true> |
| 121 AllowHandleAllocation; |
| 122 |
| 123 // Scope to document where we do not expect any allocation and GC. |
| 124 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false> |
| 125 DisallowHeapAllocation; |
| 126 |
| 127 // Scope to introduce an exception to DisallowHeapAllocation. |
| 128 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true> |
| 129 AllowHeapAllocation; |
| 130 |
| 131 // Scope to document where we do not expect any handle dereferences. |
| 132 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false> |
| 133 DisallowHandleDereference; |
| 134 |
| 135 // Scope to introduce an exception to DisallowHandleDereference. |
| 136 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true> |
| 137 AllowHandleDereference; |
| 138 |
| 139 // Scope to document where we do not expect deferred handles to be dereferenced. |
| 140 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> |
| 141 DisallowDeferredHandleDereference; |
| 142 |
| 143 // Scope to introduce an exception to DisallowDeferredHandleDereference. |
| 144 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> |
| 145 AllowDeferredHandleDereference; |
| 146 |
| 147 } } // namespace v8::internal |
| 148 |
| 149 #endif // V8_ASSERT_SCOPE_H_ |
OLD | NEW |