LCOV - code coverage report
Current view: top level - src/heap - array-buffer-tracker.h (source / functions) Hit Total Coverage
Test: app.info Lines: 2 2 100.0 %
Date: 2019-03-21 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2015 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_HEAP_ARRAY_BUFFER_TRACKER_H_
       6             : #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_
       7             : 
       8             : #include <unordered_map>
       9             : 
      10             : #include "src/allocation.h"
      11             : #include "src/base/platform/mutex.h"
      12             : #include "src/globals.h"
      13             : #include "src/objects/js-array-buffer.h"
      14             : 
      15             : namespace v8 {
      16             : namespace internal {
      17             : 
      18             : class MarkingState;
      19             : class Page;
      20             : class Space;
      21             : 
      22             : class ArrayBufferTracker : public AllStatic {
      23             :  public:
      24             :   enum ProcessingMode {
      25             :     kUpdateForwardedRemoveOthers,
      26             :     kUpdateForwardedKeepOthers,
      27             :   };
      28             : 
      29             :   // The following methods are used to track raw C++ pointers to externally
      30             :   // allocated memory used as backing store in live array buffers.
      31             : 
      32             :   // Register/unregister a new JSArrayBuffer |buffer| for tracking. Guards all
      33             :   // access to the tracker by taking the page lock for the corresponding page.
      34             :   inline static void RegisterNew(Heap* heap, JSArrayBuffer buffer);
      35             :   inline static void Unregister(Heap* heap, JSArrayBuffer buffer);
      36             : 
      37             :   // Identifies all backing store pointers for dead JSArrayBuffers in new space.
      38             :   // Does not take any locks and can only be called during Scavenge.
      39             :   static void PrepareToFreeDeadInNewSpace(Heap* heap);
      40             : 
      41             :   // Frees all backing store pointers for dead JSArrayBuffer on a given page.
      42             :   // Requires marking information to be present. Requires the page lock to be
      43             :   // taken by the caller.
      44             :   template <typename MarkingState>
      45             :   static void FreeDead(Page* page, MarkingState* marking_state);
      46             : 
      47             :   // Frees all remaining, live or dead, array buffers on a page. Only useful
      48             :   // during tear down.
      49             :   static void FreeAll(Page* page);
      50             : 
      51             :   // Processes all array buffers on a given page. |mode| specifies the action
      52             :   // to perform on the buffers. Returns whether the tracker is empty or not.
      53             :   static bool ProcessBuffers(Page* page, ProcessingMode mode);
      54             : 
      55             :   // Returns whether a buffer is currently tracked.
      56             :   static bool IsTracked(JSArrayBuffer buffer);
      57             : 
      58             :   // Tears down the tracker and frees up all registered array buffers.
      59             :   static void TearDown(Heap* heap);
      60             : };
      61             : 
      62             : // LocalArrayBufferTracker tracks internalized array buffers.
      63             : //
      64             : // Never use directly but instead always call through |ArrayBufferTracker|.
      65             : class LocalArrayBufferTracker {
      66             :  public:
      67             :   enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry };
      68             :   enum FreeMode { kFreeDead, kFreeAll };
      69             : 
      70      445884 :   explicit LocalArrayBufferTracker(Page* page) : page_(page) {}
      71             :   ~LocalArrayBufferTracker();
      72             : 
      73             :   inline void Add(JSArrayBuffer buffer, size_t length);
      74             :   inline void Remove(JSArrayBuffer buffer, size_t length);
      75             : 
      76             :   // Frees up array buffers.
      77             :   //
      78             :   // Sample usage:
      79             :   // Free([](HeapObject array_buffer) {
      80             :   //    if (should_free_internal(array_buffer)) return true;
      81             :   //    return false;
      82             :   // });
      83             :   template <typename Callback>
      84             :   void Free(Callback should_free);
      85             : 
      86             :   // Processes buffers one by one. The CallbackResult of the callback decides
      87             :   // what action to take on the buffer.
      88             :   //
      89             :   // Callback should be of type:
      90             :   //   CallbackResult fn(JSArrayBuffer buffer, JSArrayBuffer* new_buffer);
      91             :   template <typename Callback>
      92             :   void Process(Callback callback);
      93             : 
      94             :   bool IsEmpty() const { return array_buffers_.empty(); }
      95             : 
      96             :   bool IsTracked(JSArrayBuffer buffer) const {
      97             :     return array_buffers_.find(buffer) != array_buffers_.end();
      98             :   }
      99             : 
     100             :  private:
     101             :   class Hasher {
     102             :    public:
     103             :     size_t operator()(JSArrayBuffer buffer) const {
     104      550428 :       return static_cast<size_t>(buffer.ptr() >> 3);
     105             :     }
     106             :   };
     107             : 
     108             :   // Keep track of the backing store and the corresponding length at time of
     109             :   // registering. The length is accessed from JavaScript and can be a
     110             :   // HeapNumber. The reason for tracking the length is that in the case of
     111             :   // length being a HeapNumber, the buffer and its length may be stored on
     112             :   // different memory pages, making it impossible to guarantee order of freeing.
     113             :   typedef std::unordered_map<JSArrayBuffer, JSArrayBuffer::Allocation, Hasher>
     114             :       TrackingData;
     115             : 
     116             :   // Internal version of add that does not update counters. Requires separate
     117             :   // logic for updating external memory counters.
     118             :   inline void AddInternal(JSArrayBuffer buffer, size_t length);
     119             : 
     120             :   inline Space* space();
     121             : 
     122             :   Page* page_;
     123             :   // The set contains raw heap pointers which are removed by the GC upon
     124             :   // processing the tracker through its owning page.
     125             :   TrackingData array_buffers_;
     126             : };
     127             : 
     128             : }  // namespace internal
     129             : }  // namespace v8
     130             : #endif  // V8_HEAP_ARRAY_BUFFER_TRACKER_H_

Generated by: LCOV version 1.10