1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef ECMASCRIPT_MEM_ASSERT_SCOPE_INL_H
17 #define ECMASCRIPT_MEM_ASSERT_SCOPE_INL_H
18
19 #include "assert_scope.h"
20
21 namespace panda::ecmascript {
22 // Thread-local storage for assert data. Default all asserts to "allow".
23 // NOLINTNEXTLINE(hicpp-signed-bitwise)
24 static thread_local size_t currentAssertData(~0);
25
26 template<AssertType type, bool isAllow>
AssertScopeT()27 AssertScopeT<type, isAllow, true>::AssertScopeT() : oldData_(currentAssertData)
28 {
29 switch (type) {
30 case AssertType::GARBAGE_COLLECTION_ASSERT:
31 currentAssertData = AssertGarbageCollectBit::Update(oldData_.value(), isAllow);
32 break;
33 case AssertType::HEAP_ALLOC_ASSERT:
34 currentAssertData = AssertHeapAllocBit::Update(oldData_.value(), isAllow);
35 break;
36 default:
37 break;
38 }
39 }
40
41 template<AssertType type, bool isAllow>
~AssertScopeT()42 AssertScopeT<type, isAllow, true>::~AssertScopeT()
43 {
44 if (!oldData_.has_value()) {
45 return;
46 }
47
48 currentAssertData = oldData_.value();
49 oldData_.reset();
50 }
51
52 // static
53 template<AssertType type, bool isAllow>
IsAllowed()54 bool AssertScopeT<type, isAllow, true>::IsAllowed()
55 {
56 switch (type) {
57 case AssertType::GARBAGE_COLLECTION_ASSERT:
58 return AssertGarbageCollectBit::Decode(currentAssertData);
59 case AssertType::HEAP_ALLOC_ASSERT:
60 return AssertHeapAllocBit::Decode(currentAssertData);
61 default:
62 return true;
63 }
64 }
65 } // namespace panda::ecmascript
66
67 #endif // ECMASCRIPT_MEM_ASSERT_SCOPE_INL_H