• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 #include "libpandabase/utils/logger.h"
17 #include "runtime/mark_word.h"
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/panda_vm.h"
20 #include "runtime/mem/gc/cmc-gc-adapter/cmc-gc-adapter.h"
21 #include "objects/ref_field.h"
22 #include "heap/heap_visitor.h"
23 
24 namespace ark::mem::ets {
25 
VisitVmRoots(const GCRootVisitor & visitor,PandaVM * vm)26 void VisitVmRoots(const GCRootVisitor &visitor, PandaVM *vm)
27 {
28     trace::ScopedTrace scopedTrace(__FUNCTION__);
29     RootManager<EtsLanguageConfig> rootManager(vm);
30     rootManager.VisitNonHeapRoots(visitor, VisitGCRootFlags::ACCESS_ROOT_ALL);
31     vm->VisitStringTable([&visitor](ObjectHeader *str) { visitor(GCRoot(RootType::STRING_TABLE, str)); },
32                          VisitGCRootFlags::ACCESS_ROOT_ALL);
33 }
34 
UpdateVmRoots(const GCRootUpdater & updater,PandaVM * vm)35 void UpdateVmRoots(const GCRootUpdater &updater, PandaVM *vm)
36 {
37     trace::ScopedTrace scopedTrace(__FUNCTION__);
38     RootManager<EtsLanguageConfig> rootManager(vm);
39     rootManager.UpdateRefsToMovedObjects(updater);
40 }
41 
42 }  // namespace ark::mem::ets
43 
44 namespace panda {
45 
GetPandaVM()46 static ark::PandaVM *GetPandaVM()
47 {
48     auto *runtime = ark::Runtime::GetCurrent();
49     if (runtime == nullptr) {
50         return nullptr;
51     }
52     return runtime->GetPandaVM();
53 }
54 
VisitStaticRoots(const RefFieldVisitor & visitor)55 void VisitStaticRoots(const RefFieldVisitor &visitor)
56 {
57     ark::PandaVM *vm = GetPandaVM();
58     if (vm == nullptr) {
59         return;
60     }
61 
62     ark::GCRootVisitor rootVisitor = [&visitor](const ark::mem::GCRoot &gcRoot) {
63         panda::RefField<> refField {reinterpret_cast<panda::BaseObject *>(gcRoot.GetObjectHeader())};
64         visitor(refField);
65     };
66     ark::mem::ets::VisitVmRoots(rootVisitor, vm);
67 }
68 
UpdateStaticRoots(const RefFieldVisitor & visitor)69 void UpdateStaticRoots(const RefFieldVisitor &visitor)
70 {
71     ark::PandaVM *vm = GetPandaVM();
72     if (vm == nullptr) {
73         return;
74     }
75 
76     ark::GCRootUpdater rootUpdater = [&visitor](ark::ObjectHeader **object) {
77         ark::ObjectHeader *prev = *object;
78         visitor(reinterpret_cast<RefField<> &>(*reinterpret_cast<BaseObject **>(object)));
79         return prev != *object;
80     };
81     ark::mem::ets::UpdateVmRoots(rootUpdater, vm);
82 }
83 
SweepStaticRoots(const WeakRefFieldVisitor & visitor)84 void SweepStaticRoots(const WeakRefFieldVisitor &visitor)
85 {
86     ark::PandaVM *vm = GetPandaVM();
87     if (vm == nullptr) {
88         return;
89     }
90     vm->SweepVmRefs([&visitor](ark::ObjectHeader *object) -> ark::ObjectStatus {
91         // Provide the reference to a local variable.
92         // The object must be forwarded during UpdateStaticRoots.
93         // Here we need to know is the object alive or not.
94         bool alive = visitor(reinterpret_cast<RefField<> &>(*reinterpret_cast<BaseObject **>(&object)));
95         return alive ? ark::ObjectStatus::ALIVE_OBJECT : ark::ObjectStatus::DEAD_OBJECT;
96     });
97 }
98 
99 }  // namespace panda
100