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

Side by Side Diff: blimp/client/app/blimp_discardable_memory_allocator.cc

Issue 2624903006: Remove all blimp client code. (Closed)
Patch Set: Update buildbot configuration Created 3 years, 11 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "blimp/client/app/blimp_discardable_memory_allocator.h"
6
7 #include "base/macros.h"
8 #include "base/memory/discardable_memory.h"
9 #include "base/trace_event/memory_allocator_dump.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/process_memory_dump.h"
12
13 namespace blimp {
14 namespace client {
15
16 namespace {
17
18 size_t kDesiredMaxMemory = 20 * 1024 * 1024; /* 20 MB */
19
20 } // namespace
21
22 // Interface to the rest of the program. These objects are owned outside of the
23 // allocator.
24 class BlimpDiscardableMemoryAllocator::DiscardableMemoryChunkImpl
25 : public base::DiscardableMemory {
26 public:
27 DiscardableMemoryChunkImpl(size_t size,
28 BlimpDiscardableMemoryAllocator* allocator)
29 : is_locked_(true),
30 size_(size),
31 data_(new uint8_t[size]),
32 allocator_(allocator) {}
33
34 ~DiscardableMemoryChunkImpl() override {
35 DCHECK(allocator_); // Required for EXPECT_DCHECK_DEATH().
36 base::AutoLock lock(allocator_->lock_);
37 // Either the memory is discarded or the memory chunk is unlocked.
38 DCHECK(data_ || !is_locked_);
39 if (!is_locked_ && data_)
40 allocator_->NotifyDestructed(unlocked_position_);
41 }
42
43 // Overridden from DiscardableMemoryChunk:
44 bool Lock() override {
45 base::AutoLock lock(allocator_->lock_);
46 DCHECK(!is_locked_);
47 if (!data_)
48 return false;
49
50 is_locked_ = true;
51 allocator_->NotifyLocked(unlocked_position_);
52 return true;
53 }
54
55 void Unlock() override {
56 base::AutoLock lock(allocator_->lock_);
57 DCHECK(is_locked_);
58 DCHECK(data_);
59 is_locked_ = false;
60 unlocked_position_ = allocator_->NotifyUnlocked(this);
61
62 // Allow the allocator to discard our memory if required.
63 allocator_->FreeUnlockedChunksIfNeeded();
64 }
65
66 void* data() const override {
67 if (data_) {
68 DCHECK(is_locked_);
69 return data_.get();
70 }
71 return nullptr;
72 }
73
74 base::trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump(
75 const char* name,
76 base::trace_event::ProcessMemoryDump* pmd) const override {
77 base::trace_event::MemoryAllocatorDump* dump =
78 pmd->CreateAllocatorDump(name);
79 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
80 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size_);
81
82 // Memory is allocated from system allocator (malloc).
83 pmd->AddSuballocation(dump->guid(),
84 base::trace_event::MemoryDumpManager::GetInstance()
85 ->system_allocator_pool_name());
86 return dump;
87 }
88
89 size_t size() const { return size_; }
90
91 void Discard() {
92 allocator_->lock_.AssertAcquired();
93 DCHECK(!is_locked_);
94 data_.reset();
95 }
96
97 #if DCHECK_IS_ON()
98 void DetachForDebug() {
99 allocator_->lock_.AssertAcquired();
100 allocator_ = nullptr;
101 }
102 #endif // DCHECK_IS_ON()
103
104 private:
105 bool is_locked_;
106 size_t size_;
107 std::unique_ptr<uint8_t[]> data_;
108 BlimpDiscardableMemoryAllocator* allocator_;
109
110 MemoryChunkList::iterator unlocked_position_;
111
112 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl);
113 };
114
115 BlimpDiscardableMemoryAllocator::BlimpDiscardableMemoryAllocator()
116 : BlimpDiscardableMemoryAllocator(kDesiredMaxMemory) {}
117
118 BlimpDiscardableMemoryAllocator::BlimpDiscardableMemoryAllocator(
119 size_t desired_max_memory)
120 : desired_max_memory_(desired_max_memory),
121 total_live_memory_(0u),
122 locked_chunks_(0) {
123 }
124
125 BlimpDiscardableMemoryAllocator::~BlimpDiscardableMemoryAllocator() {
126 DCHECK_EQ(0, locked_chunks_);
127 // DCHECK_EQ(0u, live_unlocked_chunks_.size());
128 #if DCHECK_IS_ON()
129 base::AutoLock lock(lock_);
130 for (auto unlocked_chunk : live_unlocked_chunks_) {
131 unlocked_chunk->DetachForDebug();
132 }
133 #endif // DCHECK_IS_ON()
134 }
135
136 std::unique_ptr<base::DiscardableMemory>
137 BlimpDiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) {
138 base::AutoLock lock(lock_);
139 std::unique_ptr<DiscardableMemoryChunkImpl> chunk(
140 new DiscardableMemoryChunkImpl(size, this));
141 total_live_memory_ += size;
142 locked_chunks_++;
143
144 FreeUnlockedChunksIfNeeded();
145
146 return std::move(chunk);
147 }
148
149 BlimpDiscardableMemoryAllocator::MemoryChunkList::iterator
150 BlimpDiscardableMemoryAllocator::NotifyUnlocked(
151 DiscardableMemoryChunkImpl* chunk) {
152 lock_.AssertAcquired();
153 locked_chunks_--;
154 return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk);
155 }
156
157 void BlimpDiscardableMemoryAllocator::NotifyLocked(
158 MemoryChunkList::iterator it) {
159 lock_.AssertAcquired();
160 locked_chunks_++;
161 live_unlocked_chunks_.erase(it);
162 }
163
164 void BlimpDiscardableMemoryAllocator::NotifyDestructed(
165 MemoryChunkList::iterator it) {
166 lock_.AssertAcquired();
167 live_unlocked_chunks_.erase(it);
168 }
169
170 void BlimpDiscardableMemoryAllocator::FreeUnlockedChunksIfNeeded() {
171 lock_.AssertAcquired();
172
173 // Go through the list of unlocked live chunks starting from the least
174 // recently used, freeing as many as we can until we get our size under the
175 // desired maximum.
176 auto it = live_unlocked_chunks_.begin();
177 while (total_live_memory_ > desired_max_memory_ &&
178 it != live_unlocked_chunks_.end()) {
179 total_live_memory_ -= (*it)->size();
180 (*it)->Discard();
181 it = live_unlocked_chunks_.erase(it);
182 }
183 }
184
185 } // namespace client
186 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/client/app/blimp_discardable_memory_allocator.h ('k') | blimp/client/app/blimp_discardable_memory_allocator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698