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.
5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_
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"
15 class DiscardableMemory
;
18 #if defined(COMPILER_GCC)
19 namespace BASE_HASH_NAMESPACE
{
21 struct hash
<const base::DiscardableMemory
*> {
22 size_t operator()(const base::DiscardableMemory
* ptr
) const {
23 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
26 } // namespace BASE_HASH_NAMESPACE
32 // The DiscardableMemoryProvider manages a collection of emulated
33 // DiscardableMemory instances. It is used on platforms that do not support
34 // discardable memory natively. It keeps track of all DiscardableMemory
35 // instances (in case they need to be purged), and the total amount of
36 // allocated memory (in case this forces a purge).
38 // When notified of memory pressure, the provider either purges the LRU
39 // memory -- if the pressure is moderate -- or all discardable memory
40 // if the pressure is critical.
42 // NB - this class is an implementation detail. It has been exposed for testing
43 // purposes. You should not need to use this class directly.
44 class BASE_EXPORT_PRIVATE DiscardableMemoryProvider
{
46 DiscardableMemoryProvider();
47 ~DiscardableMemoryProvider();
49 // Call this to register memory pressure listener. Must be called on a
50 // thread with a MessageLoop current.
51 void RegisterMemoryPressureListener();
53 // Call this to unregister memory pressure listener.
54 void UnregisterMemoryPressureListener();
56 // The maximum number of bytes of discardable memory that may be allocated
57 // before we force a purge. If this amount is zero, it is interpreted as
58 // having no limit at all.
59 void SetDiscardableMemoryLimit(size_t bytes
);
61 // Sets the amount of memory to reclaim when we're under moderate pressure.
62 void SetBytesToReclaimUnderModeratePressure(size_t bytes
);
64 // Adds the given discardable memory to the provider's collection.
65 void Register(const DiscardableMemory
* discardable
, size_t bytes
);
67 // Removes the given discardable memory from the provider's collection.
68 void Unregister(const DiscardableMemory
* discardable
);
70 // Returns NULL if an error occurred. Otherwise, returns the backing buffer
71 // and sets |purged| to indicate whether or not the backing buffer has been
72 // purged since last use.
73 scoped_ptr
<uint8
, FreeDeleter
> Acquire(
74 const DiscardableMemory
* discardable
, bool* purged
);
76 // Release a previously acquired backing buffer. This gives the buffer back
77 // to the provider where it can be purged if necessary.
78 void Release(const DiscardableMemory
* discardable
,
79 scoped_ptr
<uint8
, FreeDeleter
> memory
);
81 // Purges all discardable memory.
84 // Returns true if discardable memory has been added to the provider's
85 // collection. This should only be used by tests.
86 bool IsRegisteredForTest(const DiscardableMemory
* discardable
) const;
88 // Returns true if discardable memory can be purged. This should only
90 bool CanBePurgedForTest(const DiscardableMemory
* discardable
) const;
92 // Returns total amount of allocated discardable memory. This should only
94 size_t GetBytesAllocatedForTest() const;
98 explicit Allocation(size_t bytes
)
106 typedef HashingMRUCache
<const DiscardableMemory
*, Allocation
> AllocationMap
;
108 // This can be called as a hint that the system is under memory pressure.
109 void OnMemoryPressure(
110 MemoryPressureListener::MemoryPressureLevel pressure_level
);
112 // Purges |bytes_to_reclaim_under_moderate_pressure_| bytes of
113 // discardable memory.
116 // Purges least recently used memory until usage is less or equal to |limit|.
117 // Caller must acquire |lock_| prior to calling this function.
118 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit
);
120 // Ensures that we don't allocate beyond our memory limit.
121 // Caller must acquire |lock_| prior to calling this function.
122 void EnforcePolicyWithLockAcquired();
124 // Needs to be held when accessing members.
127 // A MRU cache of all allocated bits of discardable memory. Used for purging.
128 AllocationMap allocations_
;
130 // The total amount of allocated discardable memory.
131 size_t bytes_allocated_
;
133 // The maximum number of bytes of discardable memory that may be allocated
134 // before we assume moderate memory pressure.
135 size_t discardable_memory_limit_
;
137 // Under moderate memory pressure, we will purge this amount of memory.
138 size_t bytes_to_reclaim_under_moderate_pressure_
;
140 // Allows us to be respond when the system reports that it is under memory
142 scoped_ptr
<MemoryPressureListener
> memory_pressure_listener_
;
144 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider
);
147 } // namespace internal
150 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_