2
* Copyright (C) 2007 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "PageCache.h"
30
#include "CachedPage.h"
31
#include "FrameLoader.h"
32
#include "HistoryItem.h"
34
#include "SystemTime.h"
40
static const double autoreleaseInterval = 3;
42
PageCache* pageCache()
44
static PageCache* staticPageCache = new PageCache;
45
return staticPageCache;
48
PageCache::PageCache()
53
, m_autoreleaseTimer(this, &PageCache::releaseAutoreleasedPagesNowOrReschedule)
57
void PageCache::setCapacity(int capacity)
59
ASSERT(capacity >= 0);
60
m_capacity = max(capacity, 0);
65
void PageCache::add(PassRefPtr<HistoryItem> prpItem, PassRefPtr<CachedPage> cachedPage)
70
HistoryItem* item = prpItem.releaseRef(); // Balanced in remove().
72
// Remove stale cache entry if necessary.
73
if (item->m_cachedPage)
76
item->m_cachedPage = cachedPage;
83
void PageCache::remove(HistoryItem* item)
85
// Safely ignore attempts to remove items not in the cache.
86
if (!item || !item->m_cachedPage)
89
autorelease(item->m_cachedPage.release());
90
removeFromLRUList(item);
93
item->deref(); // Balanced in add().
96
void PageCache::prune()
98
while (m_size > m_capacity) {
99
ASSERT(m_tail && m_tail->m_cachedPage);
104
void PageCache::addToLRUList(HistoryItem* item)
106
item->m_next = m_head;
111
m_head->m_prev = item;
120
void PageCache::removeFromLRUList(HistoryItem* item)
123
ASSERT(item == m_tail);
124
m_tail = item->m_prev;
126
ASSERT(item != m_tail);
127
item->m_next->m_prev = item->m_prev;
131
ASSERT(item == m_head);
132
m_head = item->m_next;
134
ASSERT(item != m_head);
135
item->m_prev->m_next = item->m_next;
139
void PageCache::releaseAutoreleasedPagesNowOrReschedule(Timer<PageCache>* timer)
141
double loadDelta = currentTime() - FrameLoader::timeOfLastCompletedLoad();
142
float userDelta = userIdleTime();
144
// FIXME: <rdar://problem/5211190> This limit of 42 risks growing the page cache far beyond its nominal capacity.
145
if ((userDelta < 0.5 || loadDelta < 1.25) && m_autoreleaseSet.size() < 42) {
146
LOG(PageCache, "WebCorePageCache: Postponing releaseAutoreleasedPagesNowOrReschedule() - %f since last load, %f since last input, %i objects pending release", loadDelta, userDelta, m_autoreleaseSet.size());
147
timer->startOneShot(autoreleaseInterval);
151
LOG(PageCache, "WebCorePageCache: Releasing page caches - %f seconds since last load, %f since last input, %i objects pending release", loadDelta, userDelta, m_autoreleaseSet.size());
152
releaseAutoreleasedPagesNow();
155
void PageCache::releaseAutoreleasedPagesNow()
157
m_autoreleaseTimer.stop();
159
// Postpone dead pruning until all our resources have gone dead.
160
cache()->setPruneEnabled(false);
163
tmp.swap(m_autoreleaseSet);
165
CachedPageSet::iterator end = tmp.end();
166
for (CachedPageSet::iterator it = tmp.begin(); it != end; ++it)
170
cache()->setPruneEnabled(true);
174
void PageCache::autorelease(PassRefPtr<CachedPage> page)
177
ASSERT(!m_autoreleaseSet.contains(page.get()));
178
m_autoreleaseSet.add(page);
179
if (!m_autoreleaseTimer.isActive())
180
m_autoreleaseTimer.startOneShot(autoreleaseInterval);
183
} // namespace WebCore