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

Side by Side Diff: src/wasm/managed.h

Issue 2676513008: [wasm] Managed<T> ensures T's lifetime does not leak past Isolate's (Closed)
Patch Set: renamed to ManagedObjectFinalizer, and using "finalizer"` 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
« no previous file with comments | « src/v8.gyp ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef V8_WASM_MANAGED_H_
6 #define V8_WASM_MANAGED_H_
7
8 #include "src/factory.h"
9 #include "src/global-handles.h"
10 #include "src/handles.h"
11 #include "src/isolate.h"
12
13 namespace v8 {
14 namespace internal {
15 // An object that wraps a pointer to a C++ object and optionally deletes it
16 // when the managed wrapper object is garbage collected.
17 template <class CppType>
18 class Managed : public Foreign {
19 public:
20 V8_INLINE CppType* get() {
21 return reinterpret_cast<CppType*>(foreign_address());
22 }
23
24 static Managed<CppType>* cast(Object* obj) {
25 SLOW_DCHECK(obj->IsForeign());
26 return reinterpret_cast<Managed<CppType>*>(obj);
27 }
28
29 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr,
30 bool delete_on_gc = true) {
31 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
32 isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr)));
33 if (delete_on_gc) {
34 RegisterWeakCallbackForDelete(isolate, handle);
35 }
36 return handle;
37 }
38
39 private:
40 static void RegisterWeakCallbackForDelete(Isolate* isolate,
41 Handle<Managed<CppType>> handle) {
42 Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
43 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(),
44 &Managed<CppType>::Delete,
45 v8::WeakCallbackType::kFinalizer);
46 }
47 static void Delete(const v8::WeakCallbackInfo<void>& data) {
48 Managed<CppType>** p =
49 reinterpret_cast<Managed<CppType>**>(data.GetParameter());
50 delete (*p)->get();
51 (*p)->set_foreign_address(0);
52 GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
53 }
54 };
55 } // namespace internal
56 } // namespace v8
57
58 #endif // V8_WASM_MANAGED_H_
OLDNEW
« no previous file with comments | « src/v8.gyp ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698