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 | |
49 Segment* AccountingAllocator::AllocateSegment(size_t bytes) { | 16 Segment* AccountingAllocator::AllocateSegment(size_t bytes) { |
50 void* memory = malloc(bytes); | 17 void* memory = malloc(bytes); |
51 if (memory) { | 18 if (memory) { |
52 base::AtomicWord current = | 19 base::AtomicWord current = |
53 base::NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); | 20 base::NoBarrier_AtomicIncrement(¤t_memory_usage_, bytes); |
54 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_); | 21 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_); |
55 while (current > max) { | 22 while (current > max) { |
56 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); | 23 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); |
57 } | 24 } |
58 } | 25 } |
59 return reinterpret_cast<Segment*>(memory); | 26 return reinterpret_cast<Segment*>(memory); |
60 } | 27 } |
61 | 28 |
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 | |
72 void AccountingAllocator::FreeSegment(Segment* memory) { | 29 void AccountingAllocator::FreeSegment(Segment* memory) { |
73 base::NoBarrier_AtomicIncrement( | 30 base::NoBarrier_AtomicIncrement( |
74 ¤t_memory_usage_, -static_cast<base::AtomicWord>(memory->size())); | 31 ¤t_memory_usage_, -static_cast<base::AtomicWord>(memory->size())); |
75 memory->ZapHeader(); | 32 memory->ZapHeader(); |
76 free(memory); | 33 free(memory); |
77 } | 34 } |
78 | 35 |
79 size_t AccountingAllocator::GetCurrentMemoryUsage() const { | 36 size_t AccountingAllocator::GetCurrentMemoryUsage() const { |
80 return base::NoBarrier_Load(¤t_memory_usage_); | 37 return base::NoBarrier_Load(¤t_memory_usage_); |
81 } | 38 } |
82 | 39 |
83 size_t AccountingAllocator::GetMaxMemoryUsage() const { | 40 size_t AccountingAllocator::GetMaxMemoryUsage() const { |
84 return base::NoBarrier_Load(&max_memory_usage_); | 41 return base::NoBarrier_Load(&max_memory_usage_); |
85 } | 42 } |
86 | 43 |
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 | |
164 } // namespace internal | 44 } // namespace internal |
165 } // namespace v8 | 45 } // namespace v8 |
OLD | NEW |