LCOV - code coverage report
Current view: top level - src/objects - managed.h (source / functions) Hit Total Coverage
Test: app.info Lines: 27 27 100.0 %
Date: 2019-04-19 Functions: 90 90 100.0 %

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

Generated by: LCOV version 1.10