1 /** 2 * Copyright (c) 2021-2022 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_HANDLEBASE_H 17 #define PANDA_RUNTIME_HANDLEBASE_H 18 19 #include "runtime/include/coretypes/tagged_value.h" 20 21 namespace panda { 22 template <typename T> 23 class EscapeHandleScope; 24 class ManagedThread; 25 26 /* 27 * HandleBase: A HandleBase provides a reference to an object that survives relocation by the garbage collector. 28 * 29 * HandleScope: Handles are only valid within a HandleScope. When a Basehandle is created for an object a cell is 30 * allocated in the current HandleScope. 31 * 32 * HandleStorage: HandleStorage is the storage structure of the object pointer. GC will use the stored pointer as root 33 * and update the stored value after the object is moved 34 * 35 * HandleBase ---- HandleStorage ----- heap 36 * | | | 37 * address-----> store: T* ------> object 38 * 39 * { 40 * HandleScope scope2(thread); 41 * JHandle<T> jhandle(thread, obj4); 42 * JHandle<T> jhandle(thread, obj5); 43 * JHandle<T> jhandle(thread, obj6); 44 * JHandle<T> jhandle(thread, obj7); 45 * } 46 * 47 * // out of scope, The obj pointer in node will be free (obj7, obj6, obj5, obj4) and PopTopNode(top_node = prev_node) 48 * 49 * | | | obj5 | 50 * | | scope2-> | obj4 | 51 * | | | obj3 | 52 * | obj7 | | obj2 | 53 * |__obj6__| scope1-> |__obj1___| 54 * top_node ---------> prev_node------>nullptr 55 * 56 * example: 57 * JSHandle<T> handle; 58 * { 59 * HandleScope(thread); 60 * JSHandle<T> jshandle(thread, T*); // JSHandle extend Handle; 61 * JHandle<T> jhandle(thread, T*); 62 * jshandle->method(); // to invoke method of T 63 * handle = jshandle; 64 * } 65 * handle->method(); // error! do not used handle out of scope 66 */ 67 class HandleBase { 68 public: HandleBase()69 HandleBase() : address_(reinterpret_cast<uintptr_t>(nullptr)) {} 70 ~HandleBase() = default; 71 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(HandleBase); 72 DEFAULT_COPY_SEMANTIC(HandleBase); 73 GetAddress()74 inline uintptr_t GetAddress() const 75 { 76 return address_; 77 } 78 79 template <typename T> 80 explicit HandleBase(ManagedThread *thread, T value); 81 82 protected: HandleBase(uintptr_t addr)83 explicit HandleBase(uintptr_t addr) : address_(addr) {} 84 85 uintptr_t address_; // NOLINT(misc-non-private-member-variables-in-classes) 86 87 template <typename T> 88 friend class EscapeHandleScope; 89 }; 90 } // namespace panda 91 #endif // PANDA_RUNTIME_HANDLEBASE_H 92