| 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 static DiscardableMemoryProvider* GetInstance(); | |
| 50 | |
| 51 // Sets the instance of DiscardableMemoryProvider to be returned by | |
| 52 // GetInstance. This should only be used by tests and must be called | |
| 53 // prior to GetInstance(). The ownership of the given provider is | |
| 54 // retained by the caller. | |
| 55 static void SetInstanceForTest(DiscardableMemoryProvider* provider); | |
| 56 | |
| 57 // 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 |
| 58 // 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 |
| 59 // having no limit at all. | 51 // having no limit at all. |
| 60 void SetDiscardableMemoryLimit(size_t bytes); | 52 void SetDiscardableMemoryLimit(size_t bytes); |
| 61 | 53 |
| 62 // 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. |
| 63 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | 55 void SetBytesToReclaimUnderModeratePressure(size_t bytes); |
| 64 | 56 |
| 65 // Adds the given discardable memory to the provider's collection. | 57 // Adds the given discardable memory to the provider's collection. |
| 66 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... |
| 100 : bytes(bytes), | 92 : bytes(bytes), |
| 101 memory(NULL) { | 93 memory(NULL) { |
| 102 } | 94 } |
| 103 | 95 |
| 104 size_t bytes; | 96 size_t bytes; |
| 105 uint8* memory; | 97 uint8* memory; |
| 106 }; | 98 }; |
| 107 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; | 99 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; |
| 108 | 100 |
| 109 // 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. |
| 110 static void NotifyMemoryPressure( | 102 void NotifyMemoryPressure( |
| 111 MemoryPressureListener::MemoryPressureLevel pressure_level); | 103 MemoryPressureListener::MemoryPressureLevel pressure_level); |
| 112 | 104 |
| 113 // Purges |bytes_to_reclaim_under_moderate_pressure_| bytes of | 105 // Purges |bytes_to_reclaim_under_moderate_pressure_| bytes of |
| 114 // discardable memory. | 106 // discardable memory. |
| 115 void Purge(); | 107 void Purge(); |
| 116 | 108 |
| 117 // 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|. |
| 118 // Caller must acquire |lock_| prior to calling this function. | 110 // Caller must acquire |lock_| prior to calling this function. |
| 119 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit); | 111 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit); |
| 120 | 112 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 142 // pressure. | 134 // pressure. |
| 143 MemoryPressureListener memory_pressure_listener_; | 135 MemoryPressureListener memory_pressure_listener_; |
| 144 | 136 |
| 145 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | 137 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); |
| 146 }; | 138 }; |
| 147 | 139 |
| 148 } // namespace internal | 140 } // namespace internal |
| 149 } // namespace base | 141 } // namespace base |
| 150 | 142 |
| 151 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | 143 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
| OLD | NEW |