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