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) { | |
Sven Panne
2013/06/03 09:58:23
Missing const.
Yang
2013/06/03 13:37:15
Done.
| |
62 return assert_states_[type]; | |
63 } | |
64 | |
65 private: | |
66 static Thread::LocalStorageKey thread_local_key; | |
Sven Panne
2013/06/03 09:58:23
This is in the wrong class: It has nothing to do w
Yang
2013/06/03 13:37:15
Done.
| |
67 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; | |
68 | |
69 friend class Isolate; | |
70 template <PerThreadAssertType, bool> friend class PerThreadAssertScope; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); | |
73 }; | |
74 #endif // DEBUG | |
75 | |
76 | |
77 template <PerThreadAssertType type, bool allow> | |
78 class PerThreadAssertScope BASE_EMBEDDED { | |
79 public: | |
80 #ifndef DEBUG | |
81 PerThreadAssertScope() { } | |
82 static void SetIsAllowed(bool is_allowed) { } | |
83 #else | |
84 PerThreadAssertScope() { | |
85 PerThreadAssertData* data = AssertData(); | |
86 old_state_ = data->get(type); | |
87 data->set(type, allow); | |
88 } | |
89 | |
90 ~PerThreadAssertScope() { AssertData()->set(type, old_state_); } | |
91 | |
92 static bool IsAllowed() { return AssertData()->get(type); } | |
93 | |
94 // Set flag without resetting it at the end of the scope. | |
95 static void SetIsAllowed(bool is_allowed) { | |
Sven Panne
2013/06/03 09:58:23
This is a real wart, because it totally breaks the
Yang
2013/06/03 13:37:15
We use this in the deoptimizer and heap as well (f
| |
96 AssertData()->set(type, is_allowed); | |
97 } | |
98 | |
99 private: | |
100 static PerThreadAssertData* AssertData() { | |
101 PerThreadAssertData* data = reinterpret_cast<PerThreadAssertData*>( | |
102 Thread::GetThreadLocal(PerThreadAssertData::thread_local_key)); | |
103 if (data == NULL) { | |
104 data = new PerThreadAssertData(); | |
105 Thread::SetThreadLocal(PerThreadAssertData::thread_local_key, data); | |
106 } | |
107 return data; | |
108 } | |
109 | |
110 bool old_state_; | |
111 #endif | |
112 }; | |
113 | |
114 // Scope to document where we do not expect handles to be created. | |
115 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false> | |
116 DisallowHandleAllocation; | |
117 | |
118 // Scope to introduce an exception to DisallowHandleAllocation. | |
119 typedef PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true> | |
120 AllowHandleAllocation; | |
121 | |
122 // Scope to document where we do not expect any allocation and GC. | |
123 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false> | |
124 DisallowHeapAllocation; | |
125 | |
126 // Scope to introduce an exception to DisallowHeapAllocation. | |
127 typedef PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true> | |
128 AllowHeapAllocation; | |
129 | |
130 // Scope to document where we do not expect any handle dereferences. | |
131 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false> | |
132 DisallowHandleDereference; | |
133 | |
134 // Scope to introduce an exception to DisallowHandleDereference. | |
135 typedef PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true> | |
136 AllowHandleDereference; | |
137 | |
138 // Scope to document where we do not expect deferred handles to be dereferenced. | |
139 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false> | |
140 DisallowDeferredHandleDereference; | |
141 | |
142 // Scope to introduce an exception to DisallowDeferredHandleDereference. | |
143 typedef PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true> | |
144 AllowDeferredHandleDereference; | |
145 | |
146 #ifdef DEBUG | |
Sven Panne
2013/06/03 09:58:23
I don't think we need this #ifdef nor the macro be
Yang
2013/06/03 13:37:15
I'll just use the variable name to state its purpo
| |
147 #define ALLOW_DEFERRED_HANDLE_DEREF(isolate, why_this_is_safe) \ | |
148 AllowDeferredHandleDereference allow_deferred_deref; | |
149 #else | |
150 #define ALLOW_DEFERRED_HANDLE_DEREF(isolate, why_this_is_safe) | |
151 #endif // DEBUG | |
152 } } // namespace v8::internal | |
153 | |
154 #endif // V8_ASSERT_SCOPE_H_ | |
OLD | NEW |