image/qpixmapcache.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#define Q_TEST_QPIXMAPCACHE -
43#include "qpixmapcache.h" -
44#include "qobject.h" -
45#include "qdebug.h" -
46#include "qpixmapcache_p.h" -
47 -
48QT_BEGIN_NAMESPACE -
49 -
50/*! -
51 \class QPixmapCache -
52 \inmodule QtGui -
53 -
54 \brief The QPixmapCache class provides an application-wide cache for pixmaps. -
55 -
56 This class is a tool for optimized drawing with QPixmap. You can -
57 use it to store temporary pixmaps that are expensive to generate -
58 without using more storage space than cacheLimit(). Use insert() -
59 to insert pixmaps, find() to find them, and clear() to empty the -
60 cache. -
61 -
62 QPixmapCache contains no member data, only static functions to -
63 access the global pixmap cache. It creates an internal QCache -
64 object for caching the pixmaps. -
65 -
66 The cache associates a pixmap with a user-provided string as a key, -
67 or with a QPixmapCache::Key that the cache generates. -
68 Using QPixmapCache::Key for keys is faster than using strings. The string API is -
69 very convenient for complex keys but the QPixmapCache::Key API will be very -
70 efficient and convenient for a one-to-one object-to-pixmap mapping - in -
71 this case, you can store the keys as members of an object. -
72 -
73 If two pixmaps are inserted into the cache using equal keys then the -
74 last pixmap will replace the first pixmap in the cache. This follows the -
75 behavior of the QHash and QCache classes. -
76 -
77 The cache becomes full when the total size of all pixmaps in the -
78 cache exceeds cacheLimit(). The initial cache limit is 10240 KB (10 MB); -
79 you can change this by calling setCacheLimit() with the required value. -
80 A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of -
81 memory. -
82 -
83 The \e{Qt Quarterly} article -
84 \l{http://doc.qt.digia.com/qq/qq12-qpixmapcache.html}{Optimizing -
85 with QPixmapCache} explains how to use QPixmapCache to speed up -
86 applications by caching the results of painting. -
87 -
88 \sa QCache, QPixmap -
89*/ -
90 -
91static int cache_limit = 10240; // 10 MB cache limit -
92 -
93/*! -
94 \class QPixmapCache::Key -
95 \brief The QPixmapCache::Key class can be used for efficient access -
96 to the QPixmapCache. -
97 \inmodule QtGui -
98 \since 4.6 -
99 -
100 Use QPixmapCache::insert() to receive an instance of Key generated -
101 by the pixmap cache. You can store the key in your own objects for -
102 a very efficient one-to-one object-to-pixmap mapping. -
103*/ -
104 -
105/*! -
106 Constructs an empty Key object. -
107*/ -
108QPixmapCache::Key::Key() : d(0) -
109{ -
110}
executed: }
Execution Count:6224
6224
111 -
112/*! -
113 \internal -
114 Constructs a copy of \a other. -
115*/ -
116QPixmapCache::Key::Key(const Key &other) -
117{ -
118 if (other.d)
partially evaluated: other.d
TRUEFALSE
yes
Evaluation Count:16956
no
Evaluation Count:0
0-16956
119 ++(other.d->ref);
executed: ++(other.d->ref);
Execution Count:16956
16956
120 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
121}
executed: }
Execution Count:16956
16956
122 -
123/*! -
124 Destroys the key. -
125*/ -
126QPixmapCache::Key::~Key() -
127{ -
128 if (d && --(d->ref) == 0)
evaluated: d
TRUEFALSE
yes
Evaluation Count:20058
yes
Evaluation Count:3115
evaluated: --(d->ref) == 0
TRUEFALSE
yes
Evaluation Count:1544
yes
Evaluation Count:18514
1544-20058
129 delete d;
executed: delete d;
Execution Count:1544
1544
130}
executed: }
Execution Count:23173
23173
131 -
132/*! -
133 \internal -
134 -
135 Returns true if this key is the same as the given \a key; otherwise returns -
136 false. -
137*/ -
138bool QPixmapCache::Key::operator ==(const Key &key) const -
139{ -
140 return (d == key.d);
executed: return (d == key.d);
Execution Count:10964
10964
141} -
142 -
143/*! -
144 \fn bool QPixmapCache::Key::operator !=(const Key &key) const -
145 \internal -
146*/ -
147 -
148/*! -
149 \internal -
150*/ -
151QPixmapCache::Key &QPixmapCache::Key::operator =(const Key &other) -
152{ -
153 if (d != other.d) {
partially evaluated: d != other.d
TRUEFALSE
yes
Evaluation Count:1558
no
Evaluation Count:0
0-1558
154 if (other.d)
partially evaluated: other.d
TRUEFALSE
yes
Evaluation Count:1558
no
Evaluation Count:0
0-1558
155 ++(other.d->ref);
executed: ++(other.d->ref);
Execution Count:1558
1558
156 if (d && --(d->ref) == 0)
evaluated: d
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:1550
partially evaluated: --(d->ref) == 0
TRUEFALSE
yes
Evaluation Count:8
no
Evaluation Count:0
0-1550
157 delete d;
executed: delete d;
Execution Count:8
8
158 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
159 }
executed: }
Execution Count:1558
1558
160 return *this;
executed: return *this;
Execution Count:1558
1558
161} -
162 -
163class QPMCache : public QObject, public QCache<QPixmapCache::Key, QPixmapCacheEntry> -
164{ -
165 Q_OBJECT -
166public: -
167 QPMCache(); -
168 ~QPMCache(); -
169 -
170 void timerEvent(QTimerEvent *); -
171 bool insert(const QString& key, const QPixmap &pixmap, int cost); -
172 QPixmapCache::Key insert(const QPixmap &pixmap, int cost); -
173 bool replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost); -
174 bool remove(const QString &key); -
175 bool remove(const QPixmapCache::Key &key); -
176 -
177 void resizeKeyArray(int size); -
178 QPixmapCache::Key createKey(); -
179 void releaseKey(const QPixmapCache::Key &key); -
180 void clear(); -
181 -
182 QPixmap *object(const QString &key) const; -
183 QPixmap *object(const QPixmapCache::Key &key) const; -
184 -
185 static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key) -
186 {return key.d;}
executed: return key.d;
Execution Count:14112
14112
187 -
188 static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key); -
189 -
190 QList< QPair<QString,QPixmap> > allPixmaps() const; -
191 bool flushDetachedPixmaps(bool nt); -
192 -
193private: -
194 enum { soon_time = 10000, flush_time = 30000 }; -
195 int *keyArray; -
196 int theid; -
197 int ps; -
198 int keyArraySize; -
199 int freeKey; -
200 QHash<QString, QPixmapCache::Key> cacheKeys; -
201 bool t; -
202}; -
203 -
204QT_BEGIN_INCLUDE_NAMESPACE -
205#include "qpixmapcache.moc" -
206QT_END_INCLUDE_NAMESPACE -
207 -
208uint qHash(const QPixmapCache::Key &k) -
209{ -
210 return qHash(QPMCache::get(k)->key);
executed: return qHash(QPMCache::get(k)->key);
Execution Count:14112
14112
211} -
212 -
213QPMCache::QPMCache() -
214 : QObject(0), -
215 QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit * 1024), -
216 keyArray(0), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false) -
217{ -
218}
executed: }
Execution Count:185
185
219QPMCache::~QPMCache() -
220{ -
221 clear();
executed (the execution status of this line is deduced): clear();
-
222 free(keyArray);
executed (the execution status of this line is deduced): free(keyArray);
-
223}
executed: }
Execution Count:185
185
224 -
225/* -
226 This is supposed to cut the cache size down by about 25% in a -
227 minute once the application becomes idle, to let any inserted pixmap -
228 remain in the cache for some time before it becomes a candidate for -
229 cleaning-up, and to not cut down the size of the cache while the -
230 cache is in active use. -
231 -
232 When the last detached pixmap has been deleted from the cache, kill the -
233 timer so Qt won't keep the CPU from going into sleep mode. Currently -
234 the timer is not restarted when the pixmap becomes unused, but it does -
235 restart once something else is added (i.e. the cache space is actually needed). -
236 -
237 Returns true if any were removed. -
238*/ -
239bool QPMCache::flushDetachedPixmaps(bool nt) -
240{ -
241 int mc = maxCost();
executed (the execution status of this line is deduced): int mc = maxCost();
-
242 setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
executed (the execution status of this line is deduced): setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
-
243 setMaxCost(mc);
executed (the execution status of this line is deduced): setMaxCost(mc);
-
244 ps = totalCost();
executed (the execution status of this line is deduced): ps = totalCost();
-
245 -
246 bool any = false;
executed (the execution status of this line is deduced): bool any = false;
-
247 QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
executed (the execution status of this line is deduced): QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
-
248 while (it != cacheKeys.end()) {
evaluated: it != cacheKeys.end()
TRUEFALSE
yes
Evaluation Count:163
yes
Evaluation Count:6
6-163
249 if (!contains(it.value())) {
evaluated: !contains(it.value())
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:157
6-157
250 releaseKey(it.value());
executed (the execution status of this line is deduced): releaseKey(it.value());
-
251 it = cacheKeys.erase(it);
executed (the execution status of this line is deduced): it = cacheKeys.erase(it);
-
252 any = true;
executed (the execution status of this line is deduced): any = true;
-
253 } else {
executed: }
Execution Count:6
6
254 ++it;
executed (the execution status of this line is deduced): ++it;
-
255 }
executed: }
Execution Count:157
157
256 } -
257 -
258 return any;
executed: return any;
Execution Count:6
6
259} -
260 -
261void QPMCache::timerEvent(QTimerEvent *) -
262{ -
263 bool nt = totalCost() == ps;
executed (the execution status of this line is deduced): bool nt = totalCost() == ps;
-
264 if (!flushDetachedPixmaps(nt)) {
partially evaluated: !flushDetachedPixmaps(nt)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:6
0-6
265 killTimer(theid);
never executed (the execution status of this line is deduced): killTimer(theid);
-
266 theid = 0;
never executed (the execution status of this line is deduced): theid = 0;
-
267 } else if (nt != t) {
never executed: }
partially evaluated: nt != t
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:6
0-6
268 killTimer(theid);
never executed (the execution status of this line is deduced): killTimer(theid);
-
269 theid = startTimer(nt ? soon_time : flush_time);
never executed (the execution status of this line is deduced): theid = startTimer(nt ? soon_time : flush_time);
-
270 t = nt;
never executed (the execution status of this line is deduced): t = nt;
-
271 }
never executed: }
0
272} -
273 -
274 -
275QPixmap *QPMCache::object(const QString &key) const -
276{ -
277 QPixmapCache::Key cacheKey = cacheKeys.value(key);
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = cacheKeys.value(key);
-
278 if (!cacheKey.d || !cacheKey.d->isValid) {
evaluated: !cacheKey.d
TRUEFALSE
yes
Evaluation Count:1562
yes
Evaluation Count:10764
partially evaluated: !cacheKey.d->isValid
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:10764
0-10764
279 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
executed (the execution status of this line is deduced): const_cast<QPMCache *>(this)->cacheKeys.remove(key);
-
280 return 0;
executed: return 0;
Execution Count:1562
1562
281 } -
282 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey);
executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey);
-
283 //We didn't find the pixmap in the cache, the key is not valid anymore -
284 if (!ptr) {
partially evaluated: !ptr
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:10764
0-10764
285 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
never executed (the execution status of this line is deduced): const_cast<QPMCache *>(this)->cacheKeys.remove(key);
-
286 }
never executed: }
0
287 return ptr;
executed: return ptr;
Execution Count:10764
10764
288} -
289 -
290QPixmap *QPMCache::object(const QPixmapCache::Key &key) const -
291{ -
292 Q_ASSERT(key.d->isValid);
executed (the execution status of this line is deduced): qt_noop();
-
293 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key);
-
294 //We didn't find the pixmap in the cache, the key is not valid anymore -
295 if (!ptr)
partially evaluated: !ptr
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
296 const_cast<QPMCache *>(this)->releaseKey(key);
never executed: const_cast<QPMCache *>(this)->releaseKey(key);
0
297 return ptr;
executed: return ptr;
Execution Count:9
9
298} -
299 -
300bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) -
301{ -
302 QPixmapCache::Key cacheKey;
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey;
-
303 QPixmapCache::Key oldCacheKey = cacheKeys.value(key);
executed (the execution status of this line is deduced): QPixmapCache::Key oldCacheKey = cacheKeys.value(key);
-
304 //If for the same key we add already a pixmap we should delete it -
305 if (oldCacheKey.d) {
partially evaluated: oldCacheKey.d
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1538
0-1538
306 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey);
never executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey);
-
307 cacheKeys.remove(key);
never executed (the execution status of this line is deduced): cacheKeys.remove(key);
-
308 }
never executed: }
0
309 -
310 //we create a new key the old one has been removed -
311 cacheKey = createKey();
executed (the execution status of this line is deduced): cacheKey = createKey();
-
312 -
313 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
314 if (success) {
partially evaluated: success
TRUEFALSE
yes
Evaluation Count:1538
no
Evaluation Count:0
0-1538
315 cacheKeys.insert(key, cacheKey);
executed (the execution status of this line is deduced): cacheKeys.insert(key, cacheKey);
-
316 if (!theid) {
evaluated: !theid
TRUEFALSE
yes
Evaluation Count:46
yes
Evaluation Count:1492
46-1492
317 theid = startTimer(flush_time);
executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
318 t = false;
executed (the execution status of this line is deduced): t = false;
-
319 }
executed: }
Execution Count:46
46
320 } else {
executed: }
Execution Count:1538
1538
321 //Insertion failed we released the new allocated key -
322 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
323 }
never executed: }
0
324 return success;
executed: return success;
Execution Count:1538
1538
325} -
326 -
327QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost) -
328{ -
329 QPixmapCache::Key cacheKey = createKey();
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = createKey();
-
330 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
331 if (success) {
partially evaluated: success
TRUEFALSE
yes
Evaluation Count:20
no
Evaluation Count:0
0-20
332 if (!theid) {
evaluated: !theid
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:18
2-18
333 theid = startTimer(flush_time);
executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
334 t = false;
executed (the execution status of this line is deduced): t = false;
-
335 }
executed: }
Execution Count:2
2
336 } else {
executed: }
Execution Count:20
20
337 //Insertion failed we released the key and return an invalid one -
338 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
339 }
never executed: }
0
340 return cacheKey;
executed: return cacheKey;
Execution Count:20
20
341} -
342 -
343bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost) -
344{ -
345 Q_ASSERT(key.d->isValid);
never executed (the execution status of this line is deduced): qt_noop();
-
346 //If for the same key we had already an entry so we should delete the pixmap and use the new one -
347 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
never executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
-
348 -
349 QPixmapCache::Key cacheKey = createKey();
never executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = createKey();
-
350 -
351 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
never executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
352 if (success) {
never evaluated: success
0
353 if(!theid) {
never evaluated: !theid
0
354 theid = startTimer(flush_time);
never executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
355 t = false;
never executed (the execution status of this line is deduced): t = false;
-
356 }
never executed: }
0
357 const_cast<QPixmapCache::Key&>(key) = cacheKey;
never executed (the execution status of this line is deduced): const_cast<QPixmapCache::Key&>(key) = cacheKey;
-
358 } else {
never executed: }
0
359 //Insertion failed we released the key -
360 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
361 }
never executed: }
0
362 return success;
never executed: return success;
0
363} -
364 -
365bool QPMCache::remove(const QString &key) -
366{ -
367 QPixmapCache::Key cacheKey = cacheKeys.value(key);
never executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = cacheKeys.value(key);
-
368 //The key was not in the cache -
369 if (!cacheKey.d)
never evaluated: !cacheKey.d
0
370 return false;
never executed: return false;
0
371 cacheKeys.remove(key);
never executed (the execution status of this line is deduced): cacheKeys.remove(key);
-
372 return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey);
never executed: return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey);
0
373} -
374 -
375bool QPMCache::remove(const QPixmapCache::Key &key) -
376{ -
377 return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
executed: return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
Execution Count:14
14
378} -
379 -
380void QPMCache::resizeKeyArray(int size) -
381{ -
382 if (size <= keyArraySize || size == 0)
partially evaluated: size <= keyArraySize
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:166
partially evaluated: size == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:166
0-166
383 return;
never executed: return;
0
384 keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray,
executed (the execution status of this line is deduced): keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray,
-
385 size * sizeof(int))));
executed (the execution status of this line is deduced): size * sizeof(int))));
-
386 for (int i = keyArraySize; i != size; ++i)
evaluated: i != size
TRUEFALSE
yes
Evaluation Count:1994
yes
Evaluation Count:166
166-1994
387 keyArray[i] = i + 1;
executed: keyArray[i] = i + 1;
Execution Count:1994
1994
388 keyArraySize = size;
executed (the execution status of this line is deduced): keyArraySize = size;
-
389}
executed: }
Execution Count:166
166
390 -
391QPixmapCache::Key QPMCache::createKey() -
392{ -
393 if (freeKey == keyArraySize)
evaluated: freeKey == keyArraySize
TRUEFALSE
yes
Evaluation Count:166
yes
Evaluation Count:1392
166-1392
394 resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2);
executed: resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2);
Execution Count:166
166
395 int id = freeKey;
executed (the execution status of this line is deduced): int id = freeKey;
-
396 freeKey = keyArray[id];
executed (the execution status of this line is deduced): freeKey = keyArray[id];
-
397 QPixmapCache::Key key;
executed (the execution status of this line is deduced): QPixmapCache::Key key;
-
398 QPixmapCache::KeyData *d = QPMCache::getKeyData(&key);
executed (the execution status of this line is deduced): QPixmapCache::KeyData *d = QPMCache::getKeyData(&key);
-
399 d->key = ++id;
executed (the execution status of this line is deduced): d->key = ++id;
-
400 return key;
executed: return key;
Execution Count:1558
1558
401} -
402 -
403void QPMCache::releaseKey(const QPixmapCache::Key &key) -
404{ -
405 if (key.d->key > keyArraySize || key.d->key <= 0)
evaluated: key.d->key > keyArraySize
TRUEFALSE
yes
Evaluation Count:1538
yes
Evaluation Count:26
evaluated: key.d->key <= 0
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:20
6-1538
406 return;
executed: return;
Execution Count:1544
1544
407 key.d->key--;
executed (the execution status of this line is deduced): key.d->key--;
-
408 keyArray[key.d->key] = freeKey;
executed (the execution status of this line is deduced): keyArray[key.d->key] = freeKey;
-
409 freeKey = key.d->key;
executed (the execution status of this line is deduced): freeKey = key.d->key;
-
410 key.d->isValid = false;
executed (the execution status of this line is deduced): key.d->isValid = false;
-
411 key.d->key = 0;
executed (the execution status of this line is deduced): key.d->key = 0;
-
412}
executed: }
Execution Count:20
20
413 -
414void QPMCache::clear() -
415{ -
416 free(keyArray);
executed (the execution status of this line is deduced): free(keyArray);
-
417 keyArray = 0;
executed (the execution status of this line is deduced): keyArray = 0;
-
418 freeKey = 0;
executed (the execution status of this line is deduced): freeKey = 0;
-
419 keyArraySize = 0;
executed (the execution status of this line is deduced): keyArraySize = 0;
-
420 //Mark all keys as invalid -
421 QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys();
executed (the execution status of this line is deduced): QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys();
-
422 for (int i = 0; i < keys.size(); ++i)
evaluated: i < keys.size()
TRUEFALSE
yes
Evaluation Count:1538
yes
Evaluation Count:685
685-1538
423 keys.at(i).d->isValid = false;
executed: keys.at(i).d->isValid = false;
Execution Count:1538
1538
424 QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear();
executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear();
-
425}
executed: }
Execution Count:685
685
426 -
427QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key) -
428{ -
429 if (!key->d)
partially evaluated: !key->d
TRUEFALSE
yes
Evaluation Count:1558
no
Evaluation Count:0
0-1558
430 key->d = new QPixmapCache::KeyData;
executed: key->d = new QPixmapCache::KeyData;
Execution Count:1558
1558
431 return key->d;
executed: return key->d;
Execution Count:1558
1558
432} -
433 -
434QList< QPair<QString,QPixmap> > QPMCache::allPixmaps() const -
435{ -
436 QList< QPair<QString,QPixmap> > r;
never executed (the execution status of this line is deduced): QList< QPair<QString,QPixmap> > r;
-
437 QHash<QString, QPixmapCache::Key>::const_iterator it = cacheKeys.begin();
never executed (the execution status of this line is deduced): QHash<QString, QPixmapCache::Key>::const_iterator it = cacheKeys.begin();
-
438 while (it != cacheKeys.end()) {
never evaluated: it != cacheKeys.end()
0
439 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(it.value());
never executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(it.value());
-
440 if (ptr)
never evaluated: ptr
0
441 r.append(QPair<QString,QPixmap>(it.key(),*ptr));
never executed: r.append(QPair<QString,QPixmap>(it.key(),*ptr));
0
442 ++it;
never executed (the execution status of this line is deduced): ++it;
-
443 }
never executed: }
0
444 return r;
never executed: return r;
0
445} -
446 -
447 -
448Q_GLOBAL_STATIC(QPMCache, pm_cache)
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:15965
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:185
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:185
yes
Evaluation Count:15780
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:185
no
Evaluation Count:0
0-15965
449 -
450int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize() -
451{ -
452 return pm_cache()->size();
never executed: return pm_cache()->size();
0
453} -
454 -
455QPixmapCacheEntry::~QPixmapCacheEntry() -
456{ -
457 pm_cache()->releaseKey(key);
executed (the execution status of this line is deduced): pm_cache()->releaseKey(key);
-
458}
executed: }
Execution Count:1558
1558
459 -
460/*! -
461 \obsolete -
462 \overload -
463 -
464 Returns the pixmap associated with the \a key in the cache, or -
465 null if there is no such pixmap. -
466 -
467 \warning If valid, you should copy the pixmap immediately (this is -
468 fast). Subsequent insertions into the cache could cause the -
469 pointer to become invalid. For this reason, we recommend you use -
470 bool find(const QString&, QPixmap*) instead. -
471 -
472 Example: -
473 \snippet code/src_gui_image_qpixmapcache.cpp 0 -
474*/ -
475 -
476QPixmap *QPixmapCache::find(const QString &key) -
477{ -
478 return pm_cache()->object(key);
executed: return pm_cache()->object(key);
Execution Count:6
6
479} -
480 -
481 -
482/*! -
483 \obsolete -
484 -
485 Use bool find(const QString&, QPixmap*) instead. -
486*/ -
487 -
488bool QPixmapCache::find(const QString &key, QPixmap& pixmap) -
489{ -
490 return find(key, &pixmap);
executed: return find(key, &pixmap);
Execution Count:10614
10614
491} -
492 -
493/*! -
494 Looks for a cached pixmap associated with the given \a key in the cache. -
495 If the pixmap is found, the function sets \a pixmap to that pixmap and -
496 returns true; otherwise it leaves \a pixmap alone and returns false. -
497 -
498 \since 4.6 -
499 -
500 Example: -
501 \snippet code/src_gui_image_qpixmapcache.cpp 1 -
502*/ -
503 -
504bool QPixmapCache::find(const QString &key, QPixmap* pixmap) -
505{ -
506 QPixmap *ptr = pm_cache()->object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = pm_cache()->object(key);
-
507 if (ptr && pixmap)
evaluated: ptr
TRUEFALSE
yes
Evaluation Count:10761
yes
Evaluation Count:1559
partially evaluated: pixmap
TRUEFALSE
yes
Evaluation Count:10761
no
Evaluation Count:0
0-10761
508 *pixmap = *ptr;
executed: *pixmap = *ptr;
Execution Count:10761
10761
509 return ptr != 0;
executed: return ptr != 0;
Execution Count:12320
12320
510} -
511 -
512/*! -
513 Looks for a cached pixmap associated with the given \a key in the cache. -
514 If the pixmap is found, the function sets \a pixmap to that pixmap and -
515 returns true; otherwise it leaves \a pixmap alone and returns false. If -
516 the pixmap is not found, it means that the \a key is no longer valid, -
517 so it will be released for the next insertion. -
518 -
519 \since 4.6 -
520*/ -
521bool QPixmapCache::find(const Key &key, QPixmap* pixmap) -
522{ -
523 //The key is not valid anymore, a flush happened before probably -
524 if (!key.d || !key.d->isValid)
evaluated: !key.d
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:13
evaluated: !key.d->isValid
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:9
4-15
525 return false;
executed: return false;
Execution Count:19
19
526 QPixmap *ptr = pm_cache()->object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = pm_cache()->object(key);
-
527 if (ptr && pixmap)
partially evaluated: ptr
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
partially evaluated: pixmap
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-9
528 *pixmap = *ptr;
executed: *pixmap = *ptr;
Execution Count:9
9
529 return ptr != 0;
executed: return ptr != 0;
Execution Count:9
9
530} -
531 -
532/*! -
533 Inserts a copy of the pixmap \a pixmap associated with the \a key into -
534 the cache. -
535 -
536 All pixmaps inserted by the Qt library have a key starting with -
537 "$qt", so your own pixmap keys should never begin "$qt". -
538 -
539 When a pixmap is inserted and the cache is about to exceed its -
540 limit, it removes pixmaps until there is enough room for the -
541 pixmap to be inserted. -
542 -
543 The oldest pixmaps (least recently accessed in the cache) are -
544 deleted when more space is needed. -
545 -
546 The function returns true if the object was inserted into the -
547 cache; otherwise it returns false. -
548 -
549 \sa setCacheLimit() -
550*/ -
551 -
552bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) -
553{ -
554 return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
executed: return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
Execution Count:1538
1538
555} -
556 -
557/*! -
558 Inserts a copy of the given \a pixmap into the cache and returns a key -
559 that can be used to retrieve it. -
560 -
561 When a pixmap is inserted and the cache is about to exceed its -
562 limit, it removes pixmaps until there is enough room for the -
563 pixmap to be inserted. -
564 -
565 The oldest pixmaps (least recently accessed in the cache) are -
566 deleted when more space is needed. -
567 -
568 \sa setCacheLimit(), replace() -
569 -
570 \since 4.6 -
571*/ -
572QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap) -
573{ -
574 return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
executed: return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
Execution Count:20
20
575} -
576 -
577/*! -
578 Replaces the pixmap associated with the given \a key with the \a pixmap -
579 specified. Returns true if the \a pixmap has been correctly inserted into -
580 the cache; otherwise returns false. -
581 -
582 \sa setCacheLimit(), insert() -
583 -
584 \since 4.6 -
585*/ -
586bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) -
587{ -
588 //The key is not valid anymore, a flush happened before probably -
589 if (!key.d || !key.d->isValid)
never evaluated: !key.d
never evaluated: !key.d->isValid
0
590 return false;
never executed: return false;
0
591 return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
never executed: return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
0
592} -
593 -
594/*! -
595 Returns the cache limit (in kilobytes). -
596 -
597 The default cache limit is 10240 KB. -
598 -
599 \sa setCacheLimit() -
600*/ -
601 -
602int QPixmapCache::cacheLimit() -
603{ -
604 return cache_limit;
executed: return cache_limit;
Execution Count:3
3
605} -
606 -
607/*! -
608 Sets the cache limit to \a n kilobytes. -
609 -
610 The default setting is 10240 KB. -
611 -
612 \sa cacheLimit() -
613*/ -
614 -
615void QPixmapCache::setCacheLimit(int n) -
616{ -
617 cache_limit = n;
never executed (the execution status of this line is deduced): cache_limit = n;
-
618 pm_cache()->setMaxCost(1024 * cache_limit);
never executed (the execution status of this line is deduced): pm_cache()->setMaxCost(1024 * cache_limit);
-
619}
never executed: }
0
620 -
621/*! -
622 Removes the pixmap associated with \a key from the cache. -
623*/ -
624void QPixmapCache::remove(const QString &key) -
625{ -
626 pm_cache()->remove(key);
never executed (the execution status of this line is deduced): pm_cache()->remove(key);
-
627}
never executed: }
0
628 -
629/*! -
630 Removes the pixmap associated with \a key from the cache and releases -
631 the key for a future insertion. -
632 -
633 \since 4.6 -
634*/ -
635void QPixmapCache::remove(const Key &key) -
636{ -
637 //The key is not valid anymore, a flush happened before probably -
638 if (!key.d || !key.d->isValid)
evaluated: !key.d
TRUEFALSE
yes
Evaluation Count:115
yes
Evaluation Count:28
evaluated: !key.d->isValid
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:14
14-115
639 return;
executed: return;
Execution Count:129
129
640 pm_cache()->remove(key);
executed (the execution status of this line is deduced): pm_cache()->remove(key);
-
641}
executed: }
Execution Count:14
14
642 -
643/*! -
644 Removes all pixmaps from the cache. -
645*/ -
646 -
647void QPixmapCache::clear() -
648{ -
649 QT_TRY {
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:500
no
Evaluation Count:0
0-500
650 pm_cache()->clear();
executed (the execution status of this line is deduced): pm_cache()->clear();
-
651 } QT_CATCH(const std::bad_alloc &) {
executed: }
Execution Count:500
500
652 // if we ran out of memory during pm_cache(), it's no leak, -
653 // so just ignore it. -
654 }
never executed: }
0
655} -
656 -
657void QPixmapCache::flushDetachedPixmaps() -
658{ -
659 pm_cache()->flushDetachedPixmaps(true);
never executed (the execution status of this line is deduced): pm_cache()->flushDetachedPixmaps(true);
-
660}
never executed: }
0
661 -
662int QPixmapCache::totalUsed() -
663{ -
664 return (pm_cache()->totalCost()+1023) / 1024;
never executed: return (pm_cache()->totalCost()+1023) / 1024;
0
665} -
666 -
667QList< QPair<QString,QPixmap> > QPixmapCache::allPixmaps() -
668{ -
669 return pm_cache()->allPixmaps();
never executed: return pm_cache()->allPixmaps();
0
670} -
671 -
672QT_END_NAMESPACE -
673 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial