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/base/accounting-allocator.h" | 5 #include "src/base/accounting-allocator.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 | 8 |
9 #if V8_LIBC_BIONIC | 9 #if V8_LIBC_BIONIC |
10 #include <malloc.h> // NOLINT | 10 #include <malloc.h> // NOLINT |
11 #endif | 11 #endif |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 void* AccountingAllocator::Allocate(size_t bytes) { | 16 void* AccountingAllocator::Allocate(size_t bytes) { |
17 void* memory = malloc(bytes); | 17 void* memory = malloc(bytes); |
18 if (memory) NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); | 18 if (memory) { |
| 19 AtomicWord current = |
| 20 NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); |
| 21 AtomicWord max = NoBarrier_Load(&max_memory_usage_); |
| 22 while (current > max) { |
| 23 max = NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); |
| 24 } |
| 25 } |
19 return memory; | 26 return memory; |
20 } | 27 } |
21 | 28 |
22 void AccountingAllocator::Free(void* memory, size_t bytes) { | 29 void AccountingAllocator::Free(void* memory, size_t bytes) { |
23 free(memory); | 30 free(memory); |
24 NoBarrier_AtomicIncrement(¤t_memory_usage_, | 31 NoBarrier_AtomicIncrement(¤t_memory_usage_, |
25 -static_cast<AtomicWord>(bytes)); | 32 -static_cast<AtomicWord>(bytes)); |
26 } | 33 } |
27 | 34 |
28 size_t AccountingAllocator::GetCurrentMemoryUsage() const { | 35 size_t AccountingAllocator::GetCurrentMemoryUsage() const { |
29 return NoBarrier_Load(¤t_memory_usage_); | 36 return NoBarrier_Load(¤t_memory_usage_); |
30 } | 37 } |
31 | 38 |
| 39 size_t AccountingAllocator::GetMaxMemoryUsage() const { |
| 40 return NoBarrier_Load(&max_memory_usage_); |
| 41 } |
| 42 |
32 } // namespace base | 43 } // namespace base |
33 } // namespace v8 | 44 } // namespace v8 |
OLD | NEW |