1 /** 2 * Copyright (c) 2021-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 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 ark { 26 class ObjectHeader; 27 class BaseClass; 28 namespace mem { 29 enum class GCPhase; 30 class GC; 31 class Reference; 32 } // namespace mem 33 } // namespace ark 34 35 namespace ark::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 using ReferenceProcessorT = typename GC::ReferenceProcessorT; 43 44 explicit ReferenceProcessor() = default; 45 NO_COPY_SEMANTIC(ReferenceProcessor); 46 NO_MOVE_SEMANTIC(ReferenceProcessor); 47 virtual ~ReferenceProcessor() = default; 48 Initialize()49 virtual void Initialize() {} 50 51 /** 52 * True if current object is Reference and it's referent is not marked yet (maybe need to process this reference) 53 * Predicate checks GC-specific conditions on this reference (i.e. if we need to skip this reference e.g. referent 54 * is not in collection set) 55 */ 56 virtual bool IsReference(const BaseClass *baseCls, const ObjectHeader *ref, 57 const ReferenceCheckPredicateT &pred) const = 0; 58 59 /** 60 * Save reference for future processing and handle it with GC point of view (mark needed fields, if necessary) 61 * Predicate checks if we should add this reference to the queue (e.g. don't process to many refs on concurrent) 62 */ 63 virtual void HandleReference(GC *gc, GCMarkingStackType *objectsStack, const BaseClass *cls, 64 const ObjectHeader *object, const ReferenceProcessPredicateT &pred) = 0; 65 /// Save reference for future processing and handle it with GC point of view (traverse needed fields, if necessary) HandleReference(GC * gc,const BaseClass * cls,const ObjectHeader * object,const ReferenceProcessorT & processor)66 virtual void HandleReference([[maybe_unused]] GC *gc, [[maybe_unused]] const BaseClass *cls, 67 [[maybe_unused]] const ObjectHeader *object, 68 [[maybe_unused]] const ReferenceProcessorT &processor) 69 { 70 ASSERT_PRINT(false, "Should be implemented by subclasses"); 71 } 72 73 /** 74 * Process all references which we discovered by GC. 75 * Predicate checks if we should process all references at once (e.g. processing takes too much time) 76 */ 77 virtual void ProcessReferences(bool concurrent, bool clearSoftReferences, GCPhase gcPhase, 78 const mem::GC::ReferenceClearPredicateT &pred) = 0; 79 /** 80 * Process all references which we discovered by GC. 81 * Predicate checks which references should be processed 82 */ ProcessReferencesAfterCompaction(const mem::GC::ReferenceClearPredicateT & pred)83 virtual void ProcessReferencesAfterCompaction([[maybe_unused]] const mem::GC::ReferenceClearPredicateT &pred) 84 { 85 ASSERT_PRINT(false, "Should be implemented by subclasses"); 86 } 87 88 /// Collect all processed references. They were cleared on the previous phase - we only collect them. 89 virtual ark::mem::Reference *CollectClearedReferences() = 0; 90 91 virtual void ScheduleForEnqueue(Reference *clearedReferences) = 0; 92 93 /// Enqueue cleared references to corresponding queue, if necessary. 94 virtual void Enqueue(ark::mem::Reference *clearedReferences) = 0; 95 96 /// Return size of the queue of references. 97 virtual size_t GetReferenceQueueSize() const = 0; 98 99 /// Process finalizers of references. ProcessFinalizers()100 virtual void ProcessFinalizers() {} 101 }; 102 103 } // namespace ark::mem 104 #endif // PANDA_MEM_GC_REFERENCE_PROCESSOR_REFERENCE_PROCESSOR_H 105