~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/history/PageCache.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Apple Inc.  All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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. 
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "PageCache.h"
 
28
 
 
29
#include "Cache.h"
 
30
#include "CachedPage.h"
 
31
#include "FrameLoader.h"
 
32
#include "HistoryItem.h"
 
33
#include "Logging.h"
 
34
#include "SystemTime.h"
 
35
 
 
36
using namespace std;
 
37
 
 
38
namespace WebCore {
 
39
 
 
40
static const double autoreleaseInterval = 3;
 
41
 
 
42
PageCache* pageCache()
 
43
{
 
44
    static PageCache* staticPageCache = new PageCache;
 
45
    return staticPageCache;
 
46
}
 
47
 
 
48
PageCache::PageCache()
 
49
    : m_capacity(0)
 
50
    , m_size(0)
 
51
    , m_head(0)
 
52
    , m_tail(0)
 
53
    , m_autoreleaseTimer(this, &PageCache::releaseAutoreleasedPagesNowOrReschedule)
 
54
{
 
55
}
 
56
 
 
57
void PageCache::setCapacity(int capacity)
 
58
{
 
59
    ASSERT(capacity >= 0);
 
60
    m_capacity = max(capacity, 0);
 
61
 
 
62
    prune();
 
63
}
 
64
 
 
65
void PageCache::add(PassRefPtr<HistoryItem> prpItem, PassRefPtr<CachedPage> cachedPage)
 
66
{
 
67
    ASSERT(prpItem);
 
68
    ASSERT(cachedPage);
 
69
    
 
70
    HistoryItem* item = prpItem.releaseRef(); // Balanced in remove().
 
71
 
 
72
    // Remove stale cache entry if necessary.
 
73
    if (item->m_cachedPage)
 
74
        remove(item);
 
75
 
 
76
    item->m_cachedPage = cachedPage;
 
77
    addToLRUList(item);
 
78
    ++m_size;
 
79
    
 
80
    prune();
 
81
}
 
82
 
 
83
void PageCache::remove(HistoryItem* item)
 
84
{
 
85
    // Safely ignore attempts to remove items not in the cache.
 
86
    if (!item || !item->m_cachedPage)
 
87
        return;
 
88
 
 
89
    autorelease(item->m_cachedPage.release());
 
90
    removeFromLRUList(item);
 
91
    --m_size;
 
92
 
 
93
    item->deref(); // Balanced in add().
 
94
}
 
95
 
 
96
void PageCache::prune()
 
97
{
 
98
    while (m_size > m_capacity) {
 
99
        ASSERT(m_tail && m_tail->m_cachedPage);
 
100
        remove(m_tail);
 
101
    }
 
102
}
 
103
 
 
104
void PageCache::addToLRUList(HistoryItem* item)
 
105
{
 
106
    item->m_next = m_head;
 
107
    item->m_prev = 0;
 
108
 
 
109
    if (m_head) {
 
110
        ASSERT(m_tail);
 
111
        m_head->m_prev = item;
 
112
    } else {
 
113
        ASSERT(!m_tail);
 
114
        m_tail = item;
 
115
    }
 
116
 
 
117
    m_head = item;
 
118
}
 
119
 
 
120
void PageCache::removeFromLRUList(HistoryItem* item)
 
121
{
 
122
    if (!item->m_next) {
 
123
        ASSERT(item == m_tail);
 
124
        m_tail = item->m_prev;
 
125
    } else {
 
126
        ASSERT(item != m_tail);
 
127
        item->m_next->m_prev = item->m_prev;
 
128
    }
 
129
 
 
130
    if (!item->m_prev) {
 
131
        ASSERT(item == m_head);
 
132
        m_head = item->m_next;
 
133
    } else {
 
134
        ASSERT(item != m_head);
 
135
        item->m_prev->m_next = item->m_next;
 
136
    }
 
137
}
 
138
 
 
139
void PageCache::releaseAutoreleasedPagesNowOrReschedule(Timer<PageCache>* timer)
 
140
{
 
141
    double loadDelta = currentTime() - FrameLoader::timeOfLastCompletedLoad();
 
142
    float userDelta = userIdleTime();
 
143
    
 
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);
 
148
        return;
 
149
    }
 
150
 
 
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();
 
153
}
 
154
 
 
155
void PageCache::releaseAutoreleasedPagesNow()
 
156
{
 
157
    m_autoreleaseTimer.stop();
 
158
 
 
159
    // Postpone dead pruning until all our resources have gone dead.
 
160
    cache()->setPruneEnabled(false);
 
161
 
 
162
    CachedPageSet tmp;
 
163
    tmp.swap(m_autoreleaseSet);
 
164
 
 
165
    CachedPageSet::iterator end = tmp.end();
 
166
    for (CachedPageSet::iterator it = tmp.begin(); it != end; ++it)
 
167
        (*it)->close();
 
168
 
 
169
    // Now do the prune.
 
170
    cache()->setPruneEnabled(true);
 
171
    cache()->prune();
 
172
}
 
173
 
 
174
void PageCache::autorelease(PassRefPtr<CachedPage> page)
 
175
{
 
176
    ASSERT(page);
 
177
    ASSERT(!m_autoreleaseSet.contains(page.get()));
 
178
    m_autoreleaseSet.add(page);
 
179
    if (!m_autoreleaseTimer.isActive())
 
180
        m_autoreleaseTimer.startOneShot(autoreleaseInterval);
 
181
}
 
182
 
 
183
} // namespace WebCore