Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / base / memory / discardable_memory_manager.h
blob8bf92891a3ea967d897053f138d09030de808e62
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/synchronization/lock.h"
12 #include "base/time/time.h"
14 namespace base {
15 namespace internal {
17 // This interface is used by the DiscardableMemoryManager class to provide some
18 // level of userspace control over discardable memory allocations.
19 class DiscardableMemoryManagerAllocation {
20 public:
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
23 // resident.
24 virtual bool AllocateAndAcquireLock() = 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;
34 // Check if allocated memory is still resident. It is illegal to call this
35 // while a lock is acquired on the allocation.
36 virtual bool IsMemoryResident() const = 0;
38 protected:
39 virtual ~DiscardableMemoryManagerAllocation() {}
42 } // namespace internal
43 } // namespace base
45 namespace base {
46 namespace internal {
48 // The DiscardableMemoryManager manages a collection of
49 // DiscardableMemoryManagerAllocation instances. It is used on platforms that
50 // need some level of userspace control over discardable memory. It keeps track
51 // of all allocation instances (in case they need to be purged), and the total
52 // amount of allocated memory (in case this forces a purge). When memory usage
53 // reaches the limit, the manager purges the LRU memory.
54 class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
55 public:
56 typedef DiscardableMemoryManagerAllocation Allocation;
58 DiscardableMemoryManager(size_t memory_limit,
59 size_t soft_memory_limit,
60 TimeDelta hard_memory_limit_expiration_time);
61 virtual ~DiscardableMemoryManager();
63 // The maximum number of bytes of memory that may be allocated before we force
64 // a purge.
65 void SetMemoryLimit(size_t bytes);
67 // The number of bytes of memory that may be allocated but unused for the hard
68 // limit expiration time without getting purged.
69 void SetSoftMemoryLimit(size_t bytes);
71 // Sets the memory usage cutoff time for hard memory limit.
72 void SetHardMemoryLimitExpirationTime(
73 TimeDelta hard_memory_limit_expiration_time);
75 // This will make sure that all purged memory is released to the OS.
76 void ReleaseFreeMemory();
78 // This will attempt to reduce memory footprint until within soft memory
79 // limit. Returns true if there's no need to call this again until allocations
80 // have been used.
81 bool ReduceMemoryUsage();
83 // This can be called to attempt to reduce memory footprint until within
84 // limit for bytes to keep under moderate pressure.
85 void ReduceMemoryUsageUntilWithinLimit(size_t bytes);
87 // Adds the given allocation to the manager's collection.
88 void Register(Allocation* allocation, size_t bytes);
90 // Removes the given allocation from the manager's collection.
91 void Unregister(Allocation* allocation);
93 // Returns false if an error occurred. Otherwise, returns true and sets
94 // |purged| to indicate whether or not allocation has been purged since last
95 // use.
96 bool AcquireLock(Allocation* allocation, bool* purged);
98 // Release a previously acquired lock on allocation. This allows the manager
99 // to purge it if necessary.
100 void ReleaseLock(Allocation* allocation);
102 // Purges all discardable memory.
103 void PurgeAll();
105 // Returns true if allocation has been added to the manager's collection. This
106 // should only be used by tests.
107 bool IsRegisteredForTest(Allocation* allocation) const;
109 // Returns true if allocation can be purged. This should only be used by
110 // tests.
111 bool CanBePurgedForTest(Allocation* allocation) const;
113 // Returns total amount of allocated discardable memory. This should only be
114 // used by tests.
115 size_t GetBytesAllocatedForTest() const;
117 private:
118 struct AllocationInfo {
119 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {}
121 const size_t bytes;
122 bool purgable;
123 TimeTicks last_usage;
125 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap;
127 // Purges memory not used since |hard_memory_limit_expiration_time_| before
128 // "right now" until usage is less or equal to |soft_memory_limit_|.
129 // Returns true if total amount of memory is less or equal to soft memory
130 // limit.
131 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit();
133 // Purges memory that has not been used since |timestamp| until usage is less
134 // or equal to |limit|.
135 // Caller must acquire |lock_| prior to calling this function.
136 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(
137 TimeTicks timestamp,
138 size_t limit);
140 // Called when a change to |bytes_allocated_| has been made.
141 void BytesAllocatedChanged(size_t new_bytes_allocated) const;
143 // Virtual for tests.
144 virtual TimeTicks Now() const;
146 // Needs to be held when accessing members.
147 mutable Lock lock_;
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 // The number of bytes of memory that may be allocated but not used for
159 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle
160 // notification.
161 size_t soft_memory_limit_;
163 // Amount of time it takes for an allocation to become affected by
164 // |soft_memory_limit_|.
165 TimeDelta hard_memory_limit_expiration_time_;
167 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager);
170 } // namespace internal
171 } // namespace base
173 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_