OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
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 BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
7 | |
8 #include "base/base_export.h" | |
9 #include "base/containers/hash_tables.h" | |
10 #include "base/containers/mru_cache.h" | |
11 #include "base/memory/memory_pressure_listener.h" | |
12 #include "base/synchronization/lock.h" | |
13 | |
14 namespace base { | |
15 class DiscardableMemory; | |
16 } // namespace base | |
17 | |
18 #if defined(COMPILER_GCC) | |
19 namespace BASE_HASH_NAMESPACE { | |
20 template <> | |
21 struct hash<base::DiscardableMemory*> { | |
22 size_t operator()(base::DiscardableMemory* ptr) const { | |
23 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
24 } | |
25 }; | |
26 } // namespace BASE_HASH_NAMESPACE | |
27 #endif // COMPILER | |
28 | |
29 namespace base { | |
30 | |
31 // The DiscardableMemoryProvider is used on platforms that do not support | |
willchan no longer on Chromium
2013/06/25 21:06:50
It's not clear if DiscardableMemoryProvider is sup
Ian Vollick
2013/06/26 01:10:13
It's an implementation detail. It's only got a hea
| |
32 // discardable memory natively. It keeps track of all DiscardableMemory | |
33 // instances (in case they need to be purged), and the total amount of allocated | |
34 // memory (in case this forces a purge). | |
35 class BASE_EXPORT DiscardableMemoryProvider { | |
36 public: | |
37 // The bool here is just a placeholder. The MRU cache acts like a map, but we | |
38 // only actually care about the keys. | |
39 typedef HashingMRUCache<DiscardableMemory*, bool> AllocationMap; | |
40 | |
41 DiscardableMemoryProvider(); | |
42 virtual ~DiscardableMemoryProvider(); | |
43 | |
44 static DiscardableMemoryProvider* GetInstance(); | |
willchan no longer on Chromium
2013/06/25 21:06:50
Who calls this? Why is it public? It looks like it
Ian Vollick
2013/06/26 01:10:13
Made it private.
| |
45 | |
46 // This can be called as a hint that the system is under memory pressure. | |
47 static void NotifyMemoryPressure( | |
willchan no longer on Chromium
2013/06/25 21:06:50
Why is this public too? Who is supposed to be call
Ian Vollick
2013/06/26 01:10:13
It's only called by the MemoryPressureListener. It
| |
48 MemoryPressureListener::MemoryPressureLevel pressureLevel); | |
willchan no longer on Chromium
2013/06/25 21:06:50
pressure_level
Ian Vollick
2013/06/26 01:10:13
Done.
| |
49 | |
50 // The maximum number of bytes of discardable memory that may be allocated | |
51 // before we assume moderate memory pressure. If this amount is zero, it is | |
52 // interpreted as having no limit at all. | |
53 void SetDiscardableMemoryLimit(size_t bytes); | |
54 | |
55 // Gets the maximum amount of discardable memory that may be allocated. | |
56 size_t discardable_memory_limit() const; | |
57 | |
58 // Sets the amount of memory to reclaim when we're under moderate pressure. | |
59 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | |
60 | |
61 // Gets the amount of memory to reclaim when we're under moderate pressure. | |
62 size_t bytes_to_reclaim_under_moderate_pressure() const; | |
63 | |
64 private: | |
65 friend class DiscardableMemory; | |
66 friend class DiscardableMemoryProviderTest; | |
67 | |
68 // Sets the instance of DiscardableMemoryProvider to be returned by | |
69 // GetInstance. This should only be used by tests. The ownership of the given | |
70 // provider is retained by the caller. | |
71 static void SetInstanceForTest(DiscardableMemoryProvider* provider); | |
72 | |
73 // Adds the given discardable memory to the provider's collection. If the | |
74 // discardable has already been added, it is first unregistered and its | |
75 // memory deallocated. | |
76 void Register(DiscardableMemory* discardable); | |
77 | |
78 // Removes the given discardable memory from the provider's collection. | |
79 void Unregister(DiscardableMemory* discardable); | |
80 | |
81 // Called by discardable memory instances to allow the provider to keep track | |
82 // of the total amout of discardable memory allocated. | |
83 void DidAllocate(size_t bytes); | |
84 | |
85 // Called by discardable memory instances to allow the provider to keep track | |
86 // of the total amount of discardable memory allocated. | |
87 void DidDeallocate(size_t bytes); | |
88 | |
89 // Called by discardable memory instances to allow the provider to maintain | |
90 // its MRU cache. Returns false if the discardable has not been registered. | |
91 bool DidAccess(DiscardableMemory* discardable); | |
92 | |
93 // Purges all discardable memory. | |
94 void PurgeAll(); | |
95 | |
96 // Purges least recently used memory. | |
97 void PurgeLRU(); | |
98 | |
99 // Ensures that we don't allocate beyond our memory limit. | |
100 void EnforcePolicy(); | |
101 | |
102 // A MRU cache of all allocated bits of discardable memory. Used for purging. | |
103 AllocationMap allocations_; | |
104 | |
105 // The total amount of allocated discardable memory. | |
106 size_t bytes_allocated_; | |
107 | |
108 // The maximum number of bytes of discardable memory that may be allocated | |
109 // before we assume moderate memory pressure. | |
110 size_t discardable_memory_limit_; | |
111 | |
112 // Under moderate memory pressure, we will purge this amount of memory. | |
113 size_t bytes_to_reclaim_under_moderate_pressure_; | |
114 | |
115 // Used to avoid reentry into EnforcePolicy. | |
116 bool enforcing_policy_; | |
117 | |
118 // Needs to be held when accessing |allocations_|. | |
119 Lock allocations_lock_; | |
120 | |
121 // Needs to be held when accessing |bytes_allocated_| or the two limits above. | |
122 // It's mutable because it must be accessed in a const function. | |
123 mutable Lock bytes_allocated_lock_; | |
124 | |
125 // Allows us to be respond when the system reports that it is under memory | |
126 // pressure. | |
127 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | |
willchan no longer on Chromium
2013/06/25 21:06:50
Why bother with a scoped_ptr?
Ian Vollick
2013/06/26 01:10:13
Got rid of the scoped_ptr.
| |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | |
130 }; | |
131 | |
132 } // namespace base | |
133 | |
134 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
OLD | NEW |