| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_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/synchronization/lock.h" | |
| 12 #include "base/time/time.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace internal { | |
| 16 | |
| 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; | |
| 25 | |
| 26 // Release a previously acquired lock on the allocation so that it can be | |
| 27 // purged by the system. | |
| 28 virtual void ReleaseLock() = 0; | |
| 29 | |
| 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; | |
| 33 | |
| 34 protected: | |
| 35 virtual ~DiscardableMemoryManagerAllocation() {} | |
| 36 }; | |
| 37 | |
| 38 } // namespace internal | |
| 39 } // namespace base | |
| 40 | |
| 41 namespace base { | |
| 42 namespace internal { | |
| 43 | |
| 44 // The DiscardableMemoryManager manages a collection of | |
| 45 // DiscardableMemoryManagerAllocation instances. It is used on platforms that | |
| 46 // need some level of userspace control over discardable memory. It keeps track | |
| 47 // of all allocation instances (in case they need to be purged), and the total | |
| 48 // amount of allocated memory (in case this forces a purge). When memory usage | |
| 49 // reaches the limit, the manager purges the LRU memory. | |
| 50 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { | |
| 51 public: | |
| 52 typedef DiscardableMemoryManagerAllocation Allocation; | |
| 53 | |
| 54 DiscardableMemoryManager(size_t memory_limit, | |
| 55 size_t soft_memory_limit, | |
| 56 TimeDelta hard_memory_limit_expiration_time); | |
| 57 virtual ~DiscardableMemoryManager(); | |
| 58 | |
| 59 // The maximum number of bytes of memory that may be allocated before we force | |
| 60 // a purge. | |
| 61 void SetMemoryLimit(size_t bytes); | |
| 62 | |
| 63 // The number of bytes of memory that may be allocated but unused for the hard | |
| 64 // limit expiration time without getting purged. | |
| 65 void SetSoftMemoryLimit(size_t bytes); | |
| 66 | |
| 67 // Sets the memory usage cutoff time for hard memory limit. | |
| 68 void SetHardMemoryLimitExpirationTime( | |
| 69 TimeDelta hard_memory_limit_expiration_time); | |
| 70 | |
| 71 // This will attempt to reduce memory footprint until within soft memory | |
| 72 // limit. Returns true if there's no need to call this again until allocations | |
| 73 // have been used. | |
| 74 bool ReduceMemoryUsage(); | |
| 75 | |
| 76 // This can be called to attempt to reduce memory footprint until within | |
| 77 // limit for bytes to keep under moderate pressure. | |
| 78 void ReduceMemoryUsageUntilWithinLimit(size_t bytes); | |
| 79 | |
| 80 // Adds the given allocation to the manager's collection. | |
| 81 void Register(Allocation* allocation, size_t bytes); | |
| 82 | |
| 83 // Removes the given allocation from the manager's collection. | |
| 84 void Unregister(Allocation* allocation); | |
| 85 | |
| 86 // Returns false if an error occurred. Otherwise, returns true and sets | |
| 87 // |purged| to indicate whether or not allocation has been purged since last | |
| 88 // use. | |
| 89 bool AcquireLock(Allocation* allocation, bool* purged); | |
| 90 | |
| 91 // Release a previously acquired lock on allocation. This allows the manager | |
| 92 // to purge it if necessary. | |
| 93 void ReleaseLock(Allocation* allocation); | |
| 94 | |
| 95 // Purges all discardable memory. | |
| 96 void PurgeAll(); | |
| 97 | |
| 98 // Returns true if allocation has been added to the manager's collection. This | |
| 99 // should only be used by tests. | |
| 100 bool IsRegisteredForTest(Allocation* allocation) const; | |
| 101 | |
| 102 // Returns true if allocation can be purged. This should only be used by | |
| 103 // tests. | |
| 104 bool CanBePurgedForTest(Allocation* allocation) const; | |
| 105 | |
| 106 // Returns total amount of allocated discardable memory. This should only be | |
| 107 // used by tests. | |
| 108 size_t GetBytesAllocatedForTest() const; | |
| 109 | |
| 110 private: | |
| 111 struct AllocationInfo { | |
| 112 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} | |
| 113 | |
| 114 const size_t bytes; | |
| 115 bool purgable; | |
| 116 TimeTicks last_usage; | |
| 117 }; | |
| 118 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; | |
| 119 | |
| 120 // Purges memory not used since |hard_memory_limit_expiration_time_| before | |
| 121 // "right now" until usage is less or equal to |soft_memory_limit_|. | |
| 122 // Returns true if total amount of memory is less or equal to soft memory | |
| 123 // limit. | |
| 124 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | |
| 125 | |
| 126 // Purges memory that has not been used since |timestamp| until usage is less | |
| 127 // or equal to |limit|. | |
| 128 // Caller must acquire |lock_| prior to calling this function. | |
| 129 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( | |
| 130 TimeTicks timestamp, | |
| 131 size_t limit); | |
| 132 | |
| 133 // Called when a change to |bytes_allocated_| has been made. | |
| 134 void BytesAllocatedChanged(size_t new_bytes_allocated) const; | |
| 135 | |
| 136 // Virtual for tests. | |
| 137 virtual TimeTicks Now() const; | |
| 138 | |
| 139 // Needs to be held when accessing members. | |
| 140 mutable Lock lock_; | |
| 141 | |
| 142 // A MRU cache of all allocated bits of memory. Used for purging. | |
| 143 AllocationMap allocations_; | |
| 144 | |
| 145 // The total amount of allocated memory. | |
| 146 size_t bytes_allocated_; | |
| 147 | |
| 148 // The maximum number of bytes of memory that may be allocated. | |
| 149 size_t memory_limit_; | |
| 150 | |
| 151 // The number of bytes of memory that may be allocated but not used for | |
| 152 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle | |
| 153 // notification. | |
| 154 size_t soft_memory_limit_; | |
| 155 | |
| 156 // Amount of time it takes for an allocation to become affected by | |
| 157 // |soft_memory_limit_|. | |
| 158 TimeDelta hard_memory_limit_expiration_time_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); | |
| 161 }; | |
| 162 | |
| 163 } // namespace internal | |
| 164 } // namespace base | |
| 165 | |
| 166 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | |
| OLD | NEW |