Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_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/memory/memory_pressure_listener.h" | |
| 12 #include "base/memory/singleton.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 | |
| 15 namespace base { | |
| 16 class DiscardableMemory; | |
| 17 class DiscardableMemoryProviderTest; | |
| 18 } // namespace base | |
| 19 | |
| 20 #if defined(COMPILER_GCC) | |
| 21 namespace BASE_HASH_NAMESPACE { | |
| 22 template <> | |
| 23 struct hash<base::DiscardableMemory*> { | |
| 24 size_t operator()(base::DiscardableMemory* ptr) const { | |
| 25 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
| 26 } | |
| 27 }; | |
| 28 } // namespace BASE_HASH_NAMESPACE | |
| 29 #endif // COMPILER | |
| 30 | |
| 31 namespace base { | |
| 32 namespace internal { | |
| 33 | |
| 34 // The DiscardableMemoryProvider manages a collection of emulated | |
| 35 // DiscardableMemory instances. It is used on platforms that do not support | |
| 36 // discardable memory natively. It keeps track of all DiscardableMemory | |
| 37 // instances (in case they need to be purged), and the total amount of allocated | |
| 38 // memory (in case this forces a purge). | |
| 39 // | |
| 40 // When notified of memory pressure, the provider either purges the LRU unlocked | |
| 41 // memory -- if the pressure is moderate -- or all unlocked discardable memory | |
| 42 // if the pressure is critical. | |
| 43 // | |
| 44 // NB - this class is an implementation detail. It has been exposed for testing | |
| 45 // purposes. You should not need to use this class directly. | |
| 46 class BASE_EXPORT DiscardableMemoryProvider { | |
|
willchan no longer on Chromium
2013/10/01 18:47:25
BASE_EXPORT_PRIVATE?
reveman
2013/10/09 22:40:24
Done.
| |
| 47 public: | |
| 48 // The bool here is just a placeholder. The MRU cache acts like a map, but we | |
| 49 // only actually care about the keys. | |
| 50 typedef HashingMRUCache<DiscardableMemory*, bool> AllocationMap; | |
| 51 | |
| 52 DiscardableMemoryProvider(); | |
| 53 ~DiscardableMemoryProvider(); | |
| 54 | |
| 55 // The maximum number of bytes of discardable memory that may be allocated | |
| 56 // before we assume moderate memory pressure. If this amount is zero, it is | |
| 57 // interpreted as having no limit at all. | |
| 58 void SetDiscardableMemoryLimit(size_t bytes); | |
| 59 | |
| 60 // Gets the maximum amount of discardable memory that may be allocated. | |
| 61 size_t discardable_memory_limit() const; | |
| 62 | |
| 63 // Sets the amount of memory to reclaim when we're under moderate pressure. | |
| 64 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | |
| 65 | |
| 66 // Gets the amount of memory to reclaim when we're under moderate pressure. | |
| 67 size_t bytes_to_reclaim_under_moderate_pressure() const; | |
| 68 | |
| 69 private: | |
| 70 friend class base::DiscardableMemory; | |
|
willchan no longer on Chromium
2013/10/01 18:47:25
Can you just move the necessary private member fun
reveman
2013/10/09 22:40:24
Done.
| |
| 71 friend class base::DiscardableMemoryProviderTest; | |
| 72 friend class Singleton<DiscardableMemoryProvider>; | |
| 73 | |
| 74 static DiscardableMemoryProvider* GetInstance(); | |
| 75 | |
| 76 // This can be called as a hint that the system is under memory pressure. | |
| 77 static void NotifyMemoryPressure( | |
| 78 MemoryPressureListener::MemoryPressureLevel pressure_level); | |
| 79 | |
| 80 // Sets the instance of DiscardableMemoryProvider to be returned by | |
| 81 // GetInstance. This should only be used by tests. The ownership of the given | |
| 82 // provider is retained by the caller. | |
|
willchan no longer on Chromium
2013/10/01 18:47:25
You should note that this should be called prior t
reveman
2013/10/09 22:40:24
Done.
| |
| 83 static void SetInstanceForTest(DiscardableMemoryProvider* provider); | |
| 84 | |
| 85 // Adds the given discardable memory to the provider's collection. If the | |
| 86 // discardable has already been added, it is first unregistered and its | |
| 87 // memory deallocated. | |
| 88 void Register(DiscardableMemory* discardable); | |
| 89 | |
| 90 // Removes the given discardable memory from the provider's collection. | |
| 91 void Unregister(DiscardableMemory* discardable); | |
| 92 | |
| 93 // Called by discardable memory instances to allow the provider to keep track | |
| 94 // of the total amout of discardable memory allocated. | |
| 95 void DidAllocate(size_t bytes); | |
| 96 | |
| 97 // Called by discardable memory instances to allow the provider to keep track | |
| 98 // of the total amount of discardable memory allocated. | |
| 99 void DidDeallocate(size_t bytes); | |
| 100 | |
| 101 // Called by discardable memory instances to allow the provider to maintain | |
| 102 // its MRU cache. Returns false if the discardable has not been registered. | |
| 103 bool DidAccess(DiscardableMemory* discardable); | |
| 104 | |
| 105 // Purges all discardable memory. | |
| 106 void PurgeAll(); | |
| 107 | |
| 108 // Purges least recently used memory. | |
| 109 void PurgeLRU(); | |
| 110 | |
| 111 // Ensures that we don't allocate beyond our memory limit. | |
| 112 void EnforcePolicy(); | |
| 113 | |
| 114 // A MRU cache of all allocated bits of discardable memory. Used for purging. | |
| 115 AllocationMap allocations_; | |
| 116 | |
| 117 // The total amount of allocated discardable memory. | |
| 118 size_t bytes_allocated_; | |
| 119 | |
| 120 // The maximum number of bytes of discardable memory that may be allocated | |
| 121 // before we assume moderate memory pressure. | |
| 122 size_t discardable_memory_limit_; | |
| 123 | |
| 124 // Under moderate memory pressure, we will purge this amount of memory. | |
| 125 size_t bytes_to_reclaim_under_moderate_pressure_; | |
| 126 | |
| 127 // Used to avoid reentry into EnforcePolicy. | |
| 128 bool enforcing_policy_; | |
| 129 | |
| 130 // Needs to be held when accessing |allocations_|. | |
| 131 Lock allocations_lock_; | |
| 132 | |
| 133 // Needs to be held when accessing |bytes_allocated_| or the two limits above. | |
| 134 // It's mutable because it must be accessed in a const function. | |
| 135 mutable Lock bytes_allocated_lock_; | |
| 136 | |
| 137 // Allows us to be respond when the system reports that it is under memory | |
| 138 // pressure. | |
| 139 MemoryPressureListener memory_pressure_listener_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | |
| 142 }; | |
| 143 | |
| 144 } // namespace internal | |
| 145 } // namespace base | |
| 146 | |
| 147 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
| OLD | NEW |