• 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_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 panda {
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     // TaggedType has been specialized for js, Other types are empty implementation
UpdateHeapObject()62     void UpdateHeapObject() {}
63 
64     // TaggedType has been specialized for js, Other types are empty implementation
VisitGCRoots(const ObjectVisitor & cb)65     void VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb) {}
66 
67     uintptr_t NewHandle(T value = 0);
68 
69     void FreeHandles(uint32_t beginIndex);
70 
71     uintptr_t GetNodeAddress(uint32_t index) const;
72 
73     uint32_t lastIndex_ {0};
74     PandaVector<std::array<T, NODE_BLOCK_SIZE> *> nodes_;
75     InternalAllocatorPtr allocator_ {nullptr};
76     friend class ManagedThread;
77     friend class HandleScope<T>;
78     friend class LocalScope;
79     friend class EscapeLocalScope;
80     friend class EscapeHandleScope<T>;
81 };
82 
83 }  // namespace panda
84 
85 #endif  // PANDA_RUNTIME_HANDLE_STORAGE_H_
86