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 PANDA_RUNTIME_MEM_GC_GC_ROOT_H_ 17 #define PANDA_RUNTIME_MEM_GC_GC_ROOT_H_ 18 19 #include <algorithm> 20 #include <ostream> 21 #include <vector> 22 23 #include "libpandabase/mem/mem.h" 24 #include "libpandabase/mem/mem_range.h" 25 #include "runtime/include/class.h" 26 #include "runtime/include/language_config.h" 27 #include "runtime/include/mem/panda_containers.h" 28 #include "runtime/interpreter/frame.h" 29 #include "runtime/mem/object_helpers.h" 30 #include "runtime/mem/gc/card_table.h" 31 32 namespace panda { 33 class PandaVM; 34 class ManagedThread; 35 } // namespace panda 36 37 namespace panda::mem { 38 39 enum class RootType { 40 ROOT_UNKNOWN = 0, 41 ROOT_CLASS, 42 ROOT_FRAME, 43 ROOT_THREAD, 44 ROOT_CLASS_LINKER, 45 ROOT_TENURED, 46 ROOT_VM, 47 ROOT_JNI_GLOBAL, 48 ROOT_JNI_LOCAL, 49 ROOT_RS_GLOBAL, 50 ROOT_PT_LOCAL, 51 ROOT_AOT_STRING_SLOT, 52 }; 53 54 enum class VisitGCRootFlags : uint32_t { 55 ACCESS_ROOT_ALL = 1U, 56 ACCESS_ROOT_ONLY_NEW = 1U << 1U, 57 ACCESS_ROOT_NONE = 1U << 2U, 58 59 ACCESS_ROOT_AOT_STRINGS_ONLY_YOUNG = 1U << 3U, 60 61 START_RECORDING_NEW_ROOT = 1U << 10U, 62 END_RECORDING_NEW_ROOT = 1U << 11U, 63 }; 64 65 uint32_t operator&(VisitGCRootFlags left, VisitGCRootFlags right); 66 67 VisitGCRootFlags operator|(VisitGCRootFlags left, VisitGCRootFlags right); 68 69 /** 70 * \brief I am groot 71 */ 72 class GCRoot { 73 public: 74 GCRoot(RootType type, ObjectHeader *obj); 75 GCRoot(RootType type, ObjectHeader *from_object, ObjectHeader *obj); 76 77 RootType GetType() const; 78 ObjectHeader *GetObjectHeader() const; 79 ObjectHeader *GetFromObjectHeader() const; 80 friend std::ostream &operator<<(std::ostream &os, const GCRoot &root); 81 82 virtual ~GCRoot() = default; 83 84 NO_COPY_SEMANTIC(GCRoot); 85 NO_MOVE_SEMANTIC(GCRoot); 86 87 private: 88 RootType type_; 89 /** 90 * From which object current root was found by reference. Usually from_object is nullptr, except when object was 91 * found from card_table 92 */ 93 ObjectHeader *from_object_; 94 ObjectHeader *object_; 95 }; 96 97 template <class LanguageConfig> 98 class RootManager final { 99 public: 100 /** 101 * Visit all non-heap roots(registers, thread locals, etc) 102 */ 103 void VisitNonHeapRoots(const GCRootVisitor &gc_root_visitor, 104 VisitGCRootFlags flags = VisitGCRootFlags::ACCESS_ROOT_ALL) const; 105 106 /** 107 * Visit local roots for frame 108 */ 109 void VisitLocalRoots(const GCRootVisitor &gc_root_visitor) const; 110 111 /** 112 * Visit card table roots 113 * @param card_table - card table for scan 114 * @param allocator - object allocator 115 * @param root_visitor 116 * @param range_checker 117 * @param range_object_checker 118 */ 119 void VisitCardTableRoots(CardTable *card_table, ObjectAllocatorBase *allocator, GCRootVisitor root_visitor, 120 MemRangeChecker range_checker, ObjectChecker range_object_checker, 121 ObjectChecker from_object_checker, uint32_t processed_flag) const; 122 123 /** 124 * Visit roots in class linker 125 */ 126 void VisitClassRoots(const GCRootVisitor &gc_root_visitor, 127 VisitGCRootFlags flags = VisitGCRootFlags::ACCESS_ROOT_ALL) const; 128 129 /** 130 * Updates references to moved objects in TLS 131 */ 132 void UpdateThreadLocals(); 133 134 void UpdateVmRefs(); 135 136 void UpdateGlobalObjectStorage(); 137 138 void UpdateClassLinkerContextRoots(); 139 SetPandaVM(PandaVM * vm)140 void SetPandaVM(PandaVM *vm) 141 { 142 vm_ = vm; 143 } 144 145 private: 146 /** 147 * Visit VM-specific roots 148 */ 149 void VisitVmRoots(const GCRootVisitor &gc_root_visitor) const; 150 151 void VisitRegisterRoot(const Frame::VRegister &v_register, const GCRootVisitor &gc_root_visitor) const; 152 153 void VisitClassLinkerContextRoots(const GCRootVisitor &gc_root_visitor) const; 154 155 /** 156 * Visit roots for \param thread 157 * @param thread 158 */ 159 void VisitRootsForThread(ManagedThread *thread, const GCRootVisitor &gc_root_visitor) const; 160 161 PandaVM *vm_ {nullptr}; 162 }; 163 164 } // namespace panda::mem 165 166 #endif // PANDA_RUNTIME_MEM_GC_GC_ROOT_H_ 167