• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H
17 #define ECMASCRIPT_MEM_ASSERT_SCOPE_H
18 
19 #include <optional>
20 
21 #include "libpandabase/macros.h"
22 #include "utils/bit_field.h"
23 
24 namespace panda::ecmascript {
25 using AssertGarbageCollectBit = panda::BitField<bool, 0, 1>;
26 using AssertHeapAllocBit = AssertGarbageCollectBit::NextFlag;
27 
28 #ifndef NDEBUG
29 constexpr bool IS_ALLOW_CHECK = true;
30 #else
31 constexpr bool IS_ALLOW_CHECK = false;
32 #endif
33 
34 enum class AssertType : uint8_t { GARBAGE_COLLECTION_ASSERT = 0, HEAP_ALLOC_ASSERT, LAST_ASSERT_TYPE };
35 
36 template<AssertType type, bool isAllow, bool IsDebug = IS_ALLOW_CHECK>
37 class AssertScopeT {
38 public:
IsAllowed()39     static bool IsAllowed()
40     {
41         return true;
42     }
43 };
44 
45 template<AssertType type, bool isAllow>
46 class AssertScopeT<type, isAllow, true> {
47 public:
48     AssertScopeT();
49 
50     ~AssertScopeT();
51 
52     static bool IsAllowed();
53 
54     NO_COPY_SEMANTIC(AssertScopeT);
55     DEFAULT_NOEXCEPT_MOVE_SEMANTIC(AssertScopeT);
56 
57 private:
58     std::optional<size_t> oldData_;
59 };
60 
61 using DisallowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, false, IS_ALLOW_CHECK>;
62 using AllowGarbageCollection = AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, true, IS_ALLOW_CHECK>;
63 using DisAllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
64 using AllowHeapAlloc = AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
65 
66 #if (!defined NDEBUG) || (defined RUN_TEST)
67 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
68 #define DISALLOW_GARBAGE_COLLECTION [[maybe_unused]] DisallowGarbageCollection no_gc
69 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
70 #define ALLOW_GARBAGE_COLLECTION [[maybe_unused]] AllowGarbageCollection allow_gc
71 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
72 #define DISALLOW_HEAP_ALLOC [[maybe_unused]] DisAllowHeapAlloc no_heap_alloc
73 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
74 #define ALLOW_HEAP_ALLOC [[maybe_unused]] AllowHeapAlloc allow_heap_alloc
75 #else
76 #define DISALLOW_GARBAGE_COLLECTION
77 #define ALLOW_GARBAGE_COLLECTION
78 #define DISALLOW_HEAP_ALLOC
79 #define ALLOW_HEAP_ALLOC
80 #endif
81 
82 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
83 #define CHECK_NO_GC ASSERT_PRINT(AllowGarbageCollection::IsAllowed(), "disallow execute garbage collection.");
84 
85 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
86 #define CHECK_NO_HEAP_ALLOC (AllowHeapAlloc::IsAllowed(), "disallow execute heap alloc.");
87 }  // namespace panda::ecmascript
88 
89 #endif  // ECMASCRIPT_MEM_ASSERT_SCOPE_H
90