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