• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
NewGlobalHandle(T value)41 inline uintptr_t GlobalHandleStorage<T>::NewGlobalHandle(T value)
42 {
43     if (count_ == GLOBAL_BLOCK_SIZE && freeList_ == nullptr) {
44         // alloc new block
45         auto block = allocator_->New<std::array<Node, GLOBAL_BLOCK_SIZE>>();
46         globalNodes_->push_back(block);
47         count_ = count_ - GLOBAL_BLOCK_SIZE;
48     }
49 
50     // use node in block first
51     if (count_ != GLOBAL_BLOCK_SIZE) {
52         globalNodes_->back()->at(count_).SetNext(nullptr);
53         globalNodes_->back()->at(count_).SetObject(value);
54         return globalNodes_->back()->at(count_++).GetObjectAddress();
55     }
56 
57     // use free_list node
58     Node *node = freeList_;
59     freeList_ = freeList_->GetNext();
60     node->SetNext(nullptr);
61     node->SetObject(value);
62     return node->GetObjectAddress();
63 }
64 
65 template <typename T>
DisposeGlobalHandle(uintptr_t nodeAddr)66 inline void GlobalHandleStorage<T>::DisposeGlobalHandle(uintptr_t nodeAddr)
67 {
68     Node *node = reinterpret_cast<Node *>(nodeAddr);
69     node->SetNext(freeList_->GetNext());
70     freeList_->SetNext(node);
71 }
72 
73 template <>
DisposeGlobalHandle(uintptr_t nodeAddr)74 inline void GlobalHandleStorage<coretypes::TaggedType>::DisposeGlobalHandle(uintptr_t nodeAddr)
75 {
76     Node *node = reinterpret_cast<Node *>(nodeAddr);
77 #ifndef NDEBUG
78     node->SetObject(coretypes::TaggedValue::VALUE_UNDEFINED);
79 #endif
80     if (freeList_ != nullptr) {
81         node->SetNext(freeList_->GetNext());
82         freeList_->SetNext(node);
83     } else {
84         freeList_ = node;
85     }
86 }
87 
88 template <>
DealUpdateObject(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index)89 inline void GlobalHandleStorage<coretypes::TaggedType>::DealUpdateObject(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
90                                                                          size_t index)
91 {
92     coretypes::TaggedValue obj(block->at(index).GetObject());
93     if (obj.IsHeapObject() && obj.GetHeapObject()->IsForwarded()) {
94         coretypes::TaggedValue value(ark::mem::GetForwardAddress(obj.GetHeapObject()));
95         block->at(index).SetObject(value.GetRawData());
96     }
97 }
98 
99 template <>
UpdateHeapObject()100 inline void GlobalHandleStorage<coretypes::TaggedType>::UpdateHeapObject()
101 {
102     if (globalNodes_->empty()) {
103         return;
104     }
105 
106     for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
107         auto block = globalNodes_->at(i);
108         for (size_t j = 0; j < GLOBAL_BLOCK_SIZE; j++) {
109             DealUpdateObject(block, j);
110         }
111     }
112 
113     auto block = globalNodes_->back();
114     for (int32_t i = 0; i < count_; i++) {
115         DealUpdateObject(block, i);
116     }
117 }
118 
119 template <>
DealVisitGCRoots(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index,const ObjectVisitor & cb)120 inline void GlobalHandleStorage<coretypes::TaggedType>::DealVisitGCRoots(std::array<Node, GLOBAL_BLOCK_SIZE> *block,
121                                                                          size_t index, const ObjectVisitor &cb)
122 {
123     coretypes::TaggedValue value(block->at(index).GetObject());
124     if (value.IsHeapObject()) {
125         cb(value.GetHeapObject());
126     }
127 }
128 
129 template <>
VisitGCRoots(const ObjectVisitor & cb)130 inline void GlobalHandleStorage<coretypes::TaggedType>::VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb)
131 {
132     if (globalNodes_->empty()) {
133         return;
134     }
135 
136     for (size_t i = 0; i < globalNodes_->size() - 1; i++) {
137         auto block = globalNodes_->at(i);
138         for (size_t j = 0; j < GLOBAL_BLOCK_SIZE; j++) {
139             DealVisitGCRoots(block, j, cb);
140         }
141     }
142 
143     auto block = globalNodes_->back();
144     for (int32_t i = 0; i < count_; i++) {
145         DealVisitGCRoots(block, i, cb);
146     }
147 }
148 
149 template class GlobalHandleStorage<coretypes::TaggedType>;
150 }  // namespace ark
151 #endif  // PANDA_RUNTIME_GLOABL_HANDLE_STORAGE_INL_H
152