• 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 
16 #ifndef PANDA_RUNTIME_HANDLE_STORAGE_INL_H
17 #define PANDA_RUNTIME_HANDLE_STORAGE_INL_H
18 
19 #include "runtime/handle_storage.h"
20 #include "runtime/mem/object_helpers.h"
21 
22 namespace panda {
23 template <typename T>
GetNodeAddress(uint32_t index)24 inline uintptr_t HandleStorage<T>::GetNodeAddress(uint32_t index) const
25 {
26     uint32_t id = index >> NODE_BLOCK_SIZE_LOG2;
27     uint32_t offset = index & NODE_BLOCK_SIZE_MASK;
28     auto node = nodes_[id];
29     auto location = &(*node)[offset];
30     return reinterpret_cast<uintptr_t>(location);
31 }
32 
33 template <typename T>
NewHandle(T value)34 inline uintptr_t HandleStorage<T>::NewHandle(T value)
35 {
36     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
37     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
38     if (nodes_.size() <= nid) {
39         auto n = allocator_->New<std::array<T, NODE_BLOCK_SIZE>>();
40         nodes_.push_back(n);
41     }
42     auto node = nodes_[nid];
43     auto loc = &(*node)[offset];
44     *loc = value;
45     lastIndex_++;
46     return reinterpret_cast<uintptr_t>(loc);
47 }
48 
49 template <typename T>
FreeHandles(uint32_t beginIndex)50 inline void HandleStorage<T>::FreeHandles(uint32_t beginIndex)
51 {
52     lastIndex_ = beginIndex;
53 #ifndef NDEBUG
54     ZapFreedHandles();
55 #endif
56     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
57     // reserve at least one block for perf.
58     nid++;
59     for (size_t i = nid; i < nodes_.size(); ++i) {
60         allocator_->Delete(nodes_[i]);
61     }
62     if (nid < nodes_.size()) {
63         nodes_.erase(nodes_.begin() + nid, nodes_.end());
64     }
65 }
66 
67 template <typename T>
ZapFreedHandles()68 void HandleStorage<T>::ZapFreedHandles()
69 {
70     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
71     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
72     for (size_t i = nid; i < nodes_.size(); ++i) {
73         auto node = nodes_.at(i);
74         if (i != nid) {
75             node->fill(reinterpret_cast<T>(static_cast<uint64_t>(0)));
76         } else {
77             for (uint32_t j = offset; j < NODE_BLOCK_SIZE; ++j) {
78                 node->at(j) = reinterpret_cast<T>(static_cast<uint64_t>(0));
79             }
80         }
81     }
82 }
83 
84 template <>
UpdateHeapObject()85 inline void HandleStorage<coretypes::TaggedType>::UpdateHeapObject()
86 {
87     if (lastIndex_ == 0) {
88         return;
89     }
90     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
91     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
92     for (uint32_t i = 0; i <= nid; ++i) {
93         auto node = nodes_.at(i);
94         uint32_t count = (i != nid) ? NODE_BLOCK_SIZE : offset;
95         for (uint32_t j = 0; j < count; ++j) {
96             coretypes::TaggedValue obj(node->at(j));
97             if (obj.IsHeapObject() && obj.GetHeapObject()->IsForwarded()) {
98                 (*node)[j] = coretypes::TaggedValue(panda::mem::GetForwardAddress(obj.GetHeapObject())).GetRawData();
99             }
100         }
101     }
102 }
103 
104 template <>
VisitGCRoots(const ObjectVisitor & cb)105 inline void HandleStorage<coretypes::TaggedType>::VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb)
106 {
107     if (lastIndex_ == 0) {
108         return;
109     }
110     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
111     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
112     if (offset == 0) {
113         nid -= 1;
114         offset = NODE_BLOCK_SIZE;
115     }
116     for (uint32_t i = 0; i <= nid; ++i) {
117         auto node = nodes_.at(i);
118         uint32_t count = (i != nid) ? NODE_BLOCK_SIZE : offset;
119         for (uint32_t j = 0; j < count; ++j) {
120             coretypes::TaggedValue obj(node->at(j));
121             if (obj.IsHeapObject()) {
122                 cb(obj.GetHeapObject());
123             }
124         }
125     }
126 }
127 
128 template <>
UpdateHeapObject()129 inline void HandleStorage<ObjectHeader *>::UpdateHeapObject()
130 {
131     if (lastIndex_ == 0) {
132         return;
133     }
134     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
135     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
136     if (offset == 0) {
137         nid -= 1;
138         offset = NODE_BLOCK_SIZE;
139     }
140     for (uint32_t i = 0; i <= nid; ++i) {
141         auto node = nodes_.at(i);
142         uint32_t count = (i != nid) ? NODE_BLOCK_SIZE : offset;
143         for (uint32_t j = 0; j < count; ++j) {
144             auto *obj = reinterpret_cast<ObjectHeader *>(node->at(j));
145             if (obj->IsForwarded()) {
146                 (*node)[j] = ::panda::mem::GetForwardAddress(obj);
147             }
148         }
149     }
150 }
151 
152 template <>
VisitGCRoots(const ObjectVisitor & cb)153 inline void HandleStorage<ObjectHeader *>::VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb)
154 {
155     if (lastIndex_ == 0) {
156         return;
157     }
158     uint32_t nid = lastIndex_ >> NODE_BLOCK_SIZE_LOG2;
159     uint32_t offset = lastIndex_ & NODE_BLOCK_SIZE_MASK;
160     for (uint32_t i = 0; i <= nid; ++i) {
161         auto node = nodes_.at(i);
162         uint32_t count = (i != nid) ? NODE_BLOCK_SIZE : offset;
163         for (uint32_t j = 0; j < count; ++j) {
164             auto obj = reinterpret_cast<ObjectHeader *>(node->at(j));
165             cb(obj);
166         }
167     }
168 }
169 
170 template class HandleStorage<coretypes::TaggedType>;
171 }  // namespace panda
172 
173 #endif  // PANDA_RUNTIME_HANDLE_STORAGE_INL_H
174