diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-02 20:40:56 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-02 20:40:56 +0000 |
commit | eeb090c9cb78b3f4808ca5479c1ca8cf62082bee (patch) | |
tree | db6d2d900e8732c3d57cb434c3a677b061f362f1 | |
parent | c60d3a08ee8d9608602808339e4b17d4c23fad8d (diff) | |
download | chromium_src-eeb090c9cb78b3f4808ca5479c1ca8cf62082bee.zip chromium_src-eeb090c9cb78b3f4808ca5479c1ca8cf62082bee.tar.gz chromium_src-eeb090c9cb78b3f4808ca5479c1ca8cf62082bee.tar.bz2 |
Add a Clear() function to MRUCache. This will be useful in my MemoryPurger class. Technically you can call ShrinkToSize(0), but that's both less clear and slower.
BUG=23400
TEST=none
Review URL: http://codereview.chromium.org/257024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27892 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/history/text_database_manager.cc | 4 | ||||
-rw-r--r-- | chrome/browser/renderer_host/backing_store_manager.cc | 10 | ||||
-rw-r--r-- | chrome/browser/renderer_host/backing_store_manager.h | 5 | ||||
-rw-r--r-- | chrome/common/mru_cache.h | 11 | ||||
-rw-r--r-- | chrome/common/mru_cache_unittest.cc | 19 |
5 files changed, 43 insertions, 6 deletions
diff --git a/chrome/browser/history/text_database_manager.cc b/chrome/browser/history/text_database_manager.cc index 868a5d8..149cd72 100644 --- a/chrome/browser/history/text_database_manager.cc +++ b/chrome/browser/history/text_database_manager.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -369,7 +369,7 @@ void TextDatabaseManager::DeleteAll() { InitDBList(); // Close all open databases. - db_cache_.ShrinkToSize(0); + db_cache_.Clear(); // Now go through and delete all the files. for (DBIdentSet::iterator i = present_databases_.begin(); diff --git a/chrome/browser/renderer_host/backing_store_manager.cc b/chrome/browser/renderer_host/backing_store_manager.cc index 4ea7349..aeaee18 100644 --- a/chrome/browser/renderer_host/backing_store_manager.cc +++ b/chrome/browser/renderer_host/backing_store_manager.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -217,6 +217,14 @@ void BackingStoreManager::RemoveBackingStore(RenderWidgetHost* host) { } // static +void BackingStoreManager::RemoveAllBackingStores() { + if (large_cache) { + large_cache->Clear(); + small_cache->Clear(); + } +} + +// static bool BackingStoreManager::ExpireBackingStoreForTest(RenderWidgetHost* host) { BackingStoreCache* cache = large_cache; diff --git a/chrome/browser/renderer_host/backing_store_manager.h b/chrome/browser/renderer_host/backing_store_manager.h index 625e87f..8344ca2 100644 --- a/chrome/browser/renderer_host/backing_store_manager.h +++ b/chrome/browser/renderer_host/backing_store_manager.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -58,6 +58,9 @@ class BackingStoreManager { // Removes the backing store for the host. static void RemoveBackingStore(RenderWidgetHost* host); + // Removes all backing stores. + static void RemoveAllBackingStores(); + // Expires the given backing store. This emulates something getting evicted // from the cache for the purpose of testing. Returns true if the host was // removed, false if it wasn't found. diff --git a/chrome/common/mru_cache.h b/chrome/common/mru_cache.h index 02e7242..9190d9d 100644 --- a/chrome/common/mru_cache.h +++ b/chrome/common/mru_cache.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -137,6 +137,15 @@ class MRUCacheBase { Erase(rbegin()); } + // Deletes everything from the cache. + void Clear() { + for (typename PayloadList::iterator i(ordering_.begin()); + i != ordering_.end(); ++i) + deletor_(i->second); + index_.clear(); + ordering_.clear(); + } + // Returns the number of elements in the cache. size_type size() const { // We don't use ordering_.size() for the return value because diff --git a/chrome/common/mru_cache_unittest.cc b/chrome/common/mru_cache_unittest.cc index 15f3dc5..bacb8a5 100644 --- a/chrome/common/mru_cache_unittest.cc +++ b/chrome/common/mru_cache_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -103,6 +103,13 @@ TEST(MRUCacheTest, Basic) { cache.Erase(cache.begin()); EXPECT_EQ(0U, cache.size()); } + + // Check that Clear() works properly. + cache.Put(kItem1Key, item1); + cache.Put(kItem2Key, item2); + EXPECT_EQ(2U, cache.size()); + cache.Clear(); + EXPECT_EQ(0U, cache.size()); } TEST(MRUCacheTest, GetVsPeek) { @@ -209,6 +216,16 @@ TEST(MRUCacheTest, Owning) { // There should be no objects leaked. EXPECT_EQ(initial_count, cached_item_live_count); + + // Check that Clear() also frees things correctly. + { + Cache cache2(Cache::NO_AUTO_EVICT); + cache2.Put(1, new CachedItem(20)); + cache2.Put(2, new CachedItem(20)); + EXPECT_EQ(initial_count + 2, cached_item_live_count); + cache2.Clear(); + EXPECT_EQ(initial_count, cached_item_live_count); + } } TEST(MRUCacheTest, AutoEvict) { |