5 #ifndef V8_OBJECTS_MANAGED_H_ 6 #define V8_OBJECTS_MANAGED_H_ 9 #include "src/global-handles.h" 10 #include "src/handles.h" 11 #include "src/heap/factory.h" 12 #include "src/isolate.h" 22 size_t estimated_size_ = 0;
25 void* shared_ptr_ptr_ =
nullptr;
26 void (*destructor_)(
void* shared_ptr) =
nullptr;
27 Address* global_handle_location_ =
nullptr;
30 void (*destructor)(
void*))
31 : estimated_size_(estimated_size),
32 shared_ptr_ptr_(shared_ptr_ptr),
33 destructor_(destructor) {}
47 template <
class CppType>
51 V8_INLINE CppType* raw() {
return GetSharedPtrPtr()->get(); }
54 V8_INLINE std::shared_ptr<CppType>
get() {
return *GetSharedPtrPtr(); }
57 SLOW_DCHECK(obj->IsForeign());
62 template <
typename... Args>
64 size_t estimated_size,
66 CppType* ptr =
new CppType(std::forward<Args>(args)...);
67 return FromSharedPtr(isolate, estimated_size,
68 std::shared_ptr<CppType>(ptr));
74 size_t estimated_size,
76 return FromSharedPtr(isolate, estimated_size,
77 std::shared_ptr<CppType>(ptr));
84 Isolate* isolate,
size_t estimated_size,
85 std::unique_ptr<CppType> unique_ptr) {
86 return FromSharedPtr(isolate, estimated_size, std::move(unique_ptr));
91 Isolate* isolate,
size_t estimated_size,
92 std::shared_ptr<CppType> shared_ptr) {
93 reinterpret_cast<v8::Isolate*
>(isolate)
94 ->AdjustAmountOfExternalAllocatedMemory(estimated_size);
96 estimated_size,
new std::shared_ptr<CppType>(shared_ptr), Destructor);
98 isolate->factory()->NewForeign(reinterpret_cast<Address>(destructor)));
99 Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
100 destructor->global_handle_location_ = global_handle.location();
101 GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor,
102 &ManagedObjectFinalizer,
103 v8::WeakCallbackType::kParameter);
104 isolate->RegisterManagedPtrDestructor(destructor);
111 std::shared_ptr<CppType>* GetSharedPtrPtr() {
114 return reinterpret_cast<std::shared_ptr<CppType>*
>(
115 destructor->shared_ptr_ptr_);
120 static void Destructor(
void* ptr) {
121 auto shared_ptr_ptr =
reinterpret_cast<std::shared_ptr<CppType>*
>(ptr);
122 delete shared_ptr_ptr;
129 #endif // V8_OBJECTS_MANAGED_H_