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