| 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 30 matching lines...) Expand all Loading... |
| 41 REMOVE_TAIL_3, | 41 REMOVE_TAIL_3, |
| 42 REMOVE_LOAD_1, | 42 REMOVE_LOAD_1, |
| 43 REMOVE_LOAD_2, | 43 REMOVE_LOAD_2, |
| 44 REMOVE_LOAD_3, | 44 REMOVE_LOAD_3, |
| 45 MAX_CRASH | 45 MAX_CRASH |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 // This class handles the ranking information for the cache. | 48 // This class handles the ranking information for the cache. |
| 49 class Rankings { | 49 class Rankings { |
| 50 public: | 50 public: |
| 51 // Possible lists of entries. |
| 52 enum List { |
| 53 NO_USE = 0, // List of entries that have not been reused. |
| 54 LOW_USE, // List of entries with low reuse. |
| 55 HIGH_USE, // List of entries with high reuse. |
| 56 DELETED, // List of recently deleted or doomed entries. |
| 57 LAST_ELEMENT |
| 58 }; |
| 59 |
| 51 // This class provides a specialized version of scoped_ptr, that calls | 60 // This class provides a specialized version of scoped_ptr, that calls |
| 52 // Rankings whenever a CacheRankingsBlock is deleted, to keep track of cache | 61 // Rankings whenever a CacheRankingsBlock is deleted, to keep track of cache |
| 53 // iterators that may go stale. | 62 // iterators that may go stale. |
| 54 class ScopedRankingsBlock : public scoped_ptr<CacheRankingsBlock> { | 63 class ScopedRankingsBlock : public scoped_ptr<CacheRankingsBlock> { |
| 55 public: | 64 public: |
| 56 explicit ScopedRankingsBlock(Rankings* rankings) : rankings_(rankings) {} | 65 explicit ScopedRankingsBlock(Rankings* rankings) : rankings_(rankings) {} |
| 57 ScopedRankingsBlock(Rankings* rankings, CacheRankingsBlock* node) | 66 ScopedRankingsBlock(Rankings* rankings, CacheRankingsBlock* node) |
| 58 : scoped_ptr<CacheRankingsBlock>(node), rankings_(rankings) {} | 67 : scoped_ptr<CacheRankingsBlock>(node), rankings_(rankings) {} |
| 59 | 68 |
| 60 ~ScopedRankingsBlock() { | 69 ~ScopedRankingsBlock() { |
| 61 rankings_->FreeRankingsBlock(get()); | 70 rankings_->FreeRankingsBlock(get()); |
| 62 } | 71 } |
| 63 | 72 |
| 64 // scoped_ptr::reset will delete the object. | 73 // scoped_ptr::reset will delete the object. |
| 65 void reset(CacheRankingsBlock* p = NULL) { | 74 void reset(CacheRankingsBlock* p = NULL) { |
| 66 if (p != get()) | 75 if (p != get()) |
| 67 rankings_->FreeRankingsBlock(get()); | 76 rankings_->FreeRankingsBlock(get()); |
| 68 scoped_ptr<CacheRankingsBlock>::reset(p); | 77 scoped_ptr<CacheRankingsBlock>::reset(p); |
| 69 } | 78 } |
| 70 | 79 |
| 71 private: | 80 private: |
| 72 Rankings* rankings_; | 81 Rankings* rankings_; |
| 73 DISALLOW_EVIL_CONSTRUCTORS(ScopedRankingsBlock); | 82 DISALLOW_EVIL_CONSTRUCTORS(ScopedRankingsBlock); |
| 74 }; | 83 }; |
| 75 | 84 |
| 76 Rankings() | 85 Rankings() : init_(false) {} |
| 77 : init_(false), head_(0), tail_(0) {} | |
| 78 ~Rankings() {} | 86 ~Rankings() {} |
| 79 | 87 |
| 80 bool Init(BackendImpl* backend); | 88 bool Init(BackendImpl* backend); |
| 81 | 89 |
| 82 // Restores original state, leaving the object ready for initialization. | 90 // Restores original state, leaving the object ready for initialization. |
| 83 void Reset(); | 91 void Reset(); |
| 84 | 92 |
| 85 // Inserts a given entry at the head of the queue. | 93 // Inserts a given entry at the head of the queue. |
| 86 void Insert(CacheRankingsBlock* node, bool modified); | 94 void Insert(CacheRankingsBlock* node, bool modified, List list); |
| 87 | 95 |
| 88 // Removes a given entry from the LRU list. | 96 // Removes a given entry from the LRU list. |
| 89 void Remove(CacheRankingsBlock* node); | 97 void Remove(CacheRankingsBlock* node, List list); |
| 90 | 98 |
| 91 // Moves a given entry to the head. | 99 // Moves a given entry to the head. |
| 92 void UpdateRank(CacheRankingsBlock* node, bool modified); | 100 void UpdateRank(CacheRankingsBlock* node, bool modified, List list); |
| 93 | 101 |
| 94 // Iterates through the list. | 102 // Iterates through the list. |
| 95 CacheRankingsBlock* GetNext(CacheRankingsBlock* node); | 103 CacheRankingsBlock* GetNext(CacheRankingsBlock* node, List list); |
| 96 CacheRankingsBlock* GetPrev(CacheRankingsBlock* node); | 104 CacheRankingsBlock* GetPrev(CacheRankingsBlock* node, List list); |
| 97 void FreeRankingsBlock(CacheRankingsBlock* node); | 105 void FreeRankingsBlock(CacheRankingsBlock* node); |
| 98 | 106 |
| 99 // Peforms a simple self-check of the list, and returns the number of items | 107 // Peforms a simple self-check of the lists, and returns the number of items |
| 100 // or an error code (negative value). | 108 // or an error code (negative value). |
| 101 int SelfCheck(); | 109 int SelfCheck(); |
| 102 | 110 |
| 103 // Returns false if the entry is clearly invalid. from_list is true if the | 111 // Returns false if the entry is clearly invalid. from_list is true if the |
| 104 // node comes from the LRU list. | 112 // node comes from the LRU list. |
| 105 bool SanityCheck(CacheRankingsBlock* node, bool from_list); | 113 bool SanityCheck(CacheRankingsBlock* node, bool from_list); |
| 106 | 114 |
| 107 private: | 115 private: |
| 108 typedef std::pair<CacheAddr, CacheRankingsBlock*> IteratorPair; | 116 typedef std::pair<CacheAddr, CacheRankingsBlock*> IteratorPair; |
| 109 typedef std::list<IteratorPair> IteratorList; | 117 typedef std::list<IteratorPair> IteratorList; |
| 110 | 118 |
| 111 Addr ReadHead(); | 119 void ReadHeads(); |
| 112 Addr ReadTail(); | 120 void ReadTails(); |
| 113 void WriteHead(); | 121 void WriteHead(List list); |
| 114 void WriteTail(); | 122 void WriteTail(List list); |
| 115 | 123 |
| 116 // Gets the rankings information for a given rankings node. | 124 // Gets the rankings information for a given rankings node. |
| 117 bool GetRanking(CacheRankingsBlock* rankings); | 125 bool GetRanking(CacheRankingsBlock* rankings); |
| 118 | 126 |
| 119 // Finishes a list modification after a crash. | 127 // Finishes a list modification after a crash. |
| 120 void CompleteTransaction(); | 128 void CompleteTransaction(); |
| 121 void FinishInsert(CacheRankingsBlock* rankings); | 129 void FinishInsert(CacheRankingsBlock* rankings); |
| 122 void RevertRemove(CacheRankingsBlock* rankings); | 130 void RevertRemove(CacheRankingsBlock* rankings); |
| 123 | 131 |
| 124 // Returns false if this entry will not be recognized as dirty (called during | 132 // Returns false if this entry will not be recognized as dirty (called during |
| 125 // selfcheck). | 133 // selfcheck). |
| 126 bool CheckEntry(CacheRankingsBlock* rankings); | 134 bool CheckEntry(CacheRankingsBlock* rankings); |
| 127 | 135 |
| 128 // Returns false if node is not properly linked. | 136 // Returns false if node is not properly linked. |
| 129 bool CheckLinks(CacheRankingsBlock* node, CacheRankingsBlock* prev, | 137 bool CheckLinks(CacheRankingsBlock* node, CacheRankingsBlock* prev, |
| 130 CacheRankingsBlock* next); | 138 CacheRankingsBlock* next, List list); |
| 131 | 139 |
| 132 // Checks the links between two consecutive nodes. | 140 // Checks the links between two consecutive nodes. |
| 133 bool CheckSingleLink(CacheRankingsBlock* prev, CacheRankingsBlock* next); | 141 bool CheckSingleLink(CacheRankingsBlock* prev, CacheRankingsBlock* next); |
| 134 | 142 |
| 143 // Peforms a simple check of the list, and returns the number of items or an |
| 144 // error code (negative value). |
| 145 int CheckList(List list); |
| 146 |
| 147 // Returns true if addr is the head or tail of any list. |
| 148 bool IsHead(CacheAddr addr); |
| 149 bool IsTail(CacheAddr addr); |
| 150 |
| 135 // Controls tracking of nodes used for enumerations. | 151 // Controls tracking of nodes used for enumerations. |
| 136 void TrackRankingsBlock(CacheRankingsBlock* node, bool start_tracking); | 152 void TrackRankingsBlock(CacheRankingsBlock* node, bool start_tracking); |
| 137 | 153 |
| 138 // Updates the iterators whenever node is being changed. | 154 // Updates the iterators whenever node is being changed. |
| 139 void UpdateIterators(CacheRankingsBlock* node); | 155 void UpdateIterators(CacheRankingsBlock* node); |
| 140 | 156 |
| 141 bool init_; | 157 bool init_; |
| 142 Addr head_; | 158 Addr heads_[LAST_ELEMENT]; |
| 143 Addr tail_; | 159 Addr tails_[LAST_ELEMENT]; |
| 144 BackendImpl* backend_; | 160 BackendImpl* backend_; |
| 145 LruData* control_data_; // Data related to the LRU lists. | 161 LruData* control_data_; // Data related to the LRU lists. |
| 146 IteratorList iterators_; | 162 IteratorList iterators_; |
| 147 | 163 |
| 148 DISALLOW_COPY_AND_ASSIGN(Rankings); | 164 DISALLOW_COPY_AND_ASSIGN(Rankings); |
| 149 }; | 165 }; |
| 150 | 166 |
| 151 } // namespace disk_cache | 167 } // namespace disk_cache |
| 152 | 168 |
| 153 #endif // NET_DISK_CACHE_RANKINGS_H_ | 169 #endif // NET_DISK_CACHE_RANKINGS_H_ |
| 154 | 170 |
| OLD | NEW |