| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 HANDLE_ALLOCATION_ASSERT, | 41 HANDLE_ALLOCATION_ASSERT, |
| 42 HANDLE_DEREFERENCE_ASSERT, | 42 HANDLE_DEREFERENCE_ASSERT, |
| 43 DEFERRED_HANDLE_DEREFERENCE_ASSERT, | 43 DEFERRED_HANDLE_DEREFERENCE_ASSERT, |
| 44 LAST_PER_THREAD_ASSERT_TYPE | 44 LAST_PER_THREAD_ASSERT_TYPE |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 | 47 |
| 48 #ifdef DEBUG | 48 #ifdef DEBUG |
| 49 class PerThreadAssertData { | 49 class PerThreadAssertData { |
| 50 public: | 50 public: |
| 51 PerThreadAssertData() { | 51 PerThreadAssertData() : nesting_level_(0) { |
| 52 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { | 52 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
| 53 assert_states_[i] = true; | 53 assert_states_[i] = true; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 void set(PerThreadAssertType type, bool allow) { | 57 void set(PerThreadAssertType type, bool allow) { |
| 58 assert_states_[type] = allow; | 58 assert_states_[type] = allow; |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool get(PerThreadAssertType type) const { | 61 bool get(PerThreadAssertType type) const { |
| 62 return assert_states_[type]; | 62 return assert_states_[type]; |
| 63 } | 63 } |
| 64 | 64 |
| 65 void increment_level() { ++nesting_level_; } |
| 66 bool decrement_level() { return --nesting_level_ == 0; } |
| 67 |
| 65 private: | 68 private: |
| 66 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; | 69 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; |
| 70 int nesting_level_; |
| 67 | 71 |
| 68 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); | 72 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); |
| 69 }; | 73 }; |
| 70 #endif // DEBUG | 74 #endif // DEBUG |
| 71 | 75 |
| 72 | 76 |
| 73 class PerThreadAssertScopeBase { | 77 class PerThreadAssertScopeBase { |
| 74 #ifdef DEBUG | 78 #ifdef DEBUG |
| 79 |
| 75 protected: | 80 protected: |
| 81 PerThreadAssertScopeBase() { |
| 82 data_ = AssertData(); |
| 83 data_->increment_level(); |
| 84 } |
| 85 |
| 86 ~PerThreadAssertScopeBase() { |
| 87 if (!data_->decrement_level()) return; |
| 88 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
| 89 ASSERT(data_->get(static_cast<PerThreadAssertType>(i))); |
| 90 } |
| 91 delete data_; |
| 92 Thread::SetThreadLocal(thread_local_key, NULL); |
| 93 } |
| 94 |
| 76 static PerThreadAssertData* AssertData() { | 95 static PerThreadAssertData* AssertData() { |
| 77 PerThreadAssertData* data = reinterpret_cast<PerThreadAssertData*>( | 96 PerThreadAssertData* data = reinterpret_cast<PerThreadAssertData*>( |
| 78 Thread::GetThreadLocal(thread_local_key)); | 97 Thread::GetThreadLocal(thread_local_key)); |
| 79 if (data == NULL) { | 98 if (data == NULL) { |
| 80 data = new PerThreadAssertData(); | 99 data = new PerThreadAssertData(); |
| 81 Thread::SetThreadLocal(thread_local_key, data); | 100 Thread::SetThreadLocal(thread_local_key, data); |
| 82 } | 101 } |
| 83 return data; | 102 return data; |
| 84 } | 103 } |
| 85 | 104 |
| 86 static Thread::LocalStorageKey thread_local_key; | 105 static Thread::LocalStorageKey thread_local_key; |
| 106 PerThreadAssertData* data_; |
| 87 friend class Isolate; | 107 friend class Isolate; |
| 88 #endif // DEBUG | 108 #endif // DEBUG |
| 89 }; | 109 }; |
| 90 | 110 |
| 91 | 111 |
| 92 | 112 |
| 93 template <PerThreadAssertType type, bool allow> | 113 template <PerThreadAssertType type, bool allow> |
| 94 class PerThreadAssertScope : public PerThreadAssertScopeBase { | 114 class PerThreadAssertScope : public PerThreadAssertScopeBase { |
| 95 public: | 115 public: |
| 96 #ifndef DEBUG | 116 #ifndef DEBUG |
| 97 PerThreadAssertScope() { } | 117 PerThreadAssertScope() { } |
| 98 static void SetIsAllowed(bool is_allowed) { } | 118 static void SetIsAllowed(bool is_allowed) { } |
| 99 #else | 119 #else |
| 100 PerThreadAssertScope() { | 120 PerThreadAssertScope() { |
| 101 PerThreadAssertData* data = AssertData(); | 121 old_state_ = data_->get(type); |
| 102 old_state_ = data->get(type); | 122 data_->set(type, allow); |
| 103 data->set(type, allow); | |
| 104 } | 123 } |
| 105 | 124 |
| 106 ~PerThreadAssertScope() { AssertData()->set(type, old_state_); } | 125 ~PerThreadAssertScope() { data_->set(type, old_state_); } |
| 107 | 126 |
| 108 static bool IsAllowed() { return AssertData()->get(type); } | 127 static bool IsAllowed() { return AssertData()->get(type); } |
| 109 | 128 |
| 110 private: | 129 private: |
| 111 bool old_state_; | 130 bool old_state_; |
| 112 #endif | 131 #endif |
| 113 }; | 132 }; |
| 114 | 133 |
| 115 // Scope to document where we do not expect handles to be created. | 134 // Scope to document where we do not expect handles to be created. |
| 116 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> | 135 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 140 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> | 159 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> |
| 141 DisallowDeferredHandleDereference; | 160 DisallowDeferredHandleDereference; |
| 142 | 161 |
| 143 // Scope to introduce an exception to DisallowDeferredHandleDereference. | 162 // Scope to introduce an exception to DisallowDeferredHandleDereference. |
| 144 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> | 163 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> |
| 145 AllowDeferredHandleDereference; | 164 AllowDeferredHandleDereference; |
| 146 | 165 |
| 147 } } // namespace v8::internal | 166 } } // namespace v8::internal |
| 148 | 167 |
| 149 #endif // V8_ASSERT_SCOPE_H_ | 168 #endif // V8_ASSERT_SCOPE_H_ |
| OLD | NEW |