• 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_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;
100     ~ContinuousStack() override = default;
101     NO_COPY_SEMANTIC(ContinuousStack);
102     NO_MOVE_SEMANTIC(ContinuousStack);
103 
BeginMarking(ContinuousStack<T> * other)104     inline void BeginMarking(ContinuousStack<T> *other)
105     {
106         currentArea_ = other->currentArea_;
107         if (currentArea_ == nullptr) {
108             currentArea_ = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE);
109         }
110         ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd());
111     }
FinishMarking(ContinuousStack<T> * other)112     inline void FinishMarking(ContinuousStack<T> *other)
113     {
114         other->currentArea_ = currentArea_;
115 
116         while (!areaList_.IsEmpty()) {
117             Area *node = areaList_.PopBack();
118             NativeAreaAllocator::FreeSpace(node);
119         }
120         while (!unusedList_.IsEmpty()) {
121             Area *node = unusedList_.PopBack();
122             NativeAreaAllocator::FreeSpace(node);
123         }
124     }
125 
PopBack()126     T *PopBack()
127     {
128         if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) {
129             if (!areaList_.IsEmpty()) {
130                 unusedList_.AddNode(currentArea_);
131                 Area *last = areaList_.PopBack();
132                 currentArea_ = last;
133                 ResetTop(currentArea_->GetBegin(), currentArea_->GetEnd());
134             } else {
135                 return nullptr;
136             }
137         }
138         return reinterpret_cast<T *>(PopBackUnchecked());
139     }
140 
PushBack(T * obj)141     void PushBack(T *obj)
142     {
143         if (UNLIKELY(top_ >= end_)) {
144             Extend();
145         }
146         PushBackUnchecked(ToUintPtr(obj));
147     }
148 
Destroy()149     inline void Destroy()
150     {
151         if (currentArea_ != nullptr) {
152             NativeAreaAllocator::FreeSpace(currentArea_);
153             currentArea_ = nullptr;
154         }
155     }
156 
157 private:
Extend()158     inline void Extend()
159     {
160         auto area = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE);
161         areaList_.AddNode(currentArea_);
162         currentArea_ = area;
163         ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd());
164     }
165 
166     Area *currentArea_ {nullptr};
167     EcmaList<Area> areaList_ {};
168     EcmaList<Area> unusedList_ {};
169 };
170 
171 using MarkStack = ContinuousStack<TaggedObject>;
172 using ProcessQueue = ContinuousStack<JSTaggedType>;
173 using JSWeakMapProcessQueue = ContinuousStack<TaggedObject>;
174 }  // namespace ecmascript
175 }  // namespace panda
176 
177 #endif  // ECMASCRIPT_MEM_MARK_STACK_H
178