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

Unified Diff: src/wasm/managed.h

Issue 2409173005: [wasm] Add a Managed<T> wrapper class for allocating C++ classes that are deleted when the wrapper … (Closed)
Patch Set: [wasm] Add a Managed<T> wrapper class for allocating C++ classes that are garbage collected. Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/v8.gyp ('k') | test/cctest/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/wasm/managed.h
diff --git a/src/wasm/managed.h b/src/wasm/managed.h
new file mode 100644
index 0000000000000000000000000000000000000000..09ddf60aaa6506d73ac59c20727d613a691b98d6
--- /dev/null
+++ b/src/wasm/managed.h
@@ -0,0 +1,55 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef V8_WASM_MANAGED_H_
+#define V8_WASM_MANAGED_H_
+
+#include "src/factory.h"
+#include "src/global-handles.h"
+#include "src/handles.h"
+#include "src/isolate.h"
+#include "src/objects-inl.h"
+
+namespace v8 {
+namespace internal {
+// An object that wraps a pointer to a C++ object and optionally deletes it
+// when the managed wrapper object is garbage collected.
+template <class CppType>
+class Managed : public Foreign {
+ public:
+ V8_INLINE CppType* get() {
+ return reinterpret_cast<CppType*>(foreign_address());
+ }
+
+ static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr,
+ bool delete_on_gc = true) {
+ Handle<Foreign> foreign =
+ isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr));
+ Handle<Managed<CppType>> handle(
+ reinterpret_cast<Managed<CppType>*>(*foreign), isolate);
+ if (delete_on_gc) {
+ RegisterWeakCallbackForDelete(isolate, handle);
+ }
+ return handle;
+ }
+
+ private:
+ static void RegisterWeakCallbackForDelete(Isolate* isolate,
+ Handle<Managed<CppType>> handle) {
+ Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
+ GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(),
+ &Managed<CppType>::Delete,
+ v8::WeakCallbackType::kFinalizer);
+ }
+ static void Delete(const v8::WeakCallbackInfo<void>& data) {
+ Managed<CppType>** p =
+ reinterpret_cast<Managed<CppType>**>(data.GetParameter());
+ delete (*p)->get();
+ GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
+ }
+};
+} // namespace internal
+} // namespace v8
+
+#endif // V8_WASM_MANAGED_H_
« no previous file with comments | « src/v8.gyp ('k') | test/cctest/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698