• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 PANDA_MEM_GC_REFERENCE_PROCESSOR_REFERENCE_PROCESSOR_H
17 #define PANDA_MEM_GC_REFERENCE_PROCESSOR_REFERENCE_PROCESSOR_H
18 
19 #include "libpandabase/macros.h"
20 #include "runtime/include/coretypes/tagged_value.h"
21 #include "runtime/include/mem/panda_containers.h"
22 #include "runtime/mem/gc/gc_root.h"
23 #include "runtime/mem/gc/gc.h"
24 
25 namespace panda {
26 class ObjectHeader;
27 class BaseClass;
28 namespace mem {
29 enum class GCPhase;
30 class GC;
31 class Reference;
32 }  // namespace mem
33 }  // namespace panda
34 
35 namespace panda::mem {
36 
37 /// General language-independent interface for ReferenceProcessing.
38 class ReferenceProcessor {
39 public:
40     using ReferenceCheckPredicateT = typename GC::ReferenceCheckPredicateT;
41     using ReferenceProcessPredicateT = typename GC::ReferenceProcessPredicateT;
42 
43     explicit ReferenceProcessor() = default;
44     NO_COPY_SEMANTIC(ReferenceProcessor);
45     NO_MOVE_SEMANTIC(ReferenceProcessor);
46     virtual ~ReferenceProcessor() = default;
47 
48     /**
49      * True if current object is Reference and it's referent is not marked yet (maybe need to process this reference)
50      * Predicate checks GC-specific conditions on this reference (i.e. if we need to skip this reference e.g. referent
51      * is not in collection set)
52      */
53     virtual bool IsReference(const BaseClass *baseCls, const ObjectHeader *ref,
54                              const ReferenceCheckPredicateT &pred) const = 0;
55 
56     /**
57      * Save reference for future processing and handle it with GC point of view (mark needed fields, if necessary)
58      * Predicate checks if we should add this reference to the queue (e.g. don't process to many refs on concurrent)
59      */
60     virtual void HandleReference(GC *gc, GCMarkingStackType *objectsStack, const BaseClass *cls,
61                                  const ObjectHeader *object, const ReferenceProcessPredicateT &pred) = 0;
62 
63     /**
64      * Process all references which we discovered by GC.
65      * Predicate checks if we should process all references at once (e.g. processing takes too much time)
66      */
67     virtual void ProcessReferences(bool concurrent, bool clearSoftReferences, GCPhase gcPhase,
68                                    const mem::GC::ReferenceClearPredicateT &pred) = 0;
69 
70     /// Collect all processed references. They were cleared on the previous phase - we only collect them.
71     virtual panda::mem::Reference *CollectClearedReferences() = 0;
72 
73     virtual void ScheduleForEnqueue(Reference *clearedReferences) = 0;
74 
75     /// Enqueue cleared references to corresponding queue, if necessary.
76     virtual void Enqueue(panda::mem::Reference *clearedReferences) = 0;
77 
78     /// Return size of the queue of references.
79     virtual size_t GetReferenceQueueSize() const = 0;
80 };
81 
82 }  // namespace panda::mem
83 #endif  // PANDA_MEM_GC_REFERENCE_PROCESSOR_REFERENCE_PROCESSOR_H
84