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

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: . Created 7 years, 6 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/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
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();
45
46 // This can be called as a hint that the system is under memory pressure.
47 static void NotifyMemoryPressure(
48 MemoryPressureListener::MemoryPressureLevel pressureLevel);
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 // Needs to be held when accessing |allocations_|.
116 Lock allocations_lock_;
117
118 // Needs to be held when accessing |bytes_allocated_| or the two limits above.
119 Lock bytes_allocated_lock_;
120
121 // Allows us to be respond when the system reports that it is under memory
122 // pressure.
123 scoped_ptr<MemoryPressureListener> memory_pressure_listener_;
124
125 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider);
126 };
127
128 } // namespace base
129
130 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698