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 <unordered_map> | 8 #include <unordered_set> |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
11 #include "src/base/platform/mutex.h" | 11 #include "src/base/platform/mutex.h" |
12 #include "src/globals.h" | 12 #include "src/globals.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 class Heap; | 17 class Heap; |
18 class JSArrayBuffer; | 18 class JSArrayBuffer; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 // Returns whether a buffer is currently tracked. | 53 // Returns whether a buffer is currently tracked. |
54 static bool IsTracked(JSArrayBuffer* buffer); | 54 static bool IsTracked(JSArrayBuffer* buffer); |
55 }; | 55 }; |
56 | 56 |
57 // LocalArrayBufferTracker tracks internalized array buffers. | 57 // LocalArrayBufferTracker tracks internalized array buffers. |
58 // | 58 // |
59 // Never use directly but instead always call through |ArrayBufferTracker|. | 59 // Never use directly but instead always call through |ArrayBufferTracker|. |
60 class LocalArrayBufferTracker { | 60 class LocalArrayBufferTracker { |
61 public: | 61 public: |
62 typedef std::pair<void*, size_t> Value; | |
63 typedef JSArrayBuffer* Key; | 62 typedef JSArrayBuffer* Key; |
64 | 63 |
65 enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; | 64 enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; |
66 enum FreeMode { kFreeDead, kFreeAll }; | 65 enum FreeMode { kFreeDead, kFreeAll }; |
67 | 66 |
68 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} | 67 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} |
69 ~LocalArrayBufferTracker(); | 68 ~LocalArrayBufferTracker(); |
70 | 69 |
71 inline void Add(Key key, const Value& value); | 70 inline void Add(Key key); |
72 inline Value Remove(Key key); | 71 inline void Remove(Key key); |
73 | 72 |
74 // Frees up array buffers determined by |free_mode|. | 73 // Frees up array buffers determined by |free_mode|. |
75 template <FreeMode free_mode> | 74 template <FreeMode free_mode> |
76 void Free(); | 75 void Free(); |
77 | 76 |
78 // Processes buffers one by one. The CallbackResult of the callback decides | 77 // Processes buffers one by one. The CallbackResult of the callback decides |
79 // what action to take on the buffer. | 78 // what action to take on the buffer. |
80 // | 79 // |
81 // Callback should be of type: | 80 // Callback should be of type: |
82 // CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer); | 81 // CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer); |
83 template <typename Callback> | 82 template <typename Callback> |
84 inline void Process(Callback callback); | 83 void Process(Callback callback); |
85 | 84 |
86 bool IsEmpty() { return array_buffers_.empty(); } | 85 bool IsEmpty() { return array_buffers_.empty(); } |
87 | 86 |
88 bool IsTracked(Key key) { | 87 bool IsTracked(Key key) { |
89 return array_buffers_.find(key) != array_buffers_.end(); | 88 return array_buffers_.find(key) != array_buffers_.end(); |
90 } | 89 } |
91 | 90 |
92 private: | 91 private: |
93 typedef std::unordered_map<Key, Value> TrackingMap; | 92 typedef std::unordered_set<Key> TrackingData; |
94 | 93 |
95 Heap* heap_; | 94 Heap* heap_; |
96 TrackingMap array_buffers_; | 95 TrackingData array_buffers_; |
97 }; | 96 }; |
98 | 97 |
99 } // namespace internal | 98 } // namespace internal |
100 } // namespace v8 | 99 } // namespace v8 |
101 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 100 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
OLD | NEW |