| OLD | NEW |
| 1 /* | 1 /* |
| 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) | 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) |
| 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) | 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) |
| 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) | 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) |
| 5 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | 5 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 6 | 6 |
| 7 This library is free software; you can redistribute it and/or | 7 This library is free software; you can redistribute it and/or |
| 8 modify it under the terms of the GNU Library General Public | 8 modify it under the terms of the GNU Library General Public |
| 9 License as published by the Free Software Foundation; either | 9 License as published by the Free Software Foundation; either |
| 10 version 2 of the License, or (at your option) any later version. | 10 version 2 of the License, or (at your option) any later version. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 { | 55 { |
| 56 memoryCache(); | 56 memoryCache(); |
| 57 MemoryCache* oldCache = gMemoryCache->release(); | 57 MemoryCache* oldCache = gMemoryCache->release(); |
| 58 *gMemoryCache = cache; | 58 *gMemoryCache = cache; |
| 59 MemoryCacheDumpProvider::instance()->setMemoryCache(cache); | 59 MemoryCacheDumpProvider::instance()->setMemoryCache(cache); |
| 60 return oldCache; | 60 return oldCache; |
| 61 } | 61 } |
| 62 | 62 |
| 63 DEFINE_TRACE(MemoryCacheEntry) | 63 DEFINE_TRACE(MemoryCacheEntry) |
| 64 { | 64 { |
| 65 visitor->trace(m_resource); | 65 visitor->template registerWeakMembers<MemoryCacheEntry, &MemoryCacheEntry::c
learResourceWeak>(this); |
| 66 visitor->trace(m_previousInLiveResourcesList); | 66 visitor->trace(m_previousInLiveResourcesList); |
| 67 visitor->trace(m_nextInLiveResourcesList); | 67 visitor->trace(m_nextInLiveResourcesList); |
| 68 visitor->trace(m_previousInAllResourcesList); | 68 visitor->trace(m_previousInAllResourcesList); |
| 69 visitor->trace(m_nextInAllResourcesList); | 69 visitor->trace(m_nextInAllResourcesList); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void MemoryCacheEntry::clearResourceWeak(Visitor* visitor) |
| 73 { |
| 74 if (!m_resource || ThreadHeap::isHeapObjectAlive(m_resource)) |
| 75 return; |
| 76 memoryCache()->remove(m_resource.get()); |
| 77 m_resource.clear(); |
| 78 } |
| 79 |
| 72 void MemoryCacheEntry::dispose() | 80 void MemoryCacheEntry::dispose() |
| 73 { | 81 { |
| 74 m_resource.clear(); | 82 m_resource.clear(); |
| 75 } | 83 } |
| 76 | 84 |
| 77 Resource* MemoryCacheEntry::resource() | 85 Resource* MemoryCacheEntry::resource() |
| 78 { | 86 { |
| 79 return m_resource.get(); | 87 return m_resource.get(); |
| 80 } | 88 } |
| 81 | 89 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 160 } |
| 153 return m_resourceMaps.get(cacheIdentifier); | 161 return m_resourceMaps.get(cacheIdentifier); |
| 154 } | 162 } |
| 155 | 163 |
| 156 void MemoryCache::add(Resource* resource) | 164 void MemoryCache::add(Resource* resource) |
| 157 { | 165 { |
| 158 ASSERT(WTF::isMainThread()); | 166 ASSERT(WTF::isMainThread()); |
| 159 ASSERT(resource->url().isValid()); | 167 ASSERT(resource->url().isValid()); |
| 160 ResourceMap* resources = ensureResourceMap(resource->cacheIdentifier()); | 168 ResourceMap* resources = ensureResourceMap(resource->cacheIdentifier()); |
| 161 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); | 169 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); |
| 162 RELEASE_ASSERT(!resources->contains(url)); | 170 CHECK(!contains(resource)); |
| 163 resources->set(url, MemoryCacheEntry::create(resource)); | 171 resources->set(url, MemoryCacheEntry::create(resource)); |
| 164 update(resource, 0, resource->size(), true); | 172 update(resource, 0, resource->size(), true); |
| 165 | 173 |
| 166 RESOURCE_LOADING_DVLOG(1) << "MemoryCache::add Added " << resource->url().ge
tString() << ", resource " << resource; | 174 RESOURCE_LOADING_DVLOG(1) << "MemoryCache::add Added " << resource->url().ge
tString() << ", resource " << resource; |
| 167 } | 175 } |
| 168 | 176 |
| 169 void MemoryCache::remove(Resource* resource) | 177 void MemoryCache::remove(Resource* resource) |
| 170 { | 178 { |
| 171 // The resource may have already been removed by someone other than our call
er, | 179 // The resource may have already been removed by someone other than our call
er, |
| 172 // who needed a fresh copy for a reload. | 180 // who needed a fresh copy for a reload. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 199 return nullptr; | 207 return nullptr; |
| 200 return entry->resource(); | 208 return entry->resource(); |
| 201 } | 209 } |
| 202 | 210 |
| 203 HeapVector<Member<Resource>> MemoryCache::resourcesForURL(const KURL& resourceUR
L) | 211 HeapVector<Member<Resource>> MemoryCache::resourcesForURL(const KURL& resourceUR
L) |
| 204 { | 212 { |
| 205 ASSERT(WTF::isMainThread()); | 213 ASSERT(WTF::isMainThread()); |
| 206 KURL url = removeFragmentIdentifierIfNeeded(resourceURL); | 214 KURL url = removeFragmentIdentifierIfNeeded(resourceURL); |
| 207 HeapVector<Member<Resource>> results; | 215 HeapVector<Member<Resource>> results; |
| 208 for (const auto& resourceMapIter : m_resourceMaps) { | 216 for (const auto& resourceMapIter : m_resourceMaps) { |
| 209 if (MemoryCacheEntry* entry = resourceMapIter.value->get(url)) | 217 if (MemoryCacheEntry* entry = resourceMapIter.value->get(url)) { |
| 210 results.append(entry->resource()); | 218 Resource* resource = entry->resource(); |
| 219 DCHECK(resource); |
| 220 results.append(resource); |
| 221 } |
| 211 } | 222 } |
| 212 return results; | 223 return results; |
| 213 } | 224 } |
| 214 | 225 |
| 215 size_t MemoryCache::deadCapacity() const | 226 size_t MemoryCache::deadCapacity() const |
| 216 { | 227 { |
| 217 // Dead resource capacity is whatever space is not occupied by live resource
s, bounded by an independent minimum and maximum. | 228 // Dead resource capacity is whatever space is not occupied by live resource
s, bounded by an independent minimum and maximum. |
| 218 size_t capacity = m_capacity - std::min(m_liveSize, m_capacity); // Start wi
th available capacity. | 229 size_t capacity = m_capacity - std::min(m_liveSize, m_capacity); // Start wi
th available capacity. |
| 219 capacity = std::max(capacity, m_minDeadCapacity); // Make sure it's above th
e minimum. | 230 capacity = std::max(capacity, m_minDeadCapacity); // Make sure it's above th
e minimum. |
| 220 capacity = std::min(capacity, m_maxDeadCapacity); // Make sure it's below th
e maximum. | 231 capacity = std::min(capacity, m_maxDeadCapacity); // Make sure it's below th
e maximum. |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 m_maxDeferredPruneDeadCapacity = cDeferredPruneDeadCapacityFactor * maxDeadB
ytes; | 362 m_maxDeferredPruneDeadCapacity = cDeferredPruneDeadCapacityFactor * maxDeadB
ytes; |
| 352 m_capacity = totalBytes; | 363 m_capacity = totalBytes; |
| 353 prune(); | 364 prune(); |
| 354 } | 365 } |
| 355 | 366 |
| 356 void MemoryCache::evict(MemoryCacheEntry* entry) | 367 void MemoryCache::evict(MemoryCacheEntry* entry) |
| 357 { | 368 { |
| 358 ASSERT(WTF::isMainThread()); | 369 ASSERT(WTF::isMainThread()); |
| 359 | 370 |
| 360 Resource* resource = entry->resource(); | 371 Resource* resource = entry->resource(); |
| 372 DCHECK(resource); |
| 361 RESOURCE_LOADING_DVLOG(1) << "Evicting resource " << resource << " for " <<
resource->url().getString() << " from cache"; | 373 RESOURCE_LOADING_DVLOG(1) << "Evicting resource " << resource << " for " <<
resource->url().getString() << " from cache"; |
| 362 TRACE_EVENT1("blink", "MemoryCache::evict", "resource", resource->url().getS
tring().utf8()); | 374 TRACE_EVENT1("blink", "MemoryCache::evict", "resource", resource->url().getS
tring().utf8()); |
| 363 // The resource may have already been removed by someone other than our call
er, | 375 // The resource may have already been removed by someone other than our call
er, |
| 364 // who needed a fresh copy for a reload. See <http://bugs.webkit.org/show_bu
g.cgi?id=12479#c6>. | 376 // who needed a fresh copy for a reload. See <http://bugs.webkit.org/show_bu
g.cgi?id=12479#c6>. |
| 365 update(resource, resource->size(), 0, false); | 377 update(resource, resource->size(), 0, false); |
| 366 removeFromLiveDecodedResourcesList(entry); | 378 removeFromLiveDecodedResourcesList(entry); |
| 367 | 379 |
| 368 ResourceMap* resources = m_resourceMaps.get(resource->cacheIdentifier()); | 380 ResourceMap* resources = m_resourceMaps.get(resource->cacheIdentifier()); |
| 369 ASSERT(resources); | 381 ASSERT(resources); |
| 370 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); | 382 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); |
| 371 ResourceMap::iterator it = resources->find(url); | 383 ResourceMap::iterator it = resources->find(url); |
| 372 ASSERT(it != resources->end()); | 384 ASSERT(it != resources->end()); |
| 373 | 385 |
| 374 MemoryCacheEntry* entryPtr = it->value; | 386 MemoryCacheEntry* entryPtr = it->value; |
| 375 resources->remove(it); | 387 resources->remove(it); |
| 376 if (entryPtr) | 388 if (entryPtr) |
| 377 entryPtr->dispose(); | 389 entryPtr->dispose(); |
| 378 } | 390 } |
| 379 | 391 |
| 380 MemoryCacheEntry* MemoryCache::getEntryForResource(const Resource* resource) con
st | 392 MemoryCacheEntry* MemoryCache::getEntryForResource(const Resource* resource) con
st |
| 381 { | 393 { |
| 382 if (resource->url().isNull() || resource->url().isEmpty()) | 394 if (!resource || resource->url().isNull() || resource->url().isEmpty()) |
| 383 return nullptr; | 395 return nullptr; |
| 384 ResourceMap* resources = m_resourceMaps.get(resource->cacheIdentifier()); | 396 ResourceMap* resources = m_resourceMaps.get(resource->cacheIdentifier()); |
| 385 if (!resources) | 397 if (!resources) |
| 386 return nullptr; | 398 return nullptr; |
| 387 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); | 399 KURL url = removeFragmentIdentifierIfNeeded(resource->url()); |
| 388 MemoryCacheEntry* entry = resources->get(url); | 400 MemoryCacheEntry* entry = resources->get(url); |
| 389 if (!entry || entry->resource() != resource) | 401 if (!entry || entry->resource() != resource) |
| 390 return nullptr; | 402 return nullptr; |
| 391 return entry; | 403 return entry; |
| 392 } | 404 } |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 encodedSize += o->encodedSize(); | 594 encodedSize += o->encodedSize(); |
| 583 encodedSizeDuplicatedInDataURLs += o->url().protocolIsData() ? o->encodedSiz
e() : 0; | 595 encodedSizeDuplicatedInDataURLs += o->url().protocolIsData() ? o->encodedSiz
e() : 0; |
| 584 } | 596 } |
| 585 | 597 |
| 586 MemoryCache::Statistics MemoryCache::getStatistics() | 598 MemoryCache::Statistics MemoryCache::getStatistics() |
| 587 { | 599 { |
| 588 Statistics stats; | 600 Statistics stats; |
| 589 for (const auto& resourceMapIter : m_resourceMaps) { | 601 for (const auto& resourceMapIter : m_resourceMaps) { |
| 590 for (const auto& resourceIter : *resourceMapIter.value) { | 602 for (const auto& resourceIter : *resourceMapIter.value) { |
| 591 Resource* resource = resourceIter.value->resource(); | 603 Resource* resource = resourceIter.value->resource(); |
| 604 DCHECK(resource); |
| 592 switch (resource->getType()) { | 605 switch (resource->getType()) { |
| 593 case Resource::Image: | 606 case Resource::Image: |
| 594 stats.images.addResource(resource); | 607 stats.images.addResource(resource); |
| 595 break; | 608 break; |
| 596 case Resource::CSSStyleSheet: | 609 case Resource::CSSStyleSheet: |
| 597 stats.cssStyleSheets.addResource(resource); | 610 stats.cssStyleSheets.addResource(resource); |
| 598 break; | 611 break; |
| 599 case Resource::Script: | 612 case Resource::Script: |
| 600 stats.scripts.addResource(resource); | 613 stats.scripts.addResource(resource); |
| 601 break; | 614 break; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 bool MemoryCache::isInSameLRUListForTest(const Resource* x, const Resource* y) | 746 bool MemoryCache::isInSameLRUListForTest(const Resource* x, const Resource* y) |
| 734 { | 747 { |
| 735 MemoryCacheEntry* ex = getEntryForResource(x); | 748 MemoryCacheEntry* ex = getEntryForResource(x); |
| 736 MemoryCacheEntry* ey = getEntryForResource(y); | 749 MemoryCacheEntry* ey = getEntryForResource(y); |
| 737 ASSERT(ex); | 750 ASSERT(ex); |
| 738 ASSERT(ey); | 751 ASSERT(ey); |
| 739 return lruListFor(ex->m_accessCount, x->size()) == lruListFor(ey->m_accessCo
unt, y->size()); | 752 return lruListFor(ex->m_accessCount, x->size()) == lruListFor(ey->m_accessCo
unt, y->size()); |
| 740 } | 753 } |
| 741 | 754 |
| 742 } // namespace blink | 755 } // namespace blink |
| OLD | NEW |