• 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 /**
38  * General language-independent interface for ReferenceProcessing.
39  */
40 class ReferenceProcessor {
41 public:
42     using ReferenceCheckPredicateT = typename GC::ReferenceCheckPredicateT;
43     using ReferenceProcessPredicateT = typename GC::ReferenceProcessPredicateT;
44 
45     explicit ReferenceProcessor() = default;
46     NO_COPY_SEMANTIC(ReferenceProcessor);
47     NO_MOVE_SEMANTIC(ReferenceProcessor);
48     virtual ~ReferenceProcessor() = default;
49 
50     /**
51      * True if current object is Reference and it's referent is not marked yet (maybe need to process this reference)
52      * Predicate checks GC-specific conditions on this reference (i.e. if we need to skip this reference e.g. referent
53      * is not in collection set)
54      */
55     virtual bool IsReference(const BaseClass *baseCls, const ObjectHeader *ref,
56                              const ReferenceCheckPredicateT &pred) const = 0;
57 
58     /**
59      * Save reference for future processing and handle it with GC point of view (mark needed fields, if necessary)
60      * Predicate checks if we should add this reference to the queue (e.g. don't process to many refs on concurrent)
61      */
62     virtual void HandleReference(GC *gc, GCMarkingStackType *objectsStack, const BaseClass *cls,
63                                  const ObjectHeader *object, const ReferenceProcessPredicateT &pred) = 0;
64 
65     /**
66      * Process all references which we discovered by GC.
67      * Predicate checks if we should process all references at once (e.g. processing takes too much time)
68      */
69     virtual void ProcessReferences(bool concurrent, bool clearSoftReferences, GCPhase gcPhase,
70                                    const mem::GC::ReferenceClearPredicateT &pred) = 0;
71 
72     /**
73      * Collect all processed references. They were cleared on the previous phase - we only collect them.
74      */
75     virtual panda::mem::Reference *CollectClearedReferences() = 0;
76 
77     virtual void ScheduleForEnqueue(Reference *clearedReferences) = 0;
78 
79     /**
80      * Enqueue cleared references to corresponding queue, if necessary.
81      */
82     virtual void Enqueue(panda::mem::Reference *clearedReferences) = 0;
83 
84     /**
85      * Return size of the queue of references.
86      */
87     virtual size_t GetReferenceQueueSize() const = 0;
88 };
89 
90 }  // namespace panda::mem
91 #endif  // PANDA_MEM_GC_REFERENCE_PROCESSOR_REFERENCE_PROCESSOR_H
92