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_set> | 8 #include <unordered_map> |
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; |
62 typedef JSArrayBuffer* Key; | 63 typedef JSArrayBuffer* Key; |
63 | 64 |
64 enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; | 65 enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; |
65 enum FreeMode { kFreeDead, kFreeAll }; | 66 enum FreeMode { kFreeDead, kFreeAll }; |
66 | 67 |
67 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} | 68 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} |
68 ~LocalArrayBufferTracker(); | 69 ~LocalArrayBufferTracker(); |
69 | 70 |
70 inline void Add(Key key); | 71 inline void Add(Key key, const Value& value); |
71 inline void Remove(Key key); | 72 inline Value Remove(Key key); |
72 | 73 |
73 // Frees up array buffers determined by |free_mode|. | 74 // Frees up array buffers determined by |free_mode|. |
74 template <FreeMode free_mode> | 75 template <FreeMode free_mode> |
75 void Free(); | 76 void Free(); |
76 | 77 |
77 // Processes buffers one by one. The CallbackResult of the callback decides | 78 // Processes buffers one by one. The CallbackResult of the callback decides |
78 // what action to take on the buffer. | 79 // what action to take on the buffer. |
79 // | 80 // |
80 // Callback should be of type: | 81 // Callback should be of type: |
81 // CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer); | 82 // CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer); |
82 template <typename Callback> | 83 template <typename Callback> |
83 void Process(Callback callback); | 84 inline void Process(Callback callback); |
84 | 85 |
85 bool IsEmpty() { return array_buffers_.empty(); } | 86 bool IsEmpty() { return array_buffers_.empty(); } |
86 | 87 |
87 bool IsTracked(Key key) { | 88 bool IsTracked(Key key) { |
88 return array_buffers_.find(key) != array_buffers_.end(); | 89 return array_buffers_.find(key) != array_buffers_.end(); |
89 } | 90 } |
90 | 91 |
91 private: | 92 private: |
92 typedef std::unordered_set<Key> TrackingData; | 93 typedef std::unordered_map<Key, Value> TrackingMap; |
93 | 94 |
94 Heap* heap_; | 95 Heap* heap_; |
95 TrackingData array_buffers_; | 96 TrackingMap array_buffers_; |
96 }; | 97 }; |
97 | 98 |
98 } // namespace internal | 99 } // namespace internal |
99 } // namespace v8 | 100 } // namespace v8 |
100 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 101 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
OLD | NEW |