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