rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 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: |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 51 | PerThreadAssertData() : nesting_level_(0) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 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 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 65 | void increment_level() { ++nesting_level_; } |
| 66 | bool decrement_level() { return --nesting_level_ == 0; } |
| 67 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 68 | private: |
| 69 | bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 70 | int nesting_level_; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); |
| 73 | }; |
| 74 | #endif // DEBUG |
| 75 | |
| 76 | |
| 77 | class PerThreadAssertScopeBase { |
| 78 | #ifdef DEBUG |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 79 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 80 | protected: |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 81 | PerThreadAssertScopeBase() { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 82 | data_ = GetAssertData(); |
| 83 | if (data_ == NULL) { |
| 84 | data_ = new PerThreadAssertData(); |
| 85 | SetThreadLocalData(data_); |
| 86 | } |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 87 | data_->increment_level(); |
| 88 | } |
| 89 | |
| 90 | ~PerThreadAssertScopeBase() { |
| 91 | if (!data_->decrement_level()) return; |
| 92 | for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
| 93 | ASSERT(data_->get(static_cast<PerThreadAssertType>(i))); |
| 94 | } |
| 95 | delete data_; |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 96 | SetThreadLocalData(NULL); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 97 | } |
| 98 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 99 | static PerThreadAssertData* GetAssertData() { |
| 100 | return reinterpret_cast<PerThreadAssertData*>( |
| 101 | Thread::GetThreadLocal(thread_local_key)); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static Thread::LocalStorageKey thread_local_key; |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 105 | PerThreadAssertData* data_; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 106 | friend class Isolate; |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | static void SetThreadLocalData(PerThreadAssertData* data) { |
| 110 | Thread::SetThreadLocal(thread_local_key, data); |
| 111 | } |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 112 | #endif // DEBUG |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | |
| 117 | template <PerThreadAssertType type, bool allow> |
| 118 | class PerThreadAssertScope : public PerThreadAssertScopeBase { |
| 119 | public: |
| 120 | #ifndef DEBUG |
| 121 | PerThreadAssertScope() { } |
| 122 | static void SetIsAllowed(bool is_allowed) { } |
| 123 | #else |
| 124 | PerThreadAssertScope() { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 125 | old_state_ = data_->get(type); |
| 126 | data_->set(type, allow); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 129 | ~PerThreadAssertScope() { data_->set(type, old_state_); } |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 130 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 131 | static bool IsAllowed() { |
| 132 | PerThreadAssertData* data = GetAssertData(); |
| 133 | return data == NULL || data->get(type); |
| 134 | } |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | bool old_state_; |
| 138 | #endif |
| 139 | }; |
| 140 | |
| 141 | // Scope to document where we do not expect handles to be created. |
| 142 | typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> |
| 143 | DisallowHandleAllocation; |
| 144 | |
| 145 | // Scope to introduce an exception to DisallowHandleAllocation. |
| 146 | typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true> |
| 147 | AllowHandleAllocation; |
| 148 | |
| 149 | // Scope to document where we do not expect any allocation and GC. |
| 150 | typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false> |
| 151 | DisallowHeapAllocation; |
| 152 | |
| 153 | // Scope to introduce an exception to DisallowHeapAllocation. |
| 154 | typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true> |
| 155 | AllowHeapAllocation; |
| 156 | |
| 157 | // Scope to document where we do not expect any handle dereferences. |
| 158 | typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false> |
| 159 | DisallowHandleDereference; |
| 160 | |
| 161 | // Scope to introduce an exception to DisallowHandleDereference. |
| 162 | typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true> |
| 163 | AllowHandleDereference; |
| 164 | |
| 165 | // Scope to document where we do not expect deferred handles to be dereferenced. |
| 166 | typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> |
| 167 | DisallowDeferredHandleDereference; |
| 168 | |
| 169 | // Scope to introduce an exception to DisallowDeferredHandleDereference. |
| 170 | typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> |
| 171 | AllowDeferredHandleDereference; |
| 172 | |
| 173 | } } // namespace v8::internal |
| 174 | |
| 175 | #endif // V8_ASSERT_SCOPE_H_ |