| OLD | NEW | 
|    1 // Copyright 2014 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |    5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 
|    6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |    6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 
|    7  |    7  | 
|    8 #include "base/base_export.h" |    8 #include "base/base_export.h" | 
|    9 #include "base/containers/hash_tables.h" |    9 #include "base/containers/hash_tables.h" | 
|   10 #include "base/containers/mru_cache.h" |   10 #include "base/containers/mru_cache.h" | 
 |   11 #include "base/memory/memory_pressure_listener.h" | 
|   11 #include "base/synchronization/lock.h" |   12 #include "base/synchronization/lock.h" | 
|   12 #include "base/time/time.h" |   13 #include "base/time/time.h" | 
|   13  |   14  | 
|   14 namespace base { |   15 namespace base { | 
|   15 namespace internal { |   16 namespace internal { | 
|   16  |   17  | 
|   17 // This interface is used by the DiscardableMemoryManager class to provide some |   18 // This interface is used by the DiscardableMemoryManager class to provide some | 
|   18 // level of userspace control over discardable memory allocations. |   19 // level of userspace control over discardable memory allocations. | 
|   19 class DiscardableMemoryManagerAllocation { |   20 class DiscardableMemoryManagerAllocation { | 
|   20  public: |   21  public: | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   52  |   53  | 
|   53 namespace base { |   54 namespace base { | 
|   54 namespace internal { |   55 namespace internal { | 
|   55  |   56  | 
|   56 // The DiscardableMemoryManager manages a collection of |   57 // The DiscardableMemoryManager manages a collection of | 
|   57 // DiscardableMemoryManagerAllocation instances. It is used on platforms that |   58 // DiscardableMemoryManagerAllocation instances. It is used on platforms that | 
|   58 // need some level of userspace control over discardable memory. It keeps track |   59 // 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 // 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 // amount of allocated memory (in case this forces a purge). When memory usage | 
|   61 // reaches the limit, the manager purges the LRU memory. |   62 // reaches the limit, the manager purges the LRU memory. | 
 |   63 // | 
 |   64 // When notified of memory pressure, the manager either purges the LRU memory -- | 
 |   65 // if the pressure is moderate -- or all discardable memory if the pressure is | 
 |   66 // critical. | 
|   62 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { |   67 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { | 
|   63  public: |   68  public: | 
|   64   typedef DiscardableMemoryManagerAllocation Allocation; |   69   typedef DiscardableMemoryManagerAllocation Allocation; | 
|   65  |   70  | 
|   66   DiscardableMemoryManager(size_t memory_limit, |   71   DiscardableMemoryManager(size_t memory_limit, | 
|   67                            size_t soft_memory_limit, |   72                            size_t soft_memory_limit, | 
 |   73                            size_t bytes_to_keep_under_moderate_pressure, | 
|   68                            TimeDelta hard_memory_limit_expiration_time); |   74                            TimeDelta hard_memory_limit_expiration_time); | 
|   69   virtual ~DiscardableMemoryManager(); |   75   virtual ~DiscardableMemoryManager(); | 
|   70  |   76  | 
 |   77   // Call this to register memory pressure listener. Must be called on a thread | 
 |   78   // with a MessageLoop current. | 
 |   79   void RegisterMemoryPressureListener(); | 
 |   80  | 
 |   81   // Call this to unregister memory pressure listener. | 
 |   82   void UnregisterMemoryPressureListener(); | 
 |   83  | 
|   71   // The maximum number of bytes of memory that may be allocated before we force |   84   // The maximum number of bytes of memory that may be allocated before we force | 
|   72   // a purge. |   85   // a purge. | 
|   73   void SetMemoryLimit(size_t bytes); |   86   void SetMemoryLimit(size_t bytes); | 
|   74  |   87  | 
|   75   // The number of bytes of memory that may be allocated but unused for the hard |   88   // The number of bytes of memory that may be allocated but unused for the hard | 
|   76   // limit expiration time without getting purged. |   89   // limit expiration time without getting purged. | 
|   77   void SetSoftMemoryLimit(size_t bytes); |   90   void SetSoftMemoryLimit(size_t bytes); | 
|   78  |   91  | 
 |   92   // Sets the amount of memory to keep when we're under moderate pressure. | 
 |   93   void SetBytesToKeepUnderModeratePressure(size_t bytes); | 
 |   94  | 
|   79   // Sets the memory usage cutoff time for hard memory limit. |   95   // Sets the memory usage cutoff time for hard memory limit. | 
|   80   void SetHardMemoryLimitExpirationTime( |   96   void SetHardMemoryLimitExpirationTime( | 
|   81       TimeDelta hard_memory_limit_expiration_time); |   97       TimeDelta hard_memory_limit_expiration_time); | 
|   82  |   98  | 
|   83   // This will attempt to reduce memory footprint until within soft memory |   99   // This will attempt to reduce memory footprint until within soft memory | 
|   84   // limit. Returns true if there's no need to call this again until allocations |  100   // limit. Returns true if there's no need to call this again until allocations | 
|   85   // have been used. |  101   // have been used. | 
|   86   bool ReduceMemoryUsage(); |  102   bool ReduceMemoryUsage(); | 
|   87  |  103  | 
|   88   // This can be called to attempt to reduce memory footprint until within |  | 
|   89   // limit for bytes to keep under moderate pressure. |  | 
|   90   void ReduceMemoryUsageUntilWithinLimit(size_t bytes); |  | 
|   91  |  | 
|   92   // Adds the given allocation to the manager's collection. |  104   // Adds the given allocation to the manager's collection. | 
|   93   void Register(Allocation* allocation, size_t bytes); |  105   void Register(Allocation* allocation, size_t bytes); | 
|   94  |  106  | 
|   95   // Removes the given allocation from the manager's collection. |  107   // Removes the given allocation from the manager's collection. | 
|   96   void Unregister(Allocation* allocation); |  108   void Unregister(Allocation* allocation); | 
|   97  |  109  | 
|   98   // Returns false if an error occurred. Otherwise, returns true and sets |  110   // Returns false if an error occurred. Otherwise, returns true and sets | 
|   99   // |purged| to indicate whether or not allocation has been purged since last |  111   // |purged| to indicate whether or not allocation has been purged since last | 
|  100   // use. |  112   // use. | 
|  101   bool AcquireLock(Allocation* allocation, bool* purged); |  113   bool AcquireLock(Allocation* allocation, bool* purged); | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
|  122  private: |  134  private: | 
|  123   struct AllocationInfo { |  135   struct AllocationInfo { | 
|  124     explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} |  136     explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} | 
|  125  |  137  | 
|  126     const size_t bytes; |  138     const size_t bytes; | 
|  127     bool purgable; |  139     bool purgable; | 
|  128     TimeTicks last_usage; |  140     TimeTicks last_usage; | 
|  129   }; |  141   }; | 
|  130   typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; |  142   typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; | 
|  131  |  143  | 
 |  144   // This can be called as a hint that the system is under memory pressure. | 
 |  145   void OnMemoryPressure( | 
 |  146       MemoryPressureListener::MemoryPressureLevel pressure_level); | 
 |  147  | 
 |  148   // Purges memory until usage is less or equal to | 
 |  149   // |bytes_to_keep_under_moderate_pressure_|. | 
 |  150   void PurgeUntilWithinBytesToKeepUnderModeratePressure(); | 
 |  151  | 
|  132   // Purges memory not used since |hard_memory_limit_expiration_time_| before |  152   // Purges memory not used since |hard_memory_limit_expiration_time_| before | 
|  133   // "right now" until usage is less or equal to |soft_memory_limit_|. |  153   // "right now" until usage is less or equal to |soft_memory_limit_|. | 
|  134   // Returns true if total amount of memory is less or equal to soft memory |  154   // Returns true if total amount of memory is less or equal to soft memory | 
|  135   // limit. |  155   // limit. | 
|  136   bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |  156   bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | 
|  137  |  157  | 
|  138   // Purges memory that has not been used since |timestamp| until usage is less |  158   // Purges memory that has not been used since |timestamp| until usage is less | 
|  139   // or equal to |limit|. |  159   // or equal to |limit|. | 
|  140   // Caller must acquire |lock_| prior to calling this function. |  160   // Caller must acquire |lock_| prior to calling this function. | 
|  141   void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( |  161   void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
|  158   size_t bytes_allocated_; |  178   size_t bytes_allocated_; | 
|  159  |  179  | 
|  160   // The maximum number of bytes of memory that may be allocated. |  180   // The maximum number of bytes of memory that may be allocated. | 
|  161   size_t memory_limit_; |  181   size_t memory_limit_; | 
|  162  |  182  | 
|  163   // The number of bytes of memory that may be allocated but not used for |  183   // The number of bytes of memory that may be allocated but not used for | 
|  164   // |hard_memory_limit_expiration_time_| amount of time when receiving an idle |  184   // |hard_memory_limit_expiration_time_| amount of time when receiving an idle | 
|  165   // notification. |  185   // notification. | 
|  166   size_t soft_memory_limit_; |  186   size_t soft_memory_limit_; | 
|  167  |  187  | 
 |  188   // Under moderate memory pressure, we will purge memory until usage is within | 
 |  189   // this limit. | 
 |  190   size_t bytes_to_keep_under_moderate_pressure_; | 
 |  191  | 
 |  192   // Allows us to be respond when the system reports that it is under memory | 
 |  193   // pressure. | 
 |  194   scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | 
 |  195  | 
|  168   // Amount of time it takes for an allocation to become affected by |  196   // Amount of time it takes for an allocation to become affected by | 
|  169   // |soft_memory_limit_|. |  197   // |soft_memory_limit_|. | 
|  170   TimeDelta hard_memory_limit_expiration_time_; |  198   TimeDelta hard_memory_limit_expiration_time_; | 
|  171  |  199  | 
|  172   DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); |  200   DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); | 
|  173 }; |  201 }; | 
|  174  |  202  | 
|  175 }  // namespace internal |  203 }  // namespace internal | 
|  176 }  // namespace base |  204 }  // namespace base | 
|  177  |  205  | 
|  178 #endif  // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |  206 #endif  // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 
| OLD | NEW |