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/zone/accounting-allocator.h" | 5 #include "src/zone/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 internal { | 14 namespace internal { |
15 | 15 |
| 16 AccountingAllocator::AccountingAllocator() : unused_segments_mutex_() { |
| 17 memory_pressure_level_.SetValue(MemoryPressureLevel::kNone); |
| 18 std::fill(unused_segments_heads_, |
| 19 unused_segments_heads_ + |
| 20 (1 + kMaxSegmentSizePower - kMinSegmentSizePower), |
| 21 nullptr); |
| 22 std::fill( |
| 23 unused_segments_sizes, |
| 24 unused_segments_sizes + (1 + kMaxSegmentSizePower - kMinSegmentSizePower), |
| 25 0); |
| 26 } |
| 27 |
| 28 AccountingAllocator::~AccountingAllocator() { ClearPool(); } |
| 29 |
| 30 void AccountingAllocator::MemoryPressureNotification( |
| 31 MemoryPressureLevel level) { |
| 32 memory_pressure_level_.SetValue(level); |
| 33 |
| 34 if (level != MemoryPressureLevel::kNone) { |
| 35 ClearPool(); |
| 36 } |
| 37 } |
| 38 |
| 39 Segment* AccountingAllocator::GetSegment(size_t bytes) { |
| 40 Segment* result = GetSegmentFromPool(bytes); |
| 41 if (result == nullptr) { |
| 42 result = AllocateSegment(bytes); |
| 43 result->Initialize(bytes); |
| 44 } |
| 45 |
| 46 return result; |
| 47 } |
| 48 |
16 Segment* AccountingAllocator::AllocateSegment(size_t bytes) { | 49 Segment* AccountingAllocator::AllocateSegment(size_t bytes) { |
17 void* memory = malloc(bytes); | 50 void* memory = malloc(bytes); |
18 if (memory) { | 51 if (memory) { |
19 base::AtomicWord current = | 52 base::AtomicWord current = |
20 base::NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); | 53 base::NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); |
21 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_); | 54 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_); |
22 while (current > max) { | 55 while (current > max) { |
23 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); | 56 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); |
24 } | 57 } |
25 } | 58 } |
26 return reinterpret_cast<Segment*>(memory); | 59 return reinterpret_cast<Segment*>(memory); |
27 } | 60 } |
28 | 61 |
| 62 void AccountingAllocator::ReturnSegment(Segment* segment) { |
| 63 segment->ZapContents(); |
| 64 |
| 65 if (memory_pressure_level_.Value() != MemoryPressureLevel::kNone) { |
| 66 FreeSegment(segment); |
| 67 } else if (!AddSegmentToPool(segment)) { |
| 68 FreeSegment(segment); |
| 69 } |
| 70 } |
| 71 |
29 void AccountingAllocator::FreeSegment(Segment* memory) { | 72 void AccountingAllocator::FreeSegment(Segment* memory) { |
30 base::NoBarrier_AtomicIncrement( | 73 base::NoBarrier_AtomicIncrement( |
31 ¤t_memory_usage_, -static_cast<base::AtomicWord>(memory->size())); | 74 ¤t_memory_usage_, -static_cast<base::AtomicWord>(memory->size())); |
32 memory->ZapHeader(); | 75 memory->ZapHeader(); |
33 free(memory); | 76 free(memory); |
34 } | 77 } |
35 | 78 |
36 size_t AccountingAllocator::GetCurrentMemoryUsage() const { | 79 size_t AccountingAllocator::GetCurrentMemoryUsage() const { |
37 return base::NoBarrier_Load(¤t_memory_usage_); | 80 return base::NoBarrier_Load(¤t_memory_usage_); |
38 } | 81 } |
39 | 82 |
40 size_t AccountingAllocator::GetMaxMemoryUsage() const { | 83 size_t AccountingAllocator::GetMaxMemoryUsage() const { |
41 return base::NoBarrier_Load(&max_memory_usage_); | 84 return base::NoBarrier_Load(&max_memory_usage_); |
42 } | 85 } |
43 | 86 |
| 87 Segment* AccountingAllocator::GetSegmentFromPool(size_t requested_size) { |
| 88 if (requested_size > (1 << kMaxSegmentSizePower)) { |
| 89 return nullptr; |
| 90 } |
| 91 |
| 92 uint8_t power = kMinSegmentSizePower; |
| 93 while (requested_size > (static_cast<size_t>(1) << power)) power++; |
| 94 |
| 95 DCHECK_GE(power, kMinSegmentSizePower + 0); |
| 96 power -= kMinSegmentSizePower; |
| 97 |
| 98 Segment* segment; |
| 99 { |
| 100 base::LockGuard<base::Mutex> lock_guard(&unused_segments_mutex_); |
| 101 |
| 102 segment = unused_segments_heads_[power]; |
| 103 |
| 104 if (segment != nullptr) { |
| 105 unused_segments_heads_[power] = segment->next(); |
| 106 segment->set_next(nullptr); |
| 107 |
| 108 unused_segments_sizes[power]--; |
| 109 unused_segments_size_ -= segment->size(); |
| 110 } |
| 111 } |
| 112 |
| 113 if (segment) { |
| 114 DCHECK_GE(segment->size(), requested_size); |
| 115 } |
| 116 return segment; |
| 117 } |
| 118 |
| 119 bool AccountingAllocator::AddSegmentToPool(Segment* segment) { |
| 120 size_t size = segment->size(); |
| 121 |
| 122 if (size >= (1 << (kMaxSegmentSizePower + 1))) return false; |
| 123 |
| 124 if (size < (1 << kMinSegmentSizePower)) return false; |
| 125 |
| 126 uint8_t power = kMaxSegmentSizePower; |
| 127 |
| 128 while (size < (static_cast<size_t>(1) << power)) power--; |
| 129 |
| 130 DCHECK_GE(power, kMinSegmentSizePower + 0); |
| 131 power -= kMinSegmentSizePower; |
| 132 |
| 133 { |
| 134 base::LockGuard<base::Mutex> lock_guard(&unused_segments_mutex_); |
| 135 |
| 136 if (unused_segments_sizes[power] >= kMaxSegmentsPerBucket) { |
| 137 return false; |
| 138 } |
| 139 |
| 140 segment->set_next(unused_segments_heads_[power]); |
| 141 unused_segments_heads_[power] = segment; |
| 142 unused_segments_size_ += size; |
| 143 unused_segments_sizes[power]++; |
| 144 } |
| 145 |
| 146 return true; |
| 147 } |
| 148 |
| 149 void AccountingAllocator::ClearPool() { |
| 150 base::LockGuard<base::Mutex> lock_guard(&unused_segments_mutex_); |
| 151 |
| 152 for (uint8_t power = 0; power <= kMaxSegmentSizePower - kMinSegmentSizePower; |
| 153 power++) { |
| 154 Segment* current = unused_segments_heads_[power]; |
| 155 while (current) { |
| 156 Segment* next = current->next(); |
| 157 FreeSegment(current); |
| 158 current = next; |
| 159 } |
| 160 unused_segments_heads_[power] = nullptr; |
| 161 } |
| 162 } |
| 163 |
44 } // namespace internal | 164 } // namespace internal |
45 } // namespace v8 | 165 } // namespace v8 |
OLD | NEW |