1 // Copyright 2014 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_MANAGER_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_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"
17 // This interface is used by the DiscardableMemoryManager class to provide some
18 // level of userspace control over discardable memory allocations.
19 class DiscardableMemoryManagerAllocation
{
21 // Allocate and acquire a lock that prevents the allocation from being purged
22 // by the system. Returns true if memory was previously allocated and is still
24 virtual bool AllocateAndAcquireLock(size_t bytes
) = 0;
26 // Release a previously acquired lock on the allocation so that it can be
27 // purged by the system.
28 virtual void ReleaseLock() = 0;
30 // Explicitly purge this allocation. It is illegal to call this while a lock
31 // is acquired on the allocation.
32 virtual void Purge() = 0;
35 virtual ~DiscardableMemoryManagerAllocation() {}
38 } // namespace internal
41 #if defined(COMPILER_GCC)
42 namespace BASE_HASH_NAMESPACE
{
44 struct hash
<base::internal::DiscardableMemoryManagerAllocation
*> {
46 base::internal::DiscardableMemoryManagerAllocation
* ptr
) const {
47 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
50 } // namespace BASE_HASH_NAMESPACE
56 // The DiscardableMemoryManager manages a collection of
57 // DiscardableMemoryManagerAllocation instances. It is used on platforms that
58 // need some level of userspace control over discardable memory. It keeps track
59 // of all allocation instances (in case they need to be purged), and the total
60 // amount of allocated memory (in case this forces a purge). When memory usage
61 // reaches the limit, the manager purges the LRU memory.
63 // When notified of memory pressure, the manager either purges the LRU memory --
64 // if the pressure is moderate -- or all discardable memory if the pressure is
66 class BASE_EXPORT_PRIVATE DiscardableMemoryManager
{
68 typedef DiscardableMemoryManagerAllocation Allocation
;
70 DiscardableMemoryManager();
71 ~DiscardableMemoryManager();
73 // Call this to register memory pressure listener. Must be called on a thread
74 // with a MessageLoop current.
75 void RegisterMemoryPressureListener();
77 // Call this to unregister memory pressure listener.
78 void UnregisterMemoryPressureListener();
80 // The maximum number of bytes of memory that may be allocated before we force
81 // a purge. If this amount is zero, it is interpreted as having no limit at
83 void SetMemoryLimit(size_t bytes
);
85 // Sets the amount of memory to keep when we're under moderate pressure.
86 void SetBytesToKeepUnderModeratePressure(size_t bytes
);
88 // Adds the given allocation to the manager's collection.
89 void Register(Allocation
* allocation
, size_t bytes
);
91 // Removes the given allocation from the manager's collection.
92 void Unregister(Allocation
* allocation
);
94 // Returns false if an error occurred. Otherwise, returns true and sets
95 // |purged| to indicate whether or not allocation has been purged since last
97 bool AcquireLock(Allocation
* allocation
, bool* purged
);
99 // Release a previously acquired lock on allocation. This allows the manager
100 // to purge it if necessary.
101 void ReleaseLock(Allocation
* allocation
);
103 // Purges all discardable memory.
106 // Returns true if allocation has been added to the manager's collection. This
107 // should only be used by tests.
108 bool IsRegisteredForTest(Allocation
* allocation
) const;
110 // Returns true if allocation can be purged. This should only be used by
112 bool CanBePurgedForTest(Allocation
* allocation
) const;
114 // Returns total amount of allocated discardable memory. This should only be
116 size_t GetBytesAllocatedForTest() const;
119 struct AllocationInfo
{
120 explicit AllocationInfo(size_t bytes
) : bytes(bytes
), purgable(false) {}
125 typedef HashingMRUCache
<Allocation
*, AllocationInfo
> AllocationMap
;
127 // This can be called as a hint that the system is under memory pressure.
128 void OnMemoryPressure(
129 MemoryPressureListener::MemoryPressureLevel pressure_level
);
131 // Purges memory until usage is within
132 // |bytes_to_keep_under_moderate_pressure_|.
135 // Purges least recently used memory until usage is less or equal to |limit|.
136 // Caller must acquire |lock_| prior to calling this function.
137 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit
);
139 // Ensures that we don't allocate beyond our memory limit. Caller must acquire
140 // |lock_| prior to calling this function.
141 void EnforcePolicyWithLockAcquired();
143 // Called when a change to |bytes_allocated_| has been made.
144 void BytesAllocatedChanged() const;
146 // Needs to be held when accessing members.
149 // A MRU cache of all allocated bits of memory. Used for purging.
150 AllocationMap allocations_
;
152 // The total amount of allocated memory.
153 size_t bytes_allocated_
;
155 // The maximum number of bytes of memory that may be allocated.
156 size_t memory_limit_
;
158 // Under moderate memory pressure, we will purge memory until usage is within
160 size_t bytes_to_keep_under_moderate_pressure_
;
162 // Allows us to be respond when the system reports that it is under memory
164 scoped_ptr
<MemoryPressureListener
> memory_pressure_listener_
;
166 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager
);
169 } // namespace internal
172 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_