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 #ifndef PANDA_RUNTIME_GLOABL_HANDLE_STORAGE_INL_H
16 #define PANDA_RUNTIME_GLOABL_HANDLE_STORAGE_INL_H
17
18 #include "runtime/global_handle_storage.h"
19 #include "libpandabase/trace/trace.h"
20 #include "runtime/include/runtime.h"
21 #include "runtime/mem/object_helpers.h"
22
23 namespace ark {
24 using InternalAllocatorPtr = mem::AllocatorPtr<mem::AllocatorPurpose::ALLOCATOR_PURPOSE_INTERNAL>;
25
26 template <typename T>
~GlobalHandleStorage()27 inline GlobalHandleStorage<T>::~GlobalHandleStorage()
28 {
29 FreeGlobalNodes();
30 allocator_->Delete(globalNodes_);
31 }
32
33 template <typename T>
FreeHandles()34 inline void GlobalHandleStorage<T>::FreeHandles()
35 {
36 FreeGlobalNodes();
37 globalNodes_->clear();
38 }
39
40 template <typename T>
41 // CC-OFFNXT(G.FUD.06) solid logic
NewGlobalHandle(T value)42 inline uintptr_t GlobalHandleStorage<T>::NewGlobalHandle(T value)
43 {
44 if (count_ == GLOBAL_BLOCK_SIZE && freeList_ == nullptr) {
45 // alloc new block
46 auto block = allocator_->New<std::array<Node, GLOBAL_BLOCK_SIZE>>();
47 globalNodes_->push_back(block);
48 count_ = count_ - GLOBAL_BLOCK_SIZE;
49 }
50
51 // use node in block first
52 if (count_ != GLOBAL_BLOCK_SIZE) {
53 globalNodes_->back()->at(count_).SetNext(nullptr);
54 globalNodes_->back()->at(count_).SetObject(value);
55 return globalNodes_->back()->at(count_++).GetObjectAddress();
56 }
57
58 // use free_list node
59 Node *node = freeList_;
60 freeList_ = freeList_->GetNext();
61 node->SetNext(nullptr);
62 node->SetObject(value);
63 return node->GetObjectAddress();
64 }
65
66 template <typename T>
DisposeGlobalHandle(uintptr_t nodeAddr)67 inline void GlobalHandleStorage<T>::DisposeGlobalHandle(uintptr_t nodeAddr)
68 {
69 Node *node = reinterpret_cast<Node *>(nodeAddr);
70 node->SetNext(freeList_->GetNext());
71 freeList_->SetNext(node);
72 }
73
74 template <>
DisposeGlobalHandle(uintptr_t nodeAddr)75 inline void GlobalHandleStorage<coretypes::TaggedType>::DisposeGlobalHandle(uintptr_t nodeAddr)
76 {
77 Node *node = reinterpret_cast<Node *>(nodeAddr);
78 #ifndef NDEBUG
79 node->SetObject(coretypes::TaggedValue::VALUE_UNDEFINED);
80 #endif
81 if (freeList_ != nullptr) {
82 node->SetNext(freeList_->GetNext());
83 freeList_->SetNext(node);
84 } else {
85 freeList_ = node;
86 }
87 }
88
89 template <>
DealUpdateObject(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index,const GCRootUpdater & gcRootUpdater)90 inline void GlobalHandleStorage<coretypes::TaggedType>::DealUpdateObject(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
91 size_t index,
92 const GCRootUpdater &gcRootUpdater)
93 {
94 coretypes::TaggedValue obj(block->at(index).GetObject());
95 if (obj.IsHeapObject()) {
96 ObjectHeader *objH = obj.GetHeapObject();
97 if (gcRootUpdater(&objH)) {
98 coretypes::TaggedValue value(objH);
99 block->at(index).SetObject(value.GetRawData());
100 };
101 }
102 }
103
104 template <>
105 // CC-OFFNXT(G.FUD.06) solid logic
UpdateGCRootsInBlock(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t size,const GCRootUpdater & gcRootUpdater)106 inline void GlobalHandleStorage<coretypes::TaggedType>::UpdateGCRootsInBlock(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
107 size_t size,
108 const GCRootUpdater &gcRootUpdater)
109 {
110 for (size_t i = 0; i < size; i++) {
111 DealUpdateObject(block, i, gcRootUpdater);
112 }
113 }
114
115 template <>
116 // CC-OFFNXT(G.FUD.06) solid logic
UpdateHeapObject(const GCRootUpdater & gcRootUpdater)117 inline void GlobalHandleStorage<coretypes::TaggedType>::UpdateHeapObject(const GCRootUpdater &gcRootUpdater)
118 {
119 if (globalNodes_->empty()) {
120 return;
121 }
122
123 for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
124 UpdateGCRootsInBlock(globalNodes_->at(i), GLOBAL_BLOCK_SIZE, gcRootUpdater);
125 }
126
127 UpdateGCRootsInBlock(globalNodes_->back(), count_, gcRootUpdater);
128 }
129
130 template <>
DealVisitGCRoots(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index,const ObjectVisitor & cb)131 inline void GlobalHandleStorage<coretypes::TaggedType>::DealVisitGCRoots(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
132 size_t index, const ObjectVisitor &cb)
133 {
134 coretypes::TaggedValue value(block->at(index).GetObject());
135 if (value.IsHeapObject()) {
136 cb(value.GetHeapObject());
137 }
138 }
139
140 template <>
141 // CC-OFFNXT(G.FUD.06) solid logic
VisitGCRootsInBlock(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t size,const ObjectVisitor & cb)142 inline void GlobalHandleStorage<coretypes::TaggedType>::VisitGCRootsInBlock(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
143 size_t size, const ObjectVisitor &cb)
144 {
145 for (size_t i = 0; i < size; i++) {
146 DealVisitGCRoots(block, i, cb);
147 }
148 }
149
150 template <>
151 // CC-OFFNXT(G.FUD.06) solid logic
VisitGCRoots(const ObjectVisitor & cb)152 inline void GlobalHandleStorage<coretypes::TaggedType>::VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb)
153 {
154 if (globalNodes_->empty()) {
155 return;
156 }
157
158 for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
159 VisitGCRootsInBlock(globalNodes_->at(i), GLOBAL_BLOCK_SIZE, cb);
160 }
161
162 VisitGCRootsInBlock(globalNodes_->back(), count_, cb);
163 }
164
165 template class GlobalHandleStorage<coretypes::TaggedType>;
166 } // namespace ark
167 #endif // PANDA_RUNTIME_GLOABL_HANDLE_STORAGE_INL_H
168