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 #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)90 inline void GlobalHandleStorage<coretypes::TaggedType>::DealUpdateObject(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
91 size_t index)
92 {
93 coretypes::TaggedValue obj(block->at(index).GetObject());
94 if (obj.IsHeapObject() && obj.GetHeapObject()->IsForwarded()) {
95 coretypes::TaggedValue value(ark::mem::GetForwardAddress(obj.GetHeapObject()));
96 block->at(index).SetObject(value.GetRawData());
97 }
98 }
99
100 template <>
101 // CC-OFFNXT(G.FUD.06) solid logic
UpdateHeapObject()102 inline void GlobalHandleStorage<coretypes::TaggedType>::UpdateHeapObject()
103 {
104 if (globalNodes_->empty()) {
105 return;
106 }
107
108 for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
109 auto block = globalNodes_->at(i);
110 for (size_t j = 0; j < GLOBAL_BLOCK_SIZE; j++) {
111 DealUpdateObject(block, j);
112 }
113 }
114
115 auto block = globalNodes_->back();
116 for (int32_t i = 0; i < count_; i++) {
117 DealUpdateObject(block, i);
118 }
119 }
120
121 template <>
DealVisitGCRoots(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index,const ObjectVisitor & cb)122 inline void GlobalHandleStorage<coretypes::TaggedType>::DealVisitGCRoots(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
123 size_t index, const ObjectVisitor &cb)
124 {
125 coretypes::TaggedValue value(block->at(index).GetObject());
126 if (value.IsHeapObject()) {
127 cb(value.GetHeapObject());
128 }
129 }
130
131 template <>
132 // CC-OFFNXT(G.FUD.06) solid logic
VisitGCRoots(const ObjectVisitor & cb)133 inline void GlobalHandleStorage<coretypes::TaggedType>::VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb)
134 {
135 if (globalNodes_->empty()) {
136 return;
137 }
138
139 for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
140 auto block = globalNodes_->at(i);
141 for (size_t j = 0; j < GLOBAL_BLOCK_SIZE; j++) {
142 DealVisitGCRoots(block, j, cb);
143 }
144 }
145
146 auto block = globalNodes_->back();
147 for (int32_t i = 0; i < count_; i++) {
148 DealVisitGCRoots(block, i, cb);
149 }
150 }
151
152 template class GlobalHandleStorage<coretypes::TaggedType>;
153 } // namespace ark
154 #endif // PANDA_RUNTIME_GLOABL_HANDLE_STORAGE_INL_H
155