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/memory/singleton.h" | |
13 #include "base/synchronization/lock.h" | |
14 | |
15 namespace base { | |
16 class DiscardableMemory; | |
17 class DiscardableMemoryProviderTest; | |
18 } // namespace base | |
19 | |
20 #if defined(COMPILER_GCC) | |
21 namespace BASE_HASH_NAMESPACE { | |
22 template <> | |
23 struct hash<const base::DiscardableMemory*> { | |
24 size_t operator()(const base::DiscardableMemory* ptr) const { | |
25 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
26 } | |
27 }; | |
28 } // namespace BASE_HASH_NAMESPACE | |
29 #endif // COMPILER | |
30 | |
31 namespace base { | |
32 namespace internal { | |
33 | |
34 // The DiscardableMemoryProvider manages a collection of emulated | |
35 // DiscardableMemory instances. It is used on platforms that do not support | |
36 // discardable memory natively. It keeps track of all DiscardableMemory | |
37 // instances (in case they need to be purged), and the total amount of | |
38 // allocated memory (in case this forces a purge). | |
39 // | |
40 // When notified of memory pressure, the provider either purges the LRU | |
41 // memory -- if the pressure is moderate -- or all discardable memory | |
42 // if the pressure is critical. | |
43 // | |
44 // NB - this class is an implementation detail. It has been exposed for testing | |
45 // purposes. You should not need to use this class directly. | |
46 class BASE_EXPORT_PRIVATE DiscardableMemoryProvider { | |
47 public: | |
48 DiscardableMemoryProvider(); | |
49 ~DiscardableMemoryProvider(); | |
50 | |
51 static DiscardableMemoryProvider* GetInstance(); | |
52 | |
53 // Sets the instance of DiscardableMemoryProvider to be returned by | |
54 // GetInstance. This should only be used by tests and must be called | |
55 // prior to GetInstance(). The ownership of the given provider is | |
56 // retained by the caller. | |
57 static void SetInstanceForTest(DiscardableMemoryProvider* provider); | |
58 | |
59 // The maximum number of bytes of discardable memory that may be allocated | |
60 // before we assume moderate memory pressure. If this amount is zero, it is | |
61 // interpreted as having no limit at all. | |
62 void SetDiscardableMemoryLimit(size_t bytes); | |
63 | |
64 // Sets the amount of memory to reclaim when we're under moderate pressure. | |
65 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | |
66 | |
67 // Adds the given discardable memory to the provider's collection. | |
68 void Register(const DiscardableMemory* discardable, size_t bytes); | |
69 | |
70 // Removes the given discardable memory from the provider's collection. | |
71 void Unregister(const DiscardableMemory* discardable); | |
72 | |
73 // Acquire the backing buffer for the given discardable memory. |purged| | |
74 // is set to true if the backing has been purged since last use. Returns | |
75 // NULL if an error occurred or discardable memory limit has been reached. | |
willchan no longer on Chromium
2013/10/21 21:29:12
This comment does not discuss what happens to |pur
reveman
2013/10/22 00:11:42
Done.
| |
76 scoped_ptr<uint8, FreeDeleter> Acquire( | |
77 const DiscardableMemory* discardable, bool* purged); | |
78 | |
79 // Release a previously acquired backing buffer. This gives the buffer back | |
80 // to the provider where it can be purged it necessary. | |
willchan no longer on Chromium
2013/10/21 21:29:12
if necessary
reveman
2013/10/22 00:11:42
Done.
| |
81 void Release(const DiscardableMemory* discardable, | |
82 scoped_ptr<uint8, FreeDeleter> memory); | |
83 | |
84 // Purges all discardable memory. | |
85 void PurgeAll(); | |
86 | |
87 // Returns true if discardable memory has been added to the provider's | |
88 // collection. This should only be used by tests. | |
89 bool IsRegisteredForTest(const DiscardableMemory* discardable) const; | |
90 | |
91 // Returns true if discardable memory can be purged. This should only | |
92 // be used by tests. | |
93 bool CanBePurgedForTest(const DiscardableMemory* discardable) const; | |
94 | |
95 // Returns total amount of allocated discardable memory. This should only | |
96 // be used by tests. | |
97 size_t GetBytesAllocatedForTest() const; | |
98 | |
99 private: | |
100 friend class Singleton<DiscardableMemoryProvider>; | |
101 | |
102 struct Allocation { | |
103 explicit Allocation(size_t bytes) | |
104 : bytes(bytes), | |
105 memory(NULL) { | |
106 } | |
107 | |
108 size_t bytes; | |
109 uint8* memory; | |
110 }; | |
111 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; | |
112 | |
113 // This can be called as a hint that the system is under memory pressure. | |
114 static void NotifyMemoryPressure( | |
115 MemoryPressureListener::MemoryPressureLevel pressure_level); | |
116 | |
117 // Purges least recently used memory until usage is below |limit|. | |
118 // Caller must acquire |lock_| prior to calling this function. | |
119 void PurgeLRU(size_t limit); | |
willchan no longer on Chromium
2013/10/21 21:29:12
It's not obvious from the function name what |limi
reveman
2013/10/22 00:11:42
At the risk of making it too long, it's now "Purge
| |
120 | |
121 // Purges least recently used memory based on the value of | |
122 // |bytes_to_reclaim_under_moderate_pressure_|. | |
123 void Purge(); | |
124 | |
125 // Ensures that we don't allocate beyond our memory limit. | |
126 // Caller must acquire |lock_| prior to calling this function. | |
127 void EnforcePolicy(); | |
128 | |
129 // Needs to be held when accessing members. | |
130 mutable Lock lock_; | |
131 | |
132 // A MRU cache of all allocated bits of discardable memory. Used for purging. | |
133 AllocationMap allocations_; | |
134 | |
135 // The total amount of allocated discardable memory. | |
136 size_t bytes_allocated_; | |
137 | |
138 // The maximum number of bytes of discardable memory that may be allocated | |
139 // before we assume moderate memory pressure. | |
140 size_t discardable_memory_limit_; | |
141 | |
142 // Under moderate memory pressure, we will purge this amount of memory. | |
143 size_t bytes_to_reclaim_under_moderate_pressure_; | |
144 | |
145 // Allows us to be respond when the system reports that it is under memory | |
146 // pressure. | |
147 MemoryPressureListener memory_pressure_listener_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | |
150 }; | |
151 | |
152 } // namespace internal | |
153 } // namespace base | |
154 | |
155 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
OLD | NEW |