• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 #ifndef PANDA_RUNTIME_MEM_GC_GC_ADAPTIVE_MARKING_STACK_H
16 #define PANDA_RUNTIME_MEM_GC_GC_ADAPTIVE_MARKING_STACK_H
17 
18 #include "runtime/mem/gc/gc_root_type.h"
19 #include "runtime/mem/gc/workers/gc_workers_tasks.h"
20 #include "runtime/include/mem/panda_containers.h"
21 #include "runtime/mem/gc/gc_adaptive_stack.h"
22 
23 namespace ark::mem {
24 
25 class GC;
26 
27 /*
28  * Adaptive stack with GC workers support.
29  * It will try to pop objects from the source stack and push
30  * it to the destination stack. if the destination stack reaches the limit,
31  * we will create a new task for worker.
32  * If stack limit is equal to zero, it means that the destination stack is unlimited.
33  */
34 class GCAdaptiveMarkingStack : public GCAdaptiveStack<ObjectHeader *> {
35 public:
36     using ObjectVisitor = std::function<void(const ObjectHeader *)>;
37     using MarkedObjects = PandaVector<PandaDeque<ObjectHeader *> *>;
38 
39     using GCAdaptiveStack<ObjectHeader *>::GCAdaptiveStack;
40 
41     NO_COPY_SEMANTIC(GCAdaptiveMarkingStack);
42     NO_MOVE_SEMANTIC(GCAdaptiveMarkingStack);
43 
44     ~GCAdaptiveMarkingStack() override;
45 
46     /**
47      * This method should be used when we find new object by field from another object.
48      * @param from_object from which object we found object by reference, nullptr for roots
49      * @param object object which will be added to the stack
50      */
51     void PushToStack(const ObjectHeader *fromObject, ObjectHeader *object);
52 
53     /**
54      * This method should be used when we find new object as a root
55      * @param root_type type of the root which we found
56      * @param object object which will be added to the stack
57      */
58     void PushToStack(RootType rootType, ObjectHeader *object);
59 
60     /**
61      * @brief Travers objects in stack via visitor and return marked objects.
62      * Visitor can push in stack, but mustn't pop from it.
63      */
64     MarkedObjects MarkObjects(const ObjectVisitor &visitor);
65 
GetAdditionalMarkingInfo()66     void *GetAdditionalMarkingInfo() const
67     {
68         return additionalMarkingInfo_;
69     }
70 
SetAdditionalMarkingInfo(void * infoPtr)71     void SetAdditionalMarkingInfo(void *infoPtr)
72     {
73         additionalMarkingInfo_ = infoPtr;
74     }
75 
76 protected:
77     GCAdaptiveMarkingStack *CreateStack() override;
78     GCWorkersTask CreateTask(GCAdaptiveStack<ObjectHeader *> *stack) override;
79 
80 private:
81     void *additionalMarkingInfo_ {nullptr};
82 };
83 
84 }  // namespace ark::mem
85 
86 #endif  // PANDA_RUNTIME_MEM_GC_GC_ADAPTIVE_MARKING_STACK_H
87