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_OBJECTS_MANAGED_H_ 6 #define V8_OBJECTS_MANAGED_H_ 7 8 #include <memory> 9 #include "src/execution/isolate.h" 10 #include "src/handles/handles.h" 11 #include "src/heap/factory.h" 12 #include "src/objects/foreign.h" 13 14 namespace v8 { 15 namespace internal { 16 17 // Implements a doubly-linked lists of destructors for the isolate. 18 struct ManagedPtrDestructor { 19 // Estimated size of external memory associated with the managed object. 20 // This is used to adjust the garbage collector's heuristics upon 21 // allocation and deallocation of a managed object. 22 size_t estimated_size_ = 0; 23 ManagedPtrDestructor* prev_ = nullptr; 24 ManagedPtrDestructor* next_ = nullptr; 25 void* shared_ptr_ptr_ = nullptr; 26 void (*destructor_)(void* shared_ptr) = nullptr; 27 Address* global_handle_location_ = nullptr; 28 ManagedPtrDestructorManagedPtrDestructor29 ManagedPtrDestructor(size_t estimated_size, void* shared_ptr_ptr, 30 void (*destructor)(void*)) 31 : estimated_size_(estimated_size), 32 shared_ptr_ptr_(shared_ptr_ptr), 33 destructor_(destructor) {} 34 }; 35 36 // The GC finalizer of a managed object, which does not depend on 37 // the template parameter. 38 V8_EXPORT_PRIVATE void ManagedObjectFinalizer( 39 const v8::WeakCallbackInfo<void>& data); 40 41 // {Managed<T>} is essentially a {std::shared_ptr<T>} allocated on the heap 42 // that can be used to manage the lifetime of C++ objects that are shared 43 // across multiple isolates. 44 // When a {Managed<T>} object is garbage collected (or an isolate which 45 // contains {Managed<T>} is torn down), the {Managed<T>} deletes its underlying 46 // {std::shared_ptr<T>}, thereby decrementing its internal reference count, 47 // which will delete the C++ object when the reference count drops to 0. 48 template <class CppType> 49 class Managed : public Foreign { 50 public: Managed()51 Managed() : Foreign() {} Managed(Address ptr)52 explicit Managed(Address ptr) : Foreign(ptr) {} 53 54 // Get a raw pointer to the C++ object. raw()55 V8_INLINE CppType* raw() { return GetSharedPtrPtr()->get(); } 56 57 // Get a reference to the shared pointer to the C++ object. get()58 V8_INLINE const std::shared_ptr<CppType>& get() { return *GetSharedPtrPtr(); } 59 cast(Object obj)60 static Managed cast(Object obj) { return Managed(obj.ptr()); } unchecked_cast(Object obj)61 static Managed unchecked_cast(Object obj) { return bit_cast<Managed>(obj); } 62 63 // Allocate a new {CppType} and wrap it in a {Managed<CppType>}. 64 template <typename... Args> 65 static Handle<Managed<CppType>> Allocate(Isolate* isolate, 66 size_t estimated_size, 67 Args&&... args); 68 69 // Create a {Managed<CppType>} from an existing raw {CppType*}. The returned 70 // object will now own the memory pointed to by {CppType}. 71 static Handle<Managed<CppType>> FromRawPtr(Isolate* isolate, 72 size_t estimated_size, 73 CppType* ptr); 74 75 // Create a {Managed<CppType>} from an existing {std::unique_ptr<CppType>}. 76 // The returned object will now own the memory pointed to by {CppType}, and 77 // the unique pointer will be released. 78 static Handle<Managed<CppType>> FromUniquePtr( 79 Isolate* isolate, size_t estimated_size, 80 std::unique_ptr<CppType> unique_ptr); 81 82 // Create a {Managed<CppType>} from an existing {std::shared_ptr<CppType>}. 83 static Handle<Managed<CppType>> FromSharedPtr( 84 Isolate* isolate, size_t estimated_size, 85 std::shared_ptr<CppType> shared_ptr); 86 87 private: 88 // Internally this {Foreign} object stores a pointer to a new 89 // std::shared_ptr<CppType>. GetSharedPtrPtr()90 std::shared_ptr<CppType>* GetSharedPtrPtr() { 91 auto destructor = 92 reinterpret_cast<ManagedPtrDestructor*>(foreign_address()); 93 return reinterpret_cast<std::shared_ptr<CppType>*>( 94 destructor->shared_ptr_ptr_); 95 } 96 97 // Called by either isolate shutdown or the {ManagedObjectFinalizer} in order 98 // to actually delete the shared pointer and decrement the shared refcount. Destructor(void * ptr)99 static void Destructor(void* ptr) { 100 auto shared_ptr_ptr = reinterpret_cast<std::shared_ptr<CppType>*>(ptr); 101 delete shared_ptr_ptr; 102 } 103 }; 104 105 } // namespace internal 106 } // namespace v8 107 108 #endif // V8_OBJECTS_MANAGED_H_ 109