| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkaccesscache.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | - | |||||||||||||
| 3 | - | |||||||||||||
| 4 | - | |||||||||||||
| 5 | - | |||||||||||||
| 6 | enum ExpiryTimeEnum { | - | ||||||||||||
| 7 | ExpiryTime = 120 | - | ||||||||||||
| 8 | }; | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | namespace { | - | ||||||||||||
| 11 | struct Receiver | - | ||||||||||||
| 12 | { | - | ||||||||||||
| 13 | QPointer<QObject> object; | - | ||||||||||||
| 14 | const char *member; | - | ||||||||||||
| 15 | }; | - | ||||||||||||
| 16 | } | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | - | |||||||||||||
| 19 | struct QNetworkAccessCache::Node | - | ||||||||||||
| 20 | { | - | ||||||||||||
| 21 | QDateTime timestamp; | - | ||||||||||||
| 22 | QQueue<Receiver> receiverQueue; | - | ||||||||||||
| 23 | QByteArray key; | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | Node *older, *newer; | - | ||||||||||||
| 26 | CacheableObject *object; | - | ||||||||||||
| 27 | - | |||||||||||||
| 28 | int useCount; | - | ||||||||||||
| 29 | - | |||||||||||||
| 30 | Node() | - | ||||||||||||
| 31 | : older(0), newer(0), object(0), useCount(0) | - | ||||||||||||
| 32 | { } executed 905 times by 8 tests: end of blockExecuted by:
| 905 | ||||||||||||
| 33 | }; | - | ||||||||||||
| 34 | - | |||||||||||||
| 35 | QNetworkAccessCache::CacheableObject::CacheableObject() | - | ||||||||||||
| 36 | { | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | - | |||||||||||||
| 39 | } | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | QNetworkAccessCache::CacheableObject::~CacheableObject() | - | ||||||||||||
| 42 | { | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | - | |||||||||||||
| 45 | - | |||||||||||||
| 46 | - | |||||||||||||
| 47 | - | |||||||||||||
| 48 | } | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | void QNetworkAccessCache::CacheableObject::setExpires(bool enable) | - | ||||||||||||
| 51 | { | - | ||||||||||||
| 52 | expires = enable; | - | ||||||||||||
| 53 | } executed 914 times by 8 tests: end of blockExecuted by:
| 914 | ||||||||||||
| 54 | - | |||||||||||||
| 55 | void QNetworkAccessCache::CacheableObject::setShareable(bool enable) | - | ||||||||||||
| 56 | { | - | ||||||||||||
| 57 | shareable = enable; | - | ||||||||||||
| 58 | } executed 914 times by 8 tests: end of blockExecuted by:
| 914 | ||||||||||||
| 59 | - | |||||||||||||
| 60 | QNetworkAccessCache::QNetworkAccessCache() | - | ||||||||||||
| 61 | : oldest(0), newest(0) | - | ||||||||||||
| 62 | { | - | ||||||||||||
| 63 | } executed 722 times by 11 tests: end of blockExecuted by:
| 722 | ||||||||||||
| 64 | - | |||||||||||||
| 65 | QNetworkAccessCache::~QNetworkAccessCache() | - | ||||||||||||
| 66 | { | - | ||||||||||||
| 67 | clear(); | - | ||||||||||||
| 68 | } executed 723 times by 12 tests: end of blockExecuted by:
| 723 | ||||||||||||
| 69 | - | |||||||||||||
| 70 | void QNetworkAccessCache::clear() | - | ||||||||||||
| 71 | { | - | ||||||||||||
| 72 | NodeHash hashCopy = hash; | - | ||||||||||||
| 73 | hash.clear(); | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | - | |||||||||||||
| 76 | NodeHash::Iterator it = hashCopy.begin(); | - | ||||||||||||
| 77 | NodeHash::Iterator end = hashCopy.end(); | - | ||||||||||||
| 78 | for ( ; it != end
| 895-2015 | ||||||||||||
| 79 | it->object->key.clear(); | - | ||||||||||||
| 80 | it->object->dispose(); | - | ||||||||||||
| 81 | } executed 895 times by 9 tests: end of blockExecuted by:
| 895 | ||||||||||||
| 82 | - | |||||||||||||
| 83 | - | |||||||||||||
| 84 | hashCopy.clear(); | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | timer.stop(); | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | oldest = newest = 0; | - | ||||||||||||
| 89 | } executed 2015 times by 12 tests: end of blockExecuted by:
| 2015 | ||||||||||||
| 90 | - | |||||||||||||
| 91 | - | |||||||||||||
| 92 | - | |||||||||||||
| 93 | - | |||||||||||||
| 94 | - | |||||||||||||
| 95 | void QNetworkAccessCache::linkEntry(const QByteArray &key) | - | ||||||||||||
| 96 | { | - | ||||||||||||
| 97 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 98 | if (it == hash.end()
| 0-628 | ||||||||||||
| 99 | return; never executed: return; | 0 | ||||||||||||
| 100 | - | |||||||||||||
| 101 | Node *const node = &it.value(); | - | ||||||||||||
| 102 | ((!(node != oldest && node != newest)) ? qt_assert("node != oldest && node != newest",__FILE__,140) : qt_noop()); | - | ||||||||||||
| 103 | ((!(node->older == 0 && node->newer == 0)) ? qt_assert("node->older == 0 && node->newer == 0",__FILE__,141) : qt_noop()); | - | ||||||||||||
| 104 | ((!(node->useCount == 0)) ? qt_assert("node->useCount == 0",__FILE__,142) : qt_noop()); | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | if (newest
| 20-608 | ||||||||||||
| 107 | ((!(newest->newer == 0)) ? qt_assert("newest->newer == 0",__FILE__,145) : qt_noop()); | - | ||||||||||||
| 108 | newest->newer = node; | - | ||||||||||||
| 109 | node->older = newest; | - | ||||||||||||
| 110 | } executed 20 times by 2 tests: end of blockExecuted by:
| 20 | ||||||||||||
| 111 | if (!oldest
| 20-608 | ||||||||||||
| 112 | - | |||||||||||||
| 113 | oldest = node; | - | ||||||||||||
| 114 | } executed 608 times by 8 tests: end of blockExecuted by:
| 608 | ||||||||||||
| 115 | - | |||||||||||||
| 116 | node->timestamp = QDateTime::currentDateTimeUtc().addSecs(ExpiryTime); | - | ||||||||||||
| 117 | newest = node; | - | ||||||||||||
| 118 | } executed 628 times by 8 tests: end of blockExecuted by:
| 628 | ||||||||||||
| 119 | - | |||||||||||||
| 120 | - | |||||||||||||
| 121 | - | |||||||||||||
| 122 | - | |||||||||||||
| 123 | - | |||||||||||||
| 124 | bool QNetworkAccessCache::unlinkEntry(const QByteArray &key) | - | ||||||||||||
| 125 | { | - | ||||||||||||
| 126 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 127 | if (it == hash.end()
| 59-905 | ||||||||||||
| 128 | return executed 905 times by 8 tests: false;return false;Executed by:
executed 905 times by 8 tests: return false;Executed by:
| 905 | ||||||||||||
| 129 | - | |||||||||||||
| 130 | Node *const node = &it.value(); | - | ||||||||||||
| 131 | - | |||||||||||||
| 132 | bool wasOldest = false; | - | ||||||||||||
| 133 | if (node == oldest
| 29-30 | ||||||||||||
| 134 | oldest = node->newer; | - | ||||||||||||
| 135 | wasOldest = true; | - | ||||||||||||
| 136 | } executed 30 times by 3 tests: end of blockExecuted by:
| 30 | ||||||||||||
| 137 | if (node == newest
| 24-35 | ||||||||||||
| 138 | newest = node->older; executed 35 times by 3 tests: newest = node->older;Executed by:
| 35 | ||||||||||||
| 139 | if (node->older
| 11-48 | ||||||||||||
| 140 | node->older->newer = node->newer; executed 11 times by 1 test: node->older->newer = node->newer;Executed by:
| 11 | ||||||||||||
| 141 | if (node->newer
| 6-53 | ||||||||||||
| 142 | node->newer->older = node->older; executed 6 times by 1 test: node->newer->older = node->older;Executed by:
| 6 | ||||||||||||
| 143 | - | |||||||||||||
| 144 | node->newer = node->older = 0; | - | ||||||||||||
| 145 | return executed 59 times by 3 tests: wasOldest;return wasOldest;Executed by:
executed 59 times by 3 tests: return wasOldest;Executed by:
| 59 | ||||||||||||
| 146 | } | - | ||||||||||||
| 147 | - | |||||||||||||
| 148 | void QNetworkAccessCache::updateTimer() | - | ||||||||||||
| 149 | { | - | ||||||||||||
| 150 | timer.stop(); | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | if (!oldest
| 30-608 | ||||||||||||
| 153 | return; executed 30 times by 3 tests: return;Executed by:
| 30 | ||||||||||||
| 154 | - | |||||||||||||
| 155 | int interval = QDateTime::currentDateTimeUtc().secsTo(oldest->timestamp); | - | ||||||||||||
| 156 | if (interval <= 0
| 0-608 | ||||||||||||
| 157 | interval = 0; | - | ||||||||||||
| 158 | } never executed: else {end of block | 0 | ||||||||||||
| 159 | - | |||||||||||||
| 160 | interval = (interval + 15) & ~16; | - | ||||||||||||
| 161 | } executed 608 times by 8 tests: end of blockExecuted by:
| 608 | ||||||||||||
| 162 | - | |||||||||||||
| 163 | timer.start(interval * 1000, this); | - | ||||||||||||
| 164 | } executed 608 times by 8 tests: end of blockExecuted by:
| 608 | ||||||||||||
| 165 | - | |||||||||||||
| 166 | bool QNetworkAccessCache::emitEntryReady(Node *node, QObject *target, const char *member) | - | ||||||||||||
| 167 | { | - | ||||||||||||
| 168 | if (!connect(this, qFlagLocation("2""entryReady(QNetworkAccessCache::CacheableObject*)" "\0" __FILE__ ":" "206"),
| 0-1 | ||||||||||||
| 169 | target, member, Qt::QueuedConnection)
| 0-1 | ||||||||||||
| 170 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||
| 171 | - | |||||||||||||
| 172 | entryReady(node->object); | - | ||||||||||||
| 173 | disconnect(qFlagLocation("2""entryReady(QNetworkAccessCache::CacheableObject*)" "\0" __FILE__ ":" "211")); | - | ||||||||||||
| 174 | - | |||||||||||||
| 175 | return executed 1 time by 1 test: true;return true;Executed by:
executed 1 time by 1 test: return true;Executed by:
| 1 | ||||||||||||
| 176 | } | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | void QNetworkAccessCache::timerEvent(QTimerEvent *) | - | ||||||||||||
| 179 | { | - | ||||||||||||
| 180 | - | |||||||||||||
| 181 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||
| 182 | - | |||||||||||||
| 183 | while (oldest
| 0 | ||||||||||||
| 184 | Node *next = oldest->newer; | - | ||||||||||||
| 185 | oldest->object->dispose(); | - | ||||||||||||
| 186 | - | |||||||||||||
| 187 | hash.remove(oldest->key); | - | ||||||||||||
| 188 | oldest = next; | - | ||||||||||||
| 189 | } never executed: end of block | 0 | ||||||||||||
| 190 | - | |||||||||||||
| 191 | - | |||||||||||||
| 192 | if (oldest
| 0 | ||||||||||||
| 193 | oldest->older = 0; never executed: oldest->older = 0; | 0 | ||||||||||||
| 194 | else | - | ||||||||||||
| 195 | newest = 0; never executed: newest = 0; | 0 | ||||||||||||
| 196 | - | |||||||||||||
| 197 | updateTimer(); | - | ||||||||||||
| 198 | } never executed: end of block | 0 | ||||||||||||
| 199 | - | |||||||||||||
| 200 | void QNetworkAccessCache::addEntry(const QByteArray &key, CacheableObject *entry) | - | ||||||||||||
| 201 | { | - | ||||||||||||
| 202 | ((!(!key.isEmpty())) ? qt_assert("!key.isEmpty()",__FILE__,240) : qt_noop()); | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | if (unlinkEntry(key)
| 0-914 | ||||||||||||
| 205 | updateTimer(); never executed: updateTimer(); | 0 | ||||||||||||
| 206 | - | |||||||||||||
| 207 | Node &node = hash[key]; | - | ||||||||||||
| 208 | if (node.useCount
| 9-905 | ||||||||||||
| 209 | QMessageLogger(__FILE__, 247, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'", executed 9 times by 1 test: QMessageLogger(__FILE__, 247, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'", key.constData());Executed by:
| 9 | ||||||||||||
| 210 | key.constData()); executed 9 times by 1 test: QMessageLogger(__FILE__, 247, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'", key.constData());Executed by:
| 9 | ||||||||||||
| 211 | if (node.object
| 9-905 | ||||||||||||
| 212 | node.object->dispose(); executed 9 times by 1 test: node.object->dispose();Executed by:
| 9 | ||||||||||||
| 213 | node.object = entry; | - | ||||||||||||
| 214 | node.object->key = key; | - | ||||||||||||
| 215 | node.key = key; | - | ||||||||||||
| 216 | node.useCount = 1; | - | ||||||||||||
| 217 | } executed 914 times by 8 tests: end of blockExecuted by:
| 914 | ||||||||||||
| 218 | - | |||||||||||||
| 219 | bool QNetworkAccessCache::hasEntry(const QByteArray &key) const | - | ||||||||||||
| 220 | { | - | ||||||||||||
| 221 | return executed 997 times by 4 tests: hash.contains(key);return hash.contains(key);Executed by:
executed 997 times by 4 tests: return hash.contains(key);Executed by:
| 997 | ||||||||||||
| 222 | } | - | ||||||||||||
| 223 | - | |||||||||||||
| 224 | bool QNetworkAccessCache::requestEntry(const QByteArray &key, QObject *target, const char *member) | - | ||||||||||||
| 225 | { | - | ||||||||||||
| 226 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 227 | if (it == hash.end()
| 1-48 | ||||||||||||
| 228 | return executed 48 times by 1 test: false;return false;Executed by:
executed 48 times by 1 test: return false;Executed by:
| 48 | ||||||||||||
| 229 | - | |||||||||||||
| 230 | Node *node = &it.value(); | - | ||||||||||||
| 231 | - | |||||||||||||
| 232 | if (node->useCount > 0
| 0-1 | ||||||||||||
| 233 | - | |||||||||||||
| 234 | - | |||||||||||||
| 235 | ((!(node->older == 0 && node->newer == 0)) ? qt_assert("node->older == 0 && node->newer == 0",__FILE__,273) : qt_noop()); | - | ||||||||||||
| 236 | Receiver receiver; | - | ||||||||||||
| 237 | receiver.object = target; | - | ||||||||||||
| 238 | receiver.member = member; | - | ||||||||||||
| 239 | node->receiverQueue.enqueue(receiver); | - | ||||||||||||
| 240 | - | |||||||||||||
| 241 | - | |||||||||||||
| 242 | return executed 1 time by 1 test: true;return true;Executed by:
executed 1 time by 1 test: return true;Executed by:
| 1 | ||||||||||||
| 243 | } else { | - | ||||||||||||
| 244 | - | |||||||||||||
| 245 | if (unlinkEntry(key)
| 0 | ||||||||||||
| 246 | updateTimer(); never executed: updateTimer(); | 0 | ||||||||||||
| 247 | - | |||||||||||||
| 248 | ++node->useCount; | - | ||||||||||||
| 249 | return never executed: emitEntryReady(node, target, member);return emitEntryReady(node, target, member);never executed: return emitEntryReady(node, target, member); | 0 | ||||||||||||
| 250 | } | - | ||||||||||||
| 251 | } | - | ||||||||||||
| 252 | - | |||||||||||||
| 253 | QNetworkAccessCache::CacheableObject *QNetworkAccessCache::requestEntryNow(const QByteArray &key) | - | ||||||||||||
| 254 | { | - | ||||||||||||
| 255 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 256 | if (it == hash.end()
| 551-718 | ||||||||||||
| 257 | return executed 551 times by 8 tests: 0;return 0;Executed by:
executed 551 times by 8 tests: return 0;Executed by:
| 551 | ||||||||||||
| 258 | if (it->useCount > 0
| 41-677 | ||||||||||||
| 259 | if (it->object->shareable
| 0-677 | ||||||||||||
| 260 | ++it->useCount; | - | ||||||||||||
| 261 | return executed 677 times by 3 tests: it->object;return it->object;Executed by:
executed 677 times by 3 tests: return it->object;Executed by:
| 677 | ||||||||||||
| 262 | } | - | ||||||||||||
| 263 | - | |||||||||||||
| 264 | - | |||||||||||||
| 265 | return never executed: 0;return 0;never executed: return 0; | 0 | ||||||||||||
| 266 | } | - | ||||||||||||
| 267 | - | |||||||||||||
| 268 | - | |||||||||||||
| 269 | bool wasOldest = unlinkEntry(key); | - | ||||||||||||
| 270 | ++it->useCount; | - | ||||||||||||
| 271 | - | |||||||||||||
| 272 | if (wasOldest
| 11-30 | ||||||||||||
| 273 | updateTimer(); executed 30 times by 3 tests: updateTimer();Executed by:
| 30 | ||||||||||||
| 274 | return executed 41 times by 3 tests: it->object;return it->object;Executed by:
executed 41 times by 3 tests: return it->object;Executed by:
| 41 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | - | |||||||||||||
| 277 | void QNetworkAccessCache::releaseEntry(const QByteArray &key) | - | ||||||||||||
| 278 | { | - | ||||||||||||
| 279 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 280 | if (it == hash.end()
| 0-1306 | ||||||||||||
| 281 | QMessageLogger(__FILE__, 319, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::releaseEntry: trying to release key '%s' that is not in cache", | - | ||||||||||||
| 282 | key.constData()); | - | ||||||||||||
| 283 | return; never executed: return; | 0 | ||||||||||||
| 284 | } | - | ||||||||||||
| 285 | - | |||||||||||||
| 286 | Node *node = &it.value(); | - | ||||||||||||
| 287 | ((!(node->useCount > 0)) ? qt_assert("node->useCount > 0",__FILE__,325) : qt_noop()); | - | ||||||||||||
| 288 | - | |||||||||||||
| 289 | - | |||||||||||||
| 290 | if (!node->receiverQueue.isEmpty()
| 1-1305 | ||||||||||||
| 291 | - | |||||||||||||
| 292 | Receiver receiver; | - | ||||||||||||
| 293 | do { | - | ||||||||||||
| 294 | receiver = node->receiverQueue.dequeue(); | - | ||||||||||||
| 295 | } executed 1 time by 1 test: while (receiver.object.isNull()end of blockExecuted by:
| 0-1 | ||||||||||||
| 296 | - | |||||||||||||
| 297 | if (!receiver.object.isNull()
| 0-1 | ||||||||||||
| 298 | emitEntryReady(node, receiver.object, receiver.member); | - | ||||||||||||
| 299 | return; executed 1 time by 1 test: return;Executed by:
| 1 | ||||||||||||
| 300 | } | - | ||||||||||||
| 301 | } never executed: end of block | 0 | ||||||||||||
| 302 | - | |||||||||||||
| 303 | if (!--node->useCount
| 628-677 | ||||||||||||
| 304 | - | |||||||||||||
| 305 | if (node->object->expires
| 0-628 | ||||||||||||
| 306 | linkEntry(key); executed 628 times by 8 tests: linkEntry(key);Executed by:
| 628 | ||||||||||||
| 307 | - | |||||||||||||
| 308 | if (oldest == node
| 20-608 | ||||||||||||
| 309 | updateTimer(); executed 608 times by 8 tests: updateTimer();Executed by:
| 608 | ||||||||||||
| 310 | } executed 628 times by 8 tests: end of blockExecuted by:
| 628 | ||||||||||||
| 311 | } executed 1305 times by 9 tests: end of blockExecuted by:
| 1305 | ||||||||||||
| 312 | - | |||||||||||||
| 313 | void QNetworkAccessCache::removeEntry(const QByteArray &key) | - | ||||||||||||
| 314 | { | - | ||||||||||||
| 315 | NodeHash::Iterator it = hash.find(key); | - | ||||||||||||
| 316 | if (it == hash.end()
| 0-9 | ||||||||||||
| 317 | QMessageLogger(__FILE__, 355, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: trying to remove key '%s' that is not in cache", | - | ||||||||||||
| 318 | key.constData()); | - | ||||||||||||
| 319 | return; never executed: return; | 0 | ||||||||||||
| 320 | } | - | ||||||||||||
| 321 | - | |||||||||||||
| 322 | Node *node = &it.value(); | - | ||||||||||||
| 323 | if (unlinkEntry(key)
| 0-9 | ||||||||||||
| 324 | updateTimer(); never executed: updateTimer(); | 0 | ||||||||||||
| 325 | if (node->useCount > 1
| 0-9 | ||||||||||||
| 326 | QMessageLogger(__FILE__, 364, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'", never executed: QMessageLogger(__FILE__, 364, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'", key.constData()); | 0 | ||||||||||||
| 327 | key.constData()); never executed: QMessageLogger(__FILE__, 364, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'", key.constData()); | 0 | ||||||||||||
| 328 | - | |||||||||||||
| 329 | node->object->key.clear(); | - | ||||||||||||
| 330 | hash.remove(node->key); | - | ||||||||||||
| 331 | } executed 9 times by 1 test: end of blockExecuted by:
| 9 | ||||||||||||
| 332 | - | |||||||||||||
| 333 | - | |||||||||||||
| Switch to Source code | Preprocessed file |