| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_PROVIDER_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // memory -- if the pressure is moderate -- or all discardable memory | 39 // memory -- if the pressure is moderate -- or all discardable memory |
| 40 // if the pressure is critical. | 40 // if the pressure is critical. |
| 41 // | 41 // |
| 42 // NB - this class is an implementation detail. It has been exposed for testing | 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. | 43 // purposes. You should not need to use this class directly. |
| 44 class BASE_EXPORT_PRIVATE DiscardableMemoryProvider { | 44 class BASE_EXPORT_PRIVATE DiscardableMemoryProvider { |
| 45 public: | 45 public: |
| 46 DiscardableMemoryProvider(); | 46 DiscardableMemoryProvider(); |
| 47 ~DiscardableMemoryProvider(); | 47 ~DiscardableMemoryProvider(); |
| 48 | 48 |
| 49 // Call this to register memory pressure listener. Must be called on a | |
| 50 // thread with a MessageLoop current. | |
| 51 void RegisterMemoryPressureListener(); | |
| 52 | |
| 53 // Call this to unregister memory pressure listener. | |
| 54 void UnregisterMemoryPressureListener(); | |
| 55 | |
| 56 // The maximum number of bytes of discardable memory that may be allocated | 49 // 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 | 50 // before we force a purge. If this amount is zero, it is interpreted as |
| 58 // having no limit at all. | 51 // having no limit at all. |
| 59 void SetDiscardableMemoryLimit(size_t bytes); | 52 void SetDiscardableMemoryLimit(size_t bytes); |
| 60 | 53 |
| 61 // Sets the amount of memory to reclaim when we're under moderate pressure. | 54 // Sets the amount of memory to reclaim when we're under moderate pressure. |
| 62 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | 55 void SetBytesToReclaimUnderModeratePressure(size_t bytes); |
| 63 | 56 |
| 64 // Adds the given discardable memory to the provider's collection. | 57 // Adds the given discardable memory to the provider's collection. |
| 65 void Register(const DiscardableMemory* discardable, size_t bytes); | 58 void Register(const DiscardableMemory* discardable, size_t bytes); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 : bytes(bytes), | 92 : bytes(bytes), |
| 100 memory(NULL) { | 93 memory(NULL) { |
| 101 } | 94 } |
| 102 | 95 |
| 103 size_t bytes; | 96 size_t bytes; |
| 104 uint8* memory; | 97 uint8* memory; |
| 105 }; | 98 }; |
| 106 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; | 99 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; |
| 107 | 100 |
| 108 // This can be called as a hint that the system is under memory pressure. | 101 // This can be called as a hint that the system is under memory pressure. |
| 109 void OnMemoryPressure( | 102 void NotifyMemoryPressure( |
| 110 MemoryPressureListener::MemoryPressureLevel pressure_level); | 103 MemoryPressureListener::MemoryPressureLevel pressure_level); |
| 111 | 104 |
| 112 // Purges |bytes_to_reclaim_under_moderate_pressure_| bytes of | 105 // Purges |bytes_to_reclaim_under_moderate_pressure_| bytes of |
| 113 // discardable memory. | 106 // discardable memory. |
| 114 void Purge(); | 107 void Purge(); |
| 115 | 108 |
| 116 // Purges least recently used memory until usage is less or equal to |limit|. | 109 // Purges least recently used memory until usage is less or equal to |limit|. |
| 117 // Caller must acquire |lock_| prior to calling this function. | 110 // Caller must acquire |lock_| prior to calling this function. |
| 118 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit); | 111 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit); |
| 119 | 112 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 132 | 125 |
| 133 // The maximum number of bytes of discardable memory that may be allocated | 126 // The maximum number of bytes of discardable memory that may be allocated |
| 134 // before we assume moderate memory pressure. | 127 // before we assume moderate memory pressure. |
| 135 size_t discardable_memory_limit_; | 128 size_t discardable_memory_limit_; |
| 136 | 129 |
| 137 // Under moderate memory pressure, we will purge this amount of memory. | 130 // Under moderate memory pressure, we will purge this amount of memory. |
| 138 size_t bytes_to_reclaim_under_moderate_pressure_; | 131 size_t bytes_to_reclaim_under_moderate_pressure_; |
| 139 | 132 |
| 140 // Allows us to be respond when the system reports that it is under memory | 133 // Allows us to be respond when the system reports that it is under memory |
| 141 // pressure. | 134 // pressure. |
| 142 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | 135 MemoryPressureListener memory_pressure_listener_; |
| 143 | 136 |
| 144 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | 137 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); |
| 145 }; | 138 }; |
| 146 | 139 |
| 147 } // namespace internal | 140 } // namespace internal |
| 148 } // namespace base | 141 } // namespace base |
| 149 | 142 |
| 150 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | 143 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
| OLD | NEW |