Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: src/managed.h

Issue 2676513008: [wasm] Managed<T> ensures T's lifetime does not leak past Isolate's (Closed)
Patch Set: Comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/isolate.cc ('K') | « src/isolate.cc ('k') | src/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_WASM_MANAGED_H_ 5 #ifndef V8_WASM_MANAGED_H_
6 #define V8_WASM_MANAGED_H_ 6 #define V8_WASM_MANAGED_H_
7 7
8 #include "src/factory.h" 8 #include "src/factory.h"
9 #include "src/global-handles.h" 9 #include "src/global-handles.h"
10 #include "src/handles.h" 10 #include "src/handles.h"
11 #include "src/isolate.h" 11 #include "src/isolate.h"
12 #include "src/objects-inl.h" 12 #include "src/objects-inl.h"
13 13
14 namespace v8 { 14 namespace v8 {
15 namespace internal { 15 namespace internal {
16 // An object that wraps a pointer to a C++ object and optionally deletes it 16 // An object that wraps a pointer to a C++ object and manages its lifetime.
17 // when the managed wrapper object is garbage collected. 17 // The C++ object will be deleted when the managed wrapper object is
18 // garbage collected, or, last resort, if the isolate is torn down before GC,
19 // as part of Isolate::Dispose().
20 // Managed<CppType> may be used polymorphically as Foreign, where the held
21 // address is typed as CppType**. The double indirection is due to the
22 // use, by Managed, of Isolate::ManagedLifeline, which has a CppType* first
23 // field.
18 template <class CppType> 24 template <class CppType>
19 class Managed : public Foreign { 25 class Managed : public Foreign {
20 public: 26 public:
21 V8_INLINE CppType* get() { 27 V8_INLINE CppType* get() {
22 return reinterpret_cast<CppType*>(foreign_address()); 28 return *(reinterpret_cast<CppType**>(foreign_address()));
23 } 29 }
24 30
25 static Managed<CppType>* cast(Object* obj) { 31 static Managed<CppType>* cast(Object* obj) {
26 SLOW_DCHECK(obj->IsForeign()); 32 SLOW_DCHECK(obj->IsForeign());
27 return reinterpret_cast<Managed<CppType>*>(obj); 33 return reinterpret_cast<Managed<CppType>*>(obj);
28 } 34 }
29 35
30 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr, 36 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr) {
31 bool delete_on_gc = true) { 37 Isolate::ManagedLifeline* node = isolate->RegisterForReleaseAtTeardown(
38 ptr, Managed<CppType>::NativeDelete);
32 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast( 39 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
33 isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr))); 40 isolate->factory()->NewForeign(reinterpret_cast<Address>(node)));
34 if (delete_on_gc) { 41 RegisterWeakCallbackForDelete(isolate, handle);
35 RegisterWeakCallbackForDelete(isolate, handle);
36 }
37 return handle; 42 return handle;
38 } 43 }
39 44
40 private: 45 private:
41 static void RegisterWeakCallbackForDelete(Isolate* isolate, 46 static void RegisterWeakCallbackForDelete(Isolate* isolate,
42 Handle<Managed<CppType>> handle) { 47 Handle<Managed<CppType>> handle) {
43 Handle<Object> global_handle = isolate->global_handles()->Create(*handle); 48 Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
44 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), 49 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(),
45 &Managed<CppType>::Delete, 50 &Managed<CppType>::GCDelete,
46 v8::WeakCallbackType::kFinalizer); 51 v8::WeakCallbackType::kFinalizer);
47 } 52 }
48 static void Delete(const v8::WeakCallbackInfo<void>& data) { 53
54 static void GCDelete(const v8::WeakCallbackInfo<void>& data) {
49 Managed<CppType>** p = 55 Managed<CppType>** p =
50 reinterpret_cast<Managed<CppType>**>(data.GetParameter()); 56 reinterpret_cast<Managed<CppType>**>(data.GetParameter());
51 delete (*p)->get(); 57
52 (*p)->set_foreign_address(0); 58 Isolate::ManagedLifeline* lifeline = (*p)->GetLifeline();
59
60 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
61 lifeline->Dispose();
62 isolate->UnregisterFromReleaseAtTeardown(lifeline);
63
64 (*p)->set_foreign_address(static_cast<Address>(nullptr));
53 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); 65 GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
54 } 66 }
67
68 static void NativeDelete(void* value) {
69 CppType* typed_value = reinterpret_cast<CppType*>(value);
70 delete typed_value;
71 }
72
73 Isolate::ManagedLifeline* GetLifeline() {
74 return reinterpret_cast<Isolate::ManagedLifeline*>(foreign_address());
75 }
55 }; 76 };
56 } // namespace internal 77 } // namespace internal
57 } // namespace v8 78 } // namespace v8
58 79
59 #endif // V8_WASM_MANAGED_H_ 80 #endif // V8_WASM_MANAGED_H_
OLDNEW
« src/isolate.cc ('K') | « src/isolate.cc ('k') | src/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698