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_MEM_VM_HANDLE_H_ 17 #define PANDA_RUNTIME_MEM_VM_HANDLE_H_ 18 19 #include "libpandabase/macros.h" 20 #include "runtime/handle_base.h" 21 #include "runtime/include/managed_thread.h" 22 #include "runtime/handle_scope.h" 23 24 namespace panda { 25 26 using TaggedType = coretypes::TaggedType; 27 using TaggedValue = coretypes::TaggedValue; 28 29 // VMHandle should be used in language-agnostic part of runtime 30 template <typename T> 31 class VMHandle : public HandleBase { 32 public: VMHandle()33 inline explicit VMHandle() : HandleBase(reinterpret_cast<uintptr_t>(nullptr)) {} 34 VMHandle(ManagedThread * thread,ObjectHeader * object)35 explicit VMHandle(ManagedThread *thread, ObjectHeader *object) 36 { 37 if (object != nullptr) { 38 address_ = thread->GetTopScope<ObjectHeader *>()->NewHandle(object); 39 } else { 40 address_ = reinterpret_cast<uintptr_t>(nullptr); 41 } 42 } 43 44 ~VMHandle() = default; 45 46 NO_COPY_SEMANTIC(VMHandle); 47 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(VMHandle); 48 GetPtr()49 T *GetPtr() const 50 { 51 if (address_ == reinterpret_cast<uintptr_t>(nullptr)) { 52 return nullptr; 53 } 54 return *(reinterpret_cast<T **>(GetAddress())); 55 } 56 57 explicit operator T *() const 58 { 59 return GetPtr(); 60 } 61 62 T *operator->() const 63 { 64 return GetPtr(); 65 } 66 }; 67 68 } // namespace panda 69 70 #endif // PANDA_RUNTIME_MEM_VM_HANDLE_H_ 71