OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See net/disk_cache/disk_cache.h for the public interface. | 5 // See net/disk_cache/disk_cache.h for the public interface. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_RANKINGS_H_ | 7 #ifndef NET_DISK_CACHE_RANKINGS_H_ |
8 #define NET_DISK_CACHE_RANKINGS_H_ | 8 #define NET_DISK_CACHE_RANKINGS_H_ |
9 | 9 |
10 #include <list> | 10 #include <list> |
(...skipping 36 matching lines...) Loading... |
47 }; | 47 }; |
48 | 48 |
49 // This class handles the ranking information for the cache. | 49 // This class handles the ranking information for the cache. |
50 class Rankings { | 50 class Rankings { |
51 public: | 51 public: |
52 // Possible lists of entries. | 52 // Possible lists of entries. |
53 enum List { | 53 enum List { |
54 NO_USE = 0, // List of entries that have not been reused. | 54 NO_USE = 0, // List of entries that have not been reused. |
55 LOW_USE, // List of entries with low reuse. | 55 LOW_USE, // List of entries with low reuse. |
56 HIGH_USE, // List of entries with high reuse. | 56 HIGH_USE, // List of entries with high reuse. |
| 57 RESERVED, // Reserved for future use. |
57 DELETED, // List of recently deleted or doomed entries. | 58 DELETED, // List of recently deleted or doomed entries. |
58 LAST_ELEMENT | 59 LAST_ELEMENT |
59 }; | 60 }; |
60 | 61 |
61 // This class provides a specialized version of scoped_ptr, that calls | 62 // This class provides a specialized version of scoped_ptr, that calls |
62 // Rankings whenever a CacheRankingsBlock is deleted, to keep track of cache | 63 // Rankings whenever a CacheRankingsBlock is deleted, to keep track of cache |
63 // iterators that may go stale. | 64 // iterators that may go stale. |
64 class ScopedRankingsBlock : public scoped_ptr<CacheRankingsBlock> { | 65 class ScopedRankingsBlock : public scoped_ptr<CacheRankingsBlock> { |
65 public: | 66 public: |
| 67 ScopedRankingsBlock() : rankings_(NULL) {} |
66 explicit ScopedRankingsBlock(Rankings* rankings) : rankings_(rankings) {} | 68 explicit ScopedRankingsBlock(Rankings* rankings) : rankings_(rankings) {} |
67 ScopedRankingsBlock(Rankings* rankings, CacheRankingsBlock* node) | 69 ScopedRankingsBlock(Rankings* rankings, CacheRankingsBlock* node) |
68 : scoped_ptr<CacheRankingsBlock>(node), rankings_(rankings) {} | 70 : scoped_ptr<CacheRankingsBlock>(node), rankings_(rankings) {} |
69 | 71 |
70 ~ScopedRankingsBlock() { | 72 ~ScopedRankingsBlock() { |
71 rankings_->FreeRankingsBlock(get()); | 73 rankings_->FreeRankingsBlock(get()); |
72 } | 74 } |
73 | 75 |
| 76 void set_rankings(Rankings* rankings) { |
| 77 rankings_ = rankings; |
| 78 } |
| 79 |
74 // scoped_ptr::reset will delete the object. | 80 // scoped_ptr::reset will delete the object. |
75 void reset(CacheRankingsBlock* p = NULL) { | 81 void reset(CacheRankingsBlock* p = NULL) { |
76 if (p != get()) | 82 if (p != get()) |
77 rankings_->FreeRankingsBlock(get()); | 83 rankings_->FreeRankingsBlock(get()); |
78 scoped_ptr<CacheRankingsBlock>::reset(p); | 84 scoped_ptr<CacheRankingsBlock>::reset(p); |
79 } | 85 } |
80 | 86 |
81 private: | 87 private: |
82 Rankings* rankings_; | 88 Rankings* rankings_; |
83 DISALLOW_EVIL_CONSTRUCTORS(ScopedRankingsBlock); | 89 DISALLOW_EVIL_CONSTRUCTORS(ScopedRankingsBlock); |
84 }; | 90 }; |
85 | 91 |
| 92 // If we have multiple lists, we have to iterate through all at the same time. |
| 93 // This structure keeps track of where we are on the iteration. |
| 94 struct Iterator { |
| 95 List list; // Which entry was returned to the user. |
| 96 CacheRankingsBlock* nodes[3]; // Nodes on the first three lists. |
| 97 Rankings* my_rankings; |
| 98 Iterator(Rankings* rankings) { |
| 99 memset(this, 0, sizeof(Iterator)); |
| 100 my_rankings = rankings; |
| 101 } |
| 102 ~Iterator() { |
| 103 for (int i = 0; i < 3; i++) |
| 104 ScopedRankingsBlock(my_rankings, nodes[i]); |
| 105 } |
| 106 }; |
| 107 |
86 Rankings() : init_(false) {} | 108 Rankings() : init_(false) {} |
87 ~Rankings() {} | 109 ~Rankings() {} |
88 | 110 |
89 bool Init(BackendImpl* backend); | 111 bool Init(BackendImpl* backend, bool count_lists); |
90 | 112 |
91 // Restores original state, leaving the object ready for initialization. | 113 // Restores original state, leaving the object ready for initialization. |
92 void Reset(); | 114 void Reset(); |
93 | 115 |
94 // Inserts a given entry at the head of the queue. | 116 // Inserts a given entry at the head of the queue. |
95 void Insert(CacheRankingsBlock* node, bool modified, List list); | 117 void Insert(CacheRankingsBlock* node, bool modified, List list); |
96 | 118 |
97 // Removes a given entry from the LRU list. | 119 // Removes a given entry from the LRU list. |
98 void Remove(CacheRankingsBlock* node, List list); | 120 void Remove(CacheRankingsBlock* node, List list); |
99 | 121 |
(...skipping 48 matching lines...) Loading... |
148 // Returns true if addr is the head or tail of any list. | 170 // Returns true if addr is the head or tail of any list. |
149 bool IsHead(CacheAddr addr); | 171 bool IsHead(CacheAddr addr); |
150 bool IsTail(CacheAddr addr); | 172 bool IsTail(CacheAddr addr); |
151 | 173 |
152 // Controls tracking of nodes used for enumerations. | 174 // Controls tracking of nodes used for enumerations. |
153 void TrackRankingsBlock(CacheRankingsBlock* node, bool start_tracking); | 175 void TrackRankingsBlock(CacheRankingsBlock* node, bool start_tracking); |
154 | 176 |
155 // Updates the iterators whenever node is being changed. | 177 // Updates the iterators whenever node is being changed. |
156 void UpdateIterators(CacheRankingsBlock* node); | 178 void UpdateIterators(CacheRankingsBlock* node); |
157 | 179 |
| 180 // Keeps track of the number of entries on a list. |
| 181 void IncrementCounter(List list); |
| 182 void DecrementCounter(List list); |
| 183 |
158 bool init_; | 184 bool init_; |
| 185 bool count_lists_; |
159 Addr heads_[LAST_ELEMENT]; | 186 Addr heads_[LAST_ELEMENT]; |
160 Addr tails_[LAST_ELEMENT]; | 187 Addr tails_[LAST_ELEMENT]; |
161 BackendImpl* backend_; | 188 BackendImpl* backend_; |
162 LruData* control_data_; // Data related to the LRU lists. | 189 LruData* control_data_; // Data related to the LRU lists. |
163 IteratorList iterators_; | 190 IteratorList iterators_; |
164 | 191 |
165 DISALLOW_COPY_AND_ASSIGN(Rankings); | 192 DISALLOW_COPY_AND_ASSIGN(Rankings); |
166 }; | 193 }; |
167 | 194 |
168 } // namespace disk_cache | 195 } // namespace disk_cache |
169 | 196 |
170 #endif // NET_DISK_CACHE_RANKINGS_H_ | 197 #endif // NET_DISK_CACHE_RANKINGS_H_ |
171 | 198 |
OLD | NEW |