Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: base/memory/discardable_memory_provider.h

Issue 17106004: Add discardable memory emulation for non-android/mac platforms (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: s/ASSERT_/EXPECT_/ Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.
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.
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);
willchan no longer on Chromium 2013/10/17 02:06:13 Make these test functions const if they don't muta
reveman 2013/10/19 21:17:04 Done.
90
91 // Returns true if discardable memory can be purged. This should only
92 // be used by tests.
93 bool CanBePurgedForTest(const DiscardableMemory* discardable);
94
95 // Returns total amount of allocated discardable memory. This should only
96 // be used by tests.
97 size_t GetBytesAllocatedForTest();
98
99 private:
100 friend class Singleton<DiscardableMemoryProvider>;
101
102 struct Allocation {
103 Allocation(size_t bytes)
willchan no longer on Chromium 2013/10/17 02:06:13 explicit
reveman 2013/10/19 21:17:04 Done.
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 |allocations_lock_| and |bytes_allocated_lock_|
119 // prior to calling this function.
120 void PurgeLRU(size_t limit);
121
122 // Purges least recently used memory based on the value of
123 // |bytes_to_reclaim_under_moderate_pressure_|.
124 void Purge();
125
126 // Ensures that we don't allocate beyond our memory limit.
127 void EnforcePolicy();
128
129 // A MRU cache of all allocated bits of discardable memory. Used for purging.
130 AllocationMap allocations_;
131
132 // The total amount of allocated discardable memory.
133 size_t bytes_allocated_;
134
135 // The maximum number of bytes of discardable memory that may be allocated
136 // before we assume moderate memory pressure.
137 size_t discardable_memory_limit_;
138
139 // Under moderate memory pressure, we will purge this amount of memory.
140 size_t bytes_to_reclaim_under_moderate_pressure_;
141
142 // Used to avoid reentry into EnforcePolicy.
143 bool enforcing_policy_;
144
145 // Needs to be held when accessing |allocations_|.
146 Lock allocations_lock_;
147
148 // Needs to be held when accessing |bytes_allocated_| or the two limits
149 // above.
150 Lock bytes_allocated_lock_;
151
152 // Allows us to be respond when the system reports that it is under memory
153 // pressure.
154 MemoryPressureListener memory_pressure_listener_;
155
156 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider);
157 };
158
159 } // namespace internal
160 } // namespace base
161
162 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698