// 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_OBJECTS_MANAGED_H_ #define V8_OBJECTS_MANAGED_H_ #include #include "src/execution/isolate.h" #include "src/handles/global-handles.h" #include "src/handles/handles.h" #include "src/heap/factory.h" #include "src/objects/foreign.h" namespace v8 { namespace internal { // Implements a doubly-linked lists of destructors for the isolate. struct ManagedPtrDestructor { // Estimated size of external memory associated with the managed object. // This is used to adjust the garbage collector's heuristics upon // allocation and deallocation of a managed object. size_t estimated_size_ = 0; ManagedPtrDestructor* prev_ = nullptr; ManagedPtrDestructor* next_ = nullptr; void* shared_ptr_ptr_ = nullptr; void (*destructor_)(void* shared_ptr) = nullptr; Address* global_handle_location_ = nullptr; ManagedPtrDestructor(size_t estimated_size, void* shared_ptr_ptr, void (*destructor)(void*)) : estimated_size_(estimated_size), shared_ptr_ptr_(shared_ptr_ptr), destructor_(destructor) {} }; // The GC finalizer of a managed object, which does not depend on // the template parameter. V8_EXPORT_PRIVATE void ManagedObjectFinalizer( const v8::WeakCallbackInfo& data); // {Managed} is essentially a {std::shared_ptr} allocated on the heap // that can be used to manage the lifetime of C++ objects that are shared // across multiple isolates. // When a {Managed} object is garbage collected (or an isolate which // contains {Managed} is torn down), the {Managed} deletes its underlying // {std::shared_ptr}, thereby decrementing its internal reference count, // which will delete the C++ object when the reference count drops to 0. template class Managed : public Foreign { public: Managed() : Foreign() {} explicit Managed(Address ptr) : Foreign(ptr) {} // Get a raw pointer to the C++ object. V8_INLINE CppType* raw() { return GetSharedPtrPtr()->get(); } // Get a reference to the shared pointer to the C++ object. V8_INLINE const std::shared_ptr& get() { return *GetSharedPtrPtr(); } static Managed cast(Object obj) { return Managed(obj.ptr()); } static Managed unchecked_cast(Object obj) { return bit_cast(obj); } // Allocate a new {CppType} and wrap it in a {Managed}. template static Handle> Allocate(Isolate* isolate, size_t estimated_size, Args&&... args) { return FromSharedPtr( isolate, estimated_size, std::make_shared(std::forward(args)...)); } // Create a {Managed} from an existing raw {CppType*}. The returned // object will now own the memory pointed to by {CppType}. static Handle> FromRawPtr(Isolate* isolate, size_t estimated_size, CppType* ptr) { return FromSharedPtr(isolate, estimated_size, std::shared_ptr{ptr}); } // Create a {Managed} from an existing {std::unique_ptr}. // The returned object will now own the memory pointed to by {CppType}, and // the unique pointer will be released. static Handle> FromUniquePtr( Isolate* isolate, size_t estimated_size, std::unique_ptr unique_ptr) { return FromSharedPtr(isolate, estimated_size, std::move(unique_ptr)); } // Create a {Managed} from an existing {std::shared_ptr}. static Handle> FromSharedPtr( Isolate* isolate, size_t estimated_size, std::shared_ptr shared_ptr) { reinterpret_cast(isolate) ->AdjustAmountOfExternalAllocatedMemory(estimated_size); auto destructor = new ManagedPtrDestructor( estimated_size, new std::shared_ptr{std::move(shared_ptr)}, Destructor); Handle> handle = Handle>::cast( isolate->factory()->NewForeign(reinterpret_cast
(destructor))); Handle global_handle = isolate->global_handles()->Create(*handle); destructor->global_handle_location_ = global_handle.location(); GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor, &ManagedObjectFinalizer, v8::WeakCallbackType::kParameter); isolate->RegisterManagedPtrDestructor(destructor); return handle; } private: // Internally this {Foreign} object stores a pointer to a new // std::shared_ptr. std::shared_ptr* GetSharedPtrPtr() { auto destructor = reinterpret_cast(foreign_address()); return reinterpret_cast*>( destructor->shared_ptr_ptr_); } // Called by either isolate shutdown or the {ManagedObjectFinalizer} in order // to actually delete the shared pointer and decrement the shared refcount. static void Destructor(void* ptr) { auto shared_ptr_ptr = reinterpret_cast*>(ptr); delete shared_ptr_ptr; } }; } // namespace internal } // namespace v8 #endif // V8_OBJECTS_MANAGED_H_