blob: 394e3257075e9d3d0eb40c1e54da812f884c96b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkDiscardableMemoryPool.h"
#include "SkDiscardableMemory.h"
#include "SkMakeUnique.h"
#include "SkMalloc.h"
#include "SkMutex.h"
#include "SkTInternalLList.h"
#include "SkTemplates.h"
// Note:
// A PoolDiscardableMemory is memory that is counted in a pool.
// A DiscardableMemoryPool is a pool of PoolDiscardableMemorys.
namespace {
class PoolDiscardableMemory;
/**
* This non-global pool can be used for unit tests to verify that the
* pool works.
*/
class DiscardableMemoryPool : public SkDiscardableMemoryPool {
public:
DiscardableMemoryPool(size_t budget);
~DiscardableMemoryPool() override;
std::unique_ptr<SkDiscardableMemory> make(size_t bytes);
SkDiscardableMemory* create(size_t bytes) override {
return this->make(bytes).release(); // TODO: change API
}
size_t getRAMUsed() override;
void setRAMBudget(size_t budget) override;
size_t getRAMBudget() override { return fBudget; }
/** purges all unlocked DMs */
void dumpPool() override;
#if SK_LAZY_CACHE_STATS // Defined in SkDiscardableMemoryPool.h
int getCacheHits() override { return fCacheHits; }
int getCacheMisses() override { return fCacheMisses; }
void resetCacheHitsAndMisses() override {
fCacheHits = fCacheMisses = 0;
}
int fCacheHits;
int fCacheMisses;
#endif // SK_LAZY_CACHE_STATS
private:
SkMutex fMutex;
size_t fBudget;
size_t fUsed;
SkTInternalLList<PoolDiscardableMemory> fList;
/** Function called to free memory if needed */
void dumpDownTo(size_t budget);
/** called by DiscardableMemoryPool upon destruction */
void removeFromPool(PoolDiscardableMemory* dm);
/** called by DiscardableMemoryPool::lock() */
bool lock(PoolDiscardableMemory* dm);
/** called by DiscardableMemoryPool::unlock() */
void unlock(PoolDiscardableMemory* dm);
friend class PoolDiscardableMemory;
typedef SkDiscardableMemory::Factory INHERITED;
};
/**
* A PoolDiscardableMemory is a SkDiscardableMemory that relies on
* a DiscardableMemoryPool object to manage the memory.
*/
class PoolDiscardableMemory : public SkDiscardableMemory {
public:
PoolDiscardableMemory(sk_sp<DiscardableMemoryPool> pool, SkAutoFree pointer, size_t bytes);
~PoolDiscardableMemory() override;
bool lock() override;
void* data() override;
void unlock() override;
friend class DiscardableMemoryPool;
private:
SK_DECLARE_INTERNAL_LLIST_INTERFACE(PoolDiscardableMemory);
sk_sp<DiscardableMemoryPool> fPool;
bool fLocked;
SkAutoFree fPointer;
const size_t fBytes;
};
PoolDiscardableMemory::PoolDiscardableMemory(sk_sp<DiscardableMemoryPool> pool,
SkAutoFree pointer,
size_t bytes)
: fPool(std::move(pool)), fLocked(true), fPointer(std::move(pointer)), fBytes(bytes) {
SkASSERT(fPool != nullptr);
SkASSERT(fPointer != nullptr);
SkASSERT(fBytes > 0);
}
PoolDiscardableMemory::~PoolDiscardableMemory() {
SkASSERT(!fLocked); // contract for SkDiscardableMemory
fPool->removeFromPool(this);
}
bool PoolDiscardableMemory::lock() {
SkASSERT(!fLocked); // contract for SkDiscardableMemory
return fPool->lock(this);
}
void* PoolDiscardableMemory::data() {
SkASSERT(fLocked); // contract for SkDiscardableMemory
return fPointer.get();
}
void PoolDiscardableMemory::unlock() {
SkASSERT(fLocked); // contract for SkDiscardableMemory
fPool->unlock(this);
}
////////////////////////////////////////////////////////////////////////////////
DiscardableMemoryPool::DiscardableMemoryPool(size_t budget)
: fBudget(budget)
, fUsed(0) {
#if SK_LAZY_CACHE_STATS
fCacheHits = 0;
fCacheMisses = 0;
#endif // SK_LAZY_CACHE_STATS
}
DiscardableMemoryPool::~DiscardableMemoryPool() {
// PoolDiscardableMemory objects that belong to this pool are
// always deleted before deleting this pool since each one has a
// ref to the pool.
SkASSERT(fList.isEmpty());
}
void DiscardableMemoryPool::dumpDownTo(size_t budget) {
fMutex.assertHeld();
if (fUsed <= budget) {
return;
}
using Iter = SkTInternalLList<PoolDiscardableMemory>::Iter;
Iter iter;
PoolDiscardableMemory* cur = iter.init(fList, Iter::kTail_IterStart);
while ((fUsed > budget) && (cur)) {
if (!cur->fLocked) {
PoolDiscardableMemory* dm = cur;
SkASSERT(dm->fPointer != nullptr);
dm->fPointer = nullptr;
SkASSERT(fUsed >= dm->fBytes);
fUsed -= dm->fBytes;
cur = iter.prev();
// Purged DMs are taken out of the list. This saves times
// looking them up. Purged DMs are NOT deleted.
fList.remove(dm);
} else {
cur = iter.prev();
}
}
}
std::unique_ptr<SkDiscardableMemory> DiscardableMemoryPool::make(size_t bytes) {
SkAutoFree addr(sk_malloc_canfail(bytes));
if (nullptr == addr) {
return nullptr;
}
auto dm = skstd::make_unique<PoolDiscardableMemory>(sk_ref_sp(this), std::move(addr), bytes);
SkAutoMutexAcquire autoMutexAcquire(fMutex);
fList.addToHead(dm.get());
fUsed += bytes;
this->dumpDownTo(fBudget);
return std::move(dm);
}
void DiscardableMemoryPool::removeFromPool(PoolDiscardableMemory* dm) {
SkAutoMutexAcquire autoMutexAcquire(fMutex);
// This is called by dm's destructor.
if (dm->fPointer != nullptr) {
SkASSERT(fUsed >= dm->fBytes);
fUsed -= dm->fBytes;
fList.remove(dm);
} else {
SkASSERT(!fList.isInList(dm));
}
}
bool DiscardableMemoryPool::lock(PoolDiscardableMemory* dm) {
SkASSERT(dm != nullptr);
SkAutoMutexAcquire autoMutexAcquire(fMutex);
if (nullptr == dm->fPointer) {
// May have been purged while waiting for lock.
#if SK_LAZY_CACHE_STATS
++fCacheMisses;
#endif // SK_LAZY_CACHE_STATS
return false;
}
dm->fLocked = true;
fList.remove(dm);
fList.addToHead(dm);
#if SK_LAZY_CACHE_STATS
++fCacheHits;
#endif // SK_LAZY_CACHE_STATS
return true;
}
void DiscardableMemoryPool::unlock(PoolDiscardableMemory* dm) {
SkASSERT(dm != nullptr);
SkAutoMutexAcquire autoMutexAcquire(fMutex);
dm->fLocked = false;
this->dumpDownTo(fBudget);
}
size_t DiscardableMemoryPool::getRAMUsed() {
return fUsed;
}
void DiscardableMemoryPool::setRAMBudget(size_t budget) {
SkAutoMutexAcquire autoMutexAcquire(fMutex);
fBudget = budget;
this->dumpDownTo(fBudget);
}
void DiscardableMemoryPool::dumpPool() {
SkAutoMutexAcquire autoMutexAcquire(fMutex);
this->dumpDownTo(0);
}
} // namespace
sk_sp<SkDiscardableMemoryPool> SkDiscardableMemoryPool::Make(size_t size) {
return sk_make_sp<DiscardableMemoryPool>(size);
}
SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
// Intentionally leak this global pool.
static SkDiscardableMemoryPool* global =
new DiscardableMemoryPool(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE);
return global;
}
|