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