OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 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 #include "src/conversions-inl.h" | 5 #include "src/conversions-inl.h" |
6 #include "src/heap/array-buffer-tracker.h" | 6 #include "src/heap/array-buffer-tracker.h" |
7 #include "src/heap/heap.h" | 7 #include "src/heap/heap.h" |
8 #include "src/objects.h" | 8 #include "src/objects.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { | 13 void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { |
14 void* data = buffer->backing_store(); | 14 void* data = buffer->backing_store(); |
15 if (!data) return; | 15 if (!data) return; |
16 | 16 |
17 size_t length = NumberToSize(buffer->byte_length()); | 17 size_t length = NumberToSize(buffer->byte_length()); |
18 Page* page = Page::FromAddress(buffer->address()); | 18 Page* page = Page::FromAddress(buffer->address()); |
19 { | 19 { |
20 base::LockGuard<base::Mutex> guard(page->mutex()); | 20 base::LockGuard<base::RecursiveMutex> guard(page->mutex()); |
21 LocalArrayBufferTracker* tracker = page->local_tracker(); | 21 LocalArrayBufferTracker* tracker = page->local_tracker(); |
22 if (tracker == nullptr) { | 22 if (tracker == nullptr) { |
23 page->AllocateLocalTracker(); | 23 page->AllocateLocalTracker(); |
24 tracker = page->local_tracker(); | 24 tracker = page->local_tracker(); |
25 } | 25 } |
26 DCHECK_NOT_NULL(tracker); | 26 DCHECK_NOT_NULL(tracker); |
27 tracker->Add(buffer, length); | 27 tracker->Add(buffer, length); |
28 } | 28 } |
29 // We may go over the limit of externally allocated memory here. We call the | 29 // We may go over the limit of externally allocated memory here. We call the |
30 // api function to trigger a GC in this case. | 30 // api function to trigger a GC in this case. |
31 reinterpret_cast<v8::Isolate*>(heap->isolate()) | 31 reinterpret_cast<v8::Isolate*>(heap->isolate()) |
32 ->AdjustAmountOfExternalAllocatedMemory(length); | 32 ->AdjustAmountOfExternalAllocatedMemory(length); |
33 } | 33 } |
34 | 34 |
35 void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) { | 35 void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) { |
36 void* data = buffer->backing_store(); | 36 void* data = buffer->backing_store(); |
37 if (!data) return; | 37 if (!data) return; |
38 | 38 |
39 Page* page = Page::FromAddress(buffer->address()); | 39 Page* page = Page::FromAddress(buffer->address()); |
40 size_t length = 0; | 40 size_t length = 0; |
41 { | 41 { |
42 base::LockGuard<base::Mutex> guard(page->mutex()); | 42 base::LockGuard<base::RecursiveMutex> guard(page->mutex()); |
43 LocalArrayBufferTracker* tracker = page->local_tracker(); | 43 LocalArrayBufferTracker* tracker = page->local_tracker(); |
44 DCHECK_NOT_NULL(tracker); | 44 DCHECK_NOT_NULL(tracker); |
45 length = tracker->Remove(buffer); | 45 length = tracker->Remove(buffer); |
46 } | 46 } |
47 heap->update_external_memory(-static_cast<intptr_t>(length)); | 47 heap->update_external_memory(-static_cast<intptr_t>(length)); |
48 } | 48 } |
49 | 49 |
50 void LocalArrayBufferTracker::Add(Key key, const Value& value) { | 50 void LocalArrayBufferTracker::Add(Key key, const Value& value) { |
51 auto ret = array_buffers_.insert(std::make_pair(key, value)); | 51 auto ret = array_buffers_.insert(std::make_pair(key, value)); |
52 USE(ret); | 52 USE(ret); |
53 // Check that we indeed inserted a new value and did not overwrite an existing | 53 // Check that we indeed inserted a new value and did not overwrite an existing |
54 // one (which would be a bug). | 54 // one (which would be a bug). |
55 DCHECK(ret.second); | 55 DCHECK(ret.second); |
56 } | 56 } |
57 | 57 |
58 LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) { | 58 LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) { |
59 TrackingData::iterator it = array_buffers_.find(key); | 59 TrackingData::iterator it = array_buffers_.find(key); |
60 // Check that we indeed find a key to remove. | 60 // Check that we indeed find a key to remove. |
61 DCHECK(it != array_buffers_.end()); | 61 DCHECK(it != array_buffers_.end()); |
62 Value value = it->second; | 62 Value value = it->second; |
63 array_buffers_.erase(it); | 63 array_buffers_.erase(it); |
64 return value; | 64 return value; |
65 } | 65 } |
66 | 66 |
67 } // namespace internal | 67 } // namespace internal |
68 } // namespace v8 | 68 } // namespace v8 |
OLD | NEW |