• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #include "global_object_storage.h"
16 
17 #include <libpandabase/os/mutex.h>
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/mem/panda_containers.h"
20 #include "runtime/include/object_header.h"
21 #include "runtime/mem/object_helpers.h"
22 #include "runtime/mem/gc/gc.h"
23 #include "runtime/mem/gc/gc_root.h"
24 #include "runtime/mem/gc/gc_phase.h"
25 #include "runtime/include/class.h"
26 #include "reference.h"
27 #include "utils/logger.h"
28 
29 namespace ark::mem {
30 
GlobalObjectStorage(mem::InternalAllocatorPtr allocator,size_t maxSize,bool enableSizeCheck)31 GlobalObjectStorage::GlobalObjectStorage(mem::InternalAllocatorPtr allocator, size_t maxSize, bool enableSizeCheck)
32     : allocator_(allocator)
33 {
34     globalStorage_ = allocator->New<ArrayStorage>(allocator, maxSize, enableSizeCheck);
35     weakStorage_ = allocator->New<ArrayStorage>(allocator, maxSize, enableSizeCheck);
36     globalFixedStorage_ = allocator->New<ArrayStorage>(allocator, maxSize, enableSizeCheck, true);
37 }
38 
~GlobalObjectStorage()39 GlobalObjectStorage::~GlobalObjectStorage()
40 {
41     allocator_->Delete(globalStorage_);
42     allocator_->Delete(weakStorage_);
43     allocator_->Delete(globalFixedStorage_);
44 }
45 
IsValidGlobalRef(const Reference * ref) const46 bool GlobalObjectStorage::IsValidGlobalRef(const Reference *ref) const
47 {
48     ASSERT(ref != nullptr);
49     Reference::ObjectType type = Reference::GetType(ref);
50     AssertType(type);
51     if (type == Reference::ObjectType::GLOBAL) {
52         if (!globalStorage_->IsValidGlobalRef(ref)) {
53             return false;
54         }
55     } else if (type == Reference::ObjectType::WEAK) {
56         if (!weakStorage_->IsValidGlobalRef(ref)) {
57             return false;
58         }
59     } else {
60         if (!globalFixedStorage_->IsValidGlobalRef(ref)) {
61             return false;
62         }
63     }
64     return true;
65 }
66 
Add(const ObjectHeader * object,Reference::ObjectType type) const67 Reference *GlobalObjectStorage::Add(const ObjectHeader *object, Reference::ObjectType type) const
68 {
69     AssertType(type);
70     if (object == nullptr) {
71         return nullptr;
72     }
73     Reference *ref = nullptr;
74     if (type == Reference::ObjectType::GLOBAL) {
75         ref = globalStorage_->Add(object);
76     } else if (type == Reference::ObjectType::WEAK) {
77         ref = weakStorage_->Add(object);
78     } else {
79         ref = globalFixedStorage_->Add(object);
80     }
81     if (ref != nullptr) {
82         ref = Reference::SetType(ref, type);
83     }
84     return ref;
85 }
86 
Remove(const Reference * reference)87 void GlobalObjectStorage::Remove(const Reference *reference)
88 {
89     if (reference == nullptr) {
90         return;
91     }
92     auto type = reference->GetType();
93     AssertType(type);
94     reference = Reference::GetRefWithoutType(reference);
95     if (type == Reference::ObjectType::GLOBAL) {
96         globalStorage_->Remove(reference);
97     } else if (type == Reference::ObjectType::WEAK) {
98         weakStorage_->Remove(reference);
99     } else {
100         globalFixedStorage_->Remove(reference);
101     }
102 }
103 
GetAllObjects()104 PandaVector<ObjectHeader *> GlobalObjectStorage::GetAllObjects()
105 {
106     auto objects = PandaVector<ObjectHeader *>(allocator_->Adapter());
107 
108     auto globalObjects = globalStorage_->GetAllObjects();
109     objects.insert(objects.end(), globalObjects.begin(), globalObjects.end());
110 
111     auto weakObjects = weakStorage_->GetAllObjects();
112     objects.insert(objects.end(), weakObjects.begin(), weakObjects.end());
113 
114     auto fixedObjects = globalFixedStorage_->GetAllObjects();
115     objects.insert(objects.end(), fixedObjects.begin(), fixedObjects.end());
116 
117     return objects;
118 }
119 
VisitObjects(const GCRootVisitor & gcRootVisitor,mem::RootType rootType)120 void GlobalObjectStorage::VisitObjects(const GCRootVisitor &gcRootVisitor, mem::RootType rootType)
121 {
122     globalStorage_->VisitObjects(gcRootVisitor, rootType);
123     globalFixedStorage_->VisitObjects(gcRootVisitor, rootType);
124 }
125 
UpdateMovedRefs(const GCRootUpdater & gcRootUpdater)126 void GlobalObjectStorage::UpdateMovedRefs(const GCRootUpdater &gcRootUpdater)
127 {
128     LOG(DEBUG, GC) << "=== GlobalStorage Update moved. BEGIN ===";
129     globalStorage_->UpdateMovedRefs(gcRootUpdater);
130     weakStorage_->UpdateMovedRefs(gcRootUpdater);
131     globalFixedStorage_->UpdateMovedRefs(gcRootUpdater);
132     LOG(DEBUG, GC) << "=== GlobalStorage Update moved. END ===";
133 }
134 
ClearUnmarkedWeakRefs(const GC * gc,const mem::GC::ReferenceClearPredicateT & pred)135 void GlobalObjectStorage::ClearUnmarkedWeakRefs(const GC *gc, const mem::GC::ReferenceClearPredicateT &pred)
136 {
137     ClearWeakRefs([gc, &pred](auto *obj) { return pred(obj) && !gc->IsMarked(obj); });
138 }
139 
ClearWeakRefs(const mem::GC::ReferenceClearPredicateT & pred)140 void GlobalObjectStorage::ClearWeakRefs(const mem::GC::ReferenceClearPredicateT &pred)
141 {
142     weakStorage_->ClearWeakRefs(pred);
143 }
144 
GetSize()145 size_t GlobalObjectStorage::GetSize()
146 {
147     return globalStorage_->GetSizeWithLock() + weakStorage_->GetSizeWithLock() + globalFixedStorage_->GetSizeWithLock();
148 }
149 
Dump()150 void GlobalObjectStorage::Dump()
151 {
152     globalStorage_->DumpWithLock();
153 }
154 }  // namespace ark::mem
155