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_MARK_STACK_H 17 #define ECMASCRIPT_MEM_MARK_STACK_H 18 19 #include "ecmascript/js_tagged_value.h" 20 #include "ecmascript/mem/area.h" 21 #include "ecmascript/mem/ecma_list.h" 22 #include "ecmascript/mem/native_area_allocator.h" 23 #include "ecmascript/mem/space.h" 24 25 namespace panda { 26 namespace ecmascript { 27 class Stack { 28 public: 29 Stack() = default; 30 virtual ~Stack() = default; 31 NO_COPY_SEMANTIC(Stack); 32 NO_MOVE_SEMANTIC(Stack); GetBegin()33 uintptr_t GetBegin() const 34 { 35 return begin_; 36 } 37 PopBackChecked()38 uintptr_t PopBackChecked() 39 { 40 if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) { 41 return 0; 42 } 43 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 44 return *--top_; 45 } 46 PushBackUnchecked(uintptr_t obj)47 void PushBackUnchecked(uintptr_t obj) 48 { 49 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 50 *top_++ = obj; 51 } 52 PopBackUnchecked()53 uintptr_t PopBackUnchecked() 54 { 55 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 56 return *--top_; 57 } 58 PushBackChecked(uintptr_t obj)59 bool PushBackChecked(uintptr_t obj) 60 { 61 if (UNLIKELY(top_ >= end_)) { 62 return false; 63 } 64 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 65 *top_++ = obj; 66 return true; 67 } 68 IsEmpty()69 bool IsEmpty() const 70 { 71 return top_ == reinterpret_cast<uintptr_t *>(begin_); 72 } 73 ResetBegin(uintptr_t begin,uintptr_t end)74 void ResetBegin(uintptr_t begin, uintptr_t end) 75 { 76 begin_ = begin; 77 top_ = reinterpret_cast<uintptr_t *>(begin); 78 end_ = reinterpret_cast<uintptr_t *>(end); 79 } 80 ResetTop(uintptr_t begin,uintptr_t end)81 void ResetTop(uintptr_t begin, uintptr_t end) 82 { 83 begin_ = begin; 84 top_ = end_ = reinterpret_cast<uintptr_t *>(end); 85 } 86 87 private: 88 template<class T> 89 friend class ContinuousStack; 90 friend class WorkNode; 91 uintptr_t begin_ {0}; 92 uintptr_t *end_ {nullptr}; 93 uintptr_t *top_ {nullptr}; 94 }; 95 96 template<class T> 97 class ContinuousStack : public Stack { 98 public: 99 ContinuousStack() = default; ContinuousStack(Heap * heap)100 explicit ContinuousStack(Heap *heap) : heap_(heap) {} 101 ~ContinuousStack() override = default; 102 NO_COPY_SEMANTIC(ContinuousStack); 103 NO_MOVE_SEMANTIC(ContinuousStack); 104 BeginMarking(Heap * heap,ContinuousStack<T> * other)105 inline void BeginMarking(Heap *heap, ContinuousStack<T> *other) 106 { 107 heap_ = heap; 108 currentArea_ = other->currentArea_; 109 if (currentArea_ == nullptr) { 110 currentArea_ = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE); 111 } 112 ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd()); 113 } FinishMarking(ContinuousStack<T> * other)114 inline void FinishMarking(ContinuousStack<T> *other) 115 { 116 other->currentArea_ = currentArea_; 117 118 while (!unusedList_.IsEmpty()) { 119 Area *node = unusedList_.PopBack(); 120 NativeAreaAllocator::FreeSpace(node); 121 } 122 } 123 PopBack()124 T *PopBack() 125 { 126 if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) { 127 if (!areaList_.IsEmpty()) { 128 unusedList_.AddNode(currentArea_); 129 Area *last = areaList_.PopBack(); 130 currentArea_ = last; 131 ResetTop(currentArea_->GetBegin(), currentArea_->GetEnd()); 132 } else { 133 return nullptr; 134 } 135 } 136 return reinterpret_cast<T *>(PopBackUnchecked()); 137 } 138 PushBack(T * obj)139 void PushBack(T *obj) 140 { 141 if (UNLIKELY(top_ >= end_)) { 142 Extend(); 143 } 144 PushBackUnchecked(ToUintPtr(obj)); 145 } 146 Destroy()147 inline void Destroy() 148 { 149 if (currentArea_ != nullptr) { 150 NativeAreaAllocator::FreeSpace(currentArea_); 151 currentArea_ = nullptr; 152 } 153 } 154 155 private: Extend()156 inline void Extend() 157 { 158 auto area = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE); 159 areaList_.AddNode(currentArea_); 160 currentArea_ = area; 161 ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd()); 162 } 163 164 Heap *heap_ {nullptr}; 165 Area *currentArea_ {nullptr}; 166 EcmaList<Area> areaList_ {}; 167 EcmaList<Area> unusedList_ {}; 168 }; 169 170 using MarkStack = ContinuousStack<TaggedObject>; 171 using ProcessQueue = ContinuousStack<JSTaggedType>; 172 } // namespace ecmascript 173 } // namespace panda 174 175 #endif // ECMASCRIPT_MEM_MARK_STACK_H 176