Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1922)

Side by Side Diff: components/discardable_memory/common/discardable_shared_memory_heap.cc

Issue 2693853002: Revert "Remove scopedvector in //components/discardable_memory/" (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium 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 "components/discardable_memory/common/discardable_shared_memory_heap.h" 5 #include "components/discardable_memory/common/discardable_shared_memory_heap.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 new Span(shared_memory.get(), 124 new Span(shared_memory.get(),
125 reinterpret_cast<size_t>(shared_memory->memory()) / block_size_, 125 reinterpret_cast<size_t>(shared_memory->memory()) / block_size_,
126 size / block_size_)); 126 size / block_size_));
127 DCHECK(spans_.find(span->start_) == spans_.end()); 127 DCHECK(spans_.find(span->start_) == spans_.end());
128 DCHECK(spans_.find(span->start_ + span->length_ - 1) == spans_.end()); 128 DCHECK(spans_.find(span->start_ + span->length_ - 1) == spans_.end());
129 RegisterSpan(span.get()); 129 RegisterSpan(span.get());
130 130
131 num_blocks_ += span->length_; 131 num_blocks_ += span->length_;
132 132
133 // Start tracking if segment is resident by adding it to |memory_segments_|. 133 // Start tracking if segment is resident by adding it to |memory_segments_|.
134 memory_segments_.push_back(base::MakeUnique<ScopedMemorySegment>( 134 memory_segments_.push_back(new ScopedMemorySegment(
135 this, std::move(shared_memory), size, id, deleted_callback)); 135 this, std::move(shared_memory), size, id, deleted_callback));
136 136
137 return span; 137 return span;
138 } 138 }
139 139
140 void DiscardableSharedMemoryHeap::MergeIntoFreeLists( 140 void DiscardableSharedMemoryHeap::MergeIntoFreeLists(
141 std::unique_ptr<Span> span) { 141 std::unique_ptr<Span> span) {
142 DCHECK(span->shared_memory_); 142 DCHECK(span->shared_memory_);
143 143
144 // First add length of |span| to |num_free_blocks_|. 144 // First add length of |span| to |num_free_blocks_|.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 return Carve(span, blocks); 219 return Carve(span, blocks);
220 } 220 }
221 221
222 return nullptr; 222 return nullptr;
223 } 223 }
224 224
225 void DiscardableSharedMemoryHeap::ReleaseFreeMemory() { 225 void DiscardableSharedMemoryHeap::ReleaseFreeMemory() {
226 // Erase all free segments after rearranging the segments in such a way 226 // Erase all free segments after rearranging the segments in such a way
227 // that used segments precede all free segments. 227 // that used segments precede all free segments.
228 memory_segments_.erase( 228 memory_segments_.erase(
229 std::partition(memory_segments_.begin(), memory_segments_.end(), 229 std::partition(
230 [](const std::unique_ptr<ScopedMemorySegment>& segment) { 230 memory_segments_.begin(), memory_segments_.end(),
231 return segment->IsUsed(); 231 [](const ScopedMemorySegment* segment) { return segment->IsUsed(); }),
232 }),
233 memory_segments_.end()); 232 memory_segments_.end());
234 } 233 }
235 234
236 void DiscardableSharedMemoryHeap::ReleasePurgedMemory() { 235 void DiscardableSharedMemoryHeap::ReleasePurgedMemory() {
237 // Erase all purged segments after rearranging the segments in such a way 236 // Erase all purged segments after rearranging the segments in such a way
238 // that resident segments precede all purged segments. 237 // that resident segments precede all purged segments.
239 memory_segments_.erase( 238 memory_segments_.erase(
240 std::partition(memory_segments_.begin(), memory_segments_.end(), 239 std::partition(memory_segments_.begin(), memory_segments_.end(),
241 [](const std::unique_ptr<ScopedMemorySegment>& segment) { 240 [](const ScopedMemorySegment* segment) {
242 return segment->IsResident(); 241 return segment->IsResident();
243 }), 242 }),
244 memory_segments_.end()); 243 memory_segments_.end());
245 } 244 }
246 245
247 size_t DiscardableSharedMemoryHeap::GetSize() const { 246 size_t DiscardableSharedMemoryHeap::GetSize() const {
248 return num_blocks_ * block_size_; 247 return num_blocks_ * block_size_;
249 } 248 }
250 249
251 size_t DiscardableSharedMemoryHeap::GetSizeOfFreeLists() const { 250 size_t DiscardableSharedMemoryHeap::GetSizeOfFreeLists() const {
252 return num_free_blocks_ * block_size_; 251 return num_free_blocks_ * block_size_;
253 } 252 }
254 253
255 bool DiscardableSharedMemoryHeap::OnMemoryDump( 254 bool DiscardableSharedMemoryHeap::OnMemoryDump(
256 base::trace_event::ProcessMemoryDump* pmd) { 255 base::trace_event::ProcessMemoryDump* pmd) {
257 std::for_each(memory_segments_.begin(), memory_segments_.end(), 256 std::for_each(memory_segments_.begin(), memory_segments_.end(),
258 [pmd](const std::unique_ptr<ScopedMemorySegment>& segment) { 257 [pmd](const ScopedMemorySegment* segment) {
259 segment->OnMemoryDump(pmd); 258 segment->OnMemoryDump(pmd);
260 }); 259 });
261 return true; 260 return true;
262 } 261 }
263 262
264 void DiscardableSharedMemoryHeap::InsertIntoFreeList( 263 void DiscardableSharedMemoryHeap::InsertIntoFreeList(
265 std::unique_ptr<DiscardableSharedMemoryHeap::Span> span) { 264 std::unique_ptr<DiscardableSharedMemoryHeap::Span> span) {
266 DCHECK(!IsInFreeList(span.get())); 265 DCHECK(!IsInFreeList(span.get()));
267 size_t index = std::min(span->length_, arraysize(free_spans_)) - 1; 266 size_t index = std::min(span->length_, arraysize(free_spans_)) - 1;
268 free_spans_[index].Append(span.release()); 267 free_spans_[index].Append(span.release());
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 const char* name, 459 const char* name,
461 base::trace_event::ProcessMemoryDump* pmd) const { 460 base::trace_event::ProcessMemoryDump* pmd) const {
462 if (!span->shared_memory()) { 461 if (!span->shared_memory()) {
463 base::trace_event::MemoryAllocatorDump* dump = 462 base::trace_event::MemoryAllocatorDump* dump =
464 pmd->CreateAllocatorDump(name); 463 pmd->CreateAllocatorDump(name);
465 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, 464 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
466 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 0u); 465 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 0u);
467 return dump; 466 return dump;
468 } 467 }
469 468
470 auto it = 469 ScopedVector<ScopedMemorySegment>::const_iterator it =
471 std::find_if(memory_segments_.begin(), memory_segments_.end(), 470 std::find_if(memory_segments_.begin(), memory_segments_.end(),
472 [span](const std::unique_ptr<ScopedMemorySegment>& segment) { 471 [span](const ScopedMemorySegment* segment) {
473 return segment->ContainsSpan(span); 472 return segment->ContainsSpan(span);
474 }); 473 });
475 DCHECK(it != memory_segments_.end()); 474 DCHECK(it != memory_segments_.end());
476 return (*it)->CreateMemoryAllocatorDump(span, block_size_, name, pmd); 475 return (*it)->CreateMemoryAllocatorDump(span, block_size_, name, pmd);
477 } 476 }
478 477
479 } // namespace discardable_memory 478 } // namespace discardable_memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698