| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 5 #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
| 6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "src/allocation.h" |
| 10 #include "src/base/platform/mutex.h" | 11 #include "src/base/platform/mutex.h" |
| 11 #include "src/globals.h" | 12 #include "src/globals.h" |
| 12 | 13 |
| 13 namespace v8 { | 14 namespace v8 { |
| 14 namespace internal { | 15 namespace internal { |
| 15 | 16 |
| 16 // Forward declarations. | |
| 17 class Heap; | 17 class Heap; |
| 18 class JSArrayBuffer; | 18 class JSArrayBuffer; |
| 19 class Page; |
| 19 | 20 |
| 20 class ArrayBufferTracker { | 21 class ArrayBufferTracker : public AllStatic { |
| 21 public: | 22 public: |
| 22 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} | 23 enum ProcessingMode { |
| 23 ~ArrayBufferTracker(); | 24 kUpdateForwardedRemoveOthers, |
| 24 | 25 kUpdateForwardedKeepOthers, |
| 25 inline Heap* heap() { return heap_; } | 26 }; |
| 26 | 27 |
| 27 // The following methods are used to track raw C++ pointers to externally | 28 // The following methods are used to track raw C++ pointers to externally |
| 28 // allocated memory used as backing store in live array buffers. | 29 // allocated memory used as backing store in live array buffers. |
| 29 | 30 |
| 30 // A new ArrayBuffer was created with |data| as backing store. | 31 // Register/unregister a new JSArrayBuffer |buffer| for tracking. Guards all |
| 31 void RegisterNew(JSArrayBuffer* buffer); | 32 // access to the tracker by taking the page lock for the corresponding page. |
| 33 static void RegisterNew(Heap* heap, JSArrayBuffer* buffer); |
| 34 static void Unregister(Heap* heap, JSArrayBuffer* buffer); |
| 32 | 35 |
| 33 // The backing store |data| is no longer owned by V8. | 36 // Frees all backing store pointers for dead JSArrayBuffers in new space. |
| 34 void Unregister(JSArrayBuffer* buffer); | 37 // Does not take any locks and can only be called during Scavenge. |
| 38 static void FreeDeadInNewSpace(Heap* heap); |
| 35 | 39 |
| 36 // A live ArrayBuffer was discovered during marking/scavenge. | 40 // Frees all backing store pointers for dead JSArrayBuffer on a given page. |
| 37 void MarkLive(JSArrayBuffer* buffer); | 41 // Requires marking information to be present. Requires the page lock to be |
| 42 // taken by the caller. |
| 43 static void FreeDead(Page* page); |
| 38 | 44 |
| 39 // Frees all backing store pointers that weren't discovered in the previous | 45 // Frees all remaining, live or dead, array buffers on a page. Only useful |
| 40 // marking or scavenge phase. | 46 // during tear down. |
| 41 void FreeDead(bool from_scavenge); | 47 static void FreeAll(Page* page); |
| 42 | 48 |
| 43 // Prepare for a new scavenge phase. A new marking phase is implicitly | 49 // Processes all array buffers on a given page. |mode| specifies the action |
| 44 // prepared by finishing the previous one. | 50 // to perform on the buffers. Returns whether the tracker is empty or not. |
| 45 void PrepareDiscoveryInNewSpace(); | 51 static bool ProcessBuffers(Page* page, ProcessingMode mode); |
| 46 | 52 |
| 47 // An ArrayBuffer moved from new space to old space. | 53 // Returns whether a buffer is currently tracked. |
| 48 void Promote(JSArrayBuffer* buffer); | 54 static bool IsTracked(JSArrayBuffer* buffer); |
| 55 }; |
| 56 |
| 57 // LocalArrayBufferTracker tracks internalized array buffers. |
| 58 // |
| 59 // Never use directly but instead always call through |ArrayBufferTracker|. |
| 60 class LocalArrayBufferTracker { |
| 61 public: |
| 62 typedef std::pair<void*, size_t> Value; |
| 63 typedef JSArrayBuffer* Key; |
| 64 |
| 65 enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; |
| 66 enum FreeMode { kFreeDead, kFreeAll }; |
| 67 |
| 68 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} |
| 69 ~LocalArrayBufferTracker(); |
| 70 |
| 71 void Add(Key key, const Value& value); |
| 72 Value Remove(Key key); |
| 73 |
| 74 // Frees up array buffers determined by |free_mode|. |
| 75 template <FreeMode free_mode> |
| 76 void Free(); |
| 77 |
| 78 // Processes buffers one by one. The CallbackResult of the callback decides |
| 79 // what action to take on the buffer. |
| 80 // |
| 81 // Callback should be of type: |
| 82 // CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer); |
| 83 template <typename Callback> |
| 84 inline void Process(Callback callback); |
| 85 |
| 86 bool IsEmpty() { return array_buffers_.empty(); } |
| 87 |
| 88 bool IsTracked(Key key) { |
| 89 return array_buffers_.find(key) != array_buffers_.end(); |
| 90 } |
| 49 | 91 |
| 50 private: | 92 private: |
| 51 base::Mutex mutex_; | 93 // TODO(mlippautz): Switch to unordered_map once it is supported on all |
| 94 // platforms. |
| 95 typedef std::map<Key, Value> TrackingMap; |
| 96 |
| 52 Heap* heap_; | 97 Heap* heap_; |
| 98 TrackingMap array_buffers_; |
| 99 }; |
| 53 | 100 |
| 54 // |live_array_buffers_| maps externally allocated memory used as backing | |
| 55 // store for ArrayBuffers to the length of the respective memory blocks. | |
| 56 // | |
| 57 // At the beginning of mark/compact, |not_yet_discovered_array_buffers_| is | |
| 58 // a copy of |live_array_buffers_| and we remove pointers as we discover live | |
| 59 // ArrayBuffer objects during marking. At the end of mark/compact, the | |
| 60 // remaining memory blocks can be freed. | |
| 61 std::map<void*, size_t> live_array_buffers_; | |
| 62 std::map<void*, size_t> not_yet_discovered_array_buffers_; | |
| 63 | |
| 64 // To be able to free memory held by ArrayBuffers during scavenge as well, we | |
| 65 // have a separate list of allocated memory held by ArrayBuffers in new space. | |
| 66 // | |
| 67 // Since mark/compact also evacuates the new space, all pointers in the | |
| 68 // |live_array_buffers_for_scavenge_| list are also in the | |
| 69 // |live_array_buffers_| list. | |
| 70 std::map<void*, size_t> live_array_buffers_for_scavenge_; | |
| 71 std::map<void*, size_t> not_yet_discovered_array_buffers_for_scavenge_; | |
| 72 }; | |
| 73 } // namespace internal | 101 } // namespace internal |
| 74 } // namespace v8 | 102 } // namespace v8 |
| 75 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 103 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
| OLD | NEW |