1 /** 2 * Copyright (c) 2021-2024 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 #ifndef RUNTIME_MEM_OBJECT_LOCAL_OBJECT_HANDLE_H 16 #define RUNTIME_MEM_OBJECT_LOCAL_OBJECT_HANDLE_H 17 18 #include "libpandabase/macros.h" 19 #include "runtime/include/managed_thread.h" 20 21 namespace ark { 22 23 template <typename T> 24 class LocalObjectHandle { 25 public: 26 NO_COPY_SEMANTIC(LocalObjectHandle); 27 NO_MOVE_SEMANTIC(LocalObjectHandle); 28 LocalObjectHandle(ManagedThread * thread,ObjectHeader * object)29 explicit LocalObjectHandle(ManagedThread *thread, ObjectHeader *object) : root_(object), thread_(thread) 30 { 31 thread_->PushLocalObject(&root_); 32 } 33 34 // Disabled when T is ObjectHeader, delayed instantiation 35 template <typename P = T, typename = std::enable_if_t<!std::is_same_v<P, ObjectHeader>>> LocalObjectHandle(ManagedThread * thread,T * object)36 explicit LocalObjectHandle(ManagedThread *thread, T *object) 37 : LocalObjectHandle(thread, reinterpret_cast<ObjectHeader *>(object)) 38 { 39 } 40 ~LocalObjectHandle()41 ~LocalObjectHandle() 42 { 43 thread_->PopLocalObject(); 44 } 45 GetPtr()46 T *GetPtr() const 47 { 48 return reinterpret_cast<T *>(root_); 49 } 50 51 explicit operator T *() 52 { 53 return GetPtr(); 54 } 55 56 T *operator->() 57 { 58 return GetPtr(); 59 } 60 GetAddress()61 inline uintptr_t GetAddress() const 62 { 63 return ToUintPtr(&root_); 64 } 65 66 private: 67 ObjectHeader *root_ {}; 68 ManagedThread *thread_ {}; 69 }; 70 71 } // namespace ark 72 73 #endif // RUNTIME_MEM_OBJECT_LOCAL_OBJECT_HANDLE_H 74