LCOV - code coverage report
Current view: top level - src/objects - managed.h (source / functions) Hit Total Coverage
Test: app.info Lines: 28 28 100.0 %
Date: 2019-03-21 Functions: 80 80 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     2961686 :         destructor_(destructor) {}
      35             : };
      36             : 
      37             : // The GC finalizer of a managed object, which does not depend on
      38             : // the template parameter.
      39             : void ManagedObjectFinalizer(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:
      51    12543952 :   Managed() : Foreign() {}
      52             :   explicit Managed(Address ptr) : Foreign(ptr) {}
      53             :   Managed* operator->() { return this; }
      54             : 
      55             :   // Get a raw pointer to the C++ object.
      56    12056846 :   V8_INLINE CppType* raw() { return GetSharedPtrPtr()->get(); }
      57             : 
      58             :   // Get a copy of the shared pointer to the C++ object.
      59         258 :   V8_INLINE std::shared_ptr<CppType> get() { return *GetSharedPtrPtr(); }
      60             : 
      61             :   static Managed cast(Object obj) { return Managed(obj->ptr()); }
      62     5054539 :   static Managed unchecked_cast(Object obj) { return bit_cast<Managed>(obj); }
      63             : 
      64             :   // Allocate a new {CppType} and wrap it in a {Managed<CppType>}.
      65             :   template <typename... Args>
      66     1597109 :   static Handle<Managed<CppType>> Allocate(Isolate* isolate,
      67             :                                            size_t estimated_size,
      68             :                                            Args&&... args) {
      69     1597133 :     CppType* ptr = new CppType(std::forward<Args>(args)...);
      70     1597108 :     return FromSharedPtr(isolate, estimated_size,
      71     3194218 :                          std::shared_ptr<CppType>(ptr));
      72             :   }
      73             : 
      74             :   // Create a {Managed<CppType>} from an existing raw {CppType*}. The returned
      75             :   // object will now own the memory pointed to by {CppType}.
      76      113659 :   static Handle<Managed<CppType>> FromRawPtr(Isolate* isolate,
      77             :                                              size_t estimated_size,
      78             :                                              CppType* ptr) {
      79      113659 :     return FromSharedPtr(isolate, estimated_size,
      80      227318 :                          std::shared_ptr<CppType>(ptr));
      81             :   }
      82             : 
      83             :   // Create a {Managed<CppType>} from an existing {std::unique_ptr<CppType>}.
      84             :   // The returned object will now own the memory pointed to by {CppType}, and
      85             :   // the unique pointer will be released.
      86       12292 :   static Handle<Managed<CppType>> FromUniquePtr(
      87             :       Isolate* isolate, size_t estimated_size,
      88             :       std::unique_ptr<CppType> unique_ptr) {
      89       24584 :     return FromSharedPtr(isolate, estimated_size, std::move(unique_ptr));
      90             :   }
      91             : 
      92             :   // Create a {Managed<CppType>} from an existing {std::shared_ptr<CppType>}.
      93     2961679 :   static Handle<Managed<CppType>> FromSharedPtr(
      94             :       Isolate* isolate, size_t estimated_size,
      95             :       std::shared_ptr<CppType> shared_ptr) {
      96             :     reinterpret_cast<v8::Isolate*>(isolate)
      97     2961679 :         ->AdjustAmountOfExternalAllocatedMemory(estimated_size);
      98             :     auto destructor = new ManagedPtrDestructor(
      99     5923359 :         estimated_size, new std::shared_ptr<CppType>(shared_ptr), Destructor);
     100     2961681 :     Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
     101             :         isolate->factory()->NewForeign(reinterpret_cast<Address>(destructor)));
     102             :     Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
     103     2961681 :     destructor->global_handle_location_ = global_handle.location();
     104     2961681 :     GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor,
     105             :                             &ManagedObjectFinalizer,
     106             :                             v8::WeakCallbackType::kParameter);
     107     2961680 :     isolate->RegisterManagedPtrDestructor(destructor);
     108     2961681 :     return handle;
     109             :   }
     110             : 
     111             :  private:
     112             :   // Internally this {Foreign} object stores a pointer to a new
     113             :   // std::shared_ptr<CppType>.
     114    12057104 :   std::shared_ptr<CppType>* GetSharedPtrPtr() {
     115             :     auto destructor =
     116    12714903 :         reinterpret_cast<ManagedPtrDestructor*>(foreign_address());
     117             :     return reinterpret_cast<std::shared_ptr<CppType>*>(
     118    12714903 :         destructor->shared_ptr_ptr_);
     119             :   }
     120             : 
     121             :   // Called by either isolate shutdown or the {ManagedObjectFinalizer} in
     122             :   // order to actually delete the shared pointer (i.e. decrement its refcount).
     123     2961679 :   static void Destructor(void* ptr) {
     124             :     auto shared_ptr_ptr = reinterpret_cast<std::shared_ptr<CppType>*>(ptr);
     125     5923360 :     delete shared_ptr_ptr;
     126     2961680 :   }
     127             : };
     128             : 
     129             : }  // namespace internal
     130             : }  // namespace v8
     131             : 
     132             : #endif  // V8_OBJECTS_MANAGED_H_

Generated by: LCOV version 1.10