• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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_H
17 #define PANDA_RUNTIME_HANDLE_STORAGE_H
18 
19 #include "runtime/include/coretypes/tagged_value.h"
20 #include "runtime/include/mem/allocator.h"
21 #include "runtime/include/mem/panda_containers.h"
22 
23 namespace ark {
24 class LocalScope;
25 class EscapeLocalScope;
26 
27 template <class T>
28 class HandleScope;
29 
30 template <class T>
31 class EscapeHandleScope;
32 
33 using InternalAllocatorPtr = mem::AllocatorPtr<mem::AllocatorPurpose::ALLOCATOR_PURPOSE_INTERNAL>;
34 
35 // storage is the storage structure of the object pointer
36 template <typename T>
37 class HandleStorage {
38 public:
HandleStorage(InternalAllocatorPtr allocator)39     explicit HandleStorage(InternalAllocatorPtr allocator) : allocator_(allocator)
40     {
41         ASSERT(allocator_ != nullptr);
42     };
~HandleStorage()43     ~HandleStorage()
44     {
45         for (auto n : nodes_) {
46             allocator_->Delete(n);
47         }
48         nodes_.clear();
49     }
50 
51     NO_COPY_SEMANTIC(HandleStorage);
52     NO_MOVE_SEMANTIC(HandleStorage);
53 
54 private:
55     static const uint32_t NODE_BLOCK_SIZE_LOG2 = 10;
56     static const uint32_t NODE_BLOCK_SIZE = 1U << NODE_BLOCK_SIZE_LOG2;
57     static const uint32_t NODE_BLOCK_SIZE_MASK = NODE_BLOCK_SIZE - 1;
58 
59     void ZapFreedHandles();
60 
61     void FreeExtraNodes(uint32_t nid);
62 
63     void ZapFreedHandlesForNode(std::array<T, NODE_BLOCK_SIZE> *node, uint32_t start = 0);
64 
65     void UpdateHeapObjectForNode(std::array<coretypes::TaggedType, NODE_BLOCK_SIZE> *node, uint32_t size,
66                                  const GCRootUpdater &gcRootUpdater);
67 
68     void VisitGCRootsForNode(std::array<coretypes::TaggedType, NODE_BLOCK_SIZE> *node, uint32_t size,
69                              const ObjectVisitor &cb);
70 
71     void UpdateHeapObjectForNode(std::array<ObjectHeader *, NODE_BLOCK_SIZE> *node, uint32_t size,
72                                  const GCRootUpdater &gcRootUpdater);
73 
74     void VisitGCRootsForNode(std::array<ObjectHeader *, NODE_BLOCK_SIZE> *node, uint32_t size, const ObjectVisitor &cb);
75 
76     // TaggedType has been specialized for js, Other types are empty implementation
UpdateHeapObject(const GCRootUpdater & gcRootUpdater)77     void UpdateHeapObject([[maybe_unused]] const GCRootUpdater &gcRootUpdater) {}
78 
79     // TaggedType has been specialized for js, Other types are empty implementation
VisitGCRoots(const ObjectVisitor & cb)80     void VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb) {}
81 
82     uintptr_t NewHandle(T value = 0);
83 
84     void FreeHandles(uint32_t beginIdx);
85 
86     uintptr_t GetNodeAddress(uint32_t index) const;
87 
88     uint32_t lastIndex_ {0};
89     PandaVector<std::array<T, NODE_BLOCK_SIZE> *> nodes_;
90     InternalAllocatorPtr allocator_ {nullptr};
91     friend class ManagedThread;
92     friend class HandleScope<T>;
93     friend class LocalScope;
94     friend class EscapeLocalScope;
95     friend class EscapeHandleScope<T>;
96 };
97 }  // namespace ark
98 #endif  // PANDA_RUNTIME_HANDLE_STORAGE_H
99