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/mem/ecma_list.h" 20 #include "ecmascript/mem/space.h" 21 #include "ecmascript/mem/area.h" 22 #include "ecmascript/mem/native_area_allocator.h" 23 #include "ecmascript/js_tagged_value.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 105 inline void BeginMarking(Heap *heap, ContinuousStack<T> *other); 106 inline void FinishMarking(ContinuousStack<T> *other); 107 PopBack()108 T *PopBack() 109 { 110 if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) { 111 if (!areaList_.IsEmpty()) { 112 unusedList_.AddNode(currentArea_); 113 Area *last = areaList_.PopBack(); 114 currentArea_ = last; 115 ResetTop(currentArea_->GetBegin(), currentArea_->GetEnd()); 116 } else { 117 return nullptr; 118 } 119 } 120 return reinterpret_cast<T *>(PopBackUnchecked()); 121 } 122 PushBack(T * obj)123 void PushBack(T *obj) 124 { 125 if (UNLIKELY(top_ >= end_)) { 126 Extend(); 127 } 128 PushBackUnchecked(ToUintPtr(obj)); 129 } 130 131 inline void Destroy(); 132 133 private: 134 inline void Extend(); 135 136 Heap *heap_{nullptr}; 137 Area *currentArea_{nullptr}; 138 EcmaList<Area> areaList_{}; 139 EcmaList<Area> unusedList_{}; 140 }; 141 142 using MarkStack = ContinuousStack<TaggedObject>; 143 using ProcessQueue = ContinuousStack<JSTaggedType>; 144 } // namespace ecmascript 145 } // namespace panda 146 147 #endif // ECMASCRIPT_MEM_MARK_STACK_H 148