• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DISK_CACHE_BLOCKFILE_EVICTION_H_
6 #define NET_DISK_CACHE_BLOCKFILE_EVICTION_H_
7 
8 #include "base/memory/raw_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/disk_cache/blockfile/rankings.h"
11 
12 namespace disk_cache {
13 
14 class BackendImpl;
15 class EntryImpl;
16 struct IndexHeader;
17 
18 // This class implements the eviction algorithm for the cache and it is tightly
19 // integrated with BackendImpl.
20 class Eviction {
21  public:
22   Eviction();
23 
24   Eviction(const Eviction&) = delete;
25   Eviction& operator=(const Eviction&) = delete;
26 
27   ~Eviction();
28 
29   void Init(BackendImpl* backend);
30   void Stop();
31 
32   // Deletes entries from the cache until the current size is below the limit.
33   // If empty is true, the whole cache will be trimmed, regardless of being in
34   // use.
35   void TrimCache(bool empty);
36 
37   // Updates the ranking information for an entry.
38   void UpdateRank(EntryImpl* entry, bool modified);
39 
40   // Notifications of interesting events for a given entry.
41   void OnOpenEntry(EntryImpl* entry);
42   void OnCreateEntry(EntryImpl* entry);
43   void OnDoomEntry(EntryImpl* entry);
44   void OnDestroyEntry(EntryImpl* entry);
45 
46   // Testing interface.
47   void SetTestMode();
48   void TrimDeletedList(bool empty);
49 
50  private:
51   void PostDelayedTrim();
52   void DelayedTrim();
53   bool ShouldTrim();
54   bool ShouldTrimDeleted();
55   void ReportTrimTimes(EntryImpl* entry);
56   Rankings::List GetListForEntry(EntryImpl* entry);
57   bool EvictEntry(CacheRankingsBlock* node, bool empty, Rankings::List list);
58 
59   // We'll just keep for a while a separate set of methods that implement the
60   // new eviction algorithm. This code will replace the original methods when
61   // finished.
62   void TrimCacheV2(bool empty);
63   void UpdateRankV2(EntryImpl* entry, bool modified);
64   void OnOpenEntryV2(EntryImpl* entry);
65   void OnCreateEntryV2(EntryImpl* entry);
66   void OnDoomEntryV2(EntryImpl* entry);
67   void OnDestroyEntryV2(EntryImpl* entry);
68   Rankings::List GetListForEntryV2(EntryImpl* entry);
69   void TrimDeleted(bool empty);
70   bool RemoveDeletedNode(CacheRankingsBlock* node);
71 
72   bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
73   int SelectListByLength(Rankings::ScopedRankingsBlock* next);
74   void ReportListStats();
75 
76   raw_ptr<BackendImpl> backend_ = nullptr;
77   raw_ptr<Rankings> rankings_;
78 
79   // May point to a mapped file's unmapped memory at destruction time.
80   raw_ptr<IndexHeader, DisableDanglingPtrDetection> header_;
81 
82   int max_size_;
83   int trim_delays_;
84   int index_size_;
85   bool new_eviction_;
86   bool first_trim_;
87   bool trimming_;
88   bool delay_trim_;
89   bool init_ = false;
90   bool test_mode_;
91   base::WeakPtrFactory<Eviction> ptr_factory_{this};
92 };
93 
94 }  // namespace disk_cache
95 
96 #endif  // NET_DISK_CACHE_BLOCKFILE_EVICTION_H_
97