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_H_ 17 #define PANDA_RUNTIME_GLOBAL_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 using InternalAllocatorPtr = mem::AllocatorPtr<mem::AllocatorPurpose::ALLOCATOR_PURPOSE_INTERNAL>; 25 26 template <class T> 27 class HandleScope; 28 29 // storage is the storage structure of the object pointer 30 template <typename T> 31 class GlobalHandleStorage { 32 public: 33 static const int32_t GLOBAL_BLOCK_SIZE = 256; 34 GlobalHandleStorage(InternalAllocatorPtr allocator)35 explicit GlobalHandleStorage(InternalAllocatorPtr allocator) : allocator_(allocator) 36 { 37 ASSERT(allocator_ != nullptr); 38 globalNodes_ = allocator_->New<PandaVector<std::array<Node, GLOBAL_BLOCK_SIZE> *>>(allocator_->Adapter()); 39 }; 40 ~GlobalHandleStorage(); 41 42 NO_COPY_SEMANTIC(GlobalHandleStorage); 43 NO_MOVE_SEMANTIC(GlobalHandleStorage); 44 45 class Node { 46 public: 47 void PushNodeToFreeList(); 48 GetObject()49 T GetObject() const 50 { 51 return obj_; 52 } 53 GetNext()54 Node *GetNext() const 55 { 56 return next_; 57 } 58 SetNext(Node * node)59 void SetNext(Node *node) 60 { 61 next_ = node; 62 } 63 SetObject(T obj)64 void SetObject(T obj) 65 { 66 obj_ = obj; 67 } 68 GetObjectAddress()69 uintptr_t GetObjectAddress() const 70 { 71 return reinterpret_cast<uintptr_t>(&obj_); 72 } 73 74 private: 75 T obj_; 76 Node *next_; 77 }; 78 79 inline uintptr_t NewGlobalHandle(T value); 80 81 inline void DisposeGlobalHandle(uintptr_t addr); 82 GetNodes()83 inline PandaVector<std::array<Node, GLOBAL_BLOCK_SIZE> *> *GetNodes() const 84 { 85 return globalNodes_; 86 } 87 GetCount()88 inline int32_t GetCount() const 89 { 90 return count_; 91 } 92 93 private: 94 // TaggedType has been specialized for js, Other types are empty implementation UpdateHeapObject()95 inline void UpdateHeapObject() {} 96 97 // TaggedType has been specialized for js, Other types are empty implementation VisitGCRoots(const ObjectVisitor & cb)98 inline void VisitGCRoots([[maybe_unused]] const ObjectVisitor &cb) {} 99 100 // TaggedType has been specialized for js, Other types are empty implementation DealUpdateObject(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index)101 inline void DealUpdateObject([[maybe_unused]] std::array<Node, GLOBAL_BLOCK_SIZE> *block, 102 [[maybe_unused]] size_t index) 103 { 104 } 105 // TaggedType has been specialized for js, Other types are empty implementation DealVisitGCRoots(std::array<Node,GLOBAL_BLOCK_SIZE> * block,size_t index,const ObjectVisitor & cb)106 inline void DealVisitGCRoots([[maybe_unused]] std::array<Node, GLOBAL_BLOCK_SIZE> *block, 107 [[maybe_unused]] size_t index, [[maybe_unused]] const ObjectVisitor &cb) 108 { 109 } 110 111 PandaVector<std::array<Node, GLOBAL_BLOCK_SIZE> *> *globalNodes_ {nullptr}; 112 InternalAllocatorPtr allocator_; 113 int32_t count_ {GLOBAL_BLOCK_SIZE}; 114 Node *freeList_ {nullptr}; 115 116 friend class ManagedThread; 117 }; 118 119 } // namespace panda 120 121 #endif // PANDA_RUNTIME_GLOBAL_HANDLE_STORAGE_H_ 122