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